111 lines
3.5 KiB
C#
111 lines
3.5 KiB
C#
using FuelAccounting.Repositories;
|
||
using Microsoft.Extensions.Logging;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace FuelAccounting.Reports;
|
||
|
||
internal class DocReport
|
||
{
|
||
private readonly ICarsRepository _carsRepository;
|
||
private readonly IDriversRepository _driversRepository;
|
||
private readonly IShiftRepository _shiftRepository;
|
||
private readonly IRouteRepository _routeRepository;
|
||
|
||
private readonly ILogger<DocReport> _logger;
|
||
|
||
public DocReport (ICarsRepository carsRepository, IDriversRepository driversRepository,
|
||
IShiftRepository shiftRepository, IRouteRepository routeRepository, ILogger<DocReport> logger)
|
||
{
|
||
_carsRepository = carsRepository ??
|
||
throw new ArgumentNullException(nameof(carsRepository));
|
||
_driversRepository = driversRepository ??
|
||
throw new ArgumentNullException(nameof(driversRepository));
|
||
_shiftRepository = shiftRepository ??
|
||
throw new ArgumentNullException(nameof(shiftRepository));
|
||
_routeRepository = routeRepository ??
|
||
throw new ArgumentNullException(nameof(routeRepository));
|
||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||
}
|
||
|
||
public bool CreateDoc(string filePath, bool includeCars, bool includeDrivers, bool includeShift, bool includeRoute)
|
||
{
|
||
try
|
||
{
|
||
var builder = new WordBuilder(filePath).AddHeader("Документ со справочниками");
|
||
|
||
if (includeCars)
|
||
{
|
||
builder.AddParagraph("Автомобили").AddTable([2400, 1200, 1200], GetCars());
|
||
}
|
||
|
||
if (includeDrivers)
|
||
{
|
||
builder.AddParagraph("Водители").AddTable([2400, 2400, 1200], GetDrivers());
|
||
}
|
||
|
||
if (includeShift)
|
||
{
|
||
builder.AddParagraph("Смены").AddTable([1200, 2400], GetShift());
|
||
}
|
||
|
||
if (includeRoute)
|
||
{
|
||
builder.AddParagraph("Маршруты").AddTable([2400], GetRoute());
|
||
}
|
||
|
||
builder.Build();
|
||
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Ошибка при формировании документа");
|
||
return false;
|
||
}
|
||
}
|
||
|
||
private List<string[]> GetCars()
|
||
{
|
||
return [
|
||
["Модель", "Категория", "Id Водителя"],
|
||
.. _carsRepository
|
||
.ReadCars()
|
||
.Select(x => new string[] {x.Model, x.Category.ToString(), x.DriverID.ToString()}),
|
||
];
|
||
}
|
||
|
||
private List<string[]> GetDrivers()
|
||
{
|
||
return [
|
||
["Имя", "Фамилия", "Категория прав"],
|
||
.. _driversRepository
|
||
.ReadDrivers()
|
||
.Select(x => new string[] {x.FirstName, x.LastName, x.DriverLicenceCategory.ToString()}),
|
||
];
|
||
}
|
||
|
||
private List<string[]> GetShift()
|
||
{
|
||
return [
|
||
["Количество часов", "Описание"],
|
||
.. _shiftRepository
|
||
.ReadShifts()
|
||
.Select(x => new string[] {x.AmountOfHours.ToString(), x.Description}),
|
||
];
|
||
}
|
||
|
||
private List<string[]> GetRoute()
|
||
{
|
||
return [
|
||
["Описание"],
|
||
.. _routeRepository
|
||
.ReadRoutes()
|
||
.Select(x => new string[] {x.Description}),
|
||
];
|
||
}
|
||
}
|