PIbd-23_Baryshev_D.A._Garage_LabWork_4 #4

Closed
xysiboi wants to merge 5 commits from LabWork_4 into LabWork_3
5 changed files with 98 additions and 28 deletions
Showing only changes of commit f3eefb689e - Show all commits

View File

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

View File

@ -13,7 +13,7 @@ public class TableReport
private readonly IReplenishmentRepository _replenishmentRepository;
private readonly ITransportationRepository _transportationRepository;
private readonly ILogger<TableReport> _logger;
internal static readonly string[] item = ["Водитель", "Дата", " пришло", "Количество ушло"];
internal static readonly string[] item = ["Водитель", "Дата", "Кол-во пришло", "Кол-во ушло"];
public TableReport(IReplenishmentRepository replenishmentRepository,
ITransportationRepository transportationRepository, ILogger<TableReport> logger)
{
@ -28,11 +28,10 @@ public class TableReport
try
{
new ExcelBuilder(filePath)
.AddHeader("Сводка по движению топлива", 0, 4)
.AddParagraph("за период", 0)
.AddTable([10, 10, 15, 15], GetData(fuelId, startDate,
endDate))
.Build();
.AddHeader("Сводка по движению корма", 0, 4)
.AddParagraph($"за период c {startDate:dd.MM.yyyy} по {endDate: dd.MM.yyyy}", 0)
.AddTable([10, 10, 15, 15], GetData(fuelId, startDate, endDate))
.Build();
return true;
}
catch (Exception ex)
@ -42,15 +41,12 @@ public class TableReport
}
}
private List<string[]> GetData(int fuelId, DateTime startDate, DateTime
endDate)
private List<string[]> GetData(int fuelId, DateTime startDate, DateTime endDate)
{
var data = _replenishmentRepository
.ReadFuelReplenishment()
.Where(x => x.ReplenishmentDate >= startDate && x.ReplenishmentDate <= endDate &&
x.FuelFuelReplenishments.Any(y => y.FuelId == fuelId))
.ReadFuelReplenishment(dateForm: startDate, dateTo: endDate, fuelId: fuelId)
.Select(x => new {
x.DriverId,
x.DriverName,
Date = x.ReplenishmentDate,
CountIn = x.FuelFuelReplenishments.
FirstOrDefault(y => y.FuelId == fuelId)?.Amount,
@ -58,11 +54,11 @@ public class TableReport
})
.Union(
_transportationRepository
.ReadTransportation()
.ReadTransportation(dateForm: startDate, dateTo: endDate, fuelId: fuelId)
.Where(x => x.TransportationDate >= startDate &&
x.TransportationDate <= endDate && x.FuelId == fuelId)
.Select(x => new {
x.DriverId,
x.DriverName,
Date = x.TransportationDate,
CountIn = (int?)null,
CountOut = (int?)x.Amount
@ -73,11 +69,11 @@ public class TableReport
.Union(
data
.Select(x => new string[] {
x.DriverId.ToString(), x.Date.ToString(), x.CountIn?.ToString() ??
string.Empty, x.CountOut?.ToString() ?? string.Empty}))
x.DriverName.ToString(), x.Date.ToString("dd.MM.yyyy"), x.CountIn?.ToString("N0") ??
string.Empty, x.CountOut?.ToString("N0") ?? string.Empty}))
.Union(
[["Всего", "", data.Sum(x => x.CountIn ?? 0).ToString(),
data.Sum(x => x.CountOut ?? 0).ToString()]])
[["Всего", "", data.Sum(x => x.CountIn ?? 0).ToString("N0"),
data.Sum(x => x.CountOut ?? 0).ToString("N0")]])
.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("Получение всех объектов");
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
NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"SELECT
var querySelect = $@"SELECT
fr.*,
CONCAT(d.Fname, ' ', d.Lname) as DriverName,
ffr.FuelId,
@ -96,7 +114,8 @@ CONCAT(f.FuelName, ' ', f.Price) as FuelName
FROM fuelreplenishment fr
LEFT JOIN driver d on d.Id = fr.DriverId
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 fuelReplenishment = connection.Query<FuelReplenishment, FuelFuelReplenishment, FuelReplenishment>(querySelect,

View File

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