2024-12-24 20:20:08 +04:00

52 lines
1.8 KiB
C#

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("Транспортировка топлива")
.AddPieChart($"Отправленное топливо на {dateTime:dd MMMM yyyy}", GetData(dateTime))
.Build();
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при формировании документа");
return false;
}
}
private List<(string Caption, double Value)> GetData(DateTime dateTime)
{
return _transportationRepository
.ReadTransportation(dateForm: dateTime.Date, dateTo: dateTime.Date.AddDays(1))
.Where(x => x.TransportationDate.Date == dateTime.Date)
.GroupBy(x => x.FuelName, (key, group) => new {
FuelName = key,
Amount = group.Sum(x => x.Amount)
})
.Select(x => (x.FuelName, (double)x.Amount))
.ToList();
}
}