From 114656362057e2a1398b9785c8f40c270ba0b6d9 Mon Sep 17 00:00:00 2001 From: H0llowVoid Date: Fri, 31 Jan 2025 03:42:32 +0400 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=80=D0=BE=D0=B4=D0=B5=20=D0=B2=D1=81?= =?UTF-8?q?=D1=91=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProjectOpticsSalon/Entites/Client.cs | 2 + .../ProjectOpticsSalon/Entites/Lens.cs | 6 +-- .../ProjectOpticsSalon/Entites/MakeOrder.cs | 21 +++++++++- .../ProjectOpticsSalon/Entites/Product.cs | 5 +++ .../ProjectOpticsSalon/Entites/Production.cs | 6 +++ .../ProjectOpticsSalon/Forms/FormClients.cs | 7 +++- .../ProjectOpticsSalon/Forms/FormLenses.cs | 8 +++- .../ProjectOpticsSalon/Forms/FormOrders.cs | 8 +++- .../ProjectOpticsSalon/Forms/FormProduct.cs | 4 +- .../Forms/FormProductions.cs | 7 +++- .../ProjectOpticsSalon/Forms/FormProducts.cs | 6 ++- .../ProjectOpticsSalon/Reports/ChartReport.cs | 5 +-- .../ProjectOpticsSalon/Reports/TableReport.cs | 5 +++ .../Repositories/IProductionRepository.cs | 2 +- .../Implementations/OrderRepository.cs | 32 +++++++++++++-- .../Implementations/ProductionRepository.cs | 26 +++++++++++-- .../Implementations/QueryBuilder.cs | 39 +++++++++++++++++++ 17 files changed, 168 insertions(+), 21 deletions(-) create mode 100644 ProjectOpticsSalon/ProjectOpticsSalon/Repositories/Implementations/QueryBuilder.cs diff --git a/ProjectOpticsSalon/ProjectOpticsSalon/Entites/Client.cs b/ProjectOpticsSalon/ProjectOpticsSalon/Entites/Client.cs index 66dae9f..ad2cdf0 100644 --- a/ProjectOpticsSalon/ProjectOpticsSalon/Entites/Client.cs +++ b/ProjectOpticsSalon/ProjectOpticsSalon/Entites/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; @@ -9,6 +10,7 @@ namespace ProjectOpticsSalon.Entites; public class Client { public int Id { get; private set; } + [DisplayName("Имя клиента")] public string Name { get; private set; } public static Client CreateClient(int id, string name) { diff --git a/ProjectOpticsSalon/ProjectOpticsSalon/Entites/Lens.cs b/ProjectOpticsSalon/ProjectOpticsSalon/Entites/Lens.cs index dd3b50a..3dc4c7c 100644 --- a/ProjectOpticsSalon/ProjectOpticsSalon/Entites/Lens.cs +++ b/ProjectOpticsSalon/ProjectOpticsSalon/Entites/Lens.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -9,11 +10,10 @@ namespace ProjectOpticsSalon.Entites; public class Lens { public int Id { get; private set; } - public float Dioptres { get; private set; } = 0; - public float Astigmatism { get; private set; } = 0; - + [DisplayName("Характеристики линзы")] + public string LensChars => $"D: {Dioptres}/ A: {Astigmatism}"; public static Lens CreateLens(int id, float dioptres, float astigmatism) { return new Lens diff --git a/ProjectOpticsSalon/ProjectOpticsSalon/Entites/MakeOrder.cs b/ProjectOpticsSalon/ProjectOpticsSalon/Entites/MakeOrder.cs index 1580f5d..bf1115c 100644 --- a/ProjectOpticsSalon/ProjectOpticsSalon/Entites/MakeOrder.cs +++ b/ProjectOpticsSalon/ProjectOpticsSalon/Entites/MakeOrder.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,13 +11,23 @@ namespace ProjectOpticsSalon.Entites; public class MakeOrder { public int Id { get; private set; } + [DisplayName("Цена")] public int Price { get; private set; } = 0; + [DisplayName("Дата заказа")] public DateTime OrderDate { get; private set; } - public int ClientId { get; private set; } + + [DisplayName("Имя клиента")] + public string ClientName { get; private set; } = string.Empty; + + [DisplayName("Статус заказа")] public OrderStatus OrderStatus { get; private set; } + + [DisplayName("Список товаров")] + public string ProductsList => Products != null ? string.Join(", ", Products.Select(x => $"{x.ProductId}")) : string.Empty; + public IEnumerable Products { get; private set; } = []; public static MakeOrder CreateOrder(int id, int price, int client, OrderStatus orderStatus, IEnumerable products) { @@ -30,4 +41,12 @@ public class MakeOrder Products = products }; } + + public void SetOrderProduct(IEnumerable orderProduct) + { + if (orderProduct != null && orderProduct.Any()) + { + Products = orderProduct; + } + } } diff --git a/ProjectOpticsSalon/ProjectOpticsSalon/Entites/Product.cs b/ProjectOpticsSalon/ProjectOpticsSalon/Entites/Product.cs index 8505b7c..2fe3877 100644 --- a/ProjectOpticsSalon/ProjectOpticsSalon/Entites/Product.cs +++ b/ProjectOpticsSalon/ProjectOpticsSalon/Entites/Product.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,9 +11,13 @@ namespace ProjectOpticsSalon.Entites; public class Product { public int Id { get; private set; } + [DisplayName("Тип продукта")] public ProductType ProductType { get; private set; } + [DisplayName("Материал оправы")] public FrameMaterial FrameMaterial { get; private set; } = 0; + [DisplayName("ID левой линзы")] public int LeftLensId { get; private set; } = 0; + [DisplayName("ID правой линзы")] public int RightLensId { get; private set; } = 0; public static Product CreateProduct(int id, ProductType productType, FrameMaterial frameMaterial, int leftLenseId, int rightLensId) { diff --git a/ProjectOpticsSalon/ProjectOpticsSalon/Entites/Production.cs b/ProjectOpticsSalon/ProjectOpticsSalon/Entites/Production.cs index 1e7e1fd..7fdc877 100644 --- a/ProjectOpticsSalon/ProjectOpticsSalon/Entites/Production.cs +++ b/ProjectOpticsSalon/ProjectOpticsSalon/Entites/Production.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -9,9 +10,14 @@ namespace ProjectOpticsSalon.Entites; public class Production { public int Id { get; private set; } + [DisplayName("Стоимость компонентов")] public double ComponentsPrice { get; private set; } + [DisplayName("Стоимость работы")] public double WorkPrice { get; private set; } + [DisplayName("ID продукта")] public int ProductId { get; private set; } + + [DisplayName("Дата производства")] public DateTime Date { get; private set; } public static Production CreateProduction(int id, double componentsPrice, double workPrice, int productId) { diff --git a/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormClients.cs b/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormClients.cs index d4d8b7f..5a1957e 100644 --- a/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormClients.cs +++ b/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormClients.cs @@ -91,7 +91,12 @@ public partial class FormClients : Form } } - private void LoadList() => dataGridViewData.DataSource = _clientRepository.ReadClients(); + private void LoadList() + { + dataGridViewData.DataSource = _clientRepository.ReadClients(); + dataGridViewData.Columns["Id"].Visible = false; + } + private bool TryGetIdentifierFromSelectedRow(out int id) { id = 0; diff --git a/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormLenses.cs b/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormLenses.cs index 8f83dcc..711b329 100644 --- a/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormLenses.cs +++ b/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormLenses.cs @@ -95,7 +95,13 @@ namespace ProjectOpticsSalon.Forms } } - private void LoadList() => dataGridViewData.DataSource = _lensRepository.ReadLens(); + private void LoadList() + { + dataGridViewData.DataSource = _lensRepository.ReadLens(); + dataGridViewData.Columns["Id"].Visible = false; + dataGridViewData.Columns["LensChars"].Visible = false; + } + private bool TryGetIdentifierFromSelectedRow(out int id) { id = 0; diff --git a/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormOrders.cs b/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormOrders.cs index 917d4c4..1104579 100644 --- a/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormOrders.cs +++ b/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormOrders.cs @@ -54,6 +54,12 @@ namespace ProjectOpticsSalon.Forms } } - private void LoadList() => dataGridViewData.DataSource = _orderRepository.ReadOrder(); + private void LoadList() + { + dataGridViewData.DataSource = _orderRepository.ReadOrder(); + dataGridViewData.Columns["Id"].Visible = false; + dataGridViewData.Columns["ClientId"].Visible = false; + dataGridViewData.Columns["Products"].Visible = false; + } } } diff --git a/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormProduct.cs b/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormProduct.cs index 5a29518..9c54bdd 100644 --- a/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormProduct.cs +++ b/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormProduct.cs @@ -62,11 +62,11 @@ namespace ProjectOpticsSalon.Forms } comboBoxLeftLens.DataSource = lensRepository.ReadLens(); - comboBoxLeftLens.DisplayMember = "Id"; + comboBoxLeftLens.DisplayMember = "LensChars"; comboBoxLeftLens.ValueMember = "Id"; comboBoxRightLens.DataSource = lensRepository.ReadLens(); - comboBoxRightLens.DisplayMember= "Id"; + comboBoxRightLens.DisplayMember= "LensChars"; comboBoxRightLens.ValueMember = "Id"; foreach (var elem in Enum.GetValues(typeof(FrameMaterial))) diff --git a/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormProductions.cs b/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormProductions.cs index 5f0de79..c0f49f4 100644 --- a/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormProductions.cs +++ b/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormProductions.cs @@ -71,7 +71,12 @@ namespace ProjectOpticsSalon.Forms } } - private void LoadList() => dataGridViewData.DataSource = _productionRepository.ReadProduction(); + private void LoadList() + { + dataGridViewData.DataSource = _productionRepository.ReadProduction(); + dataGridViewData.Columns["Id"].Visible = false; + } + private bool TryGetIdentifierFromSelectedRow(out int id) { id = 0; diff --git a/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormProducts.cs b/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormProducts.cs index cb4ae41..9ccdb65 100644 --- a/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormProducts.cs +++ b/ProjectOpticsSalon/ProjectOpticsSalon/Forms/FormProducts.cs @@ -95,7 +95,11 @@ namespace ProjectOpticsSalon.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) { diff --git a/ProjectOpticsSalon/ProjectOpticsSalon/Reports/ChartReport.cs b/ProjectOpticsSalon/ProjectOpticsSalon/Reports/ChartReport.cs index cf3e68d..fd7c61e 100644 --- a/ProjectOpticsSalon/ProjectOpticsSalon/Reports/ChartReport.cs +++ b/ProjectOpticsSalon/ProjectOpticsSalon/Reports/ChartReport.cs @@ -41,8 +41,7 @@ public class ChartReport private List<(string Caption, double Value)> GetData(DateTime dateTime) { return _productionRepository - .ReadProduction() - .Where(x => x.Date.Date == dateTime.Date) + .ReadProduction(startDate: dateTime.Date, endDate: dateTime.Date) .GroupBy( x => x.Id, (key, group) => new @@ -50,7 +49,7 @@ public class ChartReport Id = key, TotalPrice = group.Sum(y => y.WorkPrice+y.ComponentsPrice) }) - .Select(x => ($"{x.Id}", (double)x.TotalPrice)) + .Select(x => ($"{x.TotalPrice}", (double)x.TotalPrice)) .ToList(); } } diff --git a/ProjectOpticsSalon/ProjectOpticsSalon/Reports/TableReport.cs b/ProjectOpticsSalon/ProjectOpticsSalon/Reports/TableReport.cs index 3cda163..2692b10 100644 --- a/ProjectOpticsSalon/ProjectOpticsSalon/Reports/TableReport.cs +++ b/ProjectOpticsSalon/ProjectOpticsSalon/Reports/TableReport.cs @@ -51,6 +51,11 @@ public class TableReport private List GetData(int clientId, DateTime startDate, DateTime endDate) { var client = _clientRepository.ReadClientById(clientId); + var productions = _productionRepository + .ReadProduction(startDate, endDate, clientId) + .Where(x => x.ProductId == clientId) + .Select(x => new { client.Name, x.Date, Price = x.ComponentsPrice + x.WorkPrice, CountOrdered = (int?)1 }) + .ToList(); var orders = _orderRepository .ReadOrder() .Where(x => x.OrderDate >= startDate && x.OrderDate <= endDate && x.ClientId == clientId && x.OrderStatus == OrderStatus.Ordered) diff --git a/ProjectOpticsSalon/ProjectOpticsSalon/Repositories/IProductionRepository.cs b/ProjectOpticsSalon/ProjectOpticsSalon/Repositories/IProductionRepository.cs index fb8ed24..2881574 100644 --- a/ProjectOpticsSalon/ProjectOpticsSalon/Repositories/IProductionRepository.cs +++ b/ProjectOpticsSalon/ProjectOpticsSalon/Repositories/IProductionRepository.cs @@ -9,7 +9,7 @@ namespace ProjectOpticsSalon.Repositories; public interface IProductionRepository { - IEnumerable ReadProduction(); + IEnumerable ReadProduction(DateTime? startDate = null, DateTime? endDate = null, int? productId = null); Production ReadProductionById(int id); void CreateProduction(Production production); void UpdateProduction(Production production); diff --git a/ProjectOpticsSalon/ProjectOpticsSalon/Repositories/Implementations/OrderRepository.cs b/ProjectOpticsSalon/ProjectOpticsSalon/Repositories/Implementations/OrderRepository.cs index c4c1574..ca6d523 100644 --- a/ProjectOpticsSalon/ProjectOpticsSalon/Repositories/Implementations/OrderRepository.cs +++ b/ProjectOpticsSalon/ProjectOpticsSalon/Repositories/Implementations/OrderRepository.cs @@ -63,10 +63,36 @@ public class OrderRepository : IOrderRepository try { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = @"SELECT * FROM MakeOrder"; - var orders = connection.Query(querySelect); + var querySelect = @" + SELECT + mo.*, + c.name as ClientName, + op.ProductId as ProductId + FROM makeorder mo + LEFT JOIN client c on c.Id = mo.ClientId + LEFT JOIN Order_Product op on mo.Id = mo.Id"; + + + var ordersDict = new Dictionary>(); + var orders = connection.Query(querySelect, + (order, order_product) => + { + if (!ordersDict.TryGetValue(order.Id, out var frr)) + { + frr = []; + ordersDict.Add(order.Id, frr); + } + frr.Add(order_product); + return order; + }, splitOn: "ProductId"); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(orders)); - return orders; + return ordersDict.Select(x => + { + var o = orders.First(y => y.Id == x.Key); + o.SetOrderProduct(x.Value); + return o; + }).ToArray(); } catch (Exception ex) { diff --git a/ProjectOpticsSalon/ProjectOpticsSalon/Repositories/Implementations/ProductionRepository.cs b/ProjectOpticsSalon/ProjectOpticsSalon/Repositories/Implementations/ProductionRepository.cs index 6a05d8f..0853586 100644 --- a/ProjectOpticsSalon/ProjectOpticsSalon/Repositories/Implementations/ProductionRepository.cs +++ b/ProjectOpticsSalon/ProjectOpticsSalon/Repositories/Implementations/ProductionRepository.cs @@ -38,14 +38,34 @@ public class ProductionRepository : IProductionRepository } } - public IEnumerable ReadProduction() + public IEnumerable ReadProduction(DateTime? startDate = null, DateTime? endDate = null, int? productId = null) { _logger.LogInformation("Получение всех объектов"); try { + var builder = new QueryBuilder(); + if (startDate.HasValue) + { + builder.AddCondition("pr.Date >= @startDate"); + } + if (endDate.HasValue) + { + builder.AddCondition("pr.Date <= @endDate"); + } + if (productId.HasValue) + { + builder.AddCondition("pr.ProductId = @productId"); + } + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = "SELECT * FROM Production"; - var productions = connection.Query(querySelect); + var querySelect = $@" + SELECT + pr.*, + p.FrameMaterial + FROM Production pr + LEFT JOIN Product p ON p.Id = pr.ProductId + {builder.Build()}"; + var productions = connection.Query(querySelect, new {startDate, endDate, productId}); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(productions)); return productions; } diff --git a/ProjectOpticsSalon/ProjectOpticsSalon/Repositories/Implementations/QueryBuilder.cs b/ProjectOpticsSalon/ProjectOpticsSalon/Repositories/Implementations/QueryBuilder.cs new file mode 100644 index 0000000..dd85c4a --- /dev/null +++ b/ProjectOpticsSalon/ProjectOpticsSalon/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 ProjectOpticsSalon.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}"; + } +}