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 _logger; internal static readonly string[] item = ["Статусы", "Дата", "Изменений статуса"]; public TableReport( ICaseRepository caseRepository, IStatusHistoryRepository statusHistoryRepository, ILogger 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 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() { item } .Union( statusData .Select(x => new string[] { x.Status.ToString(), x.Date.ToString(), x.Count.ToString() })) .Union( [ [ "Всего", "", statusData.Sum(x => x.Count).ToString() ] ]) .ToList(); } }