ISEbd-22_Rozhkov.I.E._Simple/GasStation/Reports/ChartReport.cs

43 lines
1.4 KiB
C#

using GasStation.Repositories;
using Microsoft.Extensions.Logging;
namespace GasStation.Reports;
internal class ChartReport
{
private readonly ISupplyRepository _supplyRepository;
private readonly ILogger<ChartReport> _logger;
public ChartReport(ISupplyRepository supplyRepository, ILogger<ChartReport> logger)
{
_supplyRepository = supplyRepository ??
throw new
ArgumentNullException(nameof(supplyRepository));
_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 _supplyRepository
.ReadSupply()
.Where(x => x.SupplyDate.Date == dateTime.Date)
.GroupBy(x => x.ProductID, (key, group) => new { Id = key, Count = group.Sum(x => x.Count)})
.Select(x => (x.Id.ToString(), (double)x.Count)).ToList();
}
}