diff --git a/FuelAccounting/FuelAccounting/Forms/FormDirectoryReport.cs b/FuelAccounting/FuelAccounting/Forms/FormDirectoryReport.cs index b36d84d..382e970 100644 --- a/FuelAccounting/FuelAccounting/Forms/FormDirectoryReport.cs +++ b/FuelAccounting/FuelAccounting/Forms/FormDirectoryReport.cs @@ -42,7 +42,7 @@ public partial class FormDirectoryReport : Form } if (_container.Resolve().CreateDoc(sfd.FileName, checkBoxCars.Checked, - checkBoxDrivers.Checked, checkBoxRoutes.Checked, checkBoxShifts.Checked)) + checkBoxDrivers.Checked, checkBoxShifts.Checked, checkBoxRoutes.Checked)) { MessageBox.Show("Документ сформирован", "Формирование документа", MessageBoxButtons.OK, MessageBoxIcon.Information); } diff --git a/FuelAccounting/FuelAccounting/Reports/ChartReport.cs b/FuelAccounting/FuelAccounting/Reports/ChartReport.cs index 04595da..07b28b8 100644 --- a/FuelAccounting/FuelAccounting/Reports/ChartReport.cs +++ b/FuelAccounting/FuelAccounting/Reports/ChartReport.cs @@ -23,7 +23,7 @@ internal class ChartReport { new PdfBuilder(filePath) .AddHeader("Подсчет километража") - .AddPieChart("Кол-во проезженных километров", GetData(dateTime)) + .AddPieChart($"Кол-во проезженных километров {dateTime:dd.MM.yyyy}", GetData(dateTime)) .Build(); return true; @@ -38,9 +38,9 @@ internal class ChartReport private List<(string Caption, double Value)> GetData(DateTime dateTime) { return _refuelingRepository - .ReadRefuelings() + .ReadRefuelings(dateForm: dateTime.Date, dateTo: dateTime.Date.AddDays(1)) .Where(x => x.RefuelingDate == dateTime.Date) - .GroupBy(x => x.CarId, (key, group) => new { Id = key, Count = group.Sum(x => x.Kilometers) }) + .GroupBy(x => x.CarModel, (key, group) => new { Id = key, Count = group.Sum(x => x.Kilometers) }) .Select(x => (x.Id.ToString(), (double)x.Count)) .ToList(); } diff --git a/FuelAccounting/FuelAccounting/Reports/DocReport.cs b/FuelAccounting/FuelAccounting/Reports/DocReport.cs index 8f4ec7d..78a74d9 100644 --- a/FuelAccounting/FuelAccounting/Reports/DocReport.cs +++ b/FuelAccounting/FuelAccounting/Reports/DocReport.cs @@ -74,7 +74,7 @@ internal class DocReport ["Модель", "Категория", "Id Водителя"], .. _carsRepository .ReadCars() - .Select(x => new string[] {x.Model, x.Category.ToString(), x.DriverID.ToString()}), + .Select(x => new string[] {x.Model, x.Category.ToString(), x.DriverName.ToString()}), ]; } diff --git a/FuelAccounting/FuelAccounting/Reports/TableReport.cs b/FuelAccounting/FuelAccounting/Reports/TableReport.cs index 47a0630..a13ddc6 100644 --- a/FuelAccounting/FuelAccounting/Reports/TableReport.cs +++ b/FuelAccounting/FuelAccounting/Reports/TableReport.cs @@ -29,7 +29,7 @@ internal class TableReport { new ExcelBuilder(filePath) .AddHeader("Сводка по автомобилям", 0, 4) - .AddParagraph("За период", 0) + .AddParagraph($"За период c {startDate:dd.MM.yyyy} по {endDate:dd.MM.yyyy}", 0) .AddTable([15, 15, 10, 15], GetData(carId, startDate, endDate)) .Build(); @@ -46,23 +46,21 @@ internal class TableReport { // Получение данных об экипажах var equipageData = _equipageRepository - .ReadEquipages() - .Where(x => x.EquipageDate >= startDate && x.EquipageDate <= endDate && x.CarId == carId) + .ReadEquipages(dateForm: startDate, dateTo: endDate, carId: carId) .Select(x => new { - CarId = x.CarId, - Driver = x.DriverId.ToString(), + x.CarModel, + Driver = x.DriverName.ToString(), Liters = 0.0, // Данные о заправке отсутствуют Date = x.EquipageDate }); // Получение данных о заправках var refuelingData = _refuelingRepository - .ReadRefuelings() - .Where(x => x.RefuelingDate >= startDate && x.RefuelingDate <= endDate && x.CarId == carId) + .ReadRefuelings(dateForm: startDate, dateTo: endDate, carId: carId) .Select(x => new { - x.CarId, + x.CarModel, Driver = string.Empty, // Данные о водителе отсутствуют Liters = (double)x.LitersSpent, Date = x.RefuelingDate @@ -71,10 +69,10 @@ internal class TableReport // Объединение данных об экипажах и заправках var mergedData = equipageData .Union(refuelingData) - .GroupBy(x => new { x.CarId, x.Date }) // Группировка по дате и машине + .GroupBy(x => new { x.CarModel, x.Date }) // Группировка по дате и машине .Select(g => new { - g.Key.CarId, + g.Key.CarModel, Driver = string.Join(", ", g.Select(x => x.Driver).Where(d => !string.IsNullOrEmpty(d))), Liters = g.Sum(x => x.Liters), g.Key.Date @@ -86,7 +84,7 @@ internal class TableReport .Union( mergedData.Select(x => new string[] { - x.CarId.ToString(), + x.CarModel, x.Driver, x.Liters.ToString("F2"), // Форматирование до 2-х знаков после запятой x.Date.ToString("dd.MM.yyyy") // Форматирование даты diff --git a/FuelAccounting/FuelAccounting/Repositories/Implementations/EquipageRepository.cs b/FuelAccounting/FuelAccounting/Repositories/Implementations/EquipageRepository.cs index 12608f3..0ea65d9 100644 --- a/FuelAccounting/FuelAccounting/Repositories/Implementations/EquipageRepository.cs +++ b/FuelAccounting/FuelAccounting/Repositories/Implementations/EquipageRepository.cs @@ -79,8 +79,34 @@ WHERE id = @id"; _logger.LogInformation("Получение всех объектов"); try { + var builder = new QueryBuilder(); + if (dateForm.HasValue) + { + builder.AddCondition("e.equipage_date >= @dateForm"); + } + if (dateTo.HasValue) + { + builder.AddCondition("e.equipage_date <= @dateTo"); + } + if (carId.HasValue) + { + builder.AddCondition("e.car_id = @carId"); + } + if (driverId.HasValue) + { + builder.AddCondition("e.driver_id >= @driverId"); + } + if (shiftId.HasValue) + { + builder.AddCondition("e.shift_id <= @shiftId"); + } + if (routeId.HasValue) + { + builder.AddCondition("re.route_id = @routeId"); + } + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = @" + var querySelect = $@" SELECT e.id, c.model AS CarModel, @@ -95,7 +121,8 @@ INNER JOIN RoutesEquipage re ON re.equipage_id = e.id LEFT JOIN Cars c on c.id = e.car_id LEFT JOIN Drivers d on d.id = e.driver_id LEFT JOIN Shift s on s.id = e.shift_id -LEFT JOIN Routes r on r.id = re.route_id;"; +LEFT JOIN Routes r on r.id = re.route_id +{builder.Build()}"; var equipageDict = new Dictionary>(); var equipages = connection.Query(querySelect, @@ -109,7 +136,7 @@ LEFT JOIN Routes r on r.id = re.route_id;"; re.Add(equipages); return equip; - }, splitOn: "RouteId"); + }, splitOn: "RouteId", param: new {dateForm, dateTo, carId, driverId, shiftId, routeId}); _logger.LogDebug("Найденные объекты: {json}", JsonConvert.SerializeObject(equipages)); return equipageDict.Select(x => diff --git a/FuelAccounting/FuelAccounting/Repositories/Implementations/QueryBuilder.cs b/FuelAccounting/FuelAccounting/Repositories/Implementations/QueryBuilder.cs new file mode 100644 index 0000000..c1ab788 --- /dev/null +++ b/FuelAccounting/FuelAccounting/Repositories/Implementations/QueryBuilder.cs @@ -0,0 +1,35 @@ +using System.Text; + +namespace FuelAccounting.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}"; + } +} diff --git a/FuelAccounting/FuelAccounting/Repositories/Implementations/RefuelingRepository.cs b/FuelAccounting/FuelAccounting/Repositories/Implementations/RefuelingRepository.cs index 14ff281..8484d10 100644 --- a/FuelAccounting/FuelAccounting/Repositories/Implementations/RefuelingRepository.cs +++ b/FuelAccounting/FuelAccounting/Repositories/Implementations/RefuelingRepository.cs @@ -42,8 +42,22 @@ VALUES (@CarId, @Kilometers, @LitersSpent, @TypeOfFuel, @RefuelingDate)"; _logger.LogInformation("Получение всех объектов"); try { + var builder = new QueryBuilder(); + if (dateForm.HasValue) + { + builder.AddCondition("r.refueling_date >= @dateForm"); + } + if (dateTo.HasValue) + { + builder.AddCondition("r.refueling_date <= @dateTo"); + } + if (carId.HasValue) + { + builder.AddCondition("r.car_id = @carId"); + } + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = @" + var querySelect = $@" SELECT r.id, c.model AS CarModel, @@ -52,8 +66,9 @@ SELECT r.type_of_fuel AS TypeOfFuel, r.refueling_date AS RefuelingDate FROM Refueling r -LEFT JOIN Cars c on c.id = r.car_id"; - var refuelings = connection.Query(querySelect); +LEFT JOIN Cars c on c.id = r.car_id +{builder.Build()}"; + var refuelings = connection.Query(querySelect, new { dateForm, dateTo, carId }); _logger.LogDebug("Найденные объекты: {json}", JsonConvert.SerializeObject(refuelings)); return refuelings; }