ПИбд-21. Некрасов А.А. Четвертая Лабораторная #4

Closed
alhimek17 wants to merge 2 commits from LabWork_4 into LabWork_3
75 changed files with 199 additions and 158 deletions

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;
@ -10,6 +11,7 @@ public class Client
{
public int Id { get; private set; }
[DisplayName("Имя")]
public string Name { get; private set; } = string.Empty;
public static Client CreateEntity(int id, string name)

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;
@ -10,10 +11,18 @@ public class Order
{
public int Id { get; private set; }
[Browsable(false)]
public int ClientId { get; private set;}
[DisplayName("Дата")]
public DateTime OrderDate { get; private set; }
[DisplayName("Продажи")]
public string Shoes => OrderItems != null ?
string.Join(", ", OrderItems.Select(x => $"{x.ShoesName}:{x.NumberOfPairs}")) :
string.Empty;
[Browsable(false)]
public IEnumerable<OrderItem> OrderItems { get; private set; } = [];
public static Order CreateOperation(int id, int clientid, DateTime date, IEnumerable<OrderItem> orderItems)
@ -27,14 +36,11 @@ public class Order
};
}
public static Order CreateOperation(TempOrderItem tempOrderItem, IEnumerable<OrderItem> orderItems)
public void SetIrderItems(IEnumerable<OrderItem> orderItems)
{
return new Order
if (orderItems != null && orderItems.Any())
{
Id = tempOrderItem.Id,
ClientId = tempOrderItem.ClientId,
OrderDate = tempOrderItem.OrderDate,
OrderItems = orderItems
};
OrderItems = orderItems;
}
}
}

View File

@ -12,6 +12,8 @@ public class OrderItem
public int ShoesId { get; set; }
public string ShoesName { get; set; } = string.Empty;
public int NumberOfPairs { get; set; }
public int Size { get; 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;
@ -11,10 +12,13 @@ public class Shoes
{
public int Id { get; private set; }
[DisplayName("Название")]
public string Name { get; private set; } = string.Empty;
[DisplayName("Цена")]
public int Price { get; private set; }
[DisplayName("Тип")]
public ShoesType ShoesType { get; private set;}
public static Shoes CreateEntity(int id, string name, int price, ShoesType shoestype)

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;
@ -11,10 +12,18 @@ public class Supply
{
public int Id { get; private set; }
[DisplayName("Дата")]
public DateTime DateOfReceipt { get; private set; }
[DisplayName("Тип")]
public SupplyType SupplyType { get; private set; }
[DisplayName("Поставки")]
public string Shoes => SupplyShoes != null ?
string.Join(", ", SupplyShoes.Select(x => $"{x.ShoesName}:{x.NumberOfPairs}")) :
string.Empty;
[Browsable(false)]
public IEnumerable<SupplyShoes> SupplyShoes { get; private set; } = [];
public static Supply CreateEntity(int id, SupplyType supplytype, DateTime date,IEnumerable<SupplyShoes> supplyShoes)
@ -28,14 +37,11 @@ public class Supply
};
}
public static Supply CreateEntity(TempSupplyShoes tempSupplyShoes, IEnumerable<SupplyShoes> supplyShoes)
public void SetSupplyShoes(IEnumerable<SupplyShoes> supplyShoes)
{
return new Supply
if (supplyShoes != null && supplyShoes.Any())
{
Id = tempSupplyShoes.Id,
DateOfReceipt = tempSupplyShoes.DateOfReceipt,
SupplyType = tempSupplyShoes.SupplyType,
SupplyShoes = supplyShoes
};
SupplyShoes = supplyShoes;
}
}
}

View File

@ -13,6 +13,8 @@ public class SupplyShoes
public int ShoesId { get; private set; }
public string ShoesName { get; set; } = string.Empty;
public int Size { get; private set; }
public int NumberOfPairs { get; private set; }

View File

@ -76,8 +76,11 @@ namespace ProjectShoeShop.Forms
}
}
private void LoadList() => dataGridView.DataSource = _clientRepository.ReadClient();
private void LoadList()
{
dataGridView.DataSource = _clientRepository.ReadClient();
dataGridView.Columns["Id"].Visible = false;
}
private bool TryGetIdentifierFromSelectedRow(out int id)
{
id = 0;

View File

@ -52,6 +52,10 @@ namespace ProjectShoeShop.Forms
}
}
private void LoadList() => dataGridView.DataSource = _orderRepository.ReadOrder();
private void LoadList()
{
dataGridView.DataSource = _orderRepository.ReadOrder();
dataGridView.Columns["Id"].Visible = false;
}
}
}

View File

@ -75,8 +75,11 @@ namespace ProjectShoeShop.Forms
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void LoadList() => dataGridView.DataSource = _shoesRepository.ReadShoes();
private void LoadList()
{
dataGridView.DataSource = _shoesRepository.ReadShoes();
dataGridView.Columns["Id"].Visible = false;
}
private bool TryGetIdentifierFromSelectedRow(out int id)
{
id = 0;

View File

@ -77,8 +77,11 @@ namespace ProjectShoeShop.Forms
}
}
private void LoadList() => dataGridView.DataSource = _supplyRepository.ReadSupply();
private void LoadList()
{
dataGridView.DataSource = _supplyRepository.ReadSupply();
dataGridView.Columns["Id"].Visible = false;
}
private bool TryGetIdentifierFromSelectedRow(out int id)
{
id = 0;

View File

@ -1,22 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectShoeShop.Entities;
public class TempOrderItem
{
public int Id { get; private set; }
public int ClientId { get; private set; }
public DateTime OrderDate { get; private set; }
public int ShoesId { get; set; }
public int NumberOfPairs { get; set; }
public int Size { get; set; }
}

View File

@ -1,23 +0,0 @@
using ProjectShoeShop.Entities.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectShoeShop.Entities;
public class TempSupplyShoes
{
public int Id { get; private set; }
public DateTime DateOfReceipt { get; private set; }
public SupplyType SupplyType { get; private set; }
public int ShoesId { get; private set; }
public int Size { get; private set; }
public int NumberOfPairs { get; private set; }
}

View File

@ -1,25 +0,0 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34525.116
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectShoeShop", "ProjectShoeShop\ProjectShoeShop.csproj", "{95CE577A-0BCC-430A-B9C0-447B0542AE11}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{95CE577A-0BCC-430A-B9C0-447B0542AE11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{95CE577A-0BCC-430A-B9C0-447B0542AE11}.Debug|Any CPU.Build.0 = Debug|Any CPU
{95CE577A-0BCC-430A-B9C0-447B0542AE11}.Release|Any CPU.ActiveCfg = Release|Any CPU
{95CE577A-0BCC-430A-B9C0-447B0542AE11}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1470F707-2372-4B36-B363-24D3557220BA}
EndGlobalSection
EndGlobal

View File

@ -1,17 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectShoeShop.Entities;
namespace ProjectShoeShop.Repositories;
public interface ISupplyShoesRepository
{
IEnumerable<SupplyShoes> ReadSupplyShoes(int? supplyId = null, int? shoesId = null);
void CreateSupplyShoes(SupplyShoes supplyShoes);
void DeleteSupplyShoes(int id);
}

View File

@ -1,25 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectShoeShop.Repositories.Implementations;
public class SupplyShoes : ISupplyShoesRepository
{
public void CreateSupplyShoes(Entities.SupplyShoes supplyShoes)
{
}
public void DeleteSupplyShoes(int id)
{
}
public IEnumerable<Entities.SupplyShoes> ReadSupplyShoes(int? supplyId = null, int? shoesId = null)
{
return [];
}
}

View File

@ -23,7 +23,7 @@ internal class ChartReport
{
new PdfBuilder(filePath)
.AddHeader("Продажи обуви")
.AddPieChart("Обувь", GetData(dateTime))
.AddPieChart($"Обувь на {dateTime:dd.MM.yyyy}", GetData(dateTime))
.Build();
return true;
}
@ -39,8 +39,7 @@ internal class ChartReport
.ToDictionary(f => f.Id, f => f.Name);
return _orderRepository
.ReadOrder()
.Where(x => x.OrderDate.Date == dateTime.Date)
.ReadOrder(dateTime.Date, dateTime.Date.AddDays(1))
.SelectMany(x => x.OrderItems)
.GroupBy(x => x.ShoesId)
.Select(g => (Caption: fuelNames[g.Key].ToString(), Value: (double)g.Sum(x => x.NumberOfPairs)))

View File

@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using ProjectShoeShop.Entities;
using ProjectShoeShop.Repositories;
namespace ProjectShoeShop.Reports;
@ -7,11 +8,13 @@ internal class TableReport
{
private readonly IOrderRepository _orderRepository;
private readonly ISupplyRepository _supplyRepository;
private readonly IShoesRepository _shoesRepository;
private readonly ILogger<TableReport> _logger;
internal static readonly string[] item = ["Дата", "Количество пришло", "Количество ушло"];
public TableReport(IOrderRepository orderRepository, ISupplyRepository supplyRepository, ILogger<TableReport> logger)
public TableReport(IOrderRepository orderRepository, ISupplyRepository supplyRepository, IShoesRepository shoesRepository, ILogger<TableReport> logger)
{
_shoesRepository = shoesRepository ?? throw new ArgumentNullException(nameof(shoesRepository));
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
_supplyRepository = supplyRepository ?? throw new ArgumentNullException(nameof(supplyRepository));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
@ -19,11 +22,13 @@ internal class TableReport
public bool CreateTable(string filePath, int shoesId, DateTime startDate, DateTime endDate)
{
try
{
Shoes shoe = _shoesRepository.ReadShoesById(shoesId);
new ExcelBuilder(filePath)
.AddHeader("Сводка по движению товара", 0, 3)
.AddParagraph("за период", 0)
.AddHeader($"Сводка по движению товара {shoe.Name}", 0, 3)
.AddParagraph($"за период {startDate:dd.MM.yyyy} по {endDate:dd.MM.yyyy}", 0)
.AddTable([10, 15, 15], GetData(shoesId, startDate, endDate))
.Build();
return true;
@ -38,13 +43,11 @@ internal class TableReport
private List<string[]> GetData(int shoesId, DateTime startDate, DateTime endDate)
{
var data = _orderRepository
.ReadOrder()
.Where(x => x.OrderDate >= startDate && x.OrderDate <= endDate && x.OrderItems.Any(y => y.ShoesId == shoesId))
.ReadOrder(startDate, endDate, shoesId)
.Select(x => new { Date = x.OrderDate.Date, CountIn = (int?)null, CountOut = x.OrderItems.FirstOrDefault(y => y.ShoesId == shoesId)?.NumberOfPairs })
.Union(
_supplyRepository
.ReadSupply()
.Where(x => x.DateOfReceipt >= startDate && x.DateOfReceipt <= endDate && x.SupplyShoes.Any(y => y.ShoesId == shoesId))
.ReadSupply(startDate, endDate, shoesId)
.Select(x => new { Date = x.DateOfReceipt.Date, CountIn = x.SupplyShoes.FirstOrDefault(y => y.ShoesId == shoesId)?.NumberOfPairs, CountOut = (int?)null })
)
.OrderBy(x => x.Date);

View File

@ -9,7 +9,7 @@ namespace ProjectShoeShop.Repositories;
public interface IOrderRepository
{
IEnumerable<Order> ReadOrder(DateTime? dateForm = null, DateTime? dateTo = null);
IEnumerable<Order> ReadOrder(DateTime? dateFrom = null, DateTime? dateTo = null, int? shoesId = null);
void CreateOrder(Order order);
}

View File

@ -9,7 +9,7 @@ namespace ProjectShoeShop.Repositories;
public interface ISupplyRepository
{
IEnumerable<Supply> ReadSupply(DateTime? dateForm = null, DateTime? dateTo = null);
IEnumerable<Supply> ReadSupply(DateTime? dateFrom = null, DateTime? dateTo = null, int? shoesId = null);
void CreateSupply(Supply supply);

View File

@ -8,5 +8,5 @@ namespace ProjectShoeShop.Repositories.Implementations;
public class ConnectionString : IConnectionString
{
string IConnectionString.ConnectionString => "Host=localhost;Port=5432;Username=postgres;Password=030405;Database=OTP_shoes";
string IConnectionString.ConnectionString => "Host=localhost;Port=5432;Username=postgres;Password=admin;Database=postgres";
}

View File

@ -1,4 +1,5 @@
using Dapper;
using DocumentFormat.OpenXml.Spreadsheet;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Npgsql;
@ -56,18 +57,60 @@ public class OrderRepository : IOrderRepository
}
}
public IEnumerable<Order> ReadOrder(DateTime? dateForm = null, DateTime? dateTo = null)
public IEnumerable<Order> ReadOrder(DateTime? dateFrom = null, DateTime? dateTo = null, int? shoesId = null)
{
_logger.LogInformation("Получение всех объектов");
try
{
var builder = new QueryBuilder();
if (dateFrom.HasValue)
{
builder.AddCondition("ord.OrderDate >= @dateFrom");
}
if (dateTo.HasValue)
{
builder.AddCondition("ord.OrderDate <= @dateTo");
}
if (shoesId.HasValue)
{
builder.AddCondition("ordi.ShoesId = @shoesId");
}
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"SELECT ord.*, ordi.ShoesId, ordi.NumberOfPairs, ordi.Size FROM ""Order"" ord
INNER JOIN OrderItem ordi on ord.Id = ordi.OrderId";
var contractorFuels = connection.Query<TempOrderItem>(querySelect);
var querySelect = @$"SELECT
ord.*,
cli.Name as ClientName,
ordi.ShoesId,
ordi.NumberOfPairs,
ordi.Size,
sho.Name as ShoesName
FROM ""Order"" ord
LEFT JOIN Client cli on cli.Id = ord.ClientId
INNER JOIN OrderItem ordi on ord.Id = ordi.OrderId
LEFT JOIN Shoes sho on sho.Id = ordi.ShoesId
{builder.Build()}";
var ordersDict = new Dictionary<int, List<OrderItem>>();
var orderItems = connection.Query<Order, OrderItem, Order>(querySelect,
(order, orderItem) =>
{
if (!ordersDict.TryGetValue(order.Id, out var ccf))
{
ccf = [];
ordersDict.Add(order.Id, ccf);
}
ccf.Add(orderItem);
return order;
}, splitOn: "ShoesId", param: new { dateFrom, dateTo, shoesId });
_logger.LogDebug("Полученные объекты: {json}",
JsonConvert.SerializeObject(contractorFuels));
return contractorFuels.GroupBy(x => x.Id, y => y, (key, value) => Order.CreateOperation(value.First(), value.Select(z => OrderItem.CreateElement(0, z.ShoesId, z.NumberOfPairs, z.Size)))).ToList();
JsonConvert.SerializeObject(orderItems));
return ordersDict.Select(x =>
{
var cf = orderItems.First(y => y.Id == x.Key);
cf.SetIrderItems(x.Value);
return cf;
}).ToArray();
}
catch (Exception ex)
{

View File

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

@ -56,18 +56,58 @@ public class SupplyRepository : ISupplyRepository
}
}
public IEnumerable<Supply> ReadSupply(DateTime? dateForm = null, DateTime? dateTo = null)
public IEnumerable<Supply> ReadSupply(DateTime? dateFrom = null, DateTime? dateTo = null, int? shoesId = null)
{
_logger.LogInformation("Получение всех объектов");
try
{
var builder = new QueryBuilder();
if (dateFrom.HasValue)
{
builder.AddCondition("sup.DateOfReceipt >= @dateFrom");
}
if (dateTo.HasValue)
{
builder.AddCondition("sup.DateOfReceipt <= @dateTo");
}
if (shoesId.HasValue)
{
builder.AddCondition("sups.ShoesId = @shoesId");
}
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"SELECT sup.*, sups.ShoesId, sups.Size, sups.NumberOfPairs FROM Supply sup
INNER JOIN SupplyShoes sups on sup.Id = sups.SupplyId";
var supplies = connection.Query<TempSupplyShoes>(querySelect);
var querySelect = @$"SELECT
sup.*,
sups.ShoesId,
sups.Size,
sups.NumberOfPairs,
sho.Name as ShoesName
FROM Supply sup
INNER JOIN SupplyShoes sups on sup.Id = sups.SupplyId
LEFT JOIN Shoes sho on sho.Id = sups.ShoesId
{builder.Build()}";
var suppliesDict = new Dictionary<int, List<SupplyShoes>>();
var supplyShoes = connection.Query<Supply, SupplyShoes, Supply>(querySelect,
(supply, supplyShoe) =>
{
if (!suppliesDict.TryGetValue(supply.Id, out var ccf))
{
ccf = [];
suppliesDict.Add(supply.Id, ccf);
}
ccf.Add(supplyShoe);
return supply;
}, splitOn: "ShoesId", param: new { dateFrom, dateTo, shoesId });
_logger.LogDebug("Полученные объекты: {json}",
JsonConvert.SerializeObject(supplies));
return supplies.GroupBy(x => x.Id, y => y, (key, value) => Supply.CreateEntity(value.First(), value.Select(z => SupplyShoes.CreateElement(0, z.ShoesId, z.Size, z.NumberOfPairs)))).ToList();
JsonConvert.SerializeObject(supplyShoes));
return suppliesDict.Select(x =>
{
var cf = supplyShoes.First(y => y.Id == x.Key);
cf.SetSupplyShoes(x.Value);
return cf;
}).ToArray();
}
catch (Exception ex)
{

View File

Before

Width:  |  Height:  |  Size: 5.1 KiB

After

Width:  |  Height:  |  Size: 5.1 KiB

View File

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

View File

Before

Width:  |  Height:  |  Size: 60 KiB

After

Width:  |  Height:  |  Size: 60 KiB

View File

Before

Width:  |  Height:  |  Size: 2.0 MiB

After

Width:  |  Height:  |  Size: 2.0 MiB