66 lines
2.7 KiB
C#
66 lines
2.7 KiB
C#
using Microsoft.Extensions.Logging;
|
||
using Publication.Repositories;
|
||
using Publication.Repositories.Implementations;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Publication.Reports;
|
||
|
||
public class TableReport
|
||
{
|
||
private readonly IMaterialRepository _materialRepository;
|
||
private readonly IPrintingHouseRepository _printingHouseRepository;
|
||
private readonly ILogger<TableReport> _logger;
|
||
internal static readonly string[] item = ["Дата", "Количество пришло", "Количество ушло"];
|
||
|
||
public TableReport(IPrintingHouseRepository printingHouseRepository, IMaterialRepository materialRepository, ILogger<TableReport> logger)
|
||
{
|
||
_printingHouseRepository = printingHouseRepository ?? throw new ArgumentNullException(nameof(printingHouseRepository));
|
||
_materialRepository = materialRepository ?? throw new ArgumentNullException(nameof(materialRepository));
|
||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||
}
|
||
|
||
public bool CreateTable(string filePath, DateTime startDate, DateTime endDate)
|
||
{
|
||
try
|
||
{
|
||
new ExcelBuilder(filePath)
|
||
.AddHeader("Сводка", 0, 4)
|
||
.AddParagraph("за период", 0)
|
||
.AddTable([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 = _printingHouseRepository
|
||
.ReadPrintingHouses()
|
||
.Where(x => x.Date >= startDate && x.Date <= endDate && x.printingHouseOrder.Any(y => y.PrintingHouseId == x.Id))
|
||
.Select(x => new { x.Date, CountIn = (int?)null, CountOut = (int?)x.printingHouseOrder.First(y => y.PrintingHouseId == x.Id).Count })
|
||
.Union(
|
||
_materialRepository
|
||
.ReadMaterials()
|
||
.Where(x => x.DateMaterials >= startDate && x.DateMaterials <= endDate)
|
||
.Select(x => new {Date = x.DateMaterials, CountIn = (int?)x.Count, CountOut = (int?)null }))
|
||
.OrderBy(x => x.Date);
|
||
return
|
||
new List<string[]>() { item }
|
||
.Union(
|
||
data
|
||
.Select(x => new string[] {x.Date.ToString(), x.CountIn?.ToString() ?? string.Empty, x.CountOut?.ToString() ?? string.Empty }))
|
||
.Union([["Всего", data.Sum(x => x.CountIn ?? 0).ToString(), data.Sum(x => x.CountOut ?? 0).ToString()]])
|
||
.ToList();
|
||
}
|
||
|
||
}
|