diff --git a/ProjectWorkshop/ProjectWorkshop/Entities/Assembler.cs b/ProjectWorkshop/ProjectWorkshop/Entities/Assembler.cs index 8a9b2f2..f5d822e 100644 --- a/ProjectWorkshop/ProjectWorkshop/Entities/Assembler.cs +++ b/ProjectWorkshop/ProjectWorkshop/Entities/Assembler.cs @@ -1,4 +1,5 @@ using ProjectWorkshop.Entities.Enums; +using System.ComponentModel; namespace ProjectWorkshop.Entities; @@ -6,10 +7,13 @@ public class Assembler { public int ID { get; private set; } + [DisplayName("ФИО")] public string FullName { get; private set; } = string.Empty; + [DisplayName("Уровень")] public AssemblerRank AssemblerRank { get; private set; } + [DisplayName("Стаж")] public DateTime WorkExperience { get; private set; } public static Assembler CreateEntity(int id, string fullName, AssemblerRank assemblerRank, DateTime? dateTime = null) @@ -22,5 +26,4 @@ public class Assembler WorkExperience = dateTime ?? DateTime.Now }; } - } diff --git a/ProjectWorkshop/ProjectWorkshop/Entities/AssemblerShift.cs b/ProjectWorkshop/ProjectWorkshop/Entities/AssemblerShift.cs index 917889c..cce9db6 100644 --- a/ProjectWorkshop/ProjectWorkshop/Entities/AssemblerShift.cs +++ b/ProjectWorkshop/ProjectWorkshop/Entities/AssemblerShift.cs @@ -1,15 +1,27 @@ -namespace ProjectWorkshop.Entities; +using System.ComponentModel; + +namespace ProjectWorkshop.Entities; public class AssemblerShift { public int ID { get; private set; } + [DisplayName("Количество часов")] public int WorkHours { get; private set; } + [Browsable(false)] public int AssemblerID_Assembler { get; private set; } + [Browsable(false)] public int ShiftID_Shift { get; private set; } + [DisplayName("Сборщик")] + public string AssemblerName { get; private set; } = string.Empty; + + [Browsable(false)] + public string ShiftName { get; private set; } = string.Empty; + + [DisplayName("Дата")] public DateTime AssemblerShiftDate { get; private set; } public static AssemblerShift CreateOperation(int id, int workHours, int assemblerID, int shiftID_Shift, DateTime? shiftDate = null) diff --git a/ProjectWorkshop/ProjectWorkshop/Entities/Assembly.cs b/ProjectWorkshop/ProjectWorkshop/Entities/Assembly.cs index f634168..3a68ade 100644 --- a/ProjectWorkshop/ProjectWorkshop/Entities/Assembly.cs +++ b/ProjectWorkshop/ProjectWorkshop/Entities/Assembly.cs @@ -1,4 +1,5 @@ using ProjectWorkshop.Entities.Enums; +using System.ComponentModel; namespace ProjectWorkshop.Entities; @@ -6,12 +7,24 @@ public class Assembly { public int ID { get; private set; } + [DisplayName("Количество")] public int Count { get; private set; } + [Browsable(false)] public int AssemblerID_Assembler { get; private set; } + [DisplayName("Сборщик")] + public string AssemblerName { get; private set; } = string.Empty; + + [DisplayName("Собранные продукции")] + public string Assembles => ProductAssembly != null ? + string.Join(", ", ProductAssembly.Select(x => $"{x.ProductName}:{x.Count}")) : + string.Empty; + + [Browsable(false)] public IEnumerable ProductAssembly { get; private set; } = []; + [DisplayName("Дата")] public DateTime AssemblyDate { get; private set; } public static Assembly CreateOperation(int id, int count, int assemblerID, IEnumerable productAssembly, DateTime? assemblyDate = null) @@ -26,17 +39,12 @@ public class Assembly }; } - - public static Assembly CreateOperation(TempProductAssembly tempProductAssembly, IEnumerable productAssemblies) + public void SetProductAssembly(IEnumerable productAssemblies) { - return new Assembly + if (productAssemblies != null && productAssemblies.Any()) { - ID = tempProductAssembly.ID, - Count = tempProductAssembly.Count, - AssemblerID_Assembler = tempProductAssembly.AssemblyID_Assembly, - ProductAssembly = productAssemblies, - AssemblyDate = tempProductAssembly.AssemblyDate - }; + ProductAssembly = productAssemblies; + } } } diff --git a/ProjectWorkshop/ProjectWorkshop/Entities/Product.cs b/ProjectWorkshop/ProjectWorkshop/Entities/Product.cs index 6002222..b5aa171 100644 --- a/ProjectWorkshop/ProjectWorkshop/Entities/Product.cs +++ b/ProjectWorkshop/ProjectWorkshop/Entities/Product.cs @@ -1,4 +1,5 @@ using ProjectWorkshop.Entities.Enums; +using System.ComponentModel; namespace ProjectWorkshop.Entities; @@ -6,10 +7,13 @@ public class Product { public int ID { get; private set; } + [DisplayName("Название")] public string ProductName { get; private set; } = string.Empty; + [DisplayName("Цена")] public double Price { get; private set; } + [DisplayName("Тип")] public ProductType ProductType { get; private set; } public static Product CreateEntity(int id, string productName, double price, ProductType productType) diff --git a/ProjectWorkshop/ProjectWorkshop/Entities/ProductAssembly.cs b/ProjectWorkshop/ProjectWorkshop/Entities/ProductAssembly.cs index 340bf76..3bc13d8 100644 --- a/ProjectWorkshop/ProjectWorkshop/Entities/ProductAssembly.cs +++ b/ProjectWorkshop/ProjectWorkshop/Entities/ProductAssembly.cs @@ -1,4 +1,6 @@ -namespace ProjectWorkshop.Entities; +using System.ComponentModel; + +namespace ProjectWorkshop.Entities; public class ProductAssembly { @@ -8,6 +10,8 @@ public class ProductAssembly public int AssemblyID_Assembly { get; private set; } + public string ProductName { get; private set; } = string.Empty; + public int Count { get; private set; } public static ProductAssembly CreateElement(int id, int productID, int assemblyID, int count) diff --git a/ProjectWorkshop/ProjectWorkshop/Entities/Shift.cs b/ProjectWorkshop/ProjectWorkshop/Entities/Shift.cs index c25e256..7992d89 100644 --- a/ProjectWorkshop/ProjectWorkshop/Entities/Shift.cs +++ b/ProjectWorkshop/ProjectWorkshop/Entities/Shift.cs @@ -1,9 +1,12 @@ -namespace ProjectWorkshop.Entities; +using System.ComponentModel; + +namespace ProjectWorkshop.Entities; public class Shift { public int ID { get; private set; } + [DisplayName("Дата")] public DateTime ShiftDate { get; private set; } public static Shift CreateEntity(int id, DateTime? dateTime = null) diff --git a/ProjectWorkshop/ProjectWorkshop/Entities/TempProductAssembly.cs b/ProjectWorkshop/ProjectWorkshop/Entities/TempProductAssembly.cs deleted file mode 100644 index 50303a4..0000000 --- a/ProjectWorkshop/ProjectWorkshop/Entities/TempProductAssembly.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ProjectWorkshop.Entities; - -public class TempProductAssembly -{ - public int ID { get; private set; } - - public int ProductID_Product { get; private set; } - - public int AssemblyID_Assembly { get; private set; } - - public int Count { get; private set; } - - public DateTime AssemblyDate { get; private set; } -} - \ No newline at end of file diff --git a/ProjectWorkshop/ProjectWorkshop/Forms/FormAssemblerShifts.cs b/ProjectWorkshop/ProjectWorkshop/Forms/FormAssemblerShifts.cs index 03c4eaa..7f0b062 100644 --- a/ProjectWorkshop/ProjectWorkshop/Forms/FormAssemblerShifts.cs +++ b/ProjectWorkshop/ProjectWorkshop/Forms/FormAssemblerShifts.cs @@ -49,6 +49,10 @@ namespace ProjectWorkshop.Forms MessageBox.Show(ex.Message, "Ошибка при добавлении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - private void LoadList() => dataGridViewAssemblerShifts.DataSource = _assemblerShiftRepository.ReadAssemblerShifts(); + private void LoadList() + { + dataGridViewAssemblerShifts.DataSource = _assemblerShiftRepository.ReadAssemblerShifts(); + dataGridViewAssemblerShifts.Columns["Id"].Visible = false; + } } } diff --git a/ProjectWorkshop/ProjectWorkshop/Forms/FormAssemblers.cs b/ProjectWorkshop/ProjectWorkshop/Forms/FormAssemblers.cs index 75509fd..22ffc59 100644 --- a/ProjectWorkshop/ProjectWorkshop/Forms/FormAssemblers.cs +++ b/ProjectWorkshop/ProjectWorkshop/Forms/FormAssemblers.cs @@ -96,8 +96,11 @@ namespace ProjectWorkshop.Forms } } - private void LoadList() => dataGridViewAssemblers.DataSource = _assemblerRepository.ReadAssemblers(); - + private void LoadList() + { + dataGridViewAssemblers.DataSource = _assemblerRepository.ReadAssemblers(); + dataGridViewAssemblers.Columns["Id"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { id = 0; diff --git a/ProjectWorkshop/ProjectWorkshop/Forms/FormAssemblies.cs b/ProjectWorkshop/ProjectWorkshop/Forms/FormAssemblies.cs index 766d9ba..91ae100 100644 --- a/ProjectWorkshop/ProjectWorkshop/Forms/FormAssemblies.cs +++ b/ProjectWorkshop/ProjectWorkshop/Forms/FormAssemblies.cs @@ -74,8 +74,11 @@ namespace ProjectWorkshop.Forms } } - private void LoadList() => dataGridViewAssemblies.DataSource = _assemblyRepository.ReadAssemblies(); - + private void LoadList() + { + dataGridViewAssemblies.DataSource = _assemblyRepository.ReadAssemblies(); + dataGridViewAssemblies.Columns["Id"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { id = 0; diff --git a/ProjectWorkshop/ProjectWorkshop/Forms/FormProducts.cs b/ProjectWorkshop/ProjectWorkshop/Forms/FormProducts.cs index 3e6eecd..ec4a570 100644 --- a/ProjectWorkshop/ProjectWorkshop/Forms/FormProducts.cs +++ b/ProjectWorkshop/ProjectWorkshop/Forms/FormProducts.cs @@ -93,8 +93,11 @@ namespace ProjectWorkshop.Forms } } - private void LoadList() => dataGridViewProducts.DataSource = _productRepository.ReadProducts(); - + private void LoadList() + { + dataGridViewProducts.DataSource = _productRepository.ReadProducts(); + dataGridViewProducts.Columns["Id"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { id = 0; diff --git a/ProjectWorkshop/ProjectWorkshop/Forms/FormShifts.cs b/ProjectWorkshop/ProjectWorkshop/Forms/FormShifts.cs index 3ba6719..e732da8 100644 --- a/ProjectWorkshop/ProjectWorkshop/Forms/FormShifts.cs +++ b/ProjectWorkshop/ProjectWorkshop/Forms/FormShifts.cs @@ -92,8 +92,11 @@ namespace ProjectWorkshop.Forms } } - private void LoadList() => dataGridViewShifts.DataSource = _shiftRepository.ReadShifts(); - + private void LoadList() + { + dataGridViewShifts.DataSource = _shiftRepository.ReadShifts(); + dataGridViewShifts.Columns["Id"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { id = 0; diff --git a/ProjectWorkshop/ProjectWorkshop/Reports/ChartReport.cs b/ProjectWorkshop/ProjectWorkshop/Reports/ChartReport.cs index fc1e7ca..fc41e35 100644 --- a/ProjectWorkshop/ProjectWorkshop/Reports/ChartReport.cs +++ b/ProjectWorkshop/ProjectWorkshop/Reports/ChartReport.cs @@ -32,7 +32,7 @@ namespace ProjectWorkshop.Reports new PdfBuilder(filePath) .AddHeader("Сборки изделий") - .AddPieChart("Количество сборок по сборщикам", data) + .AddPieChart($"Количество сборок по сборщикам {dateTime:dd.MM.yyyy}", data) .Build(); return true; diff --git a/ProjectWorkshop/ProjectWorkshop/Reports/TableReport.cs b/ProjectWorkshop/ProjectWorkshop/Reports/TableReport.cs index 1b8e919..521bf99 100644 --- a/ProjectWorkshop/ProjectWorkshop/Reports/TableReport.cs +++ b/ProjectWorkshop/ProjectWorkshop/Reports/TableReport.cs @@ -56,15 +56,14 @@ namespace ProjectWorkshop.Reports throw new Exception($"Сборщик с ID {assemblerID} не найден."); var shiftData = _assemblerShiftRepository - .ReadAssemblerShifts() - .Where(x => x.AssemblerShiftDate >= startDate && x.AssemblerShiftDate <= endDate && x.AssemblerID_Assembler == assemblerID) + .ReadAssemblerShifts(startDate, endDate, assemblerID) .OrderBy(x => x.AssemblerShiftDate) .ToList(); var result = new List - { - Headers - }; + { + Headers + }; int totalAssemblies = 0; int totalProducts = 0; diff --git a/ProjectWorkshop/ProjectWorkshop/Repositories/IAssemblerShiftRepository.cs b/ProjectWorkshop/ProjectWorkshop/Repositories/IAssemblerShiftRepository.cs index d923e13..5510a69 100644 --- a/ProjectWorkshop/ProjectWorkshop/Repositories/IAssemblerShiftRepository.cs +++ b/ProjectWorkshop/ProjectWorkshop/Repositories/IAssemblerShiftRepository.cs @@ -3,7 +3,7 @@ namespace ProjectWorkshop.Repositories; public interface IAssemblerShiftRepository { - IEnumerable ReadAssemblerShifts(int? workHours = null, int? assemblerID = null, int? shiftID = null); + IEnumerable ReadAssemblerShifts(DateTime? dateFrom = null, DateTime? dateTo = null, int? assemblyID = null); void CreateAssemblerShift(AssemblerShift assemblerShift); } diff --git a/ProjectWorkshop/ProjectWorkshop/Repositories/IAssemblyRepository.cs b/ProjectWorkshop/ProjectWorkshop/Repositories/IAssemblyRepository.cs index 93f082c..5458301 100644 --- a/ProjectWorkshop/ProjectWorkshop/Repositories/IAssemblyRepository.cs +++ b/ProjectWorkshop/ProjectWorkshop/Repositories/IAssemblyRepository.cs @@ -3,7 +3,7 @@ namespace ProjectWorkshop.Repositories; public interface IAssemblyRepository { - IEnumerable ReadAssemblies(DateTime? dateFrom = null, DateTime? dateTo = null, int? productID = null, int? assemblyID = null, int? count = null); + IEnumerable ReadAssemblies(DateTime? dateFrom = null, DateTime? dateTo = null, int? assemblyID = null); void CreateAssembly(Assembly assembly); diff --git a/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/AssemblerShiftRepository.cs b/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/AssemblerShiftRepository.cs index 78667ea..54f65e6 100644 --- a/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/AssemblerShiftRepository.cs +++ b/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/AssemblerShiftRepository.cs @@ -39,15 +39,30 @@ public class AssemblerShiftRepository : IAssemblerShiftRepository } } - public IEnumerable ReadAssemblerShifts(int? workHours = null, int? assemblerID = null, int? shiftID = null) + public IEnumerable ReadAssemblerShifts(DateTime? dateFrom = null, DateTime? dateTo = null, int? assemblyID = null) { _logger.LogInformation("Получение всех объектов"); try { + var builder = new QueryBuilder(); + if (dateFrom.HasValue) + { + builder.AddCondition("AssemblerShiftDate >= @dateFrom"); + } + if (dateTo.HasValue) + { + builder.AddCondition("AssemblerShiftDate <= @dateTo"); + } + if (assemblyID.HasValue) + { + builder.AddCondition("AssemblerID_Assembler = @assemblyID"); + } using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = "SELECT * FROM AssemblerShift"; - var assemblerShifts = connection.Query(querySelect); + var querySelect = @$"SELECT asss.*, assr.FullName as AssemblerName FROM AssemblerShift asss + LEFT JOIN Assembler assr on assr.Id = asss.AssemblerID_Assembler + {builder.Build()}"; + var assemblerShifts = connection.Query(querySelect, new {dateFrom, dateTo, assemblyID}); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(assemblerShifts)); return assemblerShifts; diff --git a/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/AssemblyRepository.cs b/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/AssemblyRepository.cs index 4e1a799..ed3ce1d 100644 --- a/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/AssemblyRepository.cs +++ b/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/AssemblyRepository.cs @@ -86,16 +86,44 @@ public class AssemblyRepository : IAssemblyRepository } } - public IEnumerable ReadAssemblies(DateTime? dateFrom = null, DateTime? dateTo = null, int? id = null, int? assemblyID = null, int? count = null) + public IEnumerable ReadAssemblies(DateTime? dateFrom = null, DateTime? dateTo = null, int? assemblyID = null) { _logger.LogInformation("Получение всех объектов"); try { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var selectQuery = @"SELECT * FROM Assembly"; - var assemblies = connection.Query(selectQuery); - _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(assemblies)); - return assemblies; + var selectQuery = @"SELECT + ass.*, + assr.FullName as AssemblerName, + prass.Count, + pr.ProductName as ProductName + FROM Assembly ass + LEFT JOIN Assembler assr on assr.Id = ass.AssemblerId_Assembler + INNER JOIN ProductAssembly prass on ass.Id = prass.AssemblyID_Assembly + LEFT JOIN Product pr on pr.Id = prass.ProductID_Product"; + var assembliesDict = new Dictionary>(); + + var productAssemblies = connection.Query(selectQuery, + (assembly, productAssembly) => + { + if (!assembliesDict.TryGetValue(assembly.ID, out var ccf)) + { + ccf = []; + assembliesDict.Add(assembly.ID, ccf); + } + + ccf.Add(productAssembly); + return assembly; + }, splitOn: "Count", param: new { dateFrom, dateTo, assemblyID }); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(productAssemblies)); + + return assembliesDict.Select(x => + { + var cf = productAssemblies.First(y => y.ID == x.Key); + cf.SetProductAssembly(x.Value); + return cf; + }).ToArray(); } catch (Exception ex) { diff --git a/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/QueryBuilder.cs b/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/QueryBuilder.cs new file mode 100644 index 0000000..49d1a7e --- /dev/null +++ b/ProjectWorkshop/ProjectWorkshop/Repositories/Implementations/QueryBuilder.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWorkshop.Repositories.Implementations; + +internal 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}"; + } +} \ No newline at end of file