PIBD14-Boyko-M.S.-GSM-Autot.../ProjectGSM/Documents/TableReport.cs
2024-12-25 01:34:08 +04:00

77 lines
2.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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($"за период c {startDate:dd.MM.yyyy} по{endDate: dd.MM.yyyy}", 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(startDate, endDate, 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("dd.MM.yyyy"), x.Count.ToString()
}))
.Union(
[
[
"Всего", "", statusData.Sum(x => x.Count).ToString()
]
])
.ToList();
}
}