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

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