From 16d9bd2048e07040525a061e31ed557bfccef478 Mon Sep 17 00:00:00 2001 From: aaahsap Date: Mon, 23 Dec 2024 11:47:02 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=B0=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=204(=D0=9E=D1=82=D1=80=D0=B5?= =?UTF-8?q?=D0=B4=D0=B0=D0=BA=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD?= =?UTF-8?q?=D0=BD=D0=B0=D1=8F)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProjectSellPC/Entites/ProductInCheque..cs | 3 ++ .../ProjectSellPC/Forms/Cheque/ChequeForm.cs | 54 +++++++++++++++---- .../Repos/Impements/ChequeRepo.cs | 20 ++++++- 3 files changed, 66 insertions(+), 11 deletions(-) diff --git a/ProjectSellPC/ProjectSellPC/Entites/ProductInCheque..cs b/ProjectSellPC/ProjectSellPC/Entites/ProductInCheque..cs index 268be9f..d51cf7d 100644 --- a/ProjectSellPC/ProjectSellPC/Entites/ProductInCheque..cs +++ b/ProjectSellPC/ProjectSellPC/Entites/ProductInCheque..cs @@ -14,6 +14,9 @@ namespace ProjectSellPC.Entites public int ChequeID { get; set; } [DisplayName("Количество")] public int Count { get; set; } + // Временное свойство для отображения названия товара + [Browsable(false)] // Скрываем свойство в DataGridView + public string ProductName { get; set; } public static ProductInCheque CreateElement(int id, int count) { return new ProductInCheque { ProductID = id, Count = count }; diff --git a/ProjectSellPC/ProjectSellPC/Forms/Cheque/ChequeForm.cs b/ProjectSellPC/ProjectSellPC/Forms/Cheque/ChequeForm.cs index 7680c88..ec4b599 100644 --- a/ProjectSellPC/ProjectSellPC/Forms/Cheque/ChequeForm.cs +++ b/ProjectSellPC/ProjectSellPC/Forms/Cheque/ChequeForm.cs @@ -17,16 +17,17 @@ namespace ProjectSellPC.Forms.Receipt private readonly IUnityContainer _container; private readonly IChequeRepository _ChequeRepository; private readonly IClientRepository _clientRepository; + private readonly IProductRepository _productRepository; // Добавляем репозиторий товаров - public ChequeForm(IUnityContainer unityContainer, IChequeRepository ChequeRepository, IClientRepository clientRepository) + public ChequeForm(IUnityContainer unityContainer, IChequeRepository ChequeRepository, IClientRepository clientRepository, IProductRepository productRepository) { InitializeComponent(); _container = unityContainer ?? throw new ArgumentNullException(nameof(unityContainer)); _ChequeRepository = ChequeRepository ?? throw new ArgumentNullException(nameof(ChequeRepository)); _clientRepository = clientRepository ?? throw new ArgumentNullException(nameof(clientRepository)); + _productRepository = productRepository ?? throw new ArgumentNullException(nameof(productRepository)); // Инициализация репозитория товаров } - private void ChequeForm_Load(object sender, EventArgs e) { try @@ -38,17 +39,52 @@ namespace ProjectSellPC.Forms.Receipt MessageBox.Show(ex.Message, "Ошибка при загрузке", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - private void LoadList() { - var checks = _ChequeRepository.ReadAll(); - - foreach (var check in checks) + try { - check.Client = _clientRepository.Read(check.ClientId); - } + // Получаем все чеки + var cheques = _ChequeRepository.ReadAll(); - ChequesDataGridView.DataSource = checks; + // Загружаем информацию о клиентах + foreach (var cheque in cheques) + { + cheque.Client = _clientRepository.Read(cheque.ClientId); + + // Загружаем названия товаров для каждого чека + foreach (var productInCheque in cheque.Products) + { + var product = _productRepository.Read(productInCheque.ProductID); + if (product != null) + { + productInCheque.ProductName = product.Name; // Добавляем название товара + } + else + { + productInCheque.ProductName = "Неизвестный товар"; + } + } + } + + // Привязываем данные к DataGridView + ChequesDataGridView.DataSource = cheques.Select(c => new + { + Id = c.Id, + ClientName = c.Client?.Name ?? "Неизвестно", + PurchaseDate = c.PurchaseDate, + Products = string.Join(", ", c.Products.Select(p => p.ProductName)) // Отображаем названия товаров + }).ToList(); + + // Настройка столбцов DataGridView + ChequesDataGridView.Columns["Id"].HeaderText = "ID"; + ChequesDataGridView.Columns["ClientName"].HeaderText = "Клиент"; + ChequesDataGridView.Columns["PurchaseDate"].HeaderText = "Дата покупки"; + ChequesDataGridView.Columns["Products"].HeaderText = "Товары"; + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка при загрузке", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } private void addButton_Click(object sender, EventArgs e) { diff --git a/ProjectSellPC/ProjectSellPC/Repos/Impements/ChequeRepo.cs b/ProjectSellPC/ProjectSellPC/Repos/Impements/ChequeRepo.cs index 924ed68..6f5ea96 100644 --- a/ProjectSellPC/ProjectSellPC/Repos/Impements/ChequeRepo.cs +++ b/ProjectSellPC/ProjectSellPC/Repos/Impements/ChequeRepo.cs @@ -18,14 +18,16 @@ namespace ProjectSellPC.Repos.Impements private readonly IConnectionString _connectionString; private readonly ILogger _logger; private readonly IClientRepository _clientRepository; + private readonly IProductRepository _productRepository; // Добавляем зависимость - public ChequeRepo(IConnectionString connectionString, ILoggerFactory loggerFactory, IClientRepository clientRepository) + public ChequeRepo(IConnectionString connectionString, ILoggerFactory loggerFactory, + IClientRepository clientRepository, IProductRepository productRepository) { _connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString)); _logger = loggerFactory.CreateLogger(); _clientRepository = clientRepository ?? throw new ArgumentNullException(nameof(clientRepository)); + _productRepository = productRepository ?? throw new ArgumentNullException(nameof(productRepository)); // Инициализация } - private IDbConnection CreateConnection() => new NpgsqlConnection(_connectionString.ConnectionString); public void Create(Cheque cheque) { @@ -142,6 +144,20 @@ namespace ProjectSellPC.Repos.Impements { var productQuery = "SELECT * FROM \"productincheque\" WHERE \"chequeid\" = @ChequeId"; cheque.Products = connection.Query(productQuery, new { ChequeId = cheque.Id }).ToList(); + + // Загружаем названия товаров + foreach (var productInCheque in cheque.Products) + { + var product = _productRepository.Read(productInCheque.ProductID); + if (product != null) + { + productInCheque.ProductName = product.Name; + } + else + { + productInCheque.ProductName = "Неизвестный товар"; + } + } } return cheques;