48 lines
1.6 KiB
C#

using Microsoft.Extensions.Logging;
using Publication.Repositories;
using Publication.Repositories.Implementations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Publication.Reports;
public class ChartReport
{
private readonly IMaterialRepository _materialRepository;
private readonly ILogger<ChartReport> _logger;
public ChartReport(IMaterialRepository materialRepository, ILogger<ChartReport> logger)
{
_materialRepository = materialRepository ?? throw new ArgumentNullException(nameof(materialRepository));
_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 _materialRepository
.ReadMaterials()
.Where(x => x.DateMaterials.Date == dateTime.Date)
.GroupBy(x => x.Id, (key, group) => new { ID = key, Count = group.Sum(y => y.Count) })
.Select(x => (x.ID.ToString(), (double)x.Count))
.ToList();
}
}