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

78 lines
2.7 KiB
C#
Raw Normal View History

2024-12-03 00:34:28 +04:00
using Microsoft.Extensions.Logging;
using ProjectGSM.Entities;
using ProjectGSM.Repositories;
namespace ProjectGSM.Documents;
internal class TableReport
{
private readonly ICaseRepository _caseRepository;
private readonly IStatusHistoryRepository _statusHistoryRepository;
private readonly ILogger<TableReport> _logger;
internal static readonly string[] item = ["Статусы", "Дата", "Изменений статуса"];
public TableReport(
ICaseRepository caseRepository,
IStatusHistoryRepository statusHistoryRepository,
ILogger<TableReport> logger)
{
_caseRepository = caseRepository ??
throw new
ArgumentNullException(nameof(caseRepository));
_statusHistoryRepository = statusHistoryRepository ??
throw new
ArgumentNullException(nameof(statusHistoryRepository));
_logger = logger ??
throw new ArgumentNullException(nameof(logger));
}
public bool CreateTable(string filePath, int caseId, DateTime startDate,
DateTime endDate)
{
try
{
new ExcelBuilder(filePath)
.AddHeader($"Сводка по делу \"{_caseRepository.ReadCaseById(caseId).Description}\"", 0, 4)
.AddParagraph("за период", 0)
.AddTable([10, 10, 15], GetData(caseId, startDate,
endDate))
.Build();
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при формировании документа");
return false;
}
}
private List<string[]> GetData(int caseId, DateTime startDate, DateTime endDate)
{
var statusData = _statusHistoryRepository
.ReadStatusHistories()
.Where(x => x.CreatedAt >= startDate && x.CreatedAt <= endDate && x.CaseId == caseId)
.GroupBy(x => (x.Status, x.CreatedAt.Date))
.Select(x => new
{
Status = x.Key.Status,
Date = x.Key.Date,
Count = x.Count()
})
.ToList();
return new List<string[]>() { item }
.Union(
statusData
.Select(x => new string[]
{
x.Status.ToString(), x.Date.ToString(), x.Count.ToString()
}))
.Union(
[
[
"Всего", "", statusData.Sum(x => x.Count).ToString()
]
])
.ToList();
}
}