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

43 lines
1.4 KiB
C#
Raw Normal View History

2024-12-11 18:13:28 +04:00
using GasStation.Repositories;
using Microsoft.Extensions.Logging;
namespace GasStation.Reports;
internal class ChartReport
{
2024-12-21 10:43:19 +04:00
private readonly ISupplyRepository _supplyRepository;
2024-12-11 18:13:28 +04:00
private readonly ILogger<ChartReport> _logger;
2024-12-21 10:43:19 +04:00
public ChartReport(ISupplyRepository supplyRepository, ILogger<ChartReport> logger)
2024-12-11 18:13:28 +04:00
{
2024-12-21 10:43:19 +04:00
_supplyRepository = supplyRepository ??
2024-12-11 18:13:28 +04:00
throw new
2024-12-21 10:43:19 +04:00
ArgumentNullException(nameof(supplyRepository));
2024-12-11 18:13:28 +04:00
_logger = logger ??
throw new ArgumentNullException(nameof(logger));
}
public bool CreateChart(string filePath, DateTime dateTime)
{
try
{
new PdfBuilder(filePath)
2024-12-21 10:43:19 +04:00
.AddHeader("Поступление продукта")
.AddPieChart("Поступивший товар", GetData(dateTime))
2024-12-11 18:13:28 +04:00
.Build();
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при формировании документа");
return false;
}
}
private List<(string Caption, double Value)> GetData(DateTime dateTime)
{
2024-12-21 10:43:19 +04:00
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)})
2024-12-11 18:13:28 +04:00
.Select(x => (x.Id.ToString(), (double)x.Count)).ToList();
}
}