2024-12-18 01:56:55 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using ProjectGarage.Repositories;
|
|
|
|
|
using ProjectGarage.Repositories.Implementations;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ProjectGarage.Reports;
|
|
|
|
|
|
|
|
|
|
public class ChartReport
|
|
|
|
|
{
|
|
|
|
|
private readonly ITransportationRepository _transportationRepository;
|
|
|
|
|
private readonly ILogger<ChartReport> _logger;
|
|
|
|
|
public ChartReport(ITransportationRepository transportationRepository, ILogger<ChartReport> logger)
|
|
|
|
|
{
|
|
|
|
|
_transportationRepository = transportationRepository ??
|
|
|
|
|
throw new ArgumentNullException(nameof(transportationRepository));
|
|
|
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
|
|
|
}
|
|
|
|
|
public bool CreateChart(string filePath, DateTime dateTime)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
new PdfBuilder(filePath)
|
|
|
|
|
.AddHeader("Транспортировка топлива")
|
2024-12-24 20:20:08 +04:00
|
|
|
|
.AddPieChart($"Отправленное топливо на {dateTime:dd MMMM yyyy}", GetData(dateTime))
|
2024-12-18 01:56:55 +04:00
|
|
|
|
.Build();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка при формировании документа");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private List<(string Caption, double Value)> GetData(DateTime dateTime)
|
|
|
|
|
{
|
|
|
|
|
return _transportationRepository
|
2024-12-24 20:20:08 +04:00
|
|
|
|
.ReadTransportation(dateForm: dateTime.Date, dateTo: dateTime.Date.AddDays(1))
|
2024-12-18 01:56:55 +04:00
|
|
|
|
.Where(x => x.TransportationDate.Date == dateTime.Date)
|
2024-12-24 20:20:08 +04:00
|
|
|
|
.GroupBy(x => x.FuelName, (key, group) => new {
|
|
|
|
|
FuelName = key,
|
2024-12-18 01:56:55 +04:00
|
|
|
|
Amount = group.Sum(x => x.Amount)
|
|
|
|
|
})
|
2024-12-24 20:20:08 +04:00
|
|
|
|
.Select(x => (x.FuelName, (double)x.Amount))
|
2024-12-18 01:56:55 +04:00
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|