ИСЭбд-22, Скворцов А.К., Четвертая лабораторная #4

Closed
Aleksey-Skvortsov wants to merge 1 commits from lab-4 into lab-3
20 changed files with 185 additions and 51 deletions
Showing only changes of commit 033ba7ab4c - Show all commits

View File

@ -1,8 +1,12 @@
namespace Project.Entities;
using System.ComponentModel;
namespace Project.Entities;
public class Manufacturer
{
public int Id { get; private set; }
[DisplayName("Название")]
public string Name { get; private set; } = string.Empty;
public static Manufacturer CreateOperation(int id, string name)
{

View File

@ -1,16 +1,34 @@
using Project.Entities.Enums;
using System.ComponentModel;
namespace Project.Entities;
public class Product
{
public int Id { get; private set; }
[DisplayName("Название")]
public string Name { get; private set; } = string.Empty;
[DisplayName("Описание")]
public string Description { get; private set; } = string.Empty;
[DisplayName("Цена продажи")]
public float Price { get; private set; }
[DisplayName("Цена закупки")]
public float Cost { get; private set; }
[Browsable(false)]
public int ManufacturerId { get; private set; }
[DisplayName("Производитель")]
public string ManufacturerName { get; private set; } = string.Empty;
[DisplayName("Категория")]
public CategoryType Category { get; private set; }
[DisplayName("Аттрибуты")]
public ProductAttributes Attributes { get; private set; }
public static Product CreteOperation(int id, string name, string description, float price, float cost, int manufacturerId, CategoryType category, ProductAttributes attributes)
{

View File

@ -1,16 +1,31 @@
namespace Project.Entities;
using System.ComponentModel;
namespace Project.Entities;
public class Sale
{
public int Id { get; private set; }
[DisplayName("Дата")]
public DateTime Date { get; private set; }
[DisplayName("Продажи")]
public string Sales => SaleProducts != null ?
string.Join(", ", SaleProducts.Select(x => $"{x.ProductName} {x.Quantity}")) :
string.Empty;
[Browsable(false)]
public IEnumerable<SaleProduct> SaleProducts { get; private set; } = [];
public static Sale CreteOperation(int id, DateTime date, IEnumerable<SaleProduct> saleProducts)
{
return new Sale { Id = id, Date = date, SaleProducts = saleProducts };
}
public static Sale CreteOperation(TempSaleProduct tempSaleProduct, IEnumerable<SaleProduct> saleProducts)
public void SetSaleProducts(IEnumerable<SaleProduct> saleProducts)
{
return new Sale { Id = tempSaleProduct.Id, Date = tempSaleProduct.Date, SaleProducts = saleProducts };
if (saleProducts != null && saleProducts.Any())
{
SaleProducts = saleProducts;
}
}
}

View File

@ -4,6 +4,7 @@ public class SaleProduct
{
public int Id { get; private set; }
public int ProductId { get; private set;}
public string ProductName { get; private set;} = string.Empty;
public int Quantity { get; private set; }
public static SaleProduct CreateOperation(int id, int productId, int quantity)
{

View File

@ -1,15 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Project.Entities;
public class TempSaleProduct
{
public int Id { get; private set; }
public DateTime Date { get; private set; }
public int ProductId { get; private set; }
public int Quantity { get; private set; }
}

View File

@ -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,20 @@ namespace Project.Entities;
public class Work
{
public int Id { get; private set; }
[Browsable(false)]
public int ProductId { get; private set; }
[Browsable(false)]
public int WorkTypeID { get; private set; }
[DisplayName("Продукт")]
public string ProductName { get; private set; } = string.Empty;
[DisplayName("Тип работы")]
public string WorkTypeName { get; private set; } = string.Empty;
[DisplayName("Дата")]
public DateTime Date { get; private set; }
public static Work CreateOperation(int id, int productId, int workTypeID, DateTime date)

View File

@ -1,11 +1,20 @@
namespace Project.Entities;
using System.ComponentModel;
namespace Project.Entities;
public class WorkType
{
public int Id { get; private set; }
[DisplayName("Название")]
public string Name { get; private set; } = string.Empty;
[DisplayName("Описание")]
public string Description { get; private set; } = string.Empty;
[DisplayName("Цена")]
public float Price { get; private set; }
public static WorkType CreateOperation(int id, string name, string description, float price)
{
return new WorkType { Id = id, Name = name, Description = description, Price = price };

View File

@ -70,8 +70,11 @@ public partial class FormManufacturers : Form
}
}
private void LoadList() => dataGridViewGroups.DataSource = _manufacturerRepository.GetAllManufacturers();
private void LoadList()
{
dataGridViewGroups.DataSource = _manufacturerRepository.GetAllManufacturers();
dataGridViewGroups.Columns["Id"].Visible = false;
}
private bool TryGetIdentifierFromSelectedRow(out int id)
{
id = 0;

View File

@ -70,8 +70,11 @@ public partial class FormProducts : Form
}
}
private void LoadList() => dataGridViewGroups.DataSource = _productRepository.GetAllProducts();
private void LoadList()
{
dataGridViewGroups.DataSource = _productRepository.GetAllProducts();
dataGridViewGroups.Columns["Id"].Visible = false;
}
private bool TryGetIdentifierFromSelectedRow(out int id)
{
id = 0;

View File

@ -51,8 +51,11 @@ public partial class FormSales : Form
}
}
private void LoadList() => dataGridViewGroups.DataSource = _saleRepository.GetAllSales();
private void LoadList()
{
dataGridViewGroups.DataSource = _saleRepository.GetAllSales();
dataGridViewGroups.Columns["Id"].Visible = false;
}
private bool TryGetIdentifierFromSelectedRow(out int id)
{
id = 0;

View File

@ -70,8 +70,11 @@ public partial class FormWorkTypes : Form
}
}
private void LoadList() => dataGridViewGroups.DataSource = _workTypeRepository.GetAllWorkTypes();
private void LoadList()
{
dataGridViewGroups.DataSource = _workTypeRepository.GetAllWorkTypes();
dataGridViewGroups.Columns["Id"].Visible = false;
}
private bool TryGetIdentifierFromSelectedRow(out int id)
{
id = 0;

View File

@ -51,8 +51,11 @@ public partial class FormWorks : Form
}
}
private void LoadList() => dataGridViewGroups.DataSource = _workRepository.GetAllWorks();
private void LoadList()
{
dataGridViewGroups.DataSource = _workRepository.GetAllWorks();
dataGridViewGroups.Columns["Id"].Visible = false;
}
private bool TryGetIdentifierFromSelectedRow(out int id)
{
id = 0;

View File

@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging;
using Project.Repositories;
using System;
namespace Project.Reports;
@ -22,7 +23,7 @@ internal class ChartReport
{
new PdfBuilder(filePath)
.AddHeader("Количество продаж по товарам")
.AddPieChart("Продажи товаров", GetData(date))
.AddPieChart($"Продажи товаров на {date:dd.MM.yyyy}", GetData(date))
.Build();
return true;
}
@ -33,13 +34,11 @@ internal class ChartReport
}
}
private List<(string Caption, double Value)> GetData(DateTime date)
private List<(string Caption, double Value)> GetData(DateTime dateTime)
{
var productNames = _productRepository.GetAllProducts()
.ToDictionary(p => p.Id, p => p.Name);
var sales = _saleRepository.GetAllSales()
.Where(s => s.Date.Date == date.Date)
var sales = _saleRepository.GetAllSales(dateFrom: dateTime.Date, dateTo: dateTime.Date.AddDays(1))
.SelectMany(s => s.SaleProducts)
.GroupBy(sp => sp.ProductId)
.Select(g => new

View File

@ -44,8 +44,7 @@ public class TableReport
var workTypes = _workTypeRepository.GetAllWorkTypes()
.ToDictionary(wt => wt.Id, wt => new { wt.Name, wt.Description });
var sales = _saleRepository.GetAllSales()
.Where(s => s.Date >= startDate && s.Date <= endDate)
var sales = _saleRepository.GetAllSales(startDate, endDate)
.Select(s => new
{
s.Date,
@ -55,8 +54,7 @@ public class TableReport
Comment = string.Empty
});
var works = _workRepository.GetAllWorks()
.Where(w => w.Date >= startDate && w.Date <= endDate)
var works = _workRepository.GetAllWorks(startDate, endDate)
.Select(w => new
{
w.Date,

View File

@ -4,7 +4,7 @@ namespace Project.Repositories;
public interface ISaleRepository
{
IEnumerable<Sale> GetAllSales();
IEnumerable<Sale> GetAllSales(DateTime? dateFrom = null, DateTime? dateTo = null);
void AddSale(Sale sale);
void DeleteSale(int id);
}

View File

@ -4,7 +4,7 @@ namespace Project.Repositories;
public interface IWorkRepository
{
IEnumerable<Work> GetAllWorks();
IEnumerable<Work> GetAllWorks(DateTime? dateFrom = null, DateTime? dateTo = null);
void AddWork(Work work);
void DeleteWork(int id);
}

View File

@ -63,7 +63,8 @@ public class ProductRepository : IProductRepository
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = "SELECT * FROM Product";
var querySelect = @"SELECT pr.*, ma.Name as ManufacturerName FROM Product pr
LEFT JOIN Manufacturer ma on ma.Id = pr.ManufacturerId";
var products = connection.Query<Product>(querySelect);
_logger.LogDebug("Полученные объекты: {json}",
JsonConvert.SerializeObject(products));

View File

@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Project.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}";
}
}

View File

@ -3,6 +3,7 @@ using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Npgsql;
using Project.Entities;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace Project.Repositories.Implementations;
@ -83,18 +84,48 @@ public class SaleRepository : ISaleRepository
}
}
public IEnumerable<Sale> GetAllSales()
public IEnumerable<Sale> GetAllSales(DateTime? dateFrom = null, DateTime? dateTo = null)
{
_logger.LogInformation("Получение всех объектов");
try
{
var builder = new QueryBuilder();
if (dateFrom.HasValue)
{
builder.AddCondition("sa.Date >= @dateFrom");
}
if (dateTo.HasValue)
{
builder.AddCondition("sa.Date <= @dateTo");
}
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"SELECT sa.*, sap.ProductId, sap.Quantity FROM Sale sa
INNER JOIN SaleProduct sap on sap.SaleId = sa.Id";
var sales = connection.Query<TempSaleProduct>(querySelect);
var querySelect = $@"SELECT sa.*, sap.ProductId, sap.Quantity, pr.Name as ProductName FROM Sale sa
INNER JOIN SaleProduct sap on sap.SaleId = sa.Id
LEFT JOIN Product pr on pr.Id = sap.ProductId
{builder.Build()}";
var salesDict = new Dictionary<int, List<SaleProduct>>();
var saleProducts = connection.Query<Sale, SaleProduct, Sale>(querySelect,
(sale, saleProduct) =>
{
if (!salesDict.TryGetValue(sale.Id, out var ccf))
{
ccf = [];
salesDict.Add(sale.Id, ccf);
}
ccf.Add(saleProduct);
return sale;
}, splitOn: "ProductId", param: new {dateFrom, dateTo});
_logger.LogDebug("Полученные объекты: {json}",
JsonConvert.SerializeObject(sales));
return sales.GroupBy(x => x.Id, y => y, (key, value) => Sale.CreteOperation(value.First(), value.Select(z => SaleProduct.CreateOperation(0, z.ProductId, z.Quantity)))).ToList();
JsonConvert.SerializeObject(saleProducts));
return salesDict.Select(x =>
{
var cf = saleProducts.First(y => y.Id == x.Key);
cf.SetSaleProducts(x.Value);
return cf;
}).ToArray();
}
catch (Exception ex)
{

View File

@ -56,14 +56,26 @@ public class WorkRepository : IWorkRepository
}
}
public IEnumerable<Work> GetAllWorks()
public IEnumerable<Work> GetAllWorks(DateTime? dateFrom = null, DateTime? dateTo = null)
{
_logger.LogInformation("Получение всех объектов");
try
{
var builder = new QueryBuilder();
if (dateFrom.HasValue)
{
builder.AddCondition("wo.Date >= @dateFrom");
}
if (dateTo.HasValue)
{
builder.AddCondition("wo.Date <= @dateTo");
}
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = "SELECT * FROM Work";
var works = connection.Query<Work>(querySelect);
var querySelect = $@"SELECT wo.*, wot.Name as WorkTypeName, pro.Name as ProductName FROM Work wo
LEFT JOIN WorkType wot on wot.Id = wo.WorkTypeID
LEFT JOIN Product pro on pro.Id = wo.ProductId
{builder.Build()}";
var works = connection.Query<Work>(querySelect, new { dateFrom, dateTo });
_logger.LogDebug("Полученные объекты: {json}",
JsonConvert.SerializeObject(works));
return works;