diff --git a/Workshop/Entities/Cheque.cs b/Workshop/Entities/Cheque.cs index 0ffaf91..02cf418 100644 --- a/Workshop/Entities/Cheque.cs +++ b/Workshop/Entities/Cheque.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Text; @@ -10,8 +11,13 @@ namespace Workshop.Entities; public class Cheque { public int Id { get; private set; } + [DisplayName("Дата")] public DateTime Date { get; private set; } + [DisplayName("Итоговая сумма")] public double Sum { get; private set; } + public string Product => ChequeProduct != null ? + string.Join(", ", ChequeProduct.Select(x => $"{x.ProductName} {x.Amount}")) : string.Empty; + [Browsable(false)] public IEnumerable ChequeProduct { get; private set; } = []; public static Cheque CreateOperation(int id, int sum, IEnumerable products) @@ -33,4 +39,11 @@ public class Cheque ChequeProduct = chequeProducts }; } + public void SetChequeProduct(IEnumerable chequeProducts) + { + if (chequeProducts != null && chequeProducts.Any()) + { + ChequeProduct = chequeProducts; + } + } } diff --git a/Workshop/Entities/ChequeProduct.cs b/Workshop/Entities/ChequeProduct.cs index 84389fb..596bbb2 100644 --- a/Workshop/Entities/ChequeProduct.cs +++ b/Workshop/Entities/ChequeProduct.cs @@ -10,6 +10,7 @@ public class ChequeProduct public int ProductId { get; private set; } public int ChequeId { get; private set; } public int Amount { get; private set; } + public string ProductName { get; private set; } = string.Empty; public static ChequeProduct CreateEntity(int productid, int chequeid, int amount) { return new ChequeProduct diff --git a/Workshop/Entities/Master.cs b/Workshop/Entities/Master.cs index 2d8d14c..23236ab 100644 --- a/Workshop/Entities/Master.cs +++ b/Workshop/Entities/Master.cs @@ -19,7 +19,7 @@ public class Master public int Age { get; private set; } [DisplayName("Должность")] public MasterPosition Position { get; private set; } - public string fullName => $"{Name} {LastName}"; + public string FullName => $"{Name} {LastName}"; public static Master CreateEntity(int id, string name, string lastName, int age, MasterPosition position) { diff --git a/Workshop/Entities/ProductCreate.cs b/Workshop/Entities/ProductCreate.cs index 26640f7..4c64b2d 100644 --- a/Workshop/Entities/ProductCreate.cs +++ b/Workshop/Entities/ProductCreate.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,17 @@ namespace Workshop.Entities; public class ProductCreate { public int Id { get; private set; } + [Browsable(false)] public int ProductId { get; private set; } + [Browsable(false)] public int MasterId { get; private set; } + [DisplayName("Дата создания")] public DateTime CreatingDate { get; private set; } + [DisplayName("Изделие")] + public string ProductName { get; private set; } = string.Empty; + [DisplayName("Сотрудник")] + public string MasterName { get; private set; } = string.Empty; + public static ProductCreate CreateOperation(int id, int productid, int masterid) { return new ProductCreate diff --git a/Workshop/Forms/FormCheques.cs b/Workshop/Forms/FormCheques.cs index 896302e..09f6f9e 100644 --- a/Workshop/Forms/FormCheques.cs +++ b/Workshop/Forms/FormCheques.cs @@ -50,6 +50,12 @@ namespace Workshop.Forms MessageBox.Show(ex.Message, "Ошибка при добавлении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - private void LoadList() => dataGridProductCreatings.DataSource = _chequeRepository.ReadCheques(); + private void LoadList() + { + dataGridProductCreatings.DataSource = _chequeRepository.ReadCheques(); + dataGridProductCreatings.Columns["Id"].Visible = false; + //dataGridProductCreatings.Columns["ChequeProduct"].Visible = false; + dataGridProductCreatings.Columns["Date"].DefaultCellStyle.Format = "dd MMMM yyyy hh:mm"; + } } } diff --git a/Workshop/Forms/FormMasters.cs b/Workshop/Forms/FormMasters.cs index b07398e..dc0de05 100644 --- a/Workshop/Forms/FormMasters.cs +++ b/Workshop/Forms/FormMasters.cs @@ -102,8 +102,11 @@ namespace Workshop.Forms id = Convert.ToInt32(dataGridViewMasters.SelectedRows[0].Cells["Id"].Value); return true; } - private void LoadList() => dataGridViewMasters.DataSource = _masterRepository.ReadMasters(); - - + private void LoadList() + { + dataGridViewMasters.DataSource = _masterRepository.ReadMasters(); + dataGridViewMasters.Columns["Id"].Visible = false; + dataGridViewMasters.Columns["FullName"].Visible = false; + } } } diff --git a/Workshop/Forms/FormMaterials.cs b/Workshop/Forms/FormMaterials.cs index 42f5b5b..7efb450 100644 --- a/Workshop/Forms/FormMaterials.cs +++ b/Workshop/Forms/FormMaterials.cs @@ -101,7 +101,10 @@ namespace Workshop.Forms id = Convert.ToInt32(dataGridViewMaterials.SelectedRows[0].Cells["Id"].Value); return true; } - private void LoadList() => dataGridViewMaterials.DataSource = _materialRepository.ReadMaterials(); - + private void LoadList() + { + dataGridViewMaterials.DataSource = _materialRepository.ReadMaterials(); + dataGridViewMaterials.Columns["Id"].Visible = false; + } } } diff --git a/Workshop/Forms/FormProductCreate.cs b/Workshop/Forms/FormProductCreate.cs index 73f3e50..e6d595f 100644 --- a/Workshop/Forms/FormProductCreate.cs +++ b/Workshop/Forms/FormProductCreate.cs @@ -26,7 +26,7 @@ namespace Workshop.Forms comboBoxProduct.ValueMember = "Id"; comboBoxMaster.DataSource = masterRepository.ReadMasters(); - comboBoxMaster.DisplayMember = "LastName"; + comboBoxMaster.DisplayMember = "FullName"; comboBoxMaster.ValueMember = "Id"; } diff --git a/Workshop/Forms/FormProductCreatings.cs b/Workshop/Forms/FormProductCreatings.cs index 9f25875..37abef8 100644 --- a/Workshop/Forms/FormProductCreatings.cs +++ b/Workshop/Forms/FormProductCreatings.cs @@ -68,7 +68,12 @@ namespace Workshop.Forms MessageBox.Show(ex.Message, "Ошибка при удалении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - private void LoadList() => dataGridProductCreatings.DataSource = _productCreateRepository.ReadProductCreates(); + private void LoadList() + { + dataGridProductCreatings.DataSource = _productCreateRepository.ReadProductCreates(); + dataGridProductCreatings.Columns["Id"].Visible = false; + dataGridProductCreatings.Columns["CreatingDate"].DefaultCellStyle.Format = "dd MMMM yyyy hh:mm"; + } private bool TryGetIdentifierFromSelectedRow(out int id) { id = 0; diff --git a/Workshop/Forms/FormProducts.cs b/Workshop/Forms/FormProducts.cs index d53e9e9..e79d6cc 100644 --- a/Workshop/Forms/FormProducts.cs +++ b/Workshop/Forms/FormProducts.cs @@ -100,6 +100,11 @@ namespace Workshop.Forms id = Convert.ToInt32(dataGridViewProducts.SelectedRows[0].Cells["Id"].Value); return true; } - private void LoadList() => dataGridViewProducts.DataSource = _productRepository.ReadProducts(); + private void LoadList() + { + dataGridViewProducts.DataSource = _productRepository.ReadProducts(); + dataGridViewProducts.Columns["Id"].Visible = false; + dataGridViewProducts.Columns["Materials"].Visible = false; + } } } diff --git a/Workshop/Program.cs b/Workshop/Program.cs index f80a494..afbc268 100644 --- a/Workshop/Program.cs +++ b/Workshop/Program.cs @@ -10,9 +10,7 @@ namespace Workshop { internal static class Program { - //TODO: 1. Update . - //TODO: FormProduct - //TODO: materials + //TODO: chequeId productId TableReport, ChequeRepository, IChequeRepository /// /// The main entry point for the application. /// diff --git a/Workshop/Reports/ChartReport.cs b/Workshop/Reports/ChartReport.cs index d3dc085..8ecbacb 100644 --- a/Workshop/Reports/ChartReport.cs +++ b/Workshop/Reports/ChartReport.cs @@ -43,8 +43,7 @@ internal class ChartReport private List<(string Caption, double Value)> GetData(DateTime dateTimeStart, DateTime dateTimeEnd) { return _creatingRepository - .ReadProductCreates() - .Where(x => x.CreatingDate >= dateTimeStart && x.CreatingDate <= dateTimeEnd) + .ReadProductCreates(dateFrom: dateTimeStart, dateTo: dateTimeEnd) .GroupBy(x => x.ProductId, (key, group) => new { Id = key, diff --git a/Workshop/Reports/TableReport.cs b/Workshop/Reports/TableReport.cs index 5a2a396..dff3759 100644 --- a/Workshop/Reports/TableReport.cs +++ b/Workshop/Reports/TableReport.cs @@ -17,7 +17,7 @@ namespace Workshop.Reports _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } - public bool CreateTable(string filePath, long productId, DateTime startDate, DateTime endDate) + public bool CreateTable(string filePath, int productId, DateTime startDate, DateTime endDate) { try { @@ -35,13 +35,11 @@ namespace Workshop.Reports return false; } } - private List GetData(long productId, DateTime startDate, DateTime + private List GetData(int productId, DateTime startDate, DateTime endDate) { var data = _chequeRepository - .ReadCheques() - .Where(x => x.Date >= startDate && x.Date <= endDate && - x.ChequeProduct.Any(y => y.ProductId == productId)) + .ReadCheques(dateFrom: startDate, dateTo: endDate, productId: productId) .Select(x => new { Date = x.Date, diff --git a/Workshop/Repositories/IChequeRepository.cs b/Workshop/Repositories/IChequeRepository.cs index 6ed982b..6ee68f8 100644 --- a/Workshop/Repositories/IChequeRepository.cs +++ b/Workshop/Repositories/IChequeRepository.cs @@ -9,6 +9,6 @@ namespace Workshop.Repositories; public interface IChequeRepository { - IEnumerable ReadCheques(DateTime? dateFrom = null, DateTime? dateTo = null, int? chequeId = null); + IEnumerable ReadCheques(DateTime? dateFrom = null, DateTime? dateTo = null, int? productId = null); void CreateCheque(Cheque cheque); } diff --git a/Workshop/Repositories/Implementations/ChequeRepository.cs b/Workshop/Repositories/Implementations/ChequeRepository.cs index efcaf9b..c31e074 100644 --- a/Workshop/Repositories/Implementations/ChequeRepository.cs +++ b/Workshop/Repositories/Implementations/ChequeRepository.cs @@ -54,21 +54,47 @@ VALUES (@ProductId, @ChequeID, @Amount)"; } } - public IEnumerable ReadCheques(DateTime? dateFrom = null, DateTime? dateTo = null, int? chequeId = null) + public IEnumerable ReadCheques(DateTime? dateFrom = null, DateTime? dateTo = null, int? productId = null) { _logger.LogInformation("Получение всех объектов"); try { + var builder = new QueryBuilder(); + if (dateFrom.HasValue) + builder.AddCondition("cp.CreatingDate >= @dateFrom"); + if (dateTo.HasValue) + builder.AddCondition("cp.CreatingDate <= @dateTo"); + if (productId.HasValue) + builder.AddCondition("cp.ProductId = @productId"); using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = "SELECT c.*, cp.ProductId, cp.Amount " + "FROM cheque c " + - "INNER JOIN cheque_product cp ON cp.ChequeId = c.Id"; - var cheques = connection.Query(querySelect); + var querySelect = @$"SELECT +c.*, +cp.ProductId, +cp.Amount, +p.Name as ""ProductName"" +FROM Cheque c +INNER JOIN Cheque_Product cp on cp.ChequeId = c.Id +LEFT JOIN Product p on p.Id = cp.ProductId +{builder.Build()}"; + var productDict = new Dictionary>(); + var cheques = connection.Query(querySelect, + (cheque, chequeproduct) => + { + if (!productDict.TryGetValue(cheque.Id, out var cp)) + { + cp = []; + productDict.Add(cheque.Id, cp); + } + cp.Add(chequeproduct); + return cheque; + }, splitOn: "ProductId", param: new { dateFrom, dateTo, productId }); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(cheques)); - return cheques.GroupBy(x => x.Id, y => y, - (key, value) => - Cheque.CreateOperation(value.First(), - value.Select(z => - ChequeProduct.CreateEntity(z.ProductId, z.Id, z.Amount)))).ToList(); + return productDict.Select(x => + { + var c = cheques.First(y => y.Id == x.Key); + c.SetChequeProduct(x.Value); + return c; + }).ToArray(); } catch (Exception ex) { diff --git a/Workshop/Repositories/Implementations/ProductCreateRepository.cs b/Workshop/Repositories/Implementations/ProductCreateRepository.cs index 32720a1..f8fa703 100644 --- a/Workshop/Repositories/Implementations/ProductCreateRepository.cs +++ b/Workshop/Repositories/Implementations/ProductCreateRepository.cs @@ -67,9 +67,25 @@ WHERE Id=@id"; _logger.LogInformation("Получение всех объектов"); try { + var builder = new QueryBuilder(); + if (dateFrom.HasValue) + builder.AddCondition("pc.CreatingDate >= @dateFrom"); + if (dateTo.HasValue) + builder.AddCondition("pc.CreatingDate <= @dateTo"); + if (productId.HasValue) + builder.AddCondition("pc.ProductId = @productId"); + if (masterId.HasValue) + builder.AddCondition("pc.MasterId = @masterId"); using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = "SELECT * FROM PRODUCTCREATE"; - var productCreatings = connection.Query(querySelect); + var querySelect = @$"SELECT +pc.*, +p.Name as ""ProductName"", +CONCAT(m.LastName, ' ', m.Name) as MasterName +FROM ProductCreate pc +LEFT JOIN Product p on p.Id = pc.ProductId +LEFT JOIN Master m on m.Id = pc.MasterId +{builder.Build()}"; + var productCreatings = connection.Query(querySelect, new { dateFrom, dateTo, productId, masterId }); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(productCreatings)); return productCreatings; } @@ -77,7 +93,6 @@ WHERE Id=@id"; { _logger.LogError(ex, "Ошибка при чтении объектов"); throw; - } } } diff --git a/Workshop/Repositories/Implementations/QueryBuilder.cs b/Workshop/Repositories/Implementations/QueryBuilder.cs new file mode 100644 index 0000000..f161085 --- /dev/null +++ b/Workshop/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 Workshop.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}"; + } +}