This commit is contained in:
qkrlnt 2024-12-09 13:38:18 +04:00
parent 5d151fc44f
commit 373c92633c
7 changed files with 97 additions and 22 deletions

View File

@ -42,7 +42,7 @@ public partial class FormDirectoryReport : Form
} }
if (_container.Resolve<DocReport>().CreateDoc(sfd.FileName, checkBoxCars.Checked, if (_container.Resolve<DocReport>().CreateDoc(sfd.FileName, checkBoxCars.Checked,
checkBoxDrivers.Checked, checkBoxRoutes.Checked, checkBoxShifts.Checked)) checkBoxDrivers.Checked, checkBoxShifts.Checked, checkBoxRoutes.Checked))
{ {
MessageBox.Show("Документ сформирован", "Формирование документа", MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBox.Show("Документ сформирован", "Формирование документа", MessageBoxButtons.OK, MessageBoxIcon.Information);
} }

View File

@ -23,7 +23,7 @@ internal class ChartReport
{ {
new PdfBuilder(filePath) new PdfBuilder(filePath)
.AddHeader("Подсчет километража") .AddHeader("Подсчет километража")
.AddPieChart("Кол-во проезженных километров", GetData(dateTime)) .AddPieChart($"Кол-во проезженных километров {dateTime:dd.MM.yyyy}", GetData(dateTime))
.Build(); .Build();
return true; return true;
@ -38,9 +38,9 @@ internal class ChartReport
private List<(string Caption, double Value)> GetData(DateTime dateTime) private List<(string Caption, double Value)> GetData(DateTime dateTime)
{ {
return _refuelingRepository return _refuelingRepository
.ReadRefuelings() .ReadRefuelings(dateForm: dateTime.Date, dateTo: dateTime.Date.AddDays(1))
.Where(x => x.RefuelingDate == dateTime.Date) .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)) .Select(x => (x.Id.ToString(), (double)x.Count))
.ToList(); .ToList();
} }

View File

@ -74,7 +74,7 @@ internal class DocReport
["Модель", "Категория", "Id Водителя"], ["Модель", "Категория", "Id Водителя"],
.. _carsRepository .. _carsRepository
.ReadCars() .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()}),
]; ];
} }

View File

@ -29,7 +29,7 @@ internal class TableReport
{ {
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([15, 15, 10, 15], GetData(carId, startDate, endDate)) .AddTable([15, 15, 10, 15], GetData(carId, startDate, endDate))
.Build(); .Build();
@ -46,23 +46,21 @@ internal class TableReport
{ {
// Получение данных об экипажах // Получение данных об экипажах
var equipageData = _equipageRepository var equipageData = _equipageRepository
.ReadEquipages() .ReadEquipages(dateForm: startDate, dateTo: endDate, carId: carId)
.Where(x => x.EquipageDate >= startDate && x.EquipageDate <= endDate && x.CarId == carId)
.Select(x => new .Select(x => new
{ {
CarId = x.CarId, x.CarModel,
Driver = x.DriverId.ToString(), Driver = x.DriverName.ToString(),
Liters = 0.0, // Данные о заправке отсутствуют Liters = 0.0, // Данные о заправке отсутствуют
Date = x.EquipageDate Date = x.EquipageDate
}); });
// Получение данных о заправках // Получение данных о заправках
var refuelingData = _refuelingRepository var refuelingData = _refuelingRepository
.ReadRefuelings() .ReadRefuelings(dateForm: startDate, dateTo: endDate, carId: carId)
.Where(x => x.RefuelingDate >= startDate && x.RefuelingDate <= endDate && x.CarId == carId)
.Select(x => new .Select(x => new
{ {
x.CarId, x.CarModel,
Driver = string.Empty, // Данные о водителе отсутствуют Driver = string.Empty, // Данные о водителе отсутствуют
Liters = (double)x.LitersSpent, Liters = (double)x.LitersSpent,
Date = x.RefuelingDate Date = x.RefuelingDate
@ -71,10 +69,10 @@ internal class TableReport
// Объединение данных об экипажах и заправках // Объединение данных об экипажах и заправках
var mergedData = equipageData var mergedData = equipageData
.Union(refuelingData) .Union(refuelingData)
.GroupBy(x => new { x.CarId, x.Date }) // Группировка по дате и машине .GroupBy(x => new { x.CarModel, x.Date }) // Группировка по дате и машине
.Select(g => new .Select(g => new
{ {
g.Key.CarId, g.Key.CarModel,
Driver = string.Join(", ", g.Select(x => x.Driver).Where(d => !string.IsNullOrEmpty(d))), Driver = string.Join(", ", g.Select(x => x.Driver).Where(d => !string.IsNullOrEmpty(d))),
Liters = g.Sum(x => x.Liters), Liters = g.Sum(x => x.Liters),
g.Key.Date g.Key.Date
@ -86,7 +84,7 @@ internal class TableReport
.Union( .Union(
mergedData.Select(x => new string[] mergedData.Select(x => new string[]
{ {
x.CarId.ToString(), x.CarModel,
x.Driver, x.Driver,
x.Liters.ToString("F2"), // Форматирование до 2-х знаков после запятой x.Liters.ToString("F2"), // Форматирование до 2-х знаков после запятой
x.Date.ToString("dd.MM.yyyy") // Форматирование даты x.Date.ToString("dd.MM.yyyy") // Форматирование даты

View File

@ -79,8 +79,34 @@ WHERE id = @id";
_logger.LogInformation("Получение всех объектов"); _logger.LogInformation("Получение всех объектов");
try 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); using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @" var querySelect = $@"
SELECT SELECT
e.id, e.id,
c.model AS CarModel, 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 Cars c on c.id = e.car_id
LEFT JOIN Drivers d on d.id = e.driver_id LEFT JOIN Drivers d on d.id = e.driver_id
LEFT JOIN Shift s on s.id = e.shift_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<int, List<RoutesEqipage>>(); var equipageDict = new Dictionary<int, List<RoutesEqipage>>();
var equipages = connection.Query<Equipage, RoutesEqipage, Equipage>(querySelect, var equipages = connection.Query<Equipage, RoutesEqipage, Equipage>(querySelect,
@ -109,7 +136,7 @@ LEFT JOIN Routes r on r.id = re.route_id;";
re.Add(equipages); re.Add(equipages);
return equip; return equip;
}, splitOn: "RouteId"); }, splitOn: "RouteId", param: new {dateForm, dateTo, carId, driverId, shiftId, routeId});
_logger.LogDebug("Найденные объекты: {json}", JsonConvert.SerializeObject(equipages)); _logger.LogDebug("Найденные объекты: {json}", JsonConvert.SerializeObject(equipages));
return equipageDict.Select(x => return equipageDict.Select(x =>

View File

@ -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}";
}
}

View File

@ -42,8 +42,22 @@ VALUES (@CarId, @Kilometers, @LitersSpent, @TypeOfFuel, @RefuelingDate)";
_logger.LogInformation("Получение всех объектов"); _logger.LogInformation("Получение всех объектов");
try 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); using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @" var querySelect = $@"
SELECT SELECT
r.id, r.id,
c.model AS CarModel, c.model AS CarModel,
@ -52,8 +66,9 @@ SELECT
r.type_of_fuel AS TypeOfFuel, r.type_of_fuel AS TypeOfFuel,
r.refueling_date AS RefuelingDate r.refueling_date AS RefuelingDate
FROM Refueling r FROM Refueling r
LEFT JOIN Cars c on c.id = r.car_id"; LEFT JOIN Cars c on c.id = r.car_id
var refuelings = connection.Query<Refueling>(querySelect); {builder.Build()}";
var refuelings = connection.Query<Refueling>(querySelect, new { dateForm, dateTo, carId });
_logger.LogDebug("Найденные объекты: {json}", JsonConvert.SerializeObject(refuelings)); _logger.LogDebug("Найденные объекты: {json}", JsonConvert.SerializeObject(refuelings));
return refuelings; return refuelings;
} }