52 lines
1.7 KiB
C#
Raw Normal View History

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("Транспортировка топлива")
.AddPieChart("Отправленное топливо", 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()
.Where(x => x.TransportationDate.Date == dateTime.Date)
.GroupBy(x => x.FuelId, (key, group) => new {
Id = key,
Amount = group.Sum(x => x.Amount)
})
.Select(x => (x.Id.ToString(), (double)x.Amount))
.ToList();
}
}