ISEbd-22_Rozhkov.I.E._Simple/GasStation/Reports/TableReport.cs

55 lines
2.1 KiB
C#
Raw Permalink Normal View History

2024-12-11 18:13:28 +04:00
using GasStation.Repositories;
using Microsoft.Extensions.Logging;
namespace GasStation.Reports;
internal class TableReport
{
private readonly ISellingRepository _sellingRepository;
private readonly ILogger<TableReport> _logger;
2024-12-21 10:43:19 +04:00
internal static readonly string[] item = ["Сотрудник", "Дата", "Продажа"];
public TableReport(ISellingRepository sellingRepository,
2024-12-11 18:13:28 +04:00
ILogger<TableReport> logger)
{
_sellingRepository = sellingRepository ??
2024-12-21 10:43:19 +04:00
throw new ArgumentNullException(nameof(sellingRepository));
2024-12-11 18:13:28 +04:00
_logger = logger ??
throw new ArgumentNullException(nameof(logger));
}
public bool CreateTable(string filePath, int productId, DateTime startDate, DateTime endDate)
{
try
{
new ExcelBuilder(filePath)
2024-12-21 10:43:19 +04:00
.AddHeader("Сводка по движению товара", 0, 3)
2024-12-11 18:13:28 +04:00
.AddParagraph("за период", 0)
2024-12-21 10:43:19 +04:00
.AddTable([10, 10, 15], GetData(productId, startDate, endDate))
2024-12-11 18:13:28 +04:00
.Build();
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при формировании документа");
return false;
}
}
2024-12-21 10:43:19 +04:00
2024-12-11 18:13:28 +04:00
private List<string[]> GetData(int productId, DateTime startDate, DateTime endDate)
{
2024-12-21 10:43:19 +04:00
var data = _sellingRepository
2024-12-11 18:13:28 +04:00
.ReadSelling()
.Where(x => x.SellingDateTime >= startDate && x.SellingDateTime <= endDate && x.ProdutcSellings.Any(y => y.ProductID == productId))
2024-12-21 10:43:19 +04:00
.Select(x => new {x.GasmanId, Date = x.SellingDateTime,
2024-12-11 18:13:28 +04:00
CountOut = x.ProdutcSellings.FirstOrDefault(y => y.ProductID == productId)?.Count })
2024-12-21 10:43:19 +04:00
.OrderBy(x => x.Date);
2024-12-11 18:13:28 +04:00
return new List<string[]>() { item }.Union(data
.Select(x =>
2024-12-21 10:43:19 +04:00
new string[] { x.GasmanId.ToString(), x.Date.ToString(), x.CountOut?.ToString() ?? string.Empty}))
2024-12-11 18:13:28 +04:00
.Union(
2024-12-21 10:43:19 +04:00
[["Всего", "", data.Sum(x => x.CountOut ?? 0).ToString()]]
2024-12-11 18:13:28 +04:00
).ToList();
}
}