diff --git a/GasStation/Entities/Gasman.cs b/GasStation/Entities/Gasman.cs index bf0d062..1c070b0 100644 --- a/GasStation/Entities/Gasman.cs +++ b/GasStation/Entities/Gasman.cs @@ -1,4 +1,5 @@ using GasStation.Entities.Enums; +using System.ComponentModel; namespace GasStation.Entities; @@ -6,10 +7,13 @@ public class Gasman { public int Id { get; private set; } + [DisplayName("Имя заправщика")] public string GasmanName { get; private set; } = string.Empty; + [DisplayName("Телефон")] public string PhoneNumber { get; private set; } = string.Empty; + [DisplayName("Должность")] public Post Post { get; private set; } public static Gasman CreateGasman(int id, string gasmanName, string phoneNumber, Post post) diff --git a/GasStation/Entities/Product.cs b/GasStation/Entities/Product.cs index 8768a49..c405aa2 100644 --- a/GasStation/Entities/Product.cs +++ b/GasStation/Entities/Product.cs @@ -1,4 +1,5 @@ using GasStation.Entities.Enums; +using System.ComponentModel; namespace GasStation.Entities; @@ -6,15 +7,23 @@ public class Product { public int Id { get; private set; } + [DisplayName("Стоимость товара")] public int ProductCost { get; private set; } + [DisplayName("Тип товара")] public ProductType ProductType { get; private set; } - public static Product CreateProduct(int id, int productCost, ProductType productType) + [DisplayName("Название товара")] + public string ProductName { get; private set; } = string.Empty; + + public string FullName => $"{ProductType} {ProductName}"; + + public static Product CreateProduct(int id, string productName, int productCost, ProductType productType) { return new Product { Id = id, + ProductName = productName, ProductCost = productCost, ProductType = productType }; diff --git a/GasStation/Entities/ProductSelling.cs b/GasStation/Entities/ProductSelling.cs index 58ef24c..6b40664 100644 --- a/GasStation/Entities/ProductSelling.cs +++ b/GasStation/Entities/ProductSelling.cs @@ -6,6 +6,8 @@ public class ProductSelling public int ProductID { get; private set; } + public string ProductName { get; private set; } = string.Empty; + public int Count { get; private set; } public static ProductSelling CreateSelling(int id, int productID, int count) diff --git a/GasStation/Entities/Selling.cs b/GasStation/Entities/Selling.cs index ce7e7fe..0bff168 100644 --- a/GasStation/Entities/Selling.cs +++ b/GasStation/Entities/Selling.cs @@ -1,4 +1,5 @@ using System.Collections; +using System.ComponentModel; namespace GasStation.Entities; @@ -6,10 +7,19 @@ public class Selling { public int Id { get; private set; } + [Browsable(false)] public int GasmanId { get; private set; } + [DisplayName("Имя заправщика")] + public string GasmanName { get; private set; } = string.Empty; + + [DisplayName("Товары")] + public string Product => ProdutcSellings != null ? string.Join(", ", ProdutcSellings.Select(x => $"{x.ProductName} {x.Count}")) : string.Empty; + + [Browsable(false)] public IEnumerable ProdutcSellings { get; private set; } = []; + [DisplayName("Дата продажи")] public DateTime SellingDateTime { get; private set; } public static Selling CreateSelling(int id, int gasmanId, IEnumerable produtcSellings) @@ -23,14 +33,11 @@ public class Selling }; } - public static Selling CreateSelling(TempProductSelling tempProductSelling, IEnumerable produtcSellings) + public void SetProductSelling(IEnumerable produtcSellings) { - return new Selling + if (produtcSellings != null && produtcSellings.Any()) { - Id = tempProductSelling.Id, - GasmanId = tempProductSelling.GasmanId, - SellingDateTime = tempProductSelling.SellingDateTime, - ProdutcSellings = produtcSellings - }; + ProdutcSellings = produtcSellings; + } } } diff --git a/GasStation/Entities/Supplier.cs b/GasStation/Entities/Supplier.cs index 9098421..77ecb05 100644 --- a/GasStation/Entities/Supplier.cs +++ b/GasStation/Entities/Supplier.cs @@ -1,9 +1,12 @@ -namespace GasStation.Entities; +using System.ComponentModel; + +namespace GasStation.Entities; public class Supplier { public int Id { get; private set; } + [DisplayName("Имя поставщика")] public string SupplierName { get; private set; } = string.Empty; public static Supplier CreateSupplier(int id, string supplierName) diff --git a/GasStation/Entities/Supply.cs b/GasStation/Entities/Supply.cs index 5cdcda7..a37dae6 100644 --- a/GasStation/Entities/Supply.cs +++ b/GasStation/Entities/Supply.cs @@ -1,15 +1,27 @@ -namespace GasStation.Entities; +using System.ComponentModel; + +namespace GasStation.Entities; public class Supply { public int Id { get; private set; } + [Browsable(false)] public int SupplierID { get; private set; } + [Browsable(false)] public int ProductID { get; private set; } + [DisplayName("Имя поставщика")] + public string SupplierName { get; private set; } = string.Empty; + + [DisplayName("Название товара")] + public string ProductName { get; private set; } = string.Empty; + + [DisplayName("Количество товара")] public int Count { get; private set; } + [DisplayName("Дата поставки")] public DateTime SupplyDate { get; private set; } public static Supply CreateSupply(int id, int supplierID, int productID, int count) diff --git a/GasStation/Entities/TempProductSelling.cs b/GasStation/Entities/TempProductSelling.cs deleted file mode 100644 index 883d695..0000000 --- a/GasStation/Entities/TempProductSelling.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace GasStation.Entities; - -public class TempProductSelling -{ - public int Id { get; private set; } - - public int GasmanId { get; private set; } - - public int Count { get; private set; } - - public DateTime SellingDateTime { get; private set; } - - public int ProductID { get; private set; } -} diff --git a/GasStation/Forms/FormGasmen.cs b/GasStation/Forms/FormGasmen.cs index cb825ad..22ecace 100644 --- a/GasStation/Forms/FormGasmen.cs +++ b/GasStation/Forms/FormGasmen.cs @@ -86,7 +86,11 @@ namespace GasStation.Forms } } - private void LoadList() => dataGridViewData.DataSource = _gasmanRepository.ReadGasman(); + private void LoadList() + { + dataGridViewData.DataSource = _gasmanRepository.ReadGasman(); + dataGridViewData.Columns["ID"].Visible = false; + } private bool TryGetIDFromSelectedRow(out int id) { diff --git a/GasStation/Forms/FormProduct.Designer.cs b/GasStation/Forms/FormProduct.Designer.cs index b779f44..089b869 100644 --- a/GasStation/Forms/FormProduct.Designer.cs +++ b/GasStation/Forms/FormProduct.Designer.cs @@ -34,6 +34,8 @@ comboBoxProduct = new ComboBox(); buttonSave = new Button(); buttonCancel = new Button(); + label1 = new Label(); + textBoxProduct = new TextBox(); ((System.ComponentModel.ISupportInitialize)numericUpDownCost).BeginInit(); SuspendLayout(); // @@ -42,14 +44,14 @@ label2.AutoSize = true; label2.Location = new Point(33, 29); label2.Name = "label2"; - label2.Size = new Size(39, 15); + label2.Size = new Size(67, 15); label2.TabIndex = 1; - label2.Text = "Товар"; + label2.Text = "Тип товара"; // // label3 // label3.AutoSize = true; - label3.Location = new Point(33, 99); + label3.Location = new Point(33, 140); label3.Name = "label3"; label3.Size = new Size(67, 15); label3.TabIndex = 2; @@ -57,7 +59,7 @@ // // numericUpDownCost // - numericUpDownCost.Location = new Point(178, 99); + numericUpDownCost.Location = new Point(178, 140); numericUpDownCost.Minimum = new decimal(new int[] { 1, 0, 0, 0 }); numericUpDownCost.Name = "numericUpDownCost"; numericUpDownCost.Size = new Size(169, 23); @@ -75,7 +77,7 @@ // // buttonSave // - buttonSave.Location = new Point(33, 166); + buttonSave.Location = new Point(33, 207); buttonSave.Name = "buttonSave"; buttonSave.Size = new Size(88, 25); buttonSave.TabIndex = 6; @@ -85,7 +87,7 @@ // // buttonCancel // - buttonCancel.Location = new Point(254, 166); + buttonCancel.Location = new Point(254, 207); buttonCancel.Name = "buttonCancel"; buttonCancel.Size = new Size(93, 25); buttonCancel.TabIndex = 7; @@ -93,11 +95,29 @@ buttonCancel.UseVisualStyleBackColor = true; buttonCancel.Click += ButtonCancel_Click; // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(34, 85); + label1.Name = "label1"; + label1.Size = new Size(99, 15); + label1.TabIndex = 8; + label1.Text = "Название товара"; + // + // textBoxProduct + // + textBoxProduct.Location = new Point(178, 86); + textBoxProduct.Name = "textBoxProduct"; + textBoxProduct.Size = new Size(169, 23); + textBoxProduct.TabIndex = 9; + // // FormProduct // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(414, 246); + ClientSize = new Size(414, 278); + Controls.Add(textBoxProduct); + Controls.Add(label1); Controls.Add(buttonCancel); Controls.Add(buttonSave); Controls.Add(comboBoxProduct); @@ -118,5 +138,7 @@ private ComboBox comboBoxProduct; private Button buttonSave; private Button buttonCancel; + private Label label1; + private TextBox textBoxProduct; } } \ No newline at end of file diff --git a/GasStation/Forms/FormProduct.cs b/GasStation/Forms/FormProduct.cs index 52c48db..d3bb27c 100644 --- a/GasStation/Forms/FormProduct.cs +++ b/GasStation/Forms/FormProduct.cs @@ -32,6 +32,7 @@ public partial class FormProduct : Form throw new InvalidDataException(nameof(product)); } + textBoxProduct.Text = product.ProductName; comboBoxProduct.SelectedItem = product.ProductType; _productId = value; } @@ -70,8 +71,9 @@ public partial class FormProduct : Form private void ButtonCancel_Click(object sender, EventArgs e) => Close(); - private Product CreateProduct(int id) => - Product.CreateProduct(id, Convert.ToInt32(numericUpDownCost.Value), + private Product CreateProduct(int id) => + Product.CreateProduct(id, textBoxProduct.Text, Convert.ToInt32(numericUpDownCost.Value), (ProductType)comboBoxProduct.SelectedItem!); + } diff --git a/GasStation/Forms/FormProductReport.Designer.cs b/GasStation/Forms/FormProductReport.Designer.cs index 727a673..3ccb96c 100644 --- a/GasStation/Forms/FormProductReport.Designer.cs +++ b/GasStation/Forms/FormProductReport.Designer.cs @@ -28,10 +28,8 @@ /// private void InitializeComponent() { - dateTimePickerEnd = new DateTimePicker(); - dateTimePickerBegin = new DateTimePicker(); + dateTimePicker = new DateTimePicker(); label1 = new Label(); - label2 = new Label(); label3 = new Label(); label4 = new Label(); buttonMakeReport = new Button(); @@ -40,37 +38,21 @@ textBoxFilePath = new TextBox(); SuspendLayout(); // - // dateTimePickerEnd + // dateTimePicker // - dateTimePickerEnd.Location = new Point(176, 221); - dateTimePickerEnd.Name = "dateTimePickerEnd"; - dateTimePickerEnd.Size = new Size(200, 23); - dateTimePickerEnd.TabIndex = 0; - // - // dateTimePickerBegin - // - dateTimePickerBegin.Location = new Point(176, 156); - dateTimePickerBegin.Name = "dateTimePickerBegin"; - dateTimePickerBegin.Size = new Size(200, 23); - dateTimePickerBegin.TabIndex = 1; + dateTimePicker.Location = new Point(176, 156); + dateTimePicker.Name = "dateTimePicker"; + dateTimePicker.Size = new Size(200, 23); + dateTimePicker.TabIndex = 1; // // label1 // label1.AutoSize = true; label1.Location = new Point(51, 156); label1.Name = "label1"; - label1.Size = new Size(74, 15); + label1.Size = new Size(32, 15); label1.TabIndex = 2; - label1.Text = "Дата начала"; - // - // label2 - // - label2.AutoSize = true; - label2.Location = new Point(51, 221); - label2.Name = "label2"; - label2.Size = new Size(68, 15); - label2.TabIndex = 3; - label2.Text = "Дата конца"; + label1.Text = "Дата"; // // label3 // @@ -92,7 +74,7 @@ // // buttonMakeReport // - buttonMakeReport.Location = new Point(162, 290); + buttonMakeReport.Location = new Point(152, 232); buttonMakeReport.Name = "buttonMakeReport"; buttonMakeReport.Size = new Size(114, 23); buttonMakeReport.TabIndex = 6; @@ -129,17 +111,15 @@ // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(484, 370); + ClientSize = new Size(484, 310); Controls.Add(textBoxFilePath); Controls.Add(buttonSelectFilePath); Controls.Add(comboBoxProduct); Controls.Add(buttonMakeReport); Controls.Add(label4); Controls.Add(label3); - Controls.Add(label2); Controls.Add(label1); - Controls.Add(dateTimePickerBegin); - Controls.Add(dateTimePickerEnd); + Controls.Add(dateTimePicker); Name = "FormProductReport"; Text = "FormProductReport"; ResumeLayout(false); @@ -147,11 +127,8 @@ } #endregion - - private DateTimePicker dateTimePickerEnd; - private DateTimePicker dateTimePickerBegin; + private DateTimePicker dateTimePicker; private Label label1; - private Label label2; private Label label3; private Label label4; private Button buttonMakeReport; diff --git a/GasStation/Forms/FormProductReport.cs b/GasStation/Forms/FormProductReport.cs index f11f885..1dc3093 100644 --- a/GasStation/Forms/FormProductReport.cs +++ b/GasStation/Forms/FormProductReport.cs @@ -14,7 +14,7 @@ namespace GasStation.Forms _container = container ?? throw new ArgumentNullException(nameof(container)); comboBoxProduct.DataSource = productRepository.ReadProduct(); - comboBoxProduct.DisplayMember = "ProductType"; + comboBoxProduct.DisplayMember = "ProductName"; comboBoxProduct.ValueMember = "Id"; } @@ -43,13 +43,9 @@ namespace GasStation.Forms { throw new Exception("Не выбран товар"); } - if (dateTimePickerEnd.Value <= dateTimePickerBegin.Value) - { - throw new Exception("Дата начала должна быть раньше даты окончания"); - } if (_container.Resolve().CreateTable(textBoxFilePath.Text, (int)comboBoxProduct.SelectedValue!, - dateTimePickerBegin.Value, dateTimePickerEnd.Value)) + dateTimePicker.Value)) { MessageBox.Show("Документ сформирован", "Формирование документа", diff --git a/GasStation/Forms/FormProducts.cs b/GasStation/Forms/FormProducts.cs index f69fe13..b4bfce3 100644 --- a/GasStation/Forms/FormProducts.cs +++ b/GasStation/Forms/FormProducts.cs @@ -99,6 +99,11 @@ namespace GasStation.Forms } } - private void LoadList() => dataGridViewData.DataSource = _productRepository.ReadProduct(); + private void LoadList() + { + dataGridViewData.DataSource = _productRepository.ReadProduct(); + dataGridViewData.Columns["ID"].Visible = false; + dataGridViewData.Columns["FullName"].Visible = false; + } } } diff --git a/GasStation/Forms/FormSelling.cs b/GasStation/Forms/FormSelling.cs index 3ca6712..a4415a5 100644 --- a/GasStation/Forms/FormSelling.cs +++ b/GasStation/Forms/FormSelling.cs @@ -19,7 +19,7 @@ namespace GasStation.Forms comboBoxGasman.ValueMember = "ID"; ColumnProduct.DataSource = productRepository.ReadProduct(); - ColumnProduct.DisplayMember = "ProductType"; + ColumnProduct.DisplayMember = "ProductName"; ColumnProduct.ValueMember = "ID"; } @@ -60,7 +60,7 @@ namespace GasStation.Forms Convert.ToInt32(row.Cells["ColumnCount"].Value))); } - return list.GroupBy(x => x.ProductID, x => x.Count, (id, counts) => ProductSelling.CreateSelling(0, id,counts.Sum())).ToList(); + return list.GroupBy(x => x.ProductID, x => x.Count, (id, counts) => ProductSelling.CreateSelling(0, id, counts.Sum())).ToList(); ; } diff --git a/GasStation/Forms/FormSellings.cs b/GasStation/Forms/FormSellings.cs index 756a85d..2f16909 100644 --- a/GasStation/Forms/FormSellings.cs +++ b/GasStation/Forms/FormSellings.cs @@ -31,7 +31,12 @@ namespace GasStation.Forms } } - private void LoadList() => dataGridViewData.DataSource = _sellingRepository.ReadSelling(); + private void LoadList() + { + dataGridViewData.DataSource = _sellingRepository.ReadSelling(); + dataGridViewData.Columns["ID"].Visible = false; + dataGridViewData.Columns["SupplyDate"].DefaultCellStyle.Format = "dd MMMM yyyy hh:mm"; + } private void FormSellings_Load(object sender, EventArgs e) { diff --git a/GasStation/Forms/FormSuppliers.cs b/GasStation/Forms/FormSuppliers.cs index c90c1b6..03c0310 100644 --- a/GasStation/Forms/FormSuppliers.cs +++ b/GasStation/Forms/FormSuppliers.cs @@ -86,7 +86,11 @@ namespace GasStation.Forms } } - private void LoadList() => dataGridViewData.DataSource = _supplierRepository.ReadSupplier(); + private void LoadList() + { + dataGridViewData.DataSource = _supplierRepository.ReadSupplier(); + dataGridViewData.Columns["ID"].Visible = false; + } private bool TryGetIDFromSelectedRow(out int id) { diff --git a/GasStation/Forms/FormSupplies.cs b/GasStation/Forms/FormSupplies.cs index c393032..cbbc31f 100644 --- a/GasStation/Forms/FormSupplies.cs +++ b/GasStation/Forms/FormSupplies.cs @@ -54,7 +54,12 @@ namespace GasStation.Forms } } - private void LoadList() => dataGridViewData.DataSource = _supplyRepository.ReadSupply(); + private void LoadList() + { + dataGridViewData.DataSource = _supplyRepository.ReadSupply(); + dataGridViewData.Columns["ID"].Visible = false; + dataGridViewData.Columns["SupplyDate"].DefaultCellStyle.Format = "dd.MM.yyyy"; + } private void FormSupplies_Load(object sender, EventArgs e) { diff --git a/GasStation/Forms/FormSupply.cs b/GasStation/Forms/FormSupply.cs index dfa4ee6..c088468 100644 --- a/GasStation/Forms/FormSupply.cs +++ b/GasStation/Forms/FormSupply.cs @@ -18,7 +18,7 @@ namespace GasStation.Forms comboBoxSupplier.ValueMember = "ID"; comboBoxProduct.DataSource = productRepository.ReadProduct(); - comboBoxProduct.DisplayMember = "ProductType"; + comboBoxProduct.DisplayMember = "FullName"; comboBoxProduct.ValueMember = "ID"; } diff --git a/GasStation/Reports/ChartReport.cs b/GasStation/Reports/ChartReport.cs index b4ba4dd..1b5f136 100644 --- a/GasStation/Reports/ChartReport.cs +++ b/GasStation/Reports/ChartReport.cs @@ -21,7 +21,7 @@ internal class ChartReport { new PdfBuilder(filePath) .AddHeader("Поступление продукта") - .AddPieChart("Поступивший товар", GetData(dateTime)) + .AddPieChart("Поступивший товар {dateTime:dd MM yyyy}", GetData(dateTime)) .Build(); return true; } diff --git a/GasStation/Reports/DocReport.cs b/GasStation/Reports/DocReport.cs index e1183f6..053fe9c 100644 --- a/GasStation/Reports/DocReport.cs +++ b/GasStation/Reports/DocReport.cs @@ -79,10 +79,10 @@ internal class DocReport private List GetProducts() { return [ - ["Стоимость товара", "Тип товара"], + ["Название товара", "Стоимость товара", "Тип товара"], .. _productRepository .ReadProduct() - .Select(x => new string[] { x.ProductCost.ToString(), x.ProductType.ToString() }), + .Select(x => new string[] { x.ProductName.ToString(), x.ProductCost.ToString(), x.ProductType.ToString() }), ]; } diff --git a/GasStation/Reports/TableReport.cs b/GasStation/Reports/TableReport.cs index d2f83e1..706aad2 100644 --- a/GasStation/Reports/TableReport.cs +++ b/GasStation/Reports/TableReport.cs @@ -17,14 +17,14 @@ internal class TableReport _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } - public bool CreateTable(string filePath, int productId, DateTime startDate, DateTime endDate) + public bool CreateTable(string filePath, int productId, DateTime date) { try { new ExcelBuilder(filePath) .AddHeader("Сводка по движению товара", 0, 3) .AddParagraph("за период", 0) - .AddTable([10, 10, 15], GetData(productId, startDate, endDate)) + .AddTable([10, 10, 15], GetData(productId, date)) .Build(); return true; } @@ -35,20 +35,19 @@ internal class TableReport } } - private List GetData(int productId, DateTime startDate, DateTime endDate) + private List GetData(int productId, DateTime date) { var data = _sellingRepository - .ReadSelling() - .Where(x => x.SellingDateTime >= startDate && x.SellingDateTime <= endDate && x.ProdutcSellings.Any(y => y.ProductID == productId)) + .ReadSelling(date, productId) .Select(x => new {x.GasmanId, Date = x.SellingDateTime, CountOut = x.ProdutcSellings.FirstOrDefault(y => y.ProductID == productId)?.Count }) .OrderBy(x => x.Date); return new List() { item }.Union(data .Select(x => - new string[] { x.GasmanId.ToString(), x.Date.ToString(), x.CountOut?.ToString() ?? string.Empty})) + new string[] { x.GasmanId.ToString(), x.Date.ToString("dd.MM.yyyy"), x.CountOut?.ToString("N0") ?? string.Empty})) .Union( - [["Всего", "", data.Sum(x => x.CountOut ?? 0).ToString()]] + [["Всего", "", data.Sum(x => x.CountOut ?? 0).ToString("N0")]] ).ToList(); } } diff --git a/GasStation/Repositories/Implementations/ProductRepository.cs b/GasStation/Repositories/Implementations/ProductRepository.cs index fd97ba0..51777d9 100644 --- a/GasStation/Repositories/Implementations/ProductRepository.cs +++ b/GasStation/Repositories/Implementations/ProductRepository.cs @@ -28,8 +28,8 @@ public class ProductRepository : IProductRepository { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); var queryInsert = @" -INSERT INTO product (productCost, productType) -VALUES (@ProductCost, @ProductType)"; +INSERT INTO product (productName, productCost, productType) +VALUES (@ProductName, @ProductCost, @ProductType)"; connection.Execute(queryInsert, product); } catch (Exception ex) @@ -112,6 +112,7 @@ WHERE id=@Id"; var queryUpdate = @" UPDATE product SET + productName=@ProductName, productCost=@ProductCost, productType=@ProductType WHERE id=@Id"; diff --git a/GasStation/Repositories/Implementations/QueryBuilder.cs b/GasStation/Repositories/Implementations/QueryBuilder.cs new file mode 100644 index 0000000..ae03ccc --- /dev/null +++ b/GasStation/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 GasStation.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}"; + } +} diff --git a/GasStation/Repositories/Implementations/SellingRepository.cs b/GasStation/Repositories/Implementations/SellingRepository.cs index 962a677..6086640 100644 --- a/GasStation/Repositories/Implementations/SellingRepository.cs +++ b/GasStation/Repositories/Implementations/SellingRepository.cs @@ -3,6 +3,7 @@ using GasStation.Entities; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Npgsql; +using static System.Runtime.InteropServices.JavaScript.JSType; namespace GasStation.Repositories.Implementations; @@ -54,14 +55,55 @@ VALUES (@Id, @ProductID, @Count)"; _logger.LogInformation("Получение всех объектов"); try { + var builder = new QueryBuilder(); + if (dateTime.HasValue) + { + builder.AddCondition("s.SellingDateTime = @sellingDateTime"); + } + if (count.HasValue) + { + builder.AddCondition("s.Count = @count"); + } + if (gasmanID.HasValue) + { + builder.AddCondition("s.GasmanID = @gasmanID"); + } + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); var querySelect = @" -SELECT s.*, ps.ProductID, ps.Count FROM selling s -INNER JOIN Product_Selling ps ON ps.Id = s.Id"; - var selling = connection.Query(querySelect); - _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(selling)); - return selling.GroupBy(x => x.Id, y => y, (key, value) => - Selling.CreateSelling(value.First(), value.Select(z => ProductSelling.CreateSelling(0, z.ProductID, z.Count)))).ToList(); +SELECT + fr.*, + CONCAT(p.ProductType, ' ', p.ProductName) as ProductName, + ps.ProdutId, + ps.Count, + g.GasmanName as 'GasmanName' +FROM Selling s +LEFT JOIN Gasman g on g.Id = s.GasmanId +INNER JOIN Product_Selling ps ON ps.ProductId = s.Id +LEFT JOIN Product p on p.Id = s.ProductId +{builder.Build()}"; + var sellingDict = new Dictionary>(); + var selling = connection.Query(querySelect, (sell, sellings) => + { + if (!sellingDict.TryGetValue(sell.Id, out var ps)) + { + ps = []; + sellingDict.Add(sell.Id, ps); + } + ps.Add(sellings); + return sell; + }, splitOn: "ProdutcId", param: new + {dateTime, count, gasmanID}); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(selling)); + + return sellingDict.Select(x => + { + var fr = selling.First(y => y.Id == x.Key); + fr.SetProductSelling(x.Value); + return fr; + }).ToArray(); + } catch (Exception ex) { diff --git a/GasStation/Repositories/Implementations/SupplyRepository.cs b/GasStation/Repositories/Implementations/SupplyRepository.cs index 13490e4..ce20740 100644 --- a/GasStation/Repositories/Implementations/SupplyRepository.cs +++ b/GasStation/Repositories/Implementations/SupplyRepository.cs @@ -3,6 +3,7 @@ using GasStation.Entities; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Npgsql; +using static System.Runtime.InteropServices.JavaScript.JSType; namespace GasStation.Repositories.Implementations; @@ -63,9 +64,34 @@ WHERE id=@Id"; _logger.LogInformation("Получение всех объектов"); try { + var builder = new QueryBuilder(); + if (supplyDate.HasValue) + { + builder.AddCondition("s.SupplyDate = @supplyDate"); + } + if (supplierID.HasValue) + { + builder.AddCondition("s.SupplierID = @supplierID"); + } + if (productID.HasValue) + { + builder.AddCondition("s.ProductID = @productID"); + } + if (count.HasValue) + { + builder.AddCondition("s.Count = @count"); + } + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = @"SELECT * FROM supply"; - var supply = connection.Query(querySelect); + var querySelect = @"SELECT + s.*, + CONCAT(p.ProductType, ' ', p.ProductName) as ProductName, + sup.SupplierName as SupplierName +FROM Supply s +LEFT JOIN Product p on p.Id = s.ProductId +LEFT JOIN Supplier sup on sup.Id = s.SupplierId +{builder.Build()}"; + var supply = connection.Query(querySelect, new { supplyDate, supplierID, productID, count }); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(supply)); return supply; }