diff --git a/ProjectGarage/Reports/ChartReport.cs b/ProjectGarage/Reports/ChartReport.cs index f9fd92e..81f74e0 100644 --- a/ProjectGarage/Reports/ChartReport.cs +++ b/ProjectGarage/Reports/ChartReport.cs @@ -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(); } diff --git a/ProjectGarage/Reports/TableReport.cs b/ProjectGarage/Reports/TableReport.cs index 777acfd..e4e9e69 100644 --- a/ProjectGarage/Reports/TableReport.cs +++ b/ProjectGarage/Reports/TableReport.cs @@ -13,7 +13,7 @@ public class TableReport private readonly IReplenishmentRepository _replenishmentRepository; private readonly ITransportationRepository _transportationRepository; private readonly ILogger _logger; - internal static readonly string[] item = ["Водитель", "Дата", " пришло", "Количество ушло"]; + internal static readonly string[] item = ["Водитель", "Дата", "Кол-во пришло", "Кол-во ушло"]; public TableReport(IReplenishmentRepository replenishmentRepository, ITransportationRepository transportationRepository, ILogger 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 GetData(int fuelId, DateTime startDate, DateTime - endDate) + private List 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(); } } diff --git a/ProjectGarage/Repositories/Implementations/QueryBuilder.cs b/ProjectGarage/Repositories/Implementations/QueryBuilder.cs new file mode 100644 index 0000000..8e1d82f --- /dev/null +++ b/ProjectGarage/Repositories/Implementations/QueryBuilder.cs @@ -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}"; + } +} diff --git a/ProjectGarage/Repositories/Implementations/ReplenishmentRepository.cs b/ProjectGarage/Repositories/Implementations/ReplenishmentRepository.cs index 6ba6f5e..dd0eec9 100644 --- a/ProjectGarage/Repositories/Implementations/ReplenishmentRepository.cs +++ b/ProjectGarage/Repositories/Implementations/ReplenishmentRepository.cs @@ -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>(); var fuelReplenishment = connection.Query(querySelect, diff --git a/ProjectGarage/Repositories/Implementations/TransportationRepository.cs b/ProjectGarage/Repositories/Implementations/TransportationRepository.cs index a96fa45..d498157 100644 --- a/ProjectGarage/Repositories/Implementations/TransportationRepository.cs +++ b/ProjectGarage/Repositories/Implementations/TransportationRepository.cs @@ -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(querySelect); +LEFT JOIN route r ON r.Id = t.RouteId +{builder.Build()}"; + var transportations = connection.Query(querySelect, new {dateForm, dateTo, fuelId, driverId, routeId }); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(transportations)); return transportations;