using GasStation.Repositories; using Microsoft.Extensions.Logging; namespace GasStation.Reports; internal class TableReport { private readonly ISellingRepository _sellingRepository; private readonly ILogger _logger; internal static readonly string[] item = ["Сотрудник", "Дата", "Продажа"]; public TableReport(ISellingRepository sellingRepository, ILogger logger) { _sellingRepository = sellingRepository ?? throw new ArgumentNullException(nameof(sellingRepository)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public bool CreateTable(string filePath, int productId, DateTime startDate, DateTime endDate) { try { new ExcelBuilder(filePath) .AddHeader("Сводка по движению товара", 0, 3) .AddParagraph("за период", 0) .AddTable([10, 10, 15], GetData(productId, startDate, endDate)) .Build(); return true; } catch (Exception ex) { _logger.LogError(ex, "Ошибка при формировании документа"); return false; } } private List GetData(int productId, DateTime startDate, DateTime endDate) { var data = _sellingRepository .ReadSelling() .Where(x => x.SellingDateTime >= startDate && x.SellingDateTime <= endDate && x.ProdutcSellings.Any(y => y.ProductID == productId)) .Select(x => new {x.GasmanId, Date = x.SellingDateTime, CountOut = x.ProdutcSellings.FirstOrDefault(y => y.ProductID == productId)?.Count }) .OrderBy(x => x.Date); return new List() { item }.Union(data .Select(x => new string[] { x.GasmanId.ToString(), x.Date.ToString(), x.CountOut?.ToString() ?? string.Empty})) .Union( [["Всего", "", data.Sum(x => x.CountOut ?? 0).ToString()]] ).ToList(); } }