From 72d618cfd9d076bb725980f3578446ca8c80d462 Mon Sep 17 00:00:00 2001 From: IlyasValiulov <148232695+IlyasValiulov@users.noreply.github.com> Date: Fri, 6 Dec 2024 23:41:39 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=B0=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CarpentryWorkshop/Entities/Material.cs | 10 ++++- .../Entities/MaterialReplenishment.cs | 10 ++++- .../CarpentryWorkshop/Entities/Order.cs | 20 ++++++++++ .../Entities/OrderProduct.cs | 2 + .../CarpentryWorkshop/Entities/Product.cs | 21 ++++++++++ .../Entities/ProductMaterial.cs | 1 + .../CarpentryWorkshop/Forms/FormMaterials.cs | 6 ++- .../Forms/FormMaterialsReplenishment.cs | 7 +++- .../Forms/FormOrderProduct.cs | 2 +- .../CarpentryWorkshop/Forms/FormOrders.cs | 7 +++- .../CarpentryWorkshop/Forms/FormProducts.cs | 7 +++- .../CarpentryWorkshop/Reports/ChartReport.cs | 11 +++--- .../CarpentryWorkshop/Reports/TableReport.cs | 13 +++---- .../IMaterialReplenishmentRepository.cs | 2 +- .../Repositories/IOrderMaterialsRepository.cs | 2 +- .../MaterialReplenishmentRepository.cs | 23 +++++++++-- .../OrderMaterialsRepository.cs | 22 +++++++++-- .../Implementations/OrderRepository.cs | 36 ++++++++++++++++-- .../Implementations/ProductRepository.cs | 38 +++++++++++++++++-- .../Implementations/QueryBuilder.cs | 29 ++++++++++++++ 20 files changed, 231 insertions(+), 38 deletions(-) create mode 100644 CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/QueryBuilder.cs diff --git a/CarpentryWorkshop/CarpentryWorkshop/Entities/Material.cs b/CarpentryWorkshop/CarpentryWorkshop/Entities/Material.cs index 93da8a1..ef3c77d 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Entities/Material.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Entities/Material.cs @@ -1,10 +1,18 @@ -namespace CarpentryWorkshop.Entities; +using System.ComponentModel; + +namespace CarpentryWorkshop.Entities; public class Material { public int Id { get; private set; } + + [DisplayName("Название материала")] public string Name { get; private set; } = string.Empty; + + [DisplayName("Количество на складе")] public int Count { get; private set; } + + [DisplayName("Зарезервировано")] public int ReservInWarehouse { get; private set; } public static Material CreateEntity(int id, string name, int count, int reserveInWarehouse) { diff --git a/CarpentryWorkshop/CarpentryWorkshop/Entities/MaterialReplenishment.cs b/CarpentryWorkshop/CarpentryWorkshop/Entities/MaterialReplenishment.cs index 74eeab3..f380a84 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Entities/MaterialReplenishment.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Entities/MaterialReplenishment.cs @@ -1,10 +1,18 @@ -namespace CarpentryWorkshop.Entities; +using System.ComponentModel; + +namespace CarpentryWorkshop.Entities; public class MaterialReplenishment { public int Id { get; private set; } + + [DisplayName("Дата пополнения")] public DateTime DateReplenishment { get; private set; } + + [DisplayName("Название")] public string Name { get; private set; } = string.Empty; + + [DisplayName("Количество")] public int Count { get; private set; } public static MaterialReplenishment CreateOperation(int id, string name, int count) { diff --git a/CarpentryWorkshop/CarpentryWorkshop/Entities/Order.cs b/CarpentryWorkshop/CarpentryWorkshop/Entities/Order.cs index 8af3354..704ec4b 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Entities/Order.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Entities/Order.cs @@ -1,13 +1,26 @@ using CarpentryWorkshop.Entities.Enums; +using System.ComponentModel; namespace CarpentryWorkshop.Entities; public class Order { public int Id { get; private set; } + + [DisplayName("Дата заказа")] public DateTime DataOrder { get; private set; } + + [DisplayName("Статус")] public OrderStatus Status { get; private set; } + + [DisplayName("Описание")] public string Description { get; private set; } = string.Empty; + + [DisplayName("Изделия")] + public string Products => OrderProduct != null ? + string.Join(", ", OrderProduct.Select(x => $"{x.ProductName} {x.Count}")) : string.Empty; + + [Browsable(false)] public IEnumerable OrderProduct { get; private set; } = []; public static Order CreateOperation(int id, OrderStatus status, string description, IEnumerable orderProduct) { @@ -20,4 +33,11 @@ public class Order OrderProduct = orderProduct }; } + public void SetProductMaterial(IEnumerable orderProduct) + { + if (orderProduct != null && orderProduct.Any()) + { + OrderProduct = orderProduct; + } + } } diff --git a/CarpentryWorkshop/CarpentryWorkshop/Entities/OrderProduct.cs b/CarpentryWorkshop/CarpentryWorkshop/Entities/OrderProduct.cs index ba97408..09193c2 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Entities/OrderProduct.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Entities/OrderProduct.cs @@ -5,6 +5,8 @@ public class OrderProduct public int Id { get; private set; } public int ProductId { get; private set; } public int Count { get; private set; } + public string ProductName { get; private set; } = string.Empty; + public static OrderProduct CreateOperation(int id, int productId, int count) { return new OrderProduct diff --git a/CarpentryWorkshop/CarpentryWorkshop/Entities/Product.cs b/CarpentryWorkshop/CarpentryWorkshop/Entities/Product.cs index 6498a73..99c95dc 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Entities/Product.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Entities/Product.cs @@ -1,13 +1,27 @@ using CarpentryWorkshop.Entities.Enums; +using System.ComponentModel; namespace CarpentryWorkshop.Entities; public class Product { public int Id { get; private set; } + + [DisplayName("Название")] public string Name { get; private set; } = string.Empty; + + [DisplayName("Тип")] public ProductType Type { get; private set; } + public string FullProductName => $"{Name} {Type}"; + + [DisplayName("Материалы")] + public string Products => ProductMaterial != null ? + string.Join(", ", ProductMaterial.Select(x => $"{x.MaterialName} {x.Count}")) : string.Empty; + + [DisplayName("Количество на складе")] public int CountInWarehouse { get; private set; } + + [Browsable(false)] public IEnumerable ProductMaterial { get; set; } = []; public static Product CreateEntity(int id, string name, ProductType type, int countInWarehouse, IEnumerable productMaterial) { @@ -20,4 +34,11 @@ public class Product ProductMaterial = productMaterial }; } + public void SetProductMaterial(IEnumerable productMaterial) + { + if (productMaterial != null && productMaterial.Any()) + { + ProductMaterial = productMaterial; + } + } } diff --git a/CarpentryWorkshop/CarpentryWorkshop/Entities/ProductMaterial.cs b/CarpentryWorkshop/CarpentryWorkshop/Entities/ProductMaterial.cs index fd4168d..5836644 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Entities/ProductMaterial.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Entities/ProductMaterial.cs @@ -5,6 +5,7 @@ public class ProductMaterial public int Id { get; private set; } public int MaterialId { get; set; } public int Count { get; set; } + public string MaterialName { get; private set; } = string.Empty; public static ProductMaterial CreateOperation(int id, int materialId, int count) { return new ProductMaterial diff --git a/CarpentryWorkshop/CarpentryWorkshop/Forms/FormMaterials.cs b/CarpentryWorkshop/CarpentryWorkshop/Forms/FormMaterials.cs index 0017894..979155d 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Forms/FormMaterials.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Forms/FormMaterials.cs @@ -76,7 +76,11 @@ namespace CarpentryWorkshop.Forms MessageBox.Show(ex.Message, "Ошибка при изменении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - private void LoadList() => dataGridView.DataSource = _materialRepository.ReadMaterials(); + private void LoadList() + { + dataGridView.DataSource = _materialRepository.ReadMaterials(); + dataGridView.Columns["Id"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { id = 0; diff --git a/CarpentryWorkshop/CarpentryWorkshop/Forms/FormMaterialsReplenishment.cs b/CarpentryWorkshop/CarpentryWorkshop/Forms/FormMaterialsReplenishment.cs index 0868e30..6998a11 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Forms/FormMaterialsReplenishment.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Forms/FormMaterialsReplenishment.cs @@ -55,7 +55,12 @@ namespace CarpentryWorkshop.Forms MessageBox.Show(ex.Message, "Ошибка при изменении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - private void LoadList() => dataGridView.DataSource = _materialSpentRepository.ReadMaterialsSpent(); + private void LoadList() + { + dataGridView.DataSource = _materialSpentRepository.ReadMaterialsSpent(); + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["DateReplenishment"].DefaultCellStyle.Format = "dd MMMM yyyy hh:mm"; + } private bool TryGetIdentifierFromSelectedRow(out int id) { id = 0; diff --git a/CarpentryWorkshop/CarpentryWorkshop/Forms/FormOrderProduct.cs b/CarpentryWorkshop/CarpentryWorkshop/Forms/FormOrderProduct.cs index a5d3c01..9790938 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Forms/FormOrderProduct.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Forms/FormOrderProduct.cs @@ -15,7 +15,7 @@ namespace CarpentryWorkshop.Forms comboBoxStatus.DataSource = Enum.GetValues(typeof(OrderStatus)); ColumnProducts.DataSource = productRepository.ReadProducts(); - ColumnProducts.DisplayMember = "Name"; + ColumnProducts.DisplayMember = "FullProductName"; ColumnProducts.ValueMember = "Id"; } private void ButtonAdd_Click(object sender, EventArgs e) diff --git a/CarpentryWorkshop/CarpentryWorkshop/Forms/FormOrders.cs b/CarpentryWorkshop/CarpentryWorkshop/Forms/FormOrders.cs index 2f40068..b3c6dd8 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Forms/FormOrders.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Forms/FormOrders.cs @@ -56,7 +56,12 @@ namespace CarpentryWorkshop.Forms MessageBox.Show(ex.Message, "Ошибка при удалении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - private void LoadList() => dataGridView.DataSource = _orderRepository.ReadOrders(); + private void LoadList() + { + dataGridView.DataSource = _orderRepository.ReadOrders(); + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["DataOrder"].DefaultCellStyle.Format = "dd MMMM yyyy hh:mm"; + } private bool TryGetIdentifierFromSelectedRow(out int id) { id = 0; diff --git a/CarpentryWorkshop/CarpentryWorkshop/Forms/FormProducts.cs b/CarpentryWorkshop/CarpentryWorkshop/Forms/FormProducts.cs index 9354598..59ca62d 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Forms/FormProducts.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Forms/FormProducts.cs @@ -74,7 +74,12 @@ namespace CarpentryWorkshop.Forms MessageBox.Show(ex.Message, "Ошибка при изменении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - private void LoadList() => dataGridView.DataSource = _productRepository.ReadProducts(); + private void LoadList() + { + dataGridView.DataSource = _productRepository.ReadProducts(); + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["FullProductName"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { id = 0; diff --git a/CarpentryWorkshop/CarpentryWorkshop/Reports/ChartReport.cs b/CarpentryWorkshop/CarpentryWorkshop/Reports/ChartReport.cs index 3f061cb..b79778c 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Reports/ChartReport.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Reports/ChartReport.cs @@ -20,7 +20,7 @@ internal class ChartReport { new PdfBuilder(filePath) .AddHeader("Пополенение материала") - .AddPieChart("Виды материалов", GetData(dateTime)) + .AddPieChart($"Пополнение материала на {dateTime:dd MMMM yyyy}", GetData(dateTime)) .Build(); return true; } @@ -33,10 +33,9 @@ internal class ChartReport private List<(string Caption, double Value)> GetData(DateTime dateTime) { return _materialReplenishmentRepository - .ReadMaterialsSpent() - .Where(x => x.DateReplenishment.Date == dateTime.Date) - .GroupBy(x => x.Name, (key, group) => new { Id = key, Count = group.Sum(x => x.Count) }) - .Select(x => (x.Id.ToString(), (double)x.Count)) - .ToList(); + .ReadMaterialsSpent(dateForm: dateTime.Date, dateTo: dateTime.Date.AddDays(1)) + .GroupBy(x => x.Name, (key, group) => new { Id = key, Count = group.Sum(x => x.Count) }) + .Select(x => (x.Id.ToString(), (double)x.Count)) + .ToList(); } } diff --git a/CarpentryWorkshop/CarpentryWorkshop/Reports/TableReport.cs b/CarpentryWorkshop/CarpentryWorkshop/Reports/TableReport.cs index 7151eba..cefcf25 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Reports/TableReport.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Reports/TableReport.cs @@ -1,6 +1,5 @@ using CarpentryWorkshop.Repositories; using Microsoft.Extensions.Logging; -using System.Linq; namespace CarpentryWorkshop.Reports; @@ -27,7 +26,7 @@ internal class TableReport { new ExcelBuilder(filePath) .AddHeader("Сводка по движению материала", 0, 3) - .AddParagraph("за период", 0) + .AddParagraph($"за период c {startDate:dd.MM.yyyy} по { endDate: dd.MM.yyyy}", 0) .AddTable([10, 15, 15], GetData(materialId, startDate, endDate)) .Build(); return true; @@ -42,21 +41,19 @@ internal class TableReport private List GetData(int materialId, DateTime startDate, DateTime endDate) { var data = _materialReplenishmentRepository - .ReadMaterialsSpent() - .Where(x => x.DateReplenishment >= startDate && x.DateReplenishment <= endDate && x.Name == _materialRepository.ReadMaterialById(materialId).Name) + .ReadMaterialsSpent(dateForm: startDate, dateTo: endDate, materialName: _materialRepository.ReadMaterialById(materialId).Name) .Select(x => new { Date = x.DateReplenishment, CountIn = (int?)x.Count, CountOut = (int?)null }) .Union( _orderMaterialsRepository - .ReadOrders(materialId) - .Where(x => x.DataOrder >= startDate && x.DataOrder <= endDate) + .ReadOrders(dateForm: startDate, dateTo: endDate, matid: materialId) .Select(x => new { Date = x.DataOrder, CountIn = (int?)null, CountOut = (int?)x.Count })) .OrderBy(x => x.Date); return new List() { item } .Union( data - .Select(x => new string[] {x.Date.ToString(), x.CountIn?.ToString() ?? string.Empty, x.CountOut?.ToString() ?? string.Empty})) + .Select(x => new string[] {x.Date.ToString("dd.MM.yyyy"), x.CountIn?.ToString("N0") ?? string.Empty, x.CountOut?.ToString("N0") ?? string.Empty})) .Union( - [["Всего", data.Sum(x => x.CountIn ?? 0).ToString(), data.Sum(x => x.CountOut ?? 0).ToString()]]) + [["Всего", data.Sum(x => x.CountIn ?? 0).ToString("N0"), data.Sum(x => x.CountOut ?? 0).ToString("N0")]]) .ToList(); } } diff --git a/CarpentryWorkshop/CarpentryWorkshop/Repositories/IMaterialReplenishmentRepository.cs b/CarpentryWorkshop/CarpentryWorkshop/Repositories/IMaterialReplenishmentRepository.cs index 7e10d89..200f3b1 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Repositories/IMaterialReplenishmentRepository.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Repositories/IMaterialReplenishmentRepository.cs @@ -4,7 +4,7 @@ namespace CarpentryWorkshop.Repositories; public interface IMaterialReplenishmentRepository { - IEnumerable ReadMaterialsSpent(); + IEnumerable ReadMaterialsSpent(DateTime? dateForm = null, DateTime? dateTo = null, string? materialName = null); MaterialReplenishment ReadMaterialSpentById(int id); void CreateMaterialSpent(MaterialReplenishment material); void UpdateMaterialSpent(MaterialReplenishment material); diff --git a/CarpentryWorkshop/CarpentryWorkshop/Repositories/IOrderMaterialsRepository.cs b/CarpentryWorkshop/CarpentryWorkshop/Repositories/IOrderMaterialsRepository.cs index a66342c..1c2df51 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Repositories/IOrderMaterialsRepository.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Repositories/IOrderMaterialsRepository.cs @@ -4,5 +4,5 @@ namespace CarpentryWorkshop.Repositories; public interface IOrderMaterialsRepository { - IEnumerable ReadOrders(int id); + IEnumerable ReadOrders(DateTime? dateForm = null, DateTime? dateTo = null, int? matid = null); } diff --git a/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/MaterialReplenishmentRepository.cs b/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/MaterialReplenishmentRepository.cs index aa26fcb..c3f4f4c 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/MaterialReplenishmentRepository.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/MaterialReplenishmentRepository.cs @@ -3,6 +3,7 @@ using Dapper; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Npgsql; +using static System.Runtime.InteropServices.JavaScript.JSType; namespace CarpentryWorkshop.Repositories.Implementations; @@ -80,14 +81,30 @@ public class MaterialReplenishmentRepository : IMaterialReplenishmentRepository } } - public IEnumerable ReadMaterialsSpent() + public IEnumerable ReadMaterialsSpent(DateTime? dateForm = null, DateTime? dateTo = null, string? materialName = null) { + var builder = new QueryBuilder(); + if (dateForm.HasValue) + { + builder.AddCondition("datereplenishment >= @dateForm"); + } + if (dateTo.HasValue) + { + builder.AddCondition("datereplenishment <= @dateTo"); + } + if (materialName != null) + { + builder.AddCondition("Name = @materialName"); + } + _logger.LogInformation("Получение всех объектов"); try { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = "SELECT * FROM MaterialReplenishment"; - var materials = connection.Query(querySelect); + var querySelect = @$" + SELECT * FROM MaterialReplenishment + {builder.Build()}"; + var materials = connection.Query(querySelect, new {dateForm, dateTo, materialName }); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(materials)); return materials; } diff --git a/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/OrderMaterialsRepository.cs b/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/OrderMaterialsRepository.cs index 71f1660..0435cf0 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/OrderMaterialsRepository.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/OrderMaterialsRepository.cs @@ -17,18 +17,32 @@ public class OrderMaterialsRepository : IOrderMaterialsRepository _logger = logger; } - public IEnumerable ReadOrders(int matid) + public IEnumerable ReadOrders(DateTime? dateForm = null, DateTime? dateTo = null, int? matid = null) { + var builder = new QueryBuilder(); + if (dateForm.HasValue) + { + builder.AddCondition("orders.dataorder >= @dateForm"); + } + if (dateTo.HasValue) + { + builder.AddCondition("orders.dataorder <= @dateTo"); + } + if (matid.HasValue) + { + builder.AddCondition("materialid = @matid"); + } + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = @" + var querySelect = @$" SELECT orders.*, materials.id AS materialid, pm.count*op.count AS count FROM orders INNER JOIN orders_products AS op ON orders.id = op.orderid INNER JOIN products ON op.productid = products.id INNER JOIN products_materials AS pm ON products.id = pm.productid INNER JOIN materials ON pm.materialid = materials.id - Where materials.id = @matid"; - var orderMaterials = connection.Query(querySelect, new { matid }); + {builder.Build()}"; + var orderMaterials = connection.Query(querySelect, new { dateForm, dateTo, matid }); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(orderMaterials)); return orderMaterials; } diff --git a/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/OrderRepository.cs b/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/OrderRepository.cs index 62a3663..ad9d585 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/OrderRepository.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/OrderRepository.cs @@ -75,10 +75,38 @@ public class OrderRepository : IOrderRepository try { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = @"SELECT * FROM Orders"; - var order = connection.Query(querySelect); - _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(order)); - return order; + //var querySelect = @"SELECT * FROM Orders"; + //var order = connection.Query(querySelect); + //_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(order)); + //return order; + var querySelect = @" + SELECT orders.*, products.id AS productid, products.name AS productname, op.count AS count + FROM orders + INNER JOIN orders_products AS op ON orders.id = op.orderid + INNER JOIN products ON op.productid = products.id"; + + var productsDict = new Dictionary>(); + + var products = + connection.Query(querySelect, (order, orderProducts) => + { + if (!productsDict.TryGetValue(order.Id, out var frr)) + { + frr = []; + productsDict.Add(order.Id, frr); + } + frr.Add(orderProducts); + return order; + }, splitOn: "productid", param: new { dateForm, dateTo, orderStatus, orderId}); + + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(products)); + + return productsDict.Select(x => + { + var fr = products.First(y => y.Id == x.Key); + fr.SetProductMaterial(x.Value); + return fr; + }).ToArray(); } catch (Exception ex) { diff --git a/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/ProductRepository.cs b/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/ProductRepository.cs index e2de0a3..adbe5c0 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/ProductRepository.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/ProductRepository.cs @@ -3,6 +3,7 @@ using Dapper; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Npgsql; +using static System.Runtime.InteropServices.JavaScript.JSType; namespace CarpentryWorkshop.Repositories.Implementations; @@ -75,10 +76,39 @@ public class ProductRepository : IProductRepository try { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = @"SELECT * FROM Products"; - var product = connection.Query(querySelect); - _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(product)); - return product; + //var querySelect = @"SELECT * FROM Products"; + //var product = connection.Query(querySelect); + var querySelect = @" + SELECT products.*, materials.id AS materialid, materials.name AS materialname, pm.count AS count + FROM products + INNER JOIN products_materials AS pm ON products.id = pm.productid + INNER JOIN materials ON pm.materialid = materials.id"; + + var materialsDict = new Dictionary>(); + + var materials = + connection.Query(querySelect, (product, productsMaterials) => + { + if (!materialsDict.TryGetValue(product.Id, out var frr)) + { + frr = []; + materialsDict.Add(product.Id, frr); + } + frr.Add(productsMaterials); + return product; + }, splitOn: "materialid"); + + + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(materials)); + + return materialsDict.Select(x => + { + var fr = materials.First(y => y.Id == x.Key); + fr.SetProductMaterial(x.Value); + return fr; + }).ToArray(); + + //return product; } catch (Exception ex) { diff --git a/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/QueryBuilder.cs b/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/QueryBuilder.cs new file mode 100644 index 0000000..dc7a574 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/QueryBuilder.cs @@ -0,0 +1,29 @@ +using System.Text; + +namespace CarpentryWorkshop.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 From 03404a1c7ae48c2b6d778d19324acc86354f9493 Mon Sep 17 00:00:00 2001 From: IlyasValiulov <148232695+IlyasValiulov@users.noreply.github.com> Date: Mon, 9 Dec 2024 16:11:01 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=B0=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implementations/MaterialReplenishmentRepository.cs | 1 - .../Repositories/Implementations/OrderRepository.cs | 4 ---- .../Repositories/Implementations/ProductRepository.cs | 5 ----- 3 files changed, 10 deletions(-) diff --git a/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/MaterialReplenishmentRepository.cs b/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/MaterialReplenishmentRepository.cs index c3f4f4c..dded0d3 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/MaterialReplenishmentRepository.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/MaterialReplenishmentRepository.cs @@ -3,7 +3,6 @@ using Dapper; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Npgsql; -using static System.Runtime.InteropServices.JavaScript.JSType; namespace CarpentryWorkshop.Repositories.Implementations; diff --git a/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/OrderRepository.cs b/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/OrderRepository.cs index ad9d585..201fd92 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/OrderRepository.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/OrderRepository.cs @@ -75,10 +75,6 @@ public class OrderRepository : IOrderRepository try { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - //var querySelect = @"SELECT * FROM Orders"; - //var order = connection.Query(querySelect); - //_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(order)); - //return order; var querySelect = @" SELECT orders.*, products.id AS productid, products.name AS productname, op.count AS count FROM orders diff --git a/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/ProductRepository.cs b/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/ProductRepository.cs index adbe5c0..f672296 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/ProductRepository.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Repositories/Implementations/ProductRepository.cs @@ -3,7 +3,6 @@ using Dapper; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Npgsql; -using static System.Runtime.InteropServices.JavaScript.JSType; namespace CarpentryWorkshop.Repositories.Implementations; @@ -76,8 +75,6 @@ public class ProductRepository : IProductRepository try { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - //var querySelect = @"SELECT * FROM Products"; - //var product = connection.Query(querySelect); var querySelect = @" SELECT products.*, materials.id AS materialid, materials.name AS materialname, pm.count AS count FROM products @@ -107,8 +104,6 @@ public class ProductRepository : IProductRepository fr.SetProductMaterial(x.Value); return fr; }).ToArray(); - - //return product; } catch (Exception ex) { -- 2.25.1