67 lines
2.4 KiB
C#
67 lines
2.4 KiB
C#
|
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;
|
|||
|
internal static readonly string[] item = ["Id заказа", "Книга", "Дата заказа", "Дата возврата", "Количество"];
|
|||
|
|
|||
|
|
|||
|
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();
|
|||
|
}
|
|||
|
}
|