62 lines
2.3 KiB
C#

using Microsoft.Extensions.Logging;
using LDBproject.Repositories;
namespace LDBproject.Reports
{
internal class TableReport
{
private readonly IOrderRep _orderRep;
private readonly ILogger<TableReport> _logger;
internal static readonly string[] item = { "Librarian ID", "Card ID", "Borrow Date", "Book ID", "Note" };
public TableReport(IOrderRep orderRep, ILogger<TableReport> logger)
{
_orderRep = orderRep ?? throw new ArgumentNullException(nameof(orderRep));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public bool CreateTable(string filePath, DateTime startDate, DateTime endDate)
{
try
{
new ExcelBuilder(filePath)
.AddHeader("Report about borrowed books", 0, 5) // Updated header
.AddParagraph($"Period: {startDate:yyyy-MM-dd} - {endDate:yyyy-MM-dd}", 0)
.AddTable(new[] { 4, 4, 7, 4, 7 }, GetData(startDate, endDate)) // Updated column widths
.Build();
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error while forming document");
return false;
}
}
private List<string[]> GetData(DateTime startDate, DateTime endDate)
{
var result = new List<string[]> { item };
var orders = _orderRep.GetOrdersInfo();
var flattenedData = orders
.SelectMany(order => order.Registrations
.Select(reg => new { order.LibrarianID, order.CardID,
order.BorrowDate, reg.BookID, reg.Note }))
.Where(x => startDate <= x.BorrowDate && x.BorrowDate <= endDate)
.ToList();
result.AddRange(flattenedData.Select(x => new string[]
{
x.LibrarianID.ToString(),
x.CardID.ToString(),
x.BorrowDate.ToString("yyyy-MM-dd"),
x.BookID.ToString(),
x.Note
}).ToList());
int totalBookCount = flattenedData.Count;
result.Add(new[] { "Total Books:", "", "", totalBookCount.ToString(), "" });
return result;
}
}
}