Лабораторная работа №4 (2)

This commit is contained in:
Егор 2024-12-25 08:25:47 +04:00
parent ac67eff242
commit 2194dbf7a4
12 changed files with 118 additions and 46 deletions

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -10,10 +11,20 @@ public class Delivery
{ {
public int Id { get; private set; } public int Id { get; private set; }
[Browsable(false)]
public int WorkerId { get; private set; } public int WorkerId { get; private set; }
[DisplayName("Работник")]
public string WorkerName { get; private set; } = string.Empty;
[DisplayName("Дата доставки")]
public DateTime DateDelivery { get; private set; } public DateTime DateDelivery { get; private set; }
public string Product => DeliveryProducts != null ?
string.Join(",", DeliveryProducts.Select(x => $"{x.ProductName} {x.Count}")) :
string.Empty;
[Browsable(false)]
public IEnumerable<DeliveryProduct> DeliveryProducts { get; private set; } = []; public IEnumerable<DeliveryProduct> DeliveryProducts { get; private set; } = [];
public static Delivery CreateOperation(int id, int workerId, IEnumerable<DeliveryProduct> deliveryProducts) public static Delivery CreateOperation(int id, int workerId, IEnumerable<DeliveryProduct> deliveryProducts)
@ -27,15 +38,11 @@ public class Delivery
}; };
} }
public static Delivery CreateOperation(TempDeliveryProduct tempDeliveryProduct, public void SetDeliveryProducts(IEnumerable<DeliveryProduct> deliveryProducts)
IEnumerable<DeliveryProduct> deliveryProducts)
{ {
return new Delivery if(deliveryProducts != null && deliveryProducts.Any())
{ {
Id = tempDeliveryProduct.Id, DeliveryProducts = deliveryProducts;
WorkerId = tempDeliveryProduct.WorkerId, }
DateDelivery = tempDeliveryProduct.DateDelivery,
DeliveryProducts = deliveryProducts
};
} }
} }

View File

@ -12,6 +12,8 @@ public class DeliveryProduct
public int ProductId { get; private set; } public int ProductId { get; private set; }
public string ProductName { get; private set; } = string.Empty;
public int Count { get; private set; } public int Count { get; private set; }
public static DeliveryProduct CreateElement(int id, int productId, int count) public static DeliveryProduct CreateElement(int id, int productId, int count)

View File

@ -1,6 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Security;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -10,14 +12,28 @@ public class Invoice
{ {
public int Id { get; private set; } public int Id { get; private set; }
[Browsable(false)]
public int WorkerId { get; private set; } public int WorkerId { get; private set; }
[Browsable(false)]
public int ClientId { get; private set; } public int ClientId { get; private set; }
[Browsable(false)]
public int ProductId { get; private set; } public int ProductId { get; private set; }
[DisplayName("Изделие")]
public string ProductName { get; private set; } = string.Empty;
[DisplayName("Работник")]
public string WorkerName { get; private set; } = string.Empty;
[DisplayName("Клиент")]
public string ClientName { get; private set; } = string.Empty;
[DisplayName("Дата оформления заказа")]
public DateTime Date { get; private set; } public DateTime Date { get; private set; }
[DisplayName("Количество купленных изделий")]
public int CountProduct { get; private set; } public int CountProduct { get; private set; }
public static Invoice CreateOperation(int id, int workerId, int clientId, int productId, int countProduct) public static Invoice CreateOperation(int id, int workerId, int clientId, int productId, int countProduct)

View File

@ -1,20 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureCompany.Entities;
public class TempDeliveryProduct
{
public int Id { get; private set; }
public int WorkerId { get; private set; }
public DateTime DateDelivery { get; private set; }
public int ProductId { get; private set; }
public int Count { get; private set; }
}

View File

@ -95,8 +95,12 @@ namespace FurnitureCompany.Forms
} }
} }
private void LoadList() => dataGridViewData.DataSource = _clientRepository.ReadClients(); private void LoadList()
{
dataGridViewData.DataSource = _clientRepository.ReadClients();
dataGridViewData.Columns["Id"].Visible = false;
dataGridViewData.Columns["Info"].Visible = false;
}
private bool TryGetIdentifierFromSelectedRow(out int id) private bool TryGetIdentifierFromSelectedRow(out int id)
{ {
id = 0; id = 0;

View File

@ -38,12 +38,27 @@ namespace FurnitureCompany.Forms
{ {
try try
{ {
if (dataGridViewProducts.RowCount < 1 || if (dataGridViewProducts.RowCount < 1 || dataGridViewProducts.ColumnCount == null ||
comboBoxWorker.SelectedIndex < 0) comboBoxWorker.SelectedIndex < 0)
{ {
throw new Exception("Имеются незаполненные поля"); throw new Exception("Имеются незаполненные поля");
} }
for (int i = 0; i < dataGridViewProducts.Rows.Count - 1; i++)
{
var row = dataGridViewProducts.Rows[i];
var countCell = row.Cells["ColumnCount"];
if (countCell.Value == null || string.IsNullOrWhiteSpace(countCell.Value.ToString()))
{
Console.WriteLine($"Пустая ячейка в строке {row.Index}, столбце {countCell.ColumnIndex}");
throw new Exception("Имеются незаполненные поля");
}
else
{
Console.WriteLine($"Значение в строке {row.Index}, столбце {countCell.ColumnIndex}: {countCell.Value}");
}
}
_deliveryRepository.CreateDelivery(Delivery.CreateOperation(0, _deliveryRepository.CreateDelivery(Delivery.CreateOperation(0,
(int)comboBoxWorker.SelectedValue!, CreateListDeliveryProductFromDataGrid())); (int)comboBoxWorker.SelectedValue!, CreateListDeliveryProductFromDataGrid()));

View File

@ -77,8 +77,12 @@ namespace FurnitureCompany.Forms
} }
} }
private void LoadList() => dataGridView.DataSource = _deliveryRepository.ReadDeliverys(); private void LoadList()
{
dataGridView.DataSource = _deliveryRepository.ReadDeliverys();
dataGridView.Columns["Id"].Visible = false;
dataGridView.Columns["DateDelivery"].DefaultCellStyle.Format = "dd.MM.yyyy";
}
private bool TryGetIdentifierFromSelectedRow(out int id) private bool TryGetIdentifierFromSelectedRow(out int id)
{ {
id = 0; id = 0;

View File

@ -54,6 +54,11 @@ namespace FurnitureCompany.Forms
} }
} }
private void LoadList() => dataGridViewData.DataSource = _invoiceRepository.ReadInvoices(); private void LoadList()
{
dataGridViewData.DataSource = _invoiceRepository.ReadInvoices();
dataGridViewData.Columns["Id"].Visible = false;
dataGridViewData.Columns["Date"].DefaultCellStyle.Format = "dd MMMM yyyy hh:mm";
}
} }
} }

View File

@ -95,8 +95,11 @@ namespace FurnitureCompany.Forms
} }
} }
private void LoadList() => dataGridViewData.DataSource = _productRepository.ReadProducts(); private void LoadList()
{
dataGridViewData.DataSource = _productRepository.ReadProducts();
dataGridViewData.Columns["Id"].Visible = false;
}
private bool TryGetIdentifierFromSelectedRow(out int id) private bool TryGetIdentifierFromSelectedRow(out int id)
{ {
id = 0; id = 0;

View File

@ -95,8 +95,12 @@ namespace FurnitureCompany.Forms
} }
} }
private void LoadList() => dataGridViewData.DataSource = _workerRepository.ReadWorkers(); private void LoadList()
{
dataGridViewData.DataSource = _workerRepository.ReadWorkers();
dataGridViewData.Columns["Id"].Visible = false;
dataGridViewData.Columns["FullName"].Visible = false;
}
private bool TryGetIdentifierFromSelectedRow(out int id) private bool TryGetIdentifierFromSelectedRow(out int id)
{ {
id = 0; id = 0;

View File

@ -85,13 +85,38 @@ public class DeliveryRepository : IDeliveryRepository
{ {
using var connection = new NpgsqlConnection(_connectionString.ConnectionString); using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @" var querySelect = @"
SELECT fr.*, ffr.ProductId, ffr.Count FROM Deliverys fr SELECT
INNER JOIN DeliveryProducts ffr ON ffr.DeliveryId = fr.Id"; d.*,
var deliverys = connection.Query<TempDeliveryProduct>(querySelect); CONCAT(w.LastName, ' ', w.FirstName) as WorkerName,
dp.ProductID,
dp.Count,
p.Name as ProductName
FROM Deliverys d
LEFT JOIN Workers w on w.Id = d.WorkerId
INNER JOIN DeliveryProducts dp ON dp.DeliveryId = d.Id
LEFT JOIN Products p on p.Id = dp.ProductId";
var deliveryDict = new Dictionary<int, List<DeliveryProduct>>();
var deliverys = connection.Query<Delivery, DeliveryProduct, Delivery>(querySelect,
(deliver, deliveryy) =>
{
if(!deliveryDict.TryGetValue(deliver.Id, out var dyp))
{
dyp = [];
deliveryDict.Add(deliver.Id, dyp);
}
dyp.Add(deliveryy);
return deliver;
}, splitOn: "ProductId");
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(deliverys)); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(deliverys));
return deliverys.GroupBy(x => x.Id, y => y,
(key, value) => Delivery.CreateOperation(value.First(), return deliveryDict.Select(x =>
value.Select(z => DeliveryProduct.CreateElement(0, z.ProductId, z.Count)))).ToList(); {
var d = deliverys.First(y => y.Id == x.Key);
d.SetDeliveryProducts(x.Value);
return d;
}).ToArray();
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -50,8 +50,15 @@ public class InvoiceRepository : IInvoiceRepository
try try
{ {
using var connection = new NpgsqlConnection(_connectionString.ConnectionString); using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @" var querySelect = @"SELECT
SELECT * FROM Invoices"; i.*,
p.Name as ProductName,
CONCAT(w.LastName, ' ', w.FirstName) as WorkerName,
CONCAT(c.Address, ' ', c.Name) as ClientName
FROM Invoices i
LEFT JOIN Products p on p.Id = i.ProductId
LEFT JOIN Workers w on w.Id = i.WorkerId
LEFT JOIN Clients c on c.Id = i.ClientId";
var invoices = connection.Query<Invoice>(querySelect); var invoices = connection.Query<Invoice>(querySelect);
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(invoices)); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(invoices));
return invoices; return invoices;