diff --git a/ProjectGasStation/ProjectGasStation/Entities/Contractor.cs b/ProjectGasStation/ProjectGasStation/Entities/Contractor.cs index 293a6f4..991dca2 100644 --- a/ProjectGasStation/ProjectGasStation/Entities/Contractor.cs +++ b/ProjectGasStation/ProjectGasStation/Entities/Contractor.cs @@ -1,12 +1,18 @@ using ProjectGasStation.Entities.Enums; +using System.ComponentModel; namespace ProjectGasStation.Entities; public class Contractor { public int Id { get; private set; } + + [DisplayName("Название")] public string Name { get; private set; } = string.Empty; + + [DisplayName("Поставляемое топливо")] public ContractorFuelType Types { get; private set; } + public static Contractor CreateContractor(int id, string name, ContractorFuelType types) { return new Contractor { Id = id, Name = name, Types = types }; diff --git a/ProjectGasStation/ProjectGasStation/Entities/ContractorFuel.cs b/ProjectGasStation/ProjectGasStation/Entities/ContractorFuel.cs index 6d29ad6..6f92109 100644 --- a/ProjectGasStation/ProjectGasStation/Entities/ContractorFuel.cs +++ b/ProjectGasStation/ProjectGasStation/Entities/ContractorFuel.cs @@ -1,18 +1,39 @@ -namespace ProjectGasStation.Entities; +using ProjectGasStation.Entities.Enums; +using System.ComponentModel; + +namespace ProjectGasStation.Entities; public class ContractorFuel { public int Id { get; private set; } + + [Browsable(false)] public int ContractorId { get; private set; } + + [DisplayName("Поставщик")] + public string ContractorName { get; private set; } = string.Empty; + + [DisplayName("Дата поставки")] public DateTime Date { get; private set; } + + [DisplayName("Топливо")] + public string Fuel => ContractorFuelFuel != null ? + string.Join(", ", ContractorFuelFuel.Select(x => $"{(FuelType)x.FuelName} {x.Quantity}")) : + string.Empty; + + [Browsable(false)] public IEnumerable ContractorFuelFuel { get; private set; } = []; + public static ContractorFuel CreateContractorFuel(int id, int contractorId, DateTime date, IEnumerable contractorFuelFuel) { return new ContractorFuel { Id = id, ContractorId = contractorId, Date = date, ContractorFuelFuel = contractorFuelFuel}; } - public static ContractorFuel CreateContractorFuel(TempContractorFuelFuel tempContractorFuelFuel, IEnumerable contractorFuelFuel) + public void SetContractorFuelFuel(IEnumerable contractorFuelFuel) { - return new ContractorFuel { Id = tempContractorFuelFuel.Id, ContractorId = tempContractorFuelFuel.ContractorId, Date = tempContractorFuelFuel.Date, ContractorFuelFuel = contractorFuelFuel }; + if (contractorFuelFuel != null && contractorFuelFuel.Any()) + { + ContractorFuelFuel = contractorFuelFuel; + } } } \ No newline at end of file diff --git a/ProjectGasStation/ProjectGasStation/Entities/ContractorFuelFuel.cs b/ProjectGasStation/ProjectGasStation/Entities/ContractorFuelFuel.cs index 014df03..ac2811d 100644 --- a/ProjectGasStation/ProjectGasStation/Entities/ContractorFuelFuel.cs +++ b/ProjectGasStation/ProjectGasStation/Entities/ContractorFuelFuel.cs @@ -3,8 +3,13 @@ public class ContractorFuelFuel { public int Id { get; private set; } + public int FuelId { get; private set; } + + public int FuelName { get; private set; } + public int Quantity { get; private set; } + public static ContractorFuelFuel CreateContractorFuelFuel(int id, int fuelId, int quantity) { return new ContractorFuelFuel { Id = id, FuelId = fuelId, Quantity = quantity }; diff --git a/ProjectGasStation/ProjectGasStation/Entities/Fuel.cs b/ProjectGasStation/ProjectGasStation/Entities/Fuel.cs index 17c82cc..16a4ccd 100644 --- a/ProjectGasStation/ProjectGasStation/Entities/Fuel.cs +++ b/ProjectGasStation/ProjectGasStation/Entities/Fuel.cs @@ -1,12 +1,18 @@ using ProjectGasStation.Entities.Enums; +using System.ComponentModel; namespace ProjectGasStation.Entities; public class Fuel { public int Id { get; private set; } + + [DisplayName("Тип топлива")] public FuelType Type { get; private set; } + + [DisplayName("Цена")] public double Price { get; private set; } + public static Fuel CreateFuel(int id, FuelType type, double price) { return new Fuel { Id = id, Type = type, Price = price }; diff --git a/ProjectGasStation/ProjectGasStation/Entities/FuelFuelSale.cs b/ProjectGasStation/ProjectGasStation/Entities/FuelFuelSale.cs index ed9cf35..9474945 100644 --- a/ProjectGasStation/ProjectGasStation/Entities/FuelFuelSale.cs +++ b/ProjectGasStation/ProjectGasStation/Entities/FuelFuelSale.cs @@ -3,8 +3,13 @@ public class FuelFuelSale { public int Id { get; private set; } + public int FuelId { get; private set; } + + public int FuelName { get; private set; } + public int Quantity { get; private set; } + public static FuelFuelSale CreateFuelFuelSale(int id, int fuelId, int quantity) { return new FuelFuelSale { Id = id, FuelId = fuelId, Quantity = quantity}; diff --git a/ProjectGasStation/ProjectGasStation/Entities/FuelSale.cs b/ProjectGasStation/ProjectGasStation/Entities/FuelSale.cs index cd1b0ff..59708d2 100644 --- a/ProjectGasStation/ProjectGasStation/Entities/FuelSale.cs +++ b/ProjectGasStation/ProjectGasStation/Entities/FuelSale.cs @@ -1,11 +1,36 @@ -namespace ProjectGasStation.Entities; +using ProjectGasStation.Entities.Enums; +using System.ComponentModel; + +namespace ProjectGasStation.Entities; public class FuelSale { public int Id { get; private set; } + + [Browsable(false)] public int SalespersonId { get; private set; } + + [Browsable(false)] public int ShiftId { get; private set; } + + [DisplayName("Продавец")] + public string SalespersonName { get; private set; } = string.Empty; + + [Browsable(false)] + public int TypeShift { get; private set; } + + [DisplayName("Смена")] + public string ShiftName => $"{SaleDate.Date.ToString("dd.MM.yyyy")} {(ShiftType)TypeShift}"; + + [Browsable(false)] public DateTime SaleDate { get; private set; } + + [DisplayName("Топливо")] + public string Fuel => FuelFuelSale != null ? + string.Join(", ", FuelFuelSale.Select(x => $"{(FuelType)x.FuelName} {x.Quantity}")) : + string.Empty; + + [Browsable(false)] public IEnumerable FuelFuelSale { get; private set; } = []; public static FuelSale CreateFuelSale(int id, int salesPersonId, int shiftId, DateTime saleDate, IEnumerable fuelFuelSale) @@ -20,15 +45,11 @@ public class FuelSale }; } - public static FuelSale CreateFuelSale(TempFuelFuelSale tempFuelFuelSale, IEnumerable fuelFuelSale) + public void SetFuelFuelSale(IEnumerable fuelFuelSale) { - return new FuelSale + if(fuelFuelSale != null && fuelFuelSale.Any()) { - Id = tempFuelFuelSale.Id, - SalespersonId = tempFuelFuelSale.SalespersonId, - ShiftId = tempFuelFuelSale.ShiftId, - SaleDate = tempFuelFuelSale.SaleDate, - FuelFuelSale = fuelFuelSale - }; + FuelFuelSale = fuelFuelSale; + } } } diff --git a/ProjectGasStation/ProjectGasStation/Entities/Salesperson.cs b/ProjectGasStation/ProjectGasStation/Entities/Salesperson.cs index 671848f..cb4f849 100644 --- a/ProjectGasStation/ProjectGasStation/Entities/Salesperson.cs +++ b/ProjectGasStation/ProjectGasStation/Entities/Salesperson.cs @@ -1,10 +1,17 @@ -namespace ProjectGasStation.Entities; +using System.ComponentModel; + +namespace ProjectGasStation.Entities; public class Salesperson { public int Id { get; private set; } + + [DisplayName("Имя")] public string FirstName { get; private set; } = string.Empty; + + [DisplayName("Фамилия")] public string LastName { get; private set; } = string.Empty; + public static Salesperson CreateSalesperson(int id, string firstName, string lastName) { return new Salesperson { Id = id, FirstName = firstName, LastName = lastName }; diff --git a/ProjectGasStation/ProjectGasStation/Entities/Shift.cs b/ProjectGasStation/ProjectGasStation/Entities/Shift.cs index 9dcaf72..2406273 100644 --- a/ProjectGasStation/ProjectGasStation/Entities/Shift.cs +++ b/ProjectGasStation/ProjectGasStation/Entities/Shift.cs @@ -1,15 +1,26 @@ using ProjectGasStation.Entities.Enums; +using System.ComponentModel; namespace ProjectGasStation.Entities; public class Shift { public int Id { get; private set; } + + [DisplayName("Время начала")] public TimeSpan StartTime { get; private set; } + + [DisplayName("Время конца")] public TimeSpan EndTime { get; private set; } + + [DisplayName("Дата")] public DateTime Date { get; private set; } + + [DisplayName("Тип смены")] public ShiftType Type { get; private set; } + public string DisplayName => $"{Date:yyyy-MM-dd} ({Type})"; + public static Shift CreateShift(int id, TimeSpan startTime, TimeSpan endTime, DateTime date, ShiftType type) { return new Shift { Id = id, EndTime = endTime, StartTime = startTime, Date = date, Type = type }; diff --git a/ProjectGasStation/ProjectGasStation/Entities/TempContractorFuelFuel.cs b/ProjectGasStation/ProjectGasStation/Entities/TempContractorFuelFuel.cs deleted file mode 100644 index 8e8e158..0000000 --- a/ProjectGasStation/ProjectGasStation/Entities/TempContractorFuelFuel.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ProjectGasStation.Entities; - -public class TempContractorFuelFuel -{ - public int Id { get; private set; } - public int ContractorId { get; private set; } - public DateTime Date { get; private set; } - public int FuelId { get; private set; } - public int Quantity { get; private set; } -} diff --git a/ProjectGasStation/ProjectGasStation/Entities/TempFuelFuelSale.cs b/ProjectGasStation/ProjectGasStation/Entities/TempFuelFuelSale.cs deleted file mode 100644 index f8e1f72..0000000 --- a/ProjectGasStation/ProjectGasStation/Entities/TempFuelFuelSale.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ProjectGasStation.Entities; - -public class TempFuelFuelSale -{ - public int Id { get; private set; } - public int SalespersonId { get; private set; } - public int ShiftId { get; private set; } - public DateTime SaleDate { get; private set; } - public int FuelId { get; private set; } - public int Quantity { get; private set; } -} diff --git a/ProjectGasStation/ProjectGasStation/Forms/FormContractorFuels.cs b/ProjectGasStation/ProjectGasStation/Forms/FormContractorFuels.cs index 6b7c1f8..68207ce 100644 --- a/ProjectGasStation/ProjectGasStation/Forms/FormContractorFuels.cs +++ b/ProjectGasStation/ProjectGasStation/Forms/FormContractorFuels.cs @@ -19,7 +19,7 @@ public partial class FormContractorFuels : Form try { LoadList(); - } + } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при загрузке", MessageBoxButtons.OK, MessageBoxIcon.Error); @@ -63,7 +63,11 @@ public partial class FormContractorFuels : Form } } - private void LoadList() => dataGridViewData.DataSource = _contractorFuelRepository.ReadContractorFuels(); + private void LoadList() + { + dataGridViewData.DataSource = _contractorFuelRepository.ReadContractorFuels(); + dataGridViewData.Columns["Id"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { diff --git a/ProjectGasStation/ProjectGasStation/Forms/FormContractors.cs b/ProjectGasStation/ProjectGasStation/Forms/FormContractors.cs index 13b7763..f780211 100644 --- a/ProjectGasStation/ProjectGasStation/Forms/FormContractors.cs +++ b/ProjectGasStation/ProjectGasStation/Forms/FormContractors.cs @@ -82,7 +82,11 @@ public partial class FormContractors : Form } } - private void LoadList() => dataGridViewData.DataSource = _contractorRepository.ReadContractors(); + private void LoadList() + { + dataGridViewData.DataSource = _contractorRepository.ReadContractors(); + dataGridViewData.Columns["Id"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { diff --git a/ProjectGasStation/ProjectGasStation/Forms/FormFuelSales.cs b/ProjectGasStation/ProjectGasStation/Forms/FormFuelSales.cs index 8733cd8..4c92885 100644 --- a/ProjectGasStation/ProjectGasStation/Forms/FormFuelSales.cs +++ b/ProjectGasStation/ProjectGasStation/Forms/FormFuelSales.cs @@ -19,7 +19,7 @@ public partial class FormFuelSales : Form try { LoadList(); - } + } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при загрузке", MessageBoxButtons.OK, MessageBoxIcon.Error); @@ -62,7 +62,11 @@ public partial class FormFuelSales : Form } } - private void LoadList() => dataGridViewData.DataSource = _fuelSaleRepository.ReadFuelSales(); + private void LoadList() + { + dataGridViewData.DataSource = _fuelSaleRepository.ReadFuelSales(); + dataGridViewData.Columns["Id"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { diff --git a/ProjectGasStation/ProjectGasStation/Forms/FormFuels.cs b/ProjectGasStation/ProjectGasStation/Forms/FormFuels.cs index a171c7a..787405c 100644 --- a/ProjectGasStation/ProjectGasStation/Forms/FormFuels.cs +++ b/ProjectGasStation/ProjectGasStation/Forms/FormFuels.cs @@ -19,7 +19,7 @@ public partial class FormFuels : Form try { LoadList(); - } + } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при загрузке", MessageBoxButtons.OK, MessageBoxIcon.Error); @@ -82,7 +82,11 @@ public partial class FormFuels : Form } } - private void LoadList() => dataGridViewData.DataSource = _fuelRepository.ReadFuels(); + private void LoadList() + { + dataGridViewData.DataSource = _fuelRepository.ReadFuels(); + dataGridViewData.Columns["Id"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { diff --git a/ProjectGasStation/ProjectGasStation/Forms/FormSalespersons.cs b/ProjectGasStation/ProjectGasStation/Forms/FormSalespersons.cs index 92e3698..d42c981 100644 --- a/ProjectGasStation/ProjectGasStation/Forms/FormSalespersons.cs +++ b/ProjectGasStation/ProjectGasStation/Forms/FormSalespersons.cs @@ -82,7 +82,11 @@ public partial class FormSalespersons : Form } } - private void LoadList() => dataGridViewData.DataSource = _salespersonRepository.ReadSalespersons(); + private void LoadList() + { + dataGridViewData.DataSource = _salespersonRepository.ReadSalespersons(); + dataGridViewData.Columns["Id"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { diff --git a/ProjectGasStation/ProjectGasStation/Forms/FormShifts.cs b/ProjectGasStation/ProjectGasStation/Forms/FormShifts.cs index 627966e..ac7e62d 100644 --- a/ProjectGasStation/ProjectGasStation/Forms/FormShifts.cs +++ b/ProjectGasStation/ProjectGasStation/Forms/FormShifts.cs @@ -19,7 +19,7 @@ public partial class FormShifts : Form try { LoadList(); - } + } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при загрузке", MessageBoxButtons.OK, MessageBoxIcon.Error); @@ -82,7 +82,12 @@ public partial class FormShifts : Form } } - private void LoadList() => dataGridViewData.DataSource = _shiftRepository.ReadShifts(); + private void LoadList() + { + dataGridViewData.DataSource = _shiftRepository.ReadShifts(); + dataGridViewData.Columns["Id"].Visible = false; + dataGridViewData.Columns["DisplayName"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { diff --git a/ProjectGasStation/ProjectGasStation/Repositories/Implementations/ContractorFuelRepository.cs b/ProjectGasStation/ProjectGasStation/Repositories/Implementations/ContractorFuelRepository.cs index 6bb08a8..11283e6 100644 --- a/ProjectGasStation/ProjectGasStation/Repositories/Implementations/ContractorFuelRepository.cs +++ b/ProjectGasStation/ProjectGasStation/Repositories/Implementations/ContractorFuelRepository.cs @@ -1,7 +1,9 @@ using Dapper; +using DocumentFormat.OpenXml.Spreadsheet; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Npgsql; +using PdfSharp.Pdf; using ProjectGasStation.Entities; using System.Transactions; @@ -91,12 +93,39 @@ public class ContractorFuelRepository : IContractorFuelRepository try { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = @"SELECT cf.*, cff.FuelId, cff.Quantity FROM ContractorFuel cf - INNER JOIN ContractorFuelFuel cff on cff.ContractorFuelId = cf.Id"; - var contractorFuels = connection.Query(querySelect); + var querySelect = @"SELECT + cf.*, + c.Name as ContractorName, + cff.FuelId, + cff.Quantity, + f.Type as FuelName + FROM ContractorFuel cf + LEFT JOIN Contractor c on c.Id = cf.ContractorId + INNER JOIN ContractorFuelFuel cff on cff.ContractorFuelId = cf.Id + LEFT JOIN Fuel f on f.Id = cff.FuelId"; + var contractorsDict = new Dictionary>(); + + var contractorFuels = connection.Query(querySelect, + (contractor, contractorFuel) => + { + if (!contractorsDict.TryGetValue(contractor.Id, out var ccf)) + { + ccf = []; + contractorsDict.Add(contractor.Id, ccf); + } + + ccf.Add(contractorFuel); + return contractor; + }, splitOn: "FuelId"); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(contractorFuels)); - return contractorFuels.GroupBy(x => x.Id, y => y, (key, value) => ContractorFuel.CreateContractorFuel(value.First(), value.Select(z => ContractorFuelFuel.CreateContractorFuelFuel(0, z.FuelId, z.Quantity)))).ToList(); + + return contractorsDict.Select(x => + { + var cf = contractorFuels.First(y => y.Id == x.Key); + cf.SetContractorFuelFuel(x.Value); + return cf; + }).ToArray(); } catch (Exception ex) { diff --git a/ProjectGasStation/ProjectGasStation/Repositories/Implementations/FuelSaleRepository.cs b/ProjectGasStation/ProjectGasStation/Repositories/Implementations/FuelSaleRepository.cs index a6f094b..fa89008 100644 --- a/ProjectGasStation/ProjectGasStation/Repositories/Implementations/FuelSaleRepository.cs +++ b/ProjectGasStation/ProjectGasStation/Repositories/Implementations/FuelSaleRepository.cs @@ -61,12 +61,41 @@ public class FuelSaleRepository : IFuelSaleRepository try { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = @"SELECT fs.*, ffs.FuelId, ffs.Quantity FROM FuelSale fs - INNER JOIN FuelFuelSale ffs on ffs.FuelSaleId = fs.Id"; - var fuelSales = connection.Query(querySelect); + var querySelect = @"SELECT + fs.*, + CONCAT(s.LastName, ' ', s.FirstName) as SalespersonName, + sh.Type as TypeShift, + ffs.FuelId, + ffs.Quantity, + f.Type as FuelName + FROM FuelSale fs + LEFT JOIN Salesperson s on s.Id = fs.SalespersonId + LEFT JOIN Shift sh on sh.id = fs.ShiftId + INNER JOIN FuelFuelSale ffs on ffs.FuelSaleId = fs.Id + LEFT JOIN Fuel f on f.id = ffs.FuelId"; + var salesDict = new Dictionary>(); + + var fuelSales = connection.Query(querySelect, + (sale, fuelSale) => + { + if (!salesDict.TryGetValue(sale.Id, out var fss)) + { + fss = []; + salesDict.Add(sale.Id, fss); + } + + fss.Add(fuelSale); + return sale; + }, splitOn: "FuelId"); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(fuelSales)); - return fuelSales.GroupBy(x => x.Id, y => y, (key, value) => FuelSale.CreateFuelSale(value.First(), value.Select(z => FuelFuelSale.CreateFuelFuelSale(0, z.FuelId, z.Quantity)))).ToList(); + + return salesDict.Select(x => + { + var fs = fuelSales.First(y => y.Id == x.Key); + fs.SetFuelFuelSale(x.Value); + return fs; + }).ToArray(); } catch (Exception ex) {