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
{
private readonly ISellingRepository _sellingRepository;
private readonly ILogger<ChartReport> _logger;
public ChartReport(ISellingRepository sellingRepository, ILogger<ChartReport> logger)
{
_sellingRepository = sellingRepository ??
throw new
ArgumentNullException(nameof(sellingRepository));
_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 _sellingRepository
.ReadSelling()
.Where(x => x.SellingDateTime.Date == dateTime.Date)
.GroupBy(x => x.GasmanId, (key, group) => new { Id = key, Count = group.Sum(x => x.Count)})
.Select(x => (x.Id.ToString(), (double)x.Count)).ToList();
}
}