115 lines
3.8 KiB
C#
115 lines
3.8 KiB
C#
using Microsoft.Extensions.Logging;
|
||
using ProjectGarage.Repositories;
|
||
using ProjectGarage.Repositories.Implementations;
|
||
using Serilog.Core;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace ProjectGarage.Reports;
|
||
|
||
public class DocReport
|
||
{
|
||
private readonly IFuelRepository _fuelRepository;
|
||
private readonly IDriverRepository _driverRepository;
|
||
private readonly IRouteRepository _routeRepository;
|
||
private readonly ITruckRepository _truckRepository;
|
||
|
||
private readonly ILogger<DocReport> _logger;
|
||
|
||
|
||
public DocReport(IFuelRepository fuelRepository, IDriverRepository driverRepository,
|
||
IRouteRepository routeRepository,ITruckRepository truckRepository,IConnectionString connectionstring,
|
||
ILogger<DocReport> logger)
|
||
{
|
||
_fuelRepository = fuelRepository ?? throw new ArgumentNullException(nameof(fuelRepository));
|
||
_driverRepository = driverRepository ?? throw new ArgumentNullException(nameof(driverRepository));
|
||
_routeRepository = routeRepository ?? throw new ArgumentNullException(nameof(routeRepository));
|
||
_truckRepository = truckRepository ?? throw new ArgumentNullException(nameof(truckRepository));
|
||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||
}
|
||
|
||
public bool CreateDoc(string filePath, bool includeFuels, bool includeDrivers,
|
||
bool includeRoutes, bool includeTrucks)
|
||
{
|
||
try
|
||
{
|
||
var builder = new WordBuilder(filePath)
|
||
.AddHeader("Документ со справочниками");
|
||
if (includeTrucks)
|
||
{
|
||
builder.AddParagraph("Фуры")
|
||
.AddTable([2400, 2400, 2400],
|
||
GetTrucks());
|
||
}
|
||
if (includeFuels)
|
||
{
|
||
builder.AddParagraph("Топлива")
|
||
.AddTable([2400, 1200, 1200],
|
||
GetFuels());
|
||
}
|
||
if (includeDrivers)
|
||
{
|
||
builder.AddParagraph("Водители")
|
||
.AddTable([2400, 2400, 2400],
|
||
GetDrivers());
|
||
}
|
||
if (includeRoutes)
|
||
{
|
||
builder.AddParagraph("Маршруты")
|
||
.AddTable([2400, 2400, 2400],
|
||
GetRoutes());
|
||
}
|
||
builder.Build();
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Ошибка при формировании документа");
|
||
return false;
|
||
}
|
||
}
|
||
|
||
private List<string[]> GetTrucks()
|
||
{
|
||
return [
|
||
["Тип топлива", "Название топлива", "Цена"],
|
||
.. _fuelRepository
|
||
.ReadFuels()
|
||
.Select(x => new string[] { x.FuelType.ToString(), x.FuelName, x.Price.ToString() }),
|
||
];
|
||
}
|
||
|
||
private List<string[]> GetFuels()
|
||
{
|
||
return [
|
||
["Тип топлива", "Название топлива", "Цена"],
|
||
.. _fuelRepository
|
||
.ReadFuels()
|
||
.Select(x => new string[] { x.FuelType.ToString(), x.FuelName, x.Price.ToString() }),
|
||
];
|
||
}
|
||
|
||
private List<string[]> GetDrivers()
|
||
{
|
||
return [
|
||
["Имя", "Фамилия", "Номера машины"],
|
||
.. _driverRepository
|
||
.ReadDrivers()
|
||
.Select(x => new string[] { x.Fname, x.Lname, x.TruckId.ToString() }),
|
||
];
|
||
}
|
||
|
||
private List<string[]> GetRoutes()
|
||
{
|
||
return [
|
||
["Начальная т.", "Конечная т.", "Протяженность"],
|
||
.. _routeRepository
|
||
.ReadRoute()
|
||
.Select(x => new string[] { x.StartP, x.FinalP, x.Length.ToString() }),
|
||
];
|
||
}
|
||
}
|