2024-12-18 01:56:55 +04:00

84 lines
3.2 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 ProjectGarage.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectGarage.Reports;
public class TableReport
{
private readonly IReplenishmentRepository _replenishmentRepository;
private readonly ITransportationRepository _transportationRepository;
private readonly ILogger<TableReport> _logger;
internal static readonly string[] item = ["Водитель", "Дата", " пришло", "Количество ушло"];
public TableReport(IReplenishmentRepository replenishmentRepository,
ITransportationRepository transportationRepository, ILogger<TableReport> logger)
{
_replenishmentRepository = replenishmentRepository ??
throw new ArgumentNullException(nameof(replenishmentRepository));
_transportationRepository = transportationRepository ??
throw new ArgumentNullException(nameof(transportationRepository));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public bool CreateTable(string filePath, int fuelId, DateTime startDate, DateTime endDate)
{
try
{
new ExcelBuilder(filePath)
.AddHeader("Сводка по движению топлива", 0, 4)
.AddParagraph("за период", 0)
.AddTable([10, 10, 15, 15], GetData(fuelId, startDate,
endDate))
.Build();
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при формировании документа");
return false;
}
}
private List<string[]> GetData(int fuelId, DateTime startDate, DateTime
endDate)
{
var data = _replenishmentRepository
.ReadFuelReplenishment()
.Where(x => x.ReplenishmentDate >= startDate && x.ReplenishmentDate <= endDate &&
x.FuelFuelReplenishments.Any(y => y.FuelId == fuelId))
.Select(x => new {
x.DriverId,
Date = x.ReplenishmentDate,
CountIn = x.FuelFuelReplenishments.
FirstOrDefault(y => y.FuelId == fuelId)?.Amount,
CountOut = (int?)null
})
.Union(
_transportationRepository
.ReadTransportation()
.Where(x => x.TransportationDate >= startDate &&
x.TransportationDate <= endDate && x.FuelId == fuelId)
.Select(x => new {
x.DriverId,
Date = x.TransportationDate,
CountIn = (int?)null,
CountOut = (int?)x.Amount
}))
.OrderBy(x => x.Date);
return
new List<string[]>() { item }
.Union(
data
.Select(x => new string[] {
x.DriverId.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();
}
}