From ac67eff2425d3263aa9f346793e7be1633272737 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=B3=D0=BE=D1=80?= Date: Mon, 9 Dec 2024 10:18:42 +0400 Subject: [PATCH 1/3] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=964=20(1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FurnitureCompany/FurnitureCompany/Entities/Client.cs | 7 +++++++ FurnitureCompany/FurnitureCompany/Entities/Product.cs | 4 ++++ FurnitureCompany/FurnitureCompany/Entities/Worker.cs | 10 +++++++++- .../FurnitureCompany/Forms/FormDelivery.cs | 2 +- FurnitureCompany/FurnitureCompany/Forms/FormInvoice.cs | 4 ++-- 5 files changed, 23 insertions(+), 4 deletions(-) diff --git a/FurnitureCompany/FurnitureCompany/Entities/Client.cs b/FurnitureCompany/FurnitureCompany/Entities/Client.cs index cd724c5..ed4c5d2 100644 --- a/FurnitureCompany/FurnitureCompany/Entities/Client.cs +++ b/FurnitureCompany/FurnitureCompany/Entities/Client.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -10,12 +11,18 @@ public class Client { public int Id { get; private set; } + [DisplayName("Имя")] public string Name { get; private set; } = string.Empty; + [DisplayName("Адрес")] public string Address { get; private set; } = string.Empty; + public string Info => $"{Name} {Address}"; + + [DisplayName("Возраст")] public int Age { get; private set; } + [DisplayName("Заработок")] public double Earnings { get; private set; } public static Client CreateEntity(int id, string name, string address, int age, double earnings) diff --git a/FurnitureCompany/FurnitureCompany/Entities/Product.cs b/FurnitureCompany/FurnitureCompany/Entities/Product.cs index 32e8e6c..855ba02 100644 --- a/FurnitureCompany/FurnitureCompany/Entities/Product.cs +++ b/FurnitureCompany/FurnitureCompany/Entities/Product.cs @@ -1,6 +1,7 @@ using FurnitureCompany.Entities.Enums; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -11,10 +12,13 @@ public class Product { public int Id { get; private set; } + [DisplayName("Название")] public string Name { get; private set; } = string.Empty; + [DisplayName("Метариал изделия")] public Material Material { get; private set; } + [DisplayName("Цена")] public double Price { get; private set; } public static Product CreateEntity(int id, Material material, string name, double price) diff --git a/FurnitureCompany/FurnitureCompany/Entities/Worker.cs b/FurnitureCompany/FurnitureCompany/Entities/Worker.cs index f1ccc7c..28db0a5 100644 --- a/FurnitureCompany/FurnitureCompany/Entities/Worker.cs +++ b/FurnitureCompany/FurnitureCompany/Entities/Worker.cs @@ -1,6 +1,9 @@ -using FurnitureCompany.Entities.Enums; +using DocumentFormat.OpenXml.Office2016.Drawing.ChartDrawing; +using DocumentFormat.OpenXml.Wordprocessing; +using FurnitureCompany.Entities.Enums; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -11,10 +14,15 @@ public class Worker { public int Id { get; private set; } + [DisplayName("Имя")] public string FirstName { get; private set; } = string.Empty; + [DisplayName("Фамилия")] public string LastName { get; private set; } = string.Empty; + public string FullName => $"{FirstName} {LastName}"; + + [DisplayName("Уровень опыта")] public WorkerPost WorkerPost { get; private set; } public static Worker CreateEntity(int id, string firstName, string lastName, WorkerPost workerPost) diff --git a/FurnitureCompany/FurnitureCompany/Forms/FormDelivery.cs b/FurnitureCompany/FurnitureCompany/Forms/FormDelivery.cs index 70663e1..d312af9 100644 --- a/FurnitureCompany/FurnitureCompany/Forms/FormDelivery.cs +++ b/FurnitureCompany/FurnitureCompany/Forms/FormDelivery.cs @@ -26,7 +26,7 @@ namespace FurnitureCompany.Forms throw new ArgumentNullException(nameof(deliveryRepository)); comboBoxWorker.DataSource = workerRepository.ReadWorkers(); - comboBoxWorker.DisplayMember = "FirstName"; + comboBoxWorker.DisplayMember = "FullName"; comboBoxWorker.ValueMember = "Id"; ColumnProduct.DataSource = productRepository.ReadProducts(); diff --git a/FurnitureCompany/FurnitureCompany/Forms/FormInvoice.cs b/FurnitureCompany/FurnitureCompany/Forms/FormInvoice.cs index 9e49f71..d7001de 100644 --- a/FurnitureCompany/FurnitureCompany/Forms/FormInvoice.cs +++ b/FurnitureCompany/FurnitureCompany/Forms/FormInvoice.cs @@ -27,7 +27,7 @@ namespace FurnitureCompany.Forms throw new ArgumentNullException(nameof(invoiceRepository)); comboBoxWorker.DataSource = workerRepository.ReadWorkers(); - comboBoxWorker.DisplayMember = "FirstName"; + comboBoxWorker.DisplayMember = "FullName"; comboBoxWorker.ValueMember = "Id"; comboBoxProduct.DataSource = productRepository.ReadProducts(); @@ -35,7 +35,7 @@ namespace FurnitureCompany.Forms comboBoxProduct.ValueMember = "Id"; comboBoxClient.DataSource = clientRepository.ReadClients(); - comboBoxClient.DisplayMember = "Name"; + comboBoxClient.DisplayMember = "Info"; comboBoxClient.ValueMember = "Id"; } -- 2.25.1 From 2194dbf7a4540582289cfe9b1a158c6df216a69d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=B3=D0=BE=D1=80?= Date: Wed, 25 Dec 2024 08:25:47 +0400 Subject: [PATCH 2/3] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=964=20(2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FurnitureCompany/Entities/Delivery.cs | 23 ++++++++---- .../Entities/DeliveryProduct.cs | 2 + .../FurnitureCompany/Entities/Invoice.cs | 16 ++++++++ .../Entities/TempDeliveryProduct.cs | 20 ---------- .../FurnitureCompany/Forms/FormClients.cs | 8 +++- .../FurnitureCompany/Forms/FormDelivery.cs | 17 ++++++++- .../FurnitureCompany/Forms/FormDeliverys.cs | 8 +++- .../FurnitureCompany/Forms/FormInvoices.cs | 7 +++- .../FurnitureCompany/Forms/FormProducts.cs | 7 +++- .../FurnitureCompany/Forms/FormWorkers.cs | 8 +++- .../Implementations/DeliveryRepository.cs | 37 ++++++++++++++++--- .../Implementations/InvoiceRepository.cs | 11 +++++- 12 files changed, 118 insertions(+), 46 deletions(-) delete mode 100644 FurnitureCompany/FurnitureCompany/Entities/TempDeliveryProduct.cs diff --git a/FurnitureCompany/FurnitureCompany/Entities/Delivery.cs b/FurnitureCompany/FurnitureCompany/Entities/Delivery.cs index 454617d..ff16d41 100644 --- a/FurnitureCompany/FurnitureCompany/Entities/Delivery.cs +++ b/FurnitureCompany/FurnitureCompany/Entities/Delivery.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -10,10 +11,20 @@ public class Delivery { public int Id { get; private set; } + [Browsable(false)] public int WorkerId { get; private set; } + [DisplayName("Работник")] + public string WorkerName { get; private set; } = string.Empty; + + [DisplayName("Дата доставки")] 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 DeliveryProducts { get; private set; } = []; public static Delivery CreateOperation(int id, int workerId, IEnumerable deliveryProducts) @@ -27,15 +38,11 @@ public class Delivery }; } - public static Delivery CreateOperation(TempDeliveryProduct tempDeliveryProduct, - IEnumerable deliveryProducts) + public void SetDeliveryProducts(IEnumerable deliveryProducts) { - return new Delivery + if(deliveryProducts != null && deliveryProducts.Any()) { - Id = tempDeliveryProduct.Id, - WorkerId = tempDeliveryProduct.WorkerId, - DateDelivery = tempDeliveryProduct.DateDelivery, - DeliveryProducts = deliveryProducts - }; + DeliveryProducts = deliveryProducts; + } } } diff --git a/FurnitureCompany/FurnitureCompany/Entities/DeliveryProduct.cs b/FurnitureCompany/FurnitureCompany/Entities/DeliveryProduct.cs index 48dfac4..f0e7199 100644 --- a/FurnitureCompany/FurnitureCompany/Entities/DeliveryProduct.cs +++ b/FurnitureCompany/FurnitureCompany/Entities/DeliveryProduct.cs @@ -12,6 +12,8 @@ public class DeliveryProduct public int ProductId { get; private set; } + public string ProductName { get; private set; } = string.Empty; + public int Count { get; private set; } public static DeliveryProduct CreateElement(int id, int productId, int count) diff --git a/FurnitureCompany/FurnitureCompany/Entities/Invoice.cs b/FurnitureCompany/FurnitureCompany/Entities/Invoice.cs index a5cf261..1aec72f 100644 --- a/FurnitureCompany/FurnitureCompany/Entities/Invoice.cs +++ b/FurnitureCompany/FurnitureCompany/Entities/Invoice.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; +using System.Security; using System.Text; using System.Threading.Tasks; @@ -10,14 +12,28 @@ public class Invoice { public int Id { get; private set; } + [Browsable(false)] public int WorkerId { get; private set; } + [Browsable(false)] public int ClientId { get; private set; } + [Browsable(false)] 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; } + [DisplayName("Количество купленных изделий")] public int CountProduct { get; private set; } public static Invoice CreateOperation(int id, int workerId, int clientId, int productId, int countProduct) diff --git a/FurnitureCompany/FurnitureCompany/Entities/TempDeliveryProduct.cs b/FurnitureCompany/FurnitureCompany/Entities/TempDeliveryProduct.cs deleted file mode 100644 index 9d80363..0000000 --- a/FurnitureCompany/FurnitureCompany/Entities/TempDeliveryProduct.cs +++ /dev/null @@ -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; } -} diff --git a/FurnitureCompany/FurnitureCompany/Forms/FormClients.cs b/FurnitureCompany/FurnitureCompany/Forms/FormClients.cs index e724ed4..a980c4d 100644 --- a/FurnitureCompany/FurnitureCompany/Forms/FormClients.cs +++ b/FurnitureCompany/FurnitureCompany/Forms/FormClients.cs @@ -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) { id = 0; diff --git a/FurnitureCompany/FurnitureCompany/Forms/FormDelivery.cs b/FurnitureCompany/FurnitureCompany/Forms/FormDelivery.cs index d312af9..8350b4b 100644 --- a/FurnitureCompany/FurnitureCompany/Forms/FormDelivery.cs +++ b/FurnitureCompany/FurnitureCompany/Forms/FormDelivery.cs @@ -38,12 +38,27 @@ namespace FurnitureCompany.Forms { try { - if (dataGridViewProducts.RowCount < 1 || + if (dataGridViewProducts.RowCount < 1 || dataGridViewProducts.ColumnCount == null || comboBoxWorker.SelectedIndex < 0) { 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, (int)comboBoxWorker.SelectedValue!, CreateListDeliveryProductFromDataGrid())); diff --git a/FurnitureCompany/FurnitureCompany/Forms/FormDeliverys.cs b/FurnitureCompany/FurnitureCompany/Forms/FormDeliverys.cs index 2a2c46c..6aea178 100644 --- a/FurnitureCompany/FurnitureCompany/Forms/FormDeliverys.cs +++ b/FurnitureCompany/FurnitureCompany/Forms/FormDeliverys.cs @@ -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) { id = 0; diff --git a/FurnitureCompany/FurnitureCompany/Forms/FormInvoices.cs b/FurnitureCompany/FurnitureCompany/Forms/FormInvoices.cs index cca3a96..48ab14f 100644 --- a/FurnitureCompany/FurnitureCompany/Forms/FormInvoices.cs +++ b/FurnitureCompany/FurnitureCompany/Forms/FormInvoices.cs @@ -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"; + } } } diff --git a/FurnitureCompany/FurnitureCompany/Forms/FormProducts.cs b/FurnitureCompany/FurnitureCompany/Forms/FormProducts.cs index e2d99ae..c4473a2 100644 --- a/FurnitureCompany/FurnitureCompany/Forms/FormProducts.cs +++ b/FurnitureCompany/FurnitureCompany/Forms/FormProducts.cs @@ -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) { id = 0; diff --git a/FurnitureCompany/FurnitureCompany/Forms/FormWorkers.cs b/FurnitureCompany/FurnitureCompany/Forms/FormWorkers.cs index 03de2cf..c9d6b9f 100644 --- a/FurnitureCompany/FurnitureCompany/Forms/FormWorkers.cs +++ b/FurnitureCompany/FurnitureCompany/Forms/FormWorkers.cs @@ -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) { id = 0; diff --git a/FurnitureCompany/FurnitureCompany/Repositories/Implementations/DeliveryRepository.cs b/FurnitureCompany/FurnitureCompany/Repositories/Implementations/DeliveryRepository.cs index a007a85..7fdebe0 100644 --- a/FurnitureCompany/FurnitureCompany/Repositories/Implementations/DeliveryRepository.cs +++ b/FurnitureCompany/FurnitureCompany/Repositories/Implementations/DeliveryRepository.cs @@ -85,13 +85,38 @@ public class DeliveryRepository : IDeliveryRepository { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); var querySelect = @" - SELECT fr.*, ffr.ProductId, ffr.Count FROM Deliverys fr - INNER JOIN DeliveryProducts ffr ON ffr.DeliveryId = fr.Id"; - var deliverys = connection.Query(querySelect); + SELECT + d.*, + 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>(); + + var deliverys = connection.Query(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)); - return deliverys.GroupBy(x => x.Id, y => y, - (key, value) => Delivery.CreateOperation(value.First(), - value.Select(z => DeliveryProduct.CreateElement(0, z.ProductId, z.Count)))).ToList(); + + return deliveryDict.Select(x => + { + var d = deliverys.First(y => y.Id == x.Key); + d.SetDeliveryProducts(x.Value); + return d; + }).ToArray(); } catch (Exception ex) { diff --git a/FurnitureCompany/FurnitureCompany/Repositories/Implementations/InvoiceRepository.cs b/FurnitureCompany/FurnitureCompany/Repositories/Implementations/InvoiceRepository.cs index 07722db..6cfc13a 100644 --- a/FurnitureCompany/FurnitureCompany/Repositories/Implementations/InvoiceRepository.cs +++ b/FurnitureCompany/FurnitureCompany/Repositories/Implementations/InvoiceRepository.cs @@ -50,8 +50,15 @@ public class InvoiceRepository : IInvoiceRepository try { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = @" - SELECT * FROM Invoices"; + var querySelect = @"SELECT + 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(querySelect); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(invoices)); return invoices; -- 2.25.1 From 3029870f4e25807d82610c88c3f19dbcf9fec16f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=B3=D0=BE=D1=80?= Date: Wed, 25 Dec 2024 09:05:04 +0400 Subject: [PATCH 3/3] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=964=20(3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Forms/FormDirectoryReport.Designer.cs | 2 +- .../FurnitureCompany/Reports/ChartReport.cs | 9 ++--- .../FurnitureCompany/Reports/TableReport.cs | 14 +++---- .../Implementations/DeliveryRepository.cs | 27 +++++++++++-- .../Implementations/InvoiceRepository.cs | 29 ++++++++++++-- .../Implementations/QueryBuilder.cs | 39 +++++++++++++++++++ 6 files changed, 99 insertions(+), 21 deletions(-) create mode 100644 FurnitureCompany/FurnitureCompany/Repositories/Implementations/QueryBuilder.cs diff --git a/FurnitureCompany/FurnitureCompany/Forms/FormDirectoryReport.Designer.cs b/FurnitureCompany/FurnitureCompany/Forms/FormDirectoryReport.Designer.cs index 275db51..73dbe57 100644 --- a/FurnitureCompany/FurnitureCompany/Forms/FormDirectoryReport.Designer.cs +++ b/FurnitureCompany/FurnitureCompany/Forms/FormDirectoryReport.Designer.cs @@ -84,7 +84,7 @@ Controls.Add(checkBoxWorkers); Controls.Add(checkBoxClients); Name = "FormDirectoryReport"; - Text = "FormDirectoryReport"; + Text = "Отчёт о компании"; ResumeLayout(false); PerformLayout(); } diff --git a/FurnitureCompany/FurnitureCompany/Reports/ChartReport.cs b/FurnitureCompany/FurnitureCompany/Reports/ChartReport.cs index 823eb3e..236a5b8 100644 --- a/FurnitureCompany/FurnitureCompany/Reports/ChartReport.cs +++ b/FurnitureCompany/FurnitureCompany/Reports/ChartReport.cs @@ -28,7 +28,7 @@ internal class ChartReport { new PdfBuilder(filePath) .AddHeader("Оформление заказа") - .AddPieChart("Проданные изделия", GetData(dateTime)) + .AddPieChart($"Проданные изделия за {dateTime: dd MMMM yyyy}", GetData(dateTime)) .Build(); return true; } @@ -41,10 +41,9 @@ internal class ChartReport private List<(string Caption, double Value)> GetData(DateTime dateTime) { return _invoiceRepository - .ReadInvoices() - .Where(x => x.Date.Date == dateTime.Date) - .GroupBy(x => x.ClientId, (key, group) => new { Id = key, Count = group.Sum(x => x.CountProduct) }) - .Select(x => (x.Id.ToString(), (double)x.Count)) + .ReadInvoices(dateFrom: dateTime.Date, dateTo: dateTime.Date.AddDays(1)) + .GroupBy(x => x.ClientName, (key, group) => new { ClientName = key, Count = group.Sum(x => x.CountProduct) }) + .Select(x => (x.ClientName, (double)x.Count)) .ToList(); } } diff --git a/FurnitureCompany/FurnitureCompany/Reports/TableReport.cs b/FurnitureCompany/FurnitureCompany/Reports/TableReport.cs index f8a096d..c733897 100644 --- a/FurnitureCompany/FurnitureCompany/Reports/TableReport.cs +++ b/FurnitureCompany/FurnitureCompany/Reports/TableReport.cs @@ -35,7 +35,7 @@ internal class TableReport { new ExcelBuilder(filePath) .AddHeader("Сводка по доставке изделий", 0, 4) - .AddParagraph("за период", 0) + .AddParagraph($"за период c {startDate:dd.MM.yyyy} пo {endDate: dd.MM.yyyy}", 0) .AddTable([10, 10, 15, 15], GetData(productId, startDate, endDate)) .Build(); @@ -51,20 +51,18 @@ internal class TableReport private List GetData(int productId, DateTime startDate, DateTime endDate) { var data = _deliveryRepository - .ReadDeliverys() - .Where(x => x.DateDelivery >= startDate && x.DateDelivery <= endDate && x.DeliveryProducts.Any(y => y.ProductId == productId)) - .Select(x => new { x.WorkerId, Date = x.DateDelivery, CountIn = x.DeliveryProducts.FirstOrDefault(y => y.ProductId == productId)?.Count, CountOut = (int?)null }) + .ReadDeliverys(dateFrom: startDate, dateTo: endDate, productId: productId) + .Select(x => new { x.WorkerName, Date = x.DateDelivery, CountIn = x.DeliveryProducts.FirstOrDefault(y => y.ProductId == productId)?.Count, CountOut = (int?)null }) .Union( _invoiceRepository - .ReadInvoices() - .Where(x => x.Date >= startDate && x.Date <= endDate) - .Select(x => new { x.WorkerId, Date = x.Date, CountIn = (int?)null, CountOut = (int?)x.CountProduct })) + .ReadInvoices(dateFrom: startDate, dateTo: endDate, productId: productId) + .Select(x => new { x.WorkerName, Date = x.Date, CountIn = (int?)null, CountOut = (int?)x.CountProduct })) .OrderBy(x => x.Date); return new List() { item } .Union( data - .Select(x => new string[] { x.WorkerId.ToString(), x.Date.ToString(), x.CountIn?.ToString() ?? string.Empty, x.CountOut?.ToString() ?? string.Empty })) + .Select(x => new string[] { x.WorkerName.ToString(), x.Date.ToString("dd.MM.yyyy"), x.CountIn?.ToString() ?? string.Empty, x.CountOut?.ToString() ?? string.Empty })) .Union( [["Всего", "", data.Sum(x => x.CountIn ?? 0).ToString(), data.Sum(x => x.CountOut ?? 0).ToString()]]) .ToList(); diff --git a/FurnitureCompany/FurnitureCompany/Repositories/Implementations/DeliveryRepository.cs b/FurnitureCompany/FurnitureCompany/Repositories/Implementations/DeliveryRepository.cs index 7fdebe0..defbd37 100644 --- a/FurnitureCompany/FurnitureCompany/Repositories/Implementations/DeliveryRepository.cs +++ b/FurnitureCompany/FurnitureCompany/Repositories/Implementations/DeliveryRepository.cs @@ -83,9 +83,27 @@ public class DeliveryRepository : IDeliveryRepository try { + var builder = new QueryBuilder(); + if (dateFrom.HasValue) + { + builder.AddCondition("d.DateDelivery >= @dateFrom"); + } + if (dateTo.HasValue) + { + builder.AddCondition("d.DateDelivery <= @dateTo"); + } + if (productId.HasValue) + { + builder.AddCondition("dp.ProductId = @productId"); + } + if (workerId.HasValue) + { + builder.AddCondition("d.WorkerId = @workerId"); + } + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = @" - SELECT + var querySelect = $@" +SELECT d.*, CONCAT(w.LastName, ' ', w.FirstName) as WorkerName, dp.ProductID, @@ -94,7 +112,8 @@ public class DeliveryRepository : IDeliveryRepository 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"; +LEFT JOIN Products p on p.Id = dp.ProductId +{builder.Build()}"; var deliveryDict = new Dictionary>(); var deliverys = connection.Query(querySelect, @@ -108,7 +127,7 @@ LEFT JOIN Products p on p.Id = dp.ProductId"; dyp.Add(deliveryy); return deliver; - }, splitOn: "ProductId"); + }, splitOn: "ProductId", param: new { dateFrom, dateTo, productId, workerId }); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(deliverys)); return deliveryDict.Select(x => diff --git a/FurnitureCompany/FurnitureCompany/Repositories/Implementations/InvoiceRepository.cs b/FurnitureCompany/FurnitureCompany/Repositories/Implementations/InvoiceRepository.cs index 6cfc13a..2ab529f 100644 --- a/FurnitureCompany/FurnitureCompany/Repositories/Implementations/InvoiceRepository.cs +++ b/FurnitureCompany/FurnitureCompany/Repositories/Implementations/InvoiceRepository.cs @@ -49,8 +49,30 @@ public class InvoiceRepository : IInvoiceRepository try { + var builder =new QueryBuilder(); + if (dateFrom.HasValue) + { + builder.AddCondition("i.Date >= @dateFrom"); + } + if (dateTo.HasValue) + { + builder.AddCondition("i.Date <= @dateTo"); + } + if (productId.HasValue) + { + builder.AddCondition("i.ProductId = @productId"); + } + if (workerId.HasValue) + { + builder.AddCondition("i.WorkerId = @workerId"); + } + if (clientId.HasValue) + { + builder.AddCondition("i.ClientId = @clientId"); + } + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = @"SELECT + var querySelect = $@"SELECT i.*, p.Name as ProductName, CONCAT(w.LastName, ' ', w.FirstName) as WorkerName, @@ -58,8 +80,9 @@ public class InvoiceRepository : IInvoiceRepository 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(querySelect); +LEFT JOIN Clients c on c.Id = i.ClientId +{builder.Build()}"; + var invoices = connection.Query(querySelect, new { dateFrom, dateTo, productId, workerId, clientId }); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(invoices)); return invoices; } diff --git a/FurnitureCompany/FurnitureCompany/Repositories/Implementations/QueryBuilder.cs b/FurnitureCompany/FurnitureCompany/Repositories/Implementations/QueryBuilder.cs new file mode 100644 index 0000000..0268a87 --- /dev/null +++ b/FurnitureCompany/FurnitureCompany/Repositories/Implementations/QueryBuilder.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureCompany.Repositories.Implementations; + +public class QueryBuilder +{ + private readonly StringBuilder _builder; + + public QueryBuilder() + { + _builder = new(); + } + + public QueryBuilder AddCondition(string condition) + { + if (_builder.Length > 0) + { + _builder.Append(" AND "); + } + + _builder.Append(condition); + + return this; + } + + public string Build() + { + if (_builder.Length == 0) + { + return string.Empty; + } + + return $"WHERE {_builder}"; + } +} -- 2.25.1