ProjectLib/ProjectLibrary/Reports/TableReport.cs
2024-12-21 19:56:37 +04:00

70 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 = 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<string[]> 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<string[]>() { 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;
}
}