From 6f6b08f5cf2733ffb2da666edf356dc2305d326c Mon Sep 17 00:00:00 2001 From: Lazypr0ger Date: Fri, 13 Dec 2024 19:02:50 +0300 Subject: [PATCH] error --- ProductionInCehOTP/Entities/Material.cs | 15 ++-- .../Entities/MaterialForProduct.cs | 14 +-- .../Forms/Material_Forms/FormMaterial.cs | 7 +- .../Forms/Material_Forms/FormMaterials.cs | 1 + .../Implementations/MaterialRepository.cs | 85 ++++++++++--------- 5 files changed, 62 insertions(+), 60 deletions(-) diff --git a/ProductionInCehOTP/Entities/Material.cs b/ProductionInCehOTP/Entities/Material.cs index 97c62d5..fb84b21 100644 --- a/ProductionInCehOTP/Entities/Material.cs +++ b/ProductionInCehOTP/Entities/Material.cs @@ -14,14 +14,17 @@ public class Material [DisplayName("Номеер поставки")] public int ArrivalMaterialID { get; private set; } - [DisplayName("Количество переданного")] - - public string CountToProduct => MaterialForProducts != null ? - string.Join(", ", MaterialForProducts.Select(x => $"{x.Name}{x.Count}")) : - string.Empty; + [Browsable(false)] - public IEnumerable MaterialForProducts { get; private set; } + public IEnumerable MaterialForProducts { get; set; } = []; + + [DisplayName("Количество переданного")] + public string CountToProduct => MaterialForProducts != null ? + string.Join(", ", MaterialForProducts.Select(x => $"{x.ProductID}{x.Count}")) : + string.Empty; + + [DisplayName("Дата передачи в производство")] public DateTime DateArrivalToProduct { get; private set; } diff --git a/ProductionInCehOTP/Entities/MaterialForProduct.cs b/ProductionInCehOTP/Entities/MaterialForProduct.cs index f18268b..17d0628 100644 --- a/ProductionInCehOTP/Entities/MaterialForProduct.cs +++ b/ProductionInCehOTP/Entities/MaterialForProduct.cs @@ -1,21 +1,13 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ProductionInCehOTP.Entities; +namespace ProductionInCehOTP.Entities; public class MaterialForProduct { public int ProductID { get; private set; } public int MaterialID { get; private set; } - - public string Name { get; private set; } public int Count { get; private set; } - public static MaterialForProduct CreateDependenceMaterialsToProduct(int productID, int materialsId, int count) + public static MaterialForProduct CreateDependenceMaterialsToProduct( int materialsId, int productID, int count) { - return new MaterialForProduct { ProductID = productID, MaterialID = materialsId, Count = count }; + return new MaterialForProduct { ProductID = productID, MaterialID = materialsId, Count = count}; } } diff --git a/ProductionInCehOTP/Forms/Material_Forms/FormMaterial.cs b/ProductionInCehOTP/Forms/Material_Forms/FormMaterial.cs index d096c6e..6256bf5 100644 --- a/ProductionInCehOTP/Forms/Material_Forms/FormMaterial.cs +++ b/ProductionInCehOTP/Forms/Material_Forms/FormMaterial.cs @@ -62,11 +62,10 @@ namespace ProductionInCehOTP.Forms.Material_Forms { continue; } - list.Add(MaterialForProduct.CreateDependenceMaterialsToProduct(0, - Convert.ToInt32(row.Cells["ColumnProduct"].Value), Convert. - ToInt32(row.Cells["ColumnCountMaterial"].Value))); + list.Add(MaterialForProduct.CreateDependenceMaterialsToProduct(0, Convert.ToInt32(row.Cells["ColumnProduct"].Value) + , Convert.ToInt32(row.Cells["ColumnCountMaterial"].Value))); } - return list.GroupBy(x => x.MaterialID, x => x.Count, (id,countes) => + return list.GroupBy(x => x.ProductID, x => x.Count, (id,countes) => MaterialForProduct.CreateDependenceMaterialsToProduct(0,id,countes.Sum())).ToList(); } diff --git a/ProductionInCehOTP/Forms/Material_Forms/FormMaterials.cs b/ProductionInCehOTP/Forms/Material_Forms/FormMaterials.cs index b7057c1..43de205 100644 --- a/ProductionInCehOTP/Forms/Material_Forms/FormMaterials.cs +++ b/ProductionInCehOTP/Forms/Material_Forms/FormMaterials.cs @@ -31,6 +31,7 @@ namespace ProductionInCehOTP.Forms.Material_Forms private void LoadList() { + var gh = _materialRepository.GetMaterials(); dataGridViewData.DataSource = _materialRepository.GetMaterials(); dataGridViewData.Columns["Id"].Visible = false; dataGridViewData.Columns["DateArrivalToProduct"].DefaultCellStyle.Format = "dd MMMM yyyy hh:mm"; diff --git a/ProductionInCehOTP/Repositories/Implementations/MaterialRepository.cs b/ProductionInCehOTP/Repositories/Implementations/MaterialRepository.cs index 70e926f..c4fd3fd 100644 --- a/ProductionInCehOTP/Repositories/Implementations/MaterialRepository.cs +++ b/ProductionInCehOTP/Repositories/Implementations/MaterialRepository.cs @@ -22,11 +22,14 @@ public class MaterialRepository : IMaterialReposirory throw new NotImplementedException(); } - public IEnumerable GetMaterials( DateTime? StartDate = null, DateTime? EndDate = null) + + public IEnumerable GetMaterials(DateTime? StartDate = null, DateTime? EndDate = null) { - _logger.LogInformation("Получение всех объектов"); + _logger.LogInformation("Получение всех объектов материалов"); + try { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); var builder = new QueryBuilder(); if (StartDate.HasValue) { @@ -36,70 +39,74 @@ public class MaterialRepository : IMaterialReposirory { builder.AddCondition("mat.Datearrivaltoproduct <= @EndDate"); } - using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = $@" -SELECT -mat.*, -mfp.Count, -pr.Name as ""MatForproduct"" -from material mat -inner join material_product mfp on mfp.MaterialId = mat.id -left join product pr on pr.productiontypeid = mfp.productid -{ builder.Build()}"; - var materialDict = new Dictionary>(); - var material = connection.Query(querySelect, - (matat, material) => + var query = @$" + SELECT + mat.Id AS Id, + mat.Name AS Name, + mat.ArrivalMaterialiD AS ArrivalMaterialID, + mat.DateArrivalToProduct AS DateArrivalToProduct, + mfp.ProductID AS ProductId, + mfp.MaterialID AS MaterialIdDependency, + mfp.Count AS CountToProduct + FROM material mat + LEFT JOIN material_product mfp ON mat.Id = mfp.MaterialID + {builder.Build()}"; + + var MaterialForProductCountDict = new Dictionary>(); + + var materials = connection.Query( + query, + (material, materialForProduct) => { - if (!materialDict.TryGetValue(matat.Id, out var frr)) + if (!MaterialForProductCountDict.TryGetValue(material.Id, out var forProduct)) { - frr = []; - materialDict.Add(matat.Id, frr); + forProduct = []; + MaterialForProductCountDict.Add(material.Id, forProduct); } - frr.Add(material); - return matat; - }, splitOn: "MatForproduct", param: new {StartDate, EndDate}); - _logger.LogDebug("Полученные объекты {json}", JsonConvert.SerializeObject(material)); - return materialDict.Select(x => - { - var fr = material.First(y => y.Id == x.Key); - fr.SetMaterialForProduct(x.Value); - return fr; - }).ToArray(); + forProduct.Add(materialForProduct); + return material; + }, + splitOn: "MaterialIdDependency" + ).Distinct().ToList(); + + _logger.LogDebug("Полученные объекты материалов: {json}", JsonConvert.SerializeObject(materials)); + return materials; } catch (Exception ex) { - _logger.LogError(ex, "Ошибка при чтении объектов"); + _logger.LogError(ex, "Ошибка при получении объектов материалов"); throw; } - } public void TransferMaterial(Material material) { _logger.LogInformation("Добавление объекта"); _logger.LogDebug("Объект: {json}", - JsonConvert.SerializeObject(material)); + JsonConvert.SerializeObject(material)); try { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); connection.Open(); using var transaction = connection.BeginTransaction(); + + var queryInsert = @" -INSERT INTO MATERIAL (NAME,ARRIVALMATERIALID,DateArrivalToProduct) -VALUES (@NAME, @ARRIVALMATERIALID, @DateArrivalToProduct); -SELECT MAX(Id) FROM MATERIAL"; - var materialID = - connection.QueryFirst(queryInsert, material, transaction); + INSERT INTO MATERIAL (NAME, ARRIVALMATERIALID, DateArrivalToProduct) + VALUES (@NAME, @ARRIVALMATERIALID, @DateArrivalToProduct); + SELECT MAX(Id) FROM MATERIAL"; + var materialID = connection.QueryFirst(queryInsert, material, transaction); + var querySubInsert = @" -INSERT INTO MATERIAL_PRODUCT (PRODUCTID, MATERIALID, COUNT) -VALUES (@PRODUCTID,@MATERIALID, @COUNT)"; + INSERT INTO MATERIAL_PRODUCT (PRODUCTID, MATERIALID, COUNT) + VALUES (@PRODUCTID,@MATERIALID, @COUNT)"; foreach (var elem in material.MaterialForProducts) { connection.Execute(querySubInsert, new { - elem.ProductID, materialID, + elem.ProductID, elem.Count }, transaction); }