2024-12-20 17:55:26 +04:00

66 lines
2.9 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 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 = ["Id","Дата", "Количество заказов пришло", "Количество заказов ушло"];
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($"за период {startDate:dd.MM.yyyy} по { endDate: dd.MM.yyyy}", 0)
.AddTable([10, 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 {Name = x.Title, 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 {Name = x.Material.ToString(), 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.Name.ToString(), 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();
}
}