Лабараторная 4(Отредактированная)

This commit is contained in:
aaahsap 2024-12-23 11:47:02 +04:00
parent 658b0bc060
commit 16d9bd2048
3 changed files with 66 additions and 11 deletions

View File

@ -14,6 +14,9 @@ namespace ProjectSellPC.Entites
public int ChequeID { get; set; } public int ChequeID { get; set; }
[DisplayName("Количество")] [DisplayName("Количество")]
public int Count { get; set; } public int Count { get; set; }
// Временное свойство для отображения названия товара
[Browsable(false)] // Скрываем свойство в DataGridView
public string ProductName { get; set; }
public static ProductInCheque CreateElement(int id, int count) public static ProductInCheque CreateElement(int id, int count)
{ {
return new ProductInCheque { ProductID = id, Count = count }; return new ProductInCheque { ProductID = id, Count = count };

View File

@ -17,16 +17,17 @@ namespace ProjectSellPC.Forms.Receipt
private readonly IUnityContainer _container; private readonly IUnityContainer _container;
private readonly IChequeRepository _ChequeRepository; private readonly IChequeRepository _ChequeRepository;
private readonly IClientRepository _clientRepository; 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(); InitializeComponent();
_container = unityContainer ?? throw new ArgumentNullException(nameof(unityContainer)); _container = unityContainer ?? throw new ArgumentNullException(nameof(unityContainer));
_ChequeRepository = ChequeRepository ?? throw new ArgumentNullException(nameof(ChequeRepository)); _ChequeRepository = ChequeRepository ?? throw new ArgumentNullException(nameof(ChequeRepository));
_clientRepository = clientRepository ?? throw new ArgumentNullException(nameof(clientRepository)); _clientRepository = clientRepository ?? throw new ArgumentNullException(nameof(clientRepository));
_productRepository = productRepository ?? throw new ArgumentNullException(nameof(productRepository)); // Инициализация репозитория товаров
} }
private void ChequeForm_Load(object sender, EventArgs e) private void ChequeForm_Load(object sender, EventArgs e)
{ {
try try
@ -38,17 +39,52 @@ namespace ProjectSellPC.Forms.Receipt
MessageBox.Show(ex.Message, "Ошибка при загрузке", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show(ex.Message, "Ошибка при загрузке", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
private void LoadList() private void LoadList()
{ {
var checks = _ChequeRepository.ReadAll(); try
foreach (var check in checks)
{ {
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) private void addButton_Click(object sender, EventArgs e)
{ {

View File

@ -18,14 +18,16 @@ namespace ProjectSellPC.Repos.Impements
private readonly IConnectionString _connectionString; private readonly IConnectionString _connectionString;
private readonly ILogger<ChequeRepo> _logger; private readonly ILogger<ChequeRepo> _logger;
private readonly IClientRepository _clientRepository; 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)); _connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString));
_logger = loggerFactory.CreateLogger<ChequeRepo>(); _logger = loggerFactory.CreateLogger<ChequeRepo>();
_clientRepository = clientRepository ?? throw new ArgumentNullException(nameof(clientRepository)); _clientRepository = clientRepository ?? throw new ArgumentNullException(nameof(clientRepository));
_productRepository = productRepository ?? throw new ArgumentNullException(nameof(productRepository)); // Инициализация
} }
private IDbConnection CreateConnection() => new NpgsqlConnection(_connectionString.ConnectionString); private IDbConnection CreateConnection() => new NpgsqlConnection(_connectionString.ConnectionString);
public void Create(Cheque cheque) public void Create(Cheque cheque)
{ {
@ -142,6 +144,20 @@ namespace ProjectSellPC.Repos.Impements
{ {
var productQuery = "SELECT * FROM \"productincheque\" WHERE \"chequeid\" = @ChequeId"; var productQuery = "SELECT * FROM \"productincheque\" WHERE \"chequeid\" = @ChequeId";
cheque.Products = connection.Query<ProductInCheque>(productQuery, new { ChequeId = cheque.Id }).ToList(); cheque.Products = connection.Query<ProductInCheque>(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; return cheques;