ProjectLib/ProjectLibrary/Reports/TableReport.cs

67 lines
2.4 KiB
C#
Raw Normal View History

2024-12-20 01:07:20 +04:00
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<TableReport> _logger;
2024-12-20 10:18:51 +04:00
internal static readonly string[] item = ["Id заказа","Дата заказа", "Дата возврата", "Книга", "Количество"];
2024-12-20 01:07:20 +04:00
public TableReport(IOrderRepository orderRepository, ILogger<TableReport> logger)
{
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public bool CreateTable(string filePath, DateTime startDate, DateTime endDate)
{
try
{
new ExcelBuilder(filePath)
.AddHeader("Сводка по движению книг", 0, 5)
.AddParagraph("за период", 0)
.AddTable([10, 10, 10, 15, 15], GetData(startDate, endDate))
.Build();
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при формировании документа");
return false;
}
}
private List<string[]> GetData(DateTime startDate, DateTime endDate)
{
var data = _orderRepository
.ReadOrders()
.Where(x => x.OrderDate >= startDate && x.OrderDate <= endDate && x.BookOrders.Any(y => y.OrderID == x.Id))
.Select(x => new
{
x.Id,
DateOrder = x.OrderDate,
DateReturn = x.ReturnDate,
Book = (int?)x.BookOrders.First(y => y.OrderID == x.Id).BookID,
Count = (int?)x.BookOrders.First(y => y.OrderID == x.Id).Count
})
.OrderBy(x => x.DateOrder);
return
new List<string[]>() { item }
.Union(
data
.Select(x => new string[] { x.Id.ToString(), x.DateOrder.ToString(), x.DateReturn.ToString(),
x.Book?.ToString() ?? string.Empty, x.Count?.ToString() ?? string.Empty}))
.Union([["Всего", "", "", "", data.Sum(x => x.Count ?? 0).ToString()]])
.ToList();
}
}