PIBD14-Boyko-M.S.-GSM-Autot.../ProjectGSM/Documents/ChartReport.cs

73 lines
2.8 KiB
C#
Raw Normal View History

2024-12-03 00:34:28 +04:00
using Microsoft.Extensions.Logging;
using ProjectGSM.Repositories;
namespace ProjectGSM.Documents;
public class ChartReport
{
private readonly ICaseRepository _caseRepository;
private readonly ICaseAdvocateRepository _caseAdvocateRepository;
private readonly IAdvocateRepository _advocateRepository;
private readonly ILogger<ChartReport> _logger;
public ChartReport(ICaseRepository caseRepository,
IAdvocateRepository advocateRepository,
ICaseAdvocateRepository caseAdvocateRepository,
ILogger<ChartReport> logger)
{
_caseRepository = caseRepository ??
throw new
ArgumentNullException(nameof(caseRepository));
_advocateRepository = advocateRepository ??
throw new ArgumentNullException(nameof(advocateRepository));
_caseAdvocateRepository = caseAdvocateRepository ??
throw new ArgumentNullException(nameof(caseAdvocateRepository));
_logger = logger ??
throw new ArgumentNullException(nameof(logger));
}
public bool CreateChart(string filePath, DateTime dateTime)
{
try
{
new PdfBuilder(filePath)
.AddHeader("Доля выручки адвоката")
.AddPieChart($"Всего до: {dateTime.Date}", GetData(dateTime))
.Build();
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при формировании документа");
return false;
}
}
public List<(string, double)> GetData(DateTime dateTime)
{
try
{
var advocateEarnings = _caseRepository
.ReadCases()
.Where(x => x.CreatedAt.Date <= dateTime.Date)
.Join(_caseAdvocateRepository.ReadCaseAdvocates().Where(x => x.CreatedAt.Date <= dateTime.Date),
caseItem => caseItem.Id,
caseAdvocate => caseAdvocate.CaseId,
(caseItem, caseAdvocate) => new { caseItem, caseAdvocate })
.GroupBy(x => x.caseAdvocate.AdvocateId, (key, group) => new
{
AdvocateName = _advocateRepository.ReadAdvocateById(key)?.Name ?? "Unknown",
Earnings = group.Sum(x => x.caseItem.Payment ? (x.caseItem.Verdict ? x.caseItem.VictoryPrice : x.caseItem.Price) : 0)
})
.Select(x => (x.AdvocateName, (double)x.Earnings))
.ToList();
return advocateEarnings;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error occurred while calculating advocate earnings");
return new List<(string, double)>();
}
}
}