using Microsoft.Extensions.Logging; using ProjectLibrary.Entities; using ProjectLibrary.Repositories; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ProjectLibrary.Reports; public class TableReport { private readonly IOrderRepository _orderRepository; private readonly ILogger _logger; internal static readonly string[] item = ["Id заказа", "Дата заказа", "Дата возврата","Книга", "Количество"]; public TableReport(IOrderRepository orderRepository, ILogger logger) { _orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public bool CreateTable(string filePath, DateTime? startDate = null, DateTime? endDate = null) { try { new ExcelBuilder(filePath) .AddHeader("Сводка по движению книг", 0, 5) .AddParagraph($"За период с {startDate:dd.MM.yyyy} по {endDate:dd.MM.yyyy}",0) .AddTable([10, 10, 10, 15, 15], GetData(startDate, endDate)) .Build(); return true; } catch (Exception ex) { _logger.LogError(ex, "Ошибка при формировании документа"); return false; } } private List GetData(DateTime? startDate = null, DateTime? endDate = null) { var s = _orderRepository .ReadOrders(StartDate: startDate, EndDate: endDate); var data = _orderRepository .ReadOrders(StartDate: startDate, EndDate: endDate) .Select(x => new { x.Id, DateOrder = x.OrderDate, DateReturn = x.ReturnDate, Book = x.BookOrders.FirstOrDefault(y => y.OrderID == x.Id)?.BookID,//вот эти данные не вставляются в эксель Count = x.BookOrders.FirstOrDefault(y => y.OrderID == x.Id)?.Count }) .OrderBy(x => x.Id); return new List() { item } .Union( data .Select(x => new string[] { x.Id.ToString(), x.DateOrder.ToString("dd.MM.yyyy"), x.DateReturn.ToString("dd.MM.yyyy"), x.Book?.ToString("N0") ?? string.Empty, x.Count?.ToString("N0") ?? string.Empty})) .Union([["Всего", "", "", "", data.Sum(x => x.Count ?? 0).ToString("N0")]]) .ToList(); //return null; } }