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 15:49:09 +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));
}
2024-12-20 15:49:09 +04:00
public bool CreateTable(string filePath, DateTime? startDate = null, DateTime? endDate = null)
2024-12-20 01:07:20 +04:00
{
try
{
new ExcelBuilder(filePath)
.AddHeader("Сводка по движению книг", 0, 5)
2024-12-20 11:46:58 +04:00
.AddParagraph($"За период с {startDate:dd.MM.yyyy} по {endDate:dd.MM.yyyy}",0)
2024-12-20 01:07:20 +04:00
.AddTable([10, 10, 10, 15, 15], GetData(startDate, endDate))
.Build();
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при формировании документа");
return false;
}
}
2024-12-20 15:49:09 +04:00
private List<string[]> GetData(DateTime? startDate = null, DateTime? endDate = null)
2024-12-20 01:07:20 +04:00
{
var data = _orderRepository
2024-12-20 11:46:58 +04:00
.ReadOrders(StartDate: startDate, EndDate: endDate)
2024-12-20 01:07:20 +04:00
.Select(x => new
2024-12-20 15:49:09 +04:00
{
x.Id,
DateOrder = x.OrderDate,
DateReturn = x.ReturnDate,
2024-12-21 20:33:17 +04:00
Book = x.BookOrders.FirstOrDefault(y => y.OrderID == x.Id)?.BookName,
2024-12-20 15:49:09 +04:00
Count = x.BookOrders.FirstOrDefault(y => y.OrderID == x.Id)?.Count
})
.OrderBy(x => x.Id);
2024-12-20 01:07:20 +04:00
return
new List<string[]>() { item }
.Union(
data
2024-12-20 11:46:58 +04:00
.Select(x => new string[] { x.Id.ToString(), x.DateOrder.ToString("dd.MM.yyyy"), x.DateReturn.ToString("dd.MM.yyyy"),
2024-12-21 20:33:17 +04:00
x.Book?.ToString() ?? string.Empty, x.Count?.ToString("N0") ?? string.Empty}))
2024-12-20 11:46:58 +04:00
.Union([["Всего", "", "", "", data.Sum(x => x.Count ?? 0).ToString("N0")]])
2024-12-20 01:07:20 +04:00
.ToList();
2024-12-20 11:46:58 +04:00
//return null;
2024-12-20 01:07:20 +04:00
}
}