diff --git a/CompShop/CompShop/DocumentsBuilder/QueryBuilder.cs b/CompShop/CompShop/DocumentsBuilder/QueryBuilder.cs new file mode 100644 index 0000000..f233fbd --- /dev/null +++ b/CompShop/CompShop/DocumentsBuilder/QueryBuilder.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CompShop.DocumentsBuilder +{ + 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}"; + } + } +} diff --git a/CompShop/CompShop/Entites/Check.cs b/CompShop/CompShop/Entites/Check.cs index 6802aca..10ae57f 100644 --- a/CompShop/CompShop/Entites/Check.cs +++ b/CompShop/CompShop/Entites/Check.cs @@ -1,15 +1,20 @@ using CompShop.Entites; +using System.ComponentModel; namespace CompShop.Entites { public class Check { + [System.ComponentModel.Browsable(false)] public int Id { get; set; } + [DisplayName("Товары")] public List Products { get; set; } + [DisplayName("Клиент")] public Client Client { get; set; } [System.ComponentModel.Browsable(false)] - public int ClientId { get; set; } + public int ClientId { get; set; } + [DisplayName("Дата покупки")] public DateTime PurchaseDate { get; set; } public static Check CreateEntity(int id, List products, Client client, DateTime purchaseDate) diff --git a/CompShop/CompShop/Entites/Client.cs b/CompShop/CompShop/Entites/Client.cs index 0bc9180..29734c5 100644 --- a/CompShop/CompShop/Entites/Client.cs +++ b/CompShop/CompShop/Entites/Client.cs @@ -1,6 +1,7 @@ using CompShop.Entites.Enums; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -10,8 +11,12 @@ namespace CompShop.Entites public class Client { public int Id { get; set; } + [DisplayName("Имя")] public string Name { get; set; } + [DisplayName("Номер телефона")] public string PhoneNumber { get; set; } + + [DisplayName("Вид клиента")] public ClientType ClientType { get; set; } public static Client CreateEntity(int id, string name, string phoneNumber, ClientType clientType) diff --git a/CompShop/CompShop/Entites/Product.cs b/CompShop/CompShop/Entites/Product.cs index d339bb4..d6d753e 100644 --- a/CompShop/CompShop/Entites/Product.cs +++ b/CompShop/CompShop/Entites/Product.cs @@ -1,13 +1,18 @@ using CompShop.Entites.Enums; +using System.ComponentModel; namespace CompShop.Entites { public class Product { public int ID { get; private set; } + [DisplayName("Имя")] public string Name { get; private set; } + [DisplayName("Описание")] public string Description { get; private set; } + [DisplayName("Цена")] public decimal Price { get; private set; } + [DisplayName("Вид")] public ProductType ProductType { get; private set; } public static Product CreateEntity(int id, string name, string desc, decimal price, ProductType productType) diff --git a/CompShop/CompShop/Entites/ProductInCheck.cs b/CompShop/CompShop/Entites/ProductInCheck.cs index d88b584..6a6f9e2 100644 --- a/CompShop/CompShop/Entites/ProductInCheck.cs +++ b/CompShop/CompShop/Entites/ProductInCheck.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -8,9 +9,13 @@ namespace CompShop.Entites { public class ProductInCheck { + [System.ComponentModel.Browsable(false)] public int ID { get; set; } + [Browsable(false)] public int ProductID { get; set; } + [Browsable(false)] public int CheckID { get; set; } + [DisplayName("Количество")] public int Count { get; set; } public static ProductInCheck CreateElement(int id, int count) { diff --git a/CompShop/CompShop/Entites/ProductsOnStorage.cs b/CompShop/CompShop/Entites/ProductsOnStorage.cs index e887310..d60730d 100644 --- a/CompShop/CompShop/Entites/ProductsOnStorage.cs +++ b/CompShop/CompShop/Entites/ProductsOnStorage.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -8,13 +9,17 @@ namespace CompShop.Entites { public class ProductsOnStorage { + [System.ComponentModel.Browsable(false)] public int Id { get; set; } [System.ComponentModel.Browsable(false)] public int ProductId { get; set; } + [DisplayName("Товар")] public Product Product { get; set; } [System.ComponentModel.Browsable(false)] public int StorageId { get; set; } + [DisplayName("Склад")] public Storage Storage { get; set; } + [DisplayName("Количество")] public int Count { get; set; } public static ProductsOnStorage CreateEntity(int id, Product product, Storage storage, int count) diff --git a/CompShop/CompShop/Entites/Storage.cs b/CompShop/CompShop/Entites/Storage.cs index 5e937b4..e269290 100644 --- a/CompShop/CompShop/Entites/Storage.cs +++ b/CompShop/CompShop/Entites/Storage.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,8 +10,11 @@ namespace CompShop.Entites { public class Storage { + [System.ComponentModel.Browsable(false)] public int Id { get; set; } + [DisplayName("Вместимость")] public int Size { get; set; } // Вместимость + [DisplayName("Адрес")] public string Adress { get; set; } public static Storage CreateEntity(int id, int size, string adress) diff --git a/CompShop/CompShop/Forms/Clients/ClientForm.cs b/CompShop/CompShop/Forms/Clients/ClientForm.cs index a14ddb5..5ff8bd3 100644 --- a/CompShop/CompShop/Forms/Clients/ClientForm.cs +++ b/CompShop/CompShop/Forms/Clients/ClientForm.cs @@ -37,7 +37,12 @@ namespace CompShop.Forms MessageBox.Show(ex.Message, "Ошибка при загрузке", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - private void LoadList() => productsDataGridView.DataSource = _repository.ReadAll(); + private void LoadList() + { + + productsDataGridView.DataSource = _repository.ReadAll(); + productsDataGridView.Columns["Id"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { diff --git a/CompShop/CompShop/Forms/Products/ProductForm.cs b/CompShop/CompShop/Forms/Products/ProductForm.cs index f051627..b723fb2 100644 --- a/CompShop/CompShop/Forms/Products/ProductForm.cs +++ b/CompShop/CompShop/Forms/Products/ProductForm.cs @@ -36,7 +36,12 @@ namespace CompShop MessageBox.Show(ex.Message, "Ошибка при загрузке", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - private void LoadList() => productsDataGridView.DataSource = _productRepository.ReadAll(); + private void LoadList() + { + + productsDataGridView.DataSource = _productRepository.ReadAll(); + productsDataGridView.Columns["ID"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { diff --git a/CompShop/CompShop/Forms/Receipt/CheckForm.cs b/CompShop/CompShop/Forms/Receipt/CheckForm.cs index 85639e4..b117550 100644 --- a/CompShop/CompShop/Forms/Receipt/CheckForm.cs +++ b/CompShop/CompShop/Forms/Receipt/CheckForm.cs @@ -16,13 +16,15 @@ namespace CompShop.Forms.Receipt { private readonly IUnityContainer _container; private readonly ICheckRepository _checkRepository; + private readonly IClientRepository _clientRepository; - public CheckForm(IUnityContainer unityContainer, ICheckRepository checkRepository) + public CheckForm(IUnityContainer unityContainer, ICheckRepository checkRepository, IClientRepository clientRepository) { InitializeComponent(); _container = unityContainer ?? throw new ArgumentNullException(nameof(unityContainer)); _checkRepository = checkRepository ?? throw new ArgumentNullException(nameof(checkRepository)); + _clientRepository = clientRepository ?? throw new ArgumentNullException(nameof(clientRepository)); } private void CheckForm_Load(object sender, EventArgs e) @@ -37,7 +39,17 @@ namespace CompShop.Forms.Receipt } } - private void LoadList() => checksDataGridView.DataSource = _checkRepository.ReadAll(); + private void LoadList() + { + var checks = _checkRepository.ReadAll(); + + foreach (var check in checks) + { + check.Client = _clientRepository.Read(check.ClientId); + } + + checksDataGridView.DataSource = checks; + } private void addButton_Click(object sender, EventArgs e) { diff --git a/CompShop/CompShop/Repos/ICheckRepository.cs b/CompShop/CompShop/Repos/ICheckRepository.cs index 1572f60..78f74ab 100644 --- a/CompShop/CompShop/Repos/ICheckRepository.cs +++ b/CompShop/CompShop/Repos/ICheckRepository.cs @@ -4,7 +4,7 @@ namespace CompShop.Repos { public interface ICheckRepository { - IEnumerable ReadAll(); + IEnumerable ReadAll(DateTime? startDate = null, DateTime? endDate = null, int? productId = null, int? clientId = null); Check Read(int id); void Create(Check check); } diff --git a/CompShop/CompShop/Repos/Impements/CheckRepo.cs b/CompShop/CompShop/Repos/Impements/CheckRepo.cs index 207582f..964db5a 100644 --- a/CompShop/CompShop/Repos/Impements/CheckRepo.cs +++ b/CompShop/CompShop/Repos/Impements/CheckRepo.cs @@ -1,11 +1,15 @@ -using CompShop.Entites; +using CompShop.DocumentsBuilder; +using CompShop.Entites; using Dapper; +using DocumentFormat.OpenXml.Drawing.Charts; using Microsoft.Extensions.Logging; +using Newtonsoft.Json; using Npgsql; using System; using System.Collections.Generic; using System.Data; using System.Linq; +using System.Windows.Forms; namespace CompShop.Repos.Impements { @@ -92,32 +96,54 @@ namespace CompShop.Repos.Impements } } - public IEnumerable ReadAll() + public IEnumerable ReadAll(DateTime? startDate = null, DateTime? endDate = null, int? productId = null, int? clientId = null) { - _logger.LogInformation("Чтение всех чеков"); - using (var connection = CreateConnection()) + _logger.LogInformation("Чтение всех чеков с использованием фильтров"); + try { - try - { - var checksSql = "SELECT * FROM \"Checks\""; - var checks = connection.Query(checksSql).ToList(); + var builder = new QueryBuilder(); - foreach (var check in checks) + // Добавляем условия фильтрации, если параметры заданы + if (startDate.HasValue) + builder.AddCondition("\"Checks\".\"PurchaseDate\" >= @StartDate"); + if (endDate.HasValue) + builder.AddCondition("\"Checks\".\"PurchaseDate\" <= @EndDate"); + if (productId.HasValue) + builder.AddCondition("\"ProductsInCheck\".\"ProductID\" = @ProductId"); + if (clientId.HasValue) + builder.AddCondition("\"Checks\".\"ClientId\" = @ClientId"); + + // Формируем основной SQL-запрос + var query = $"SELECT \"Checks\".*, \"Clients\".\"Name\" AS \"ClientName\" FROM \"Checks\" LEFT JOIN \"Clients\" ON \"Checks\".\"ClientId\" = \"Clients\".\"Id\" LEFT JOIN \"ProductsInCheck\" ON \"ProductsInCheck\".\"CheckId\" = \"Checks\".\"Id\" { builder.Build()} GROUP BY \"Checks\".\"Id\", \"Clients\".\"Name\" ORDER BY \"Checks\".\"PurchaseDate\""; + + // Выполняем запрос + using var connection = CreateConnection(); + var checks = connection.Query( + query, + (check, client) => { - check.Client = _clientRepository.Read(check.ClientId); + check.Client = client; + return check; + }, + new { StartDate = startDate, EndDate = endDate, ProductId = productId, ClientId = clientId }, + splitOn: "ClientName" + ).ToList(); - var productSql = "SELECT * FROM \"ProductsInCheck\" WHERE \"CheckId\" = @CheckId"; - check.Products = connection.Query(productSql, new { CheckId = check.Id }).ToList(); - } - - return checks; - } - catch (Exception ex) + // Загружаем товары для каждого чека + foreach (var check in checks) { - _logger.LogError(ex, "Ошибка при чтении всех чеков"); - throw; + var productQuery = "SELECT * FROM \"ProductsInCheck\" WHERE \"CheckId\" = @CheckId"; + check.Products = connection.Query(productQuery, new { CheckId = check.Id }).ToList(); } + + return checks; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении всех чеков"); + throw; } } + } }