using GasStation.Repositories; using Microsoft.Extensions.Logging; namespace GasStation.Reports; internal class TableReport { private readonly ISupplyRepository _supplyRepository; private readonly ISellingRepository _sellingRepository; private readonly ILogger _logger; internal static readonly string[] item = ["Сотрудник", "Дата", "Количество пришло", "Количество ушло"]; public TableReport(ISupplyRepository supplyRepository, ISellingRepository sellingRepository, ILogger logger) { _supplyRepository = supplyRepository ?? throw new ArgumentNullException(nameof(supplyRepository)); _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, 4) .AddParagraph("за период", 0) .AddTable([10, 10, 15, 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 = _supplyRepository .ReadSupply() .Where(x => x.SupplyDate >= startDate && x.SupplyDate <= endDate) .Select(x => new { x.SupplierID, Date = x.SupplyDate, CountIn = (int?)x.Count, CountOut = (int?)null }) .Union( _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, CountIn = (int?)null, 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.CountIn?.ToString() ?? string.Empty, x.CountOut?.ToString() ?? string.Empty})) .Union( [["Всего", "", data.Sum(x => x.CountIn ?? 0).ToString(), data.Sum(x => x.CountOut ?? 0).ToString()]] ).ToList(); } }