Лабораторная работа №4 (3)
This commit is contained in:
parent
2194dbf7a4
commit
3029870f4e
@ -84,7 +84,7 @@
|
||||
Controls.Add(checkBoxWorkers);
|
||||
Controls.Add(checkBoxClients);
|
||||
Name = "FormDirectoryReport";
|
||||
Text = "FormDirectoryReport";
|
||||
Text = "Отчёт о компании";
|
||||
ResumeLayout(false);
|
||||
PerformLayout();
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ internal class ChartReport
|
||||
{
|
||||
new PdfBuilder(filePath)
|
||||
.AddHeader("Оформление заказа")
|
||||
.AddPieChart("Проданные изделия", GetData(dateTime))
|
||||
.AddPieChart($"Проданные изделия за {dateTime: dd MMMM yyyy}", GetData(dateTime))
|
||||
.Build();
|
||||
return true;
|
||||
}
|
||||
@ -41,10 +41,9 @@ internal class ChartReport
|
||||
private List<(string Caption, double Value)> GetData(DateTime dateTime)
|
||||
{
|
||||
return _invoiceRepository
|
||||
.ReadInvoices()
|
||||
.Where(x => x.Date.Date == dateTime.Date)
|
||||
.GroupBy(x => x.ClientId, (key, group) => new { Id = key, Count = group.Sum(x => x.CountProduct) })
|
||||
.Select(x => (x.Id.ToString(), (double)x.Count))
|
||||
.ReadInvoices(dateFrom: dateTime.Date, dateTo: dateTime.Date.AddDays(1))
|
||||
.GroupBy(x => x.ClientName, (key, group) => new { ClientName = key, Count = group.Sum(x => x.CountProduct) })
|
||||
.Select(x => (x.ClientName, (double)x.Count))
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ internal class TableReport
|
||||
{
|
||||
new ExcelBuilder(filePath)
|
||||
.AddHeader("Сводка по доставке изделий", 0, 4)
|
||||
.AddParagraph("за период", 0)
|
||||
.AddParagraph($"за период c {startDate:dd.MM.yyyy} пo {endDate: dd.MM.yyyy}", 0)
|
||||
.AddTable([10, 10, 15, 15], GetData(productId, startDate, endDate))
|
||||
.Build();
|
||||
|
||||
@ -51,20 +51,18 @@ internal class TableReport
|
||||
private List<string[]> GetData(int productId, DateTime startDate, DateTime endDate)
|
||||
{
|
||||
var data = _deliveryRepository
|
||||
.ReadDeliverys()
|
||||
.Where(x => x.DateDelivery >= startDate && x.DateDelivery <= endDate && x.DeliveryProducts.Any(y => y.ProductId == productId))
|
||||
.Select(x => new { x.WorkerId, Date = x.DateDelivery, CountIn = x.DeliveryProducts.FirstOrDefault(y => y.ProductId == productId)?.Count, CountOut = (int?)null })
|
||||
.ReadDeliverys(dateFrom: startDate, dateTo: endDate, productId: productId)
|
||||
.Select(x => new { x.WorkerName, Date = x.DateDelivery, CountIn = x.DeliveryProducts.FirstOrDefault(y => y.ProductId == productId)?.Count, CountOut = (int?)null })
|
||||
.Union(
|
||||
_invoiceRepository
|
||||
.ReadInvoices()
|
||||
.Where(x => x.Date >= startDate && x.Date <= endDate)
|
||||
.Select(x => new { x.WorkerId, Date = x.Date, CountIn = (int?)null, CountOut = (int?)x.CountProduct }))
|
||||
.ReadInvoices(dateFrom: startDate, dateTo: endDate, productId: productId)
|
||||
.Select(x => new { x.WorkerName, Date = x.Date, CountIn = (int?)null, CountOut = (int?)x.CountProduct }))
|
||||
.OrderBy(x => x.Date);
|
||||
|
||||
return new List<string[]>() { item }
|
||||
.Union(
|
||||
data
|
||||
.Select(x => new string[] { x.WorkerId.ToString(), x.Date.ToString(), x.CountIn?.ToString() ?? string.Empty, x.CountOut?.ToString() ?? string.Empty }))
|
||||
.Select(x => new string[] { x.WorkerName.ToString(), x.Date.ToString("dd.MM.yyyy"), x.CountIn?.ToString() ?? string.Empty, x.CountOut?.ToString() ?? string.Empty }))
|
||||
.Union(
|
||||
[["Всего", "", data.Sum(x => x.CountIn ?? 0).ToString(), data.Sum(x => x.CountOut ?? 0).ToString()]])
|
||||
.ToList();
|
||||
|
@ -83,9 +83,27 @@ public class DeliveryRepository : IDeliveryRepository
|
||||
|
||||
try
|
||||
{
|
||||
var builder = new QueryBuilder();
|
||||
if (dateFrom.HasValue)
|
||||
{
|
||||
builder.AddCondition("d.DateDelivery >= @dateFrom");
|
||||
}
|
||||
if (dateTo.HasValue)
|
||||
{
|
||||
builder.AddCondition("d.DateDelivery <= @dateTo");
|
||||
}
|
||||
if (productId.HasValue)
|
||||
{
|
||||
builder.AddCondition("dp.ProductId = @productId");
|
||||
}
|
||||
if (workerId.HasValue)
|
||||
{
|
||||
builder.AddCondition("d.WorkerId = @workerId");
|
||||
}
|
||||
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT
|
||||
var querySelect = $@"
|
||||
SELECT
|
||||
d.*,
|
||||
CONCAT(w.LastName, ' ', w.FirstName) as WorkerName,
|
||||
dp.ProductID,
|
||||
@ -94,7 +112,8 @@ public class DeliveryRepository : IDeliveryRepository
|
||||
FROM Deliverys d
|
||||
LEFT JOIN Workers w on w.Id = d.WorkerId
|
||||
INNER JOIN DeliveryProducts dp ON dp.DeliveryId = d.Id
|
||||
LEFT JOIN Products p on p.Id = dp.ProductId";
|
||||
LEFT JOIN Products p on p.Id = dp.ProductId
|
||||
{builder.Build()}";
|
||||
var deliveryDict = new Dictionary<int, List<DeliveryProduct>>();
|
||||
|
||||
var deliverys = connection.Query<Delivery, DeliveryProduct, Delivery>(querySelect,
|
||||
@ -108,7 +127,7 @@ LEFT JOIN Products p on p.Id = dp.ProductId";
|
||||
|
||||
dyp.Add(deliveryy);
|
||||
return deliver;
|
||||
}, splitOn: "ProductId");
|
||||
}, splitOn: "ProductId", param: new { dateFrom, dateTo, productId, workerId });
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(deliverys));
|
||||
|
||||
return deliveryDict.Select(x =>
|
||||
|
@ -49,8 +49,30 @@ public class InvoiceRepository : IInvoiceRepository
|
||||
|
||||
try
|
||||
{
|
||||
var builder =new QueryBuilder();
|
||||
if (dateFrom.HasValue)
|
||||
{
|
||||
builder.AddCondition("i.Date >= @dateFrom");
|
||||
}
|
||||
if (dateTo.HasValue)
|
||||
{
|
||||
builder.AddCondition("i.Date <= @dateTo");
|
||||
}
|
||||
if (productId.HasValue)
|
||||
{
|
||||
builder.AddCondition("i.ProductId = @productId");
|
||||
}
|
||||
if (workerId.HasValue)
|
||||
{
|
||||
builder.AddCondition("i.WorkerId = @workerId");
|
||||
}
|
||||
if (clientId.HasValue)
|
||||
{
|
||||
builder.AddCondition("i.ClientId = @clientId");
|
||||
}
|
||||
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"SELECT
|
||||
var querySelect = $@"SELECT
|
||||
i.*,
|
||||
p.Name as ProductName,
|
||||
CONCAT(w.LastName, ' ', w.FirstName) as WorkerName,
|
||||
@ -58,8 +80,9 @@ public class InvoiceRepository : IInvoiceRepository
|
||||
FROM Invoices i
|
||||
LEFT JOIN Products p on p.Id = i.ProductId
|
||||
LEFT JOIN Workers w on w.Id = i.WorkerId
|
||||
LEFT JOIN Clients c on c.Id = i.ClientId";
|
||||
var invoices = connection.Query<Invoice>(querySelect);
|
||||
LEFT JOIN Clients c on c.Id = i.ClientId
|
||||
{builder.Build()}";
|
||||
var invoices = connection.Query<Invoice>(querySelect, new { dateFrom, dateTo, productId, workerId, clientId });
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(invoices));
|
||||
return invoices;
|
||||
}
|
||||
|
@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FurnitureCompany.Repositories.Implementations;
|
||||
|
||||
public 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}";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user