This commit is contained in:
Baryshev Dmitry 2024-12-24 20:20:08 +04:00
parent 840dd087ad
commit f3eefb689e
5 changed files with 98 additions and 28 deletions

View File

@ -25,7 +25,7 @@ public class ChartReport
{ {
new PdfBuilder(filePath) new PdfBuilder(filePath)
.AddHeader("Транспортировка топлива") .AddHeader("Транспортировка топлива")
.AddPieChart("Отправленное топливо", GetData(dateTime)) .AddPieChart($"Отправленное топливо на {dateTime:dd MMMM yyyy}", GetData(dateTime))
.Build(); .Build();
return true; return true;
} }
@ -38,13 +38,13 @@ public class ChartReport
private List<(string Caption, double Value)> GetData(DateTime dateTime) private List<(string Caption, double Value)> GetData(DateTime dateTime)
{ {
return _transportationRepository return _transportationRepository
.ReadTransportation() .ReadTransportation(dateForm: dateTime.Date, dateTo: dateTime.Date.AddDays(1))
.Where(x => x.TransportationDate.Date == dateTime.Date) .Where(x => x.TransportationDate.Date == dateTime.Date)
.GroupBy(x => x.FuelId, (key, group) => new { .GroupBy(x => x.FuelName, (key, group) => new {
Id = key, FuelName = key,
Amount = group.Sum(x => x.Amount) Amount = group.Sum(x => x.Amount)
}) })
.Select(x => (x.Id.ToString(), (double)x.Amount)) .Select(x => (x.FuelName, (double)x.Amount))
.ToList(); .ToList();
} }

View File

@ -13,7 +13,7 @@ public class TableReport
private readonly IReplenishmentRepository _replenishmentRepository; private readonly IReplenishmentRepository _replenishmentRepository;
private readonly ITransportationRepository _transportationRepository; private readonly ITransportationRepository _transportationRepository;
private readonly ILogger<TableReport> _logger; private readonly ILogger<TableReport> _logger;
internal static readonly string[] item = ["Водитель", "Дата", " пришло", "Количество ушло"]; internal static readonly string[] item = ["Водитель", "Дата", "Кол-во пришло", "Кол-во ушло"];
public TableReport(IReplenishmentRepository replenishmentRepository, public TableReport(IReplenishmentRepository replenishmentRepository,
ITransportationRepository transportationRepository, ILogger<TableReport> logger) ITransportationRepository transportationRepository, ILogger<TableReport> logger)
{ {
@ -28,10 +28,9 @@ public class TableReport
try try
{ {
new ExcelBuilder(filePath) new ExcelBuilder(filePath)
.AddHeader("Сводка по движению топлива", 0, 4) .AddHeader("Сводка по движению корма", 0, 4)
.AddParagraph("за период", 0) .AddParagraph($"за период c {startDate:dd.MM.yyyy} по {endDate: dd.MM.yyyy}", 0)
.AddTable([10, 10, 15, 15], GetData(fuelId, startDate, .AddTable([10, 10, 15, 15], GetData(fuelId, startDate, endDate))
endDate))
.Build(); .Build();
return true; return true;
} }
@ -42,15 +41,12 @@ public class TableReport
} }
} }
private List<string[]> GetData(int fuelId, DateTime startDate, DateTime private List<string[]> GetData(int fuelId, DateTime startDate, DateTime endDate)
endDate)
{ {
var data = _replenishmentRepository var data = _replenishmentRepository
.ReadFuelReplenishment() .ReadFuelReplenishment(dateForm: startDate, dateTo: endDate, fuelId: fuelId)
.Where(x => x.ReplenishmentDate >= startDate && x.ReplenishmentDate <= endDate &&
x.FuelFuelReplenishments.Any(y => y.FuelId == fuelId))
.Select(x => new { .Select(x => new {
x.DriverId, x.DriverName,
Date = x.ReplenishmentDate, Date = x.ReplenishmentDate,
CountIn = x.FuelFuelReplenishments. CountIn = x.FuelFuelReplenishments.
FirstOrDefault(y => y.FuelId == fuelId)?.Amount, FirstOrDefault(y => y.FuelId == fuelId)?.Amount,
@ -58,11 +54,11 @@ public class TableReport
}) })
.Union( .Union(
_transportationRepository _transportationRepository
.ReadTransportation() .ReadTransportation(dateForm: startDate, dateTo: endDate, fuelId: fuelId)
.Where(x => x.TransportationDate >= startDate && .Where(x => x.TransportationDate >= startDate &&
x.TransportationDate <= endDate && x.FuelId == fuelId) x.TransportationDate <= endDate && x.FuelId == fuelId)
.Select(x => new { .Select(x => new {
x.DriverId, x.DriverName,
Date = x.TransportationDate, Date = x.TransportationDate,
CountIn = (int?)null, CountIn = (int?)null,
CountOut = (int?)x.Amount CountOut = (int?)x.Amount
@ -73,11 +69,11 @@ public class TableReport
.Union( .Union(
data data
.Select(x => new string[] { .Select(x => new string[] {
x.DriverId.ToString(), x.Date.ToString(), x.CountIn?.ToString() ?? x.DriverName.ToString(), x.Date.ToString("dd.MM.yyyy"), x.CountIn?.ToString("N0") ??
string.Empty, x.CountOut?.ToString() ?? string.Empty})) string.Empty, x.CountOut?.ToString("N0") ?? string.Empty}))
.Union( .Union(
[["Всего", "", data.Sum(x => x.CountIn ?? 0).ToString(), [["Всего", "", data.Sum(x => x.CountIn ?? 0).ToString("N0"),
data.Sum(x => x.CountOut ?? 0).ToString()]]) data.Sum(x => x.CountOut ?? 0).ToString("N0")]])
.ToList(); .ToList();
} }
} }

View File

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

View File

@ -85,9 +85,27 @@ VALUES (@ReplenishmentId, @FuelId, @Amount)";
_logger.LogInformation("Получение всех объектов"); _logger.LogInformation("Получение всех объектов");
try try
{ {
var builder = new QueryBuilder();
if (dateForm.HasValue)
{
builder.AddCondition("fr.ReplenishmentDate >= @dateForm");
}
if (dateTo.HasValue)
{
builder.AddCondition("fr.ReplenishmentDate <= @dateTo");
}
if (driverId.HasValue)
{
builder.AddCondition("fr.DriverId = @driverId");
}
if (fuelId.HasValue)
{
builder.AddCondition("ffr.FuelId = @fuelId");
}
using var connection = new using var connection = new
NpgsqlConnection(_connectionString.ConnectionString); NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"SELECT var querySelect = $@"SELECT
fr.*, fr.*,
CONCAT(d.Fname, ' ', d.Lname) as DriverName, CONCAT(d.Fname, ' ', d.Lname) as DriverName,
ffr.FuelId, ffr.FuelId,
@ -96,7 +114,8 @@ CONCAT(f.FuelName, ' ', f.Price) as FuelName
FROM fuelreplenishment fr FROM fuelreplenishment fr
LEFT JOIN driver d on d.Id = fr.DriverId LEFT JOIN driver d on d.Id = fr.DriverId
INNER JOIN fuel_fuelreplenishment ffr ON ffr.ReplenishmentId = fr.Id INNER JOIN fuel_fuelreplenishment ffr ON ffr.ReplenishmentId = fr.Id
LEFT JOIN fuel f on f.Id = ffr.FuelId"; LEFT JOIN fuel f on f.Id = ffr.FuelId
{builder.Build()}";
var replenishmentDict = new Dictionary<int, List<FuelFuelReplenishment>>(); var replenishmentDict = new Dictionary<int, List<FuelFuelReplenishment>>();
var fuelReplenishment = connection.Query<FuelReplenishment, FuelFuelReplenishment, FuelReplenishment>(querySelect, var fuelReplenishment = connection.Query<FuelReplenishment, FuelFuelReplenishment, FuelReplenishment>(querySelect,

View File

@ -50,8 +50,29 @@ VALUES (@DriverId, @RouteId, @FuelId, @Amount, @TransportationDate)";
_logger.LogInformation("Получение всех объектов"); _logger.LogInformation("Получение всех объектов");
try try
{ {
var builder = new QueryBuilder();
if (dateForm.HasValue)
{
builder.AddCondition("t.TransportationDate >= @dateForm");
}
if (dateTo.HasValue)
{
builder.AddCondition("t.TransportationDate <= @dateTo");
}
if (fuelId.HasValue)
{
builder.AddCondition("t.FuelId = @fuelId");
}
if (driverId.HasValue)
{
builder.AddCondition("t.DriverId = @driverId");
}
if (routeId.HasValue)
{
builder.AddCondition("t.RouteId = @routeId");
}
using var connection = new NpgsqlConnection(_connectionString.ConnectionString); using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"SELECT var querySelect = $@"SELECT
t.*, t.*,
r.RouteName as RouteName, r.RouteName as RouteName,
CONCAT(d.Fname, ' ', d.Lname) as DriverName, CONCAT(d.Fname, ' ', d.Lname) as DriverName,
@ -59,8 +80,9 @@ VALUES (@DriverId, @RouteId, @FuelId, @Amount, @TransportationDate)";
FROM transportation t FROM transportation t
LEFT JOIN driver d ON d.Id = t.DriverId LEFT JOIN driver d ON d.Id = t.DriverId
LEFT JOIN fuel f ON f.Id = t.FuelId LEFT JOIN fuel f ON f.Id = t.FuelId
LEFT JOIN route r ON r.Id = t.RouteId"; LEFT JOIN route r ON r.Id = t.RouteId
var transportations = connection.Query<Transportation>(querySelect); {builder.Build()}";
var transportations = connection.Query<Transportation>(querySelect, new {dateForm, dateTo, fuelId, driverId, routeId });
_logger.LogDebug("Полученные объекты: {json}", _logger.LogDebug("Полученные объекты: {json}",
JsonConvert.SerializeObject(transportations)); JsonConvert.SerializeObject(transportations));
return transportations; return transportations;