80 lines
3.2 KiB
C#
80 lines
3.2 KiB
C#
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($"за период c {startDate:dd.MM.yyyy} по {endDate: dd.MM.yyyy}", 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(dateForm: startDate, dateTo: endDate, fuelId: fuelId)
|
||
.Select(x => new {
|
||
x.DriverName,
|
||
Date = x.ReplenishmentDate,
|
||
CountIn = x.FuelFuelReplenishments.
|
||
FirstOrDefault(y => y.FuelId == fuelId)?.Amount,
|
||
CountOut = (int?)null
|
||
})
|
||
.Union(
|
||
_transportationRepository
|
||
.ReadTransportation(dateForm: startDate, dateTo: endDate, fuelId: fuelId)
|
||
.Where(x => x.TransportationDate >= startDate &&
|
||
x.TransportationDate <= endDate && x.FuelId == fuelId)
|
||
.Select(x => new {
|
||
x.DriverName,
|
||
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.DriverName.ToString(), x.Date.ToString("dd.MM.yyyy"), x.CountIn?.ToString("N0") ??
|
||
string.Empty, x.CountOut?.ToString("N0") ?? string.Empty}))
|
||
.Union(
|
||
[["Всего", "", data.Sum(x => x.CountIn ?? 0).ToString("N0"),
|
||
data.Sum(x => x.CountOut ?? 0).ToString("N0")]])
|
||
.ToList();
|
||
}
|
||
}
|