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 date) { try { new ExcelBuilder(filePath) .AddHeader("Сводка по движению товара", 0, 3) .AddParagraph("за период", 0) .AddTable([10, 10, 15], GetData(productId, date)) .Build(); return true; } catch (Exception ex) { _logger.LogError(ex, "Ошибка при формировании документа"); return false; } } private List GetData(int productId, DateTime date) { var data = _sellingRepository .ReadSelling(date, 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("dd.MM.yyyy"), x.CountOut?.ToString("N0") ?? string.Empty})) .Union( [["Всего", "", data.Sum(x => x.CountOut ?? 0).ToString("N0")]] ).ToList(); } }