80 lines
3.2 KiB
C#
Raw Normal View History

2024-12-18 01:56:55 +04:00
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;
2024-12-24 20:20:08 +04:00
internal static readonly string[] item = ["Водитель", "Дата", "Кол-во пришло", "Кол-во ушло"];
2024-12-18 01:56:55 +04:00
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)
2024-12-24 20:20:08 +04:00
.AddParagraph($"за период c {startDate:dd.MM.yyyy} по {endDate: dd.MM.yyyy}", 0)
.AddTable([10, 10, 15, 15], GetData(fuelId, startDate, endDate))
.Build();
2024-12-18 01:56:55 +04:00
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при формировании документа");
return false;
}
}
2024-12-24 20:20:08 +04:00
private List<string[]> GetData(int fuelId, DateTime startDate, DateTime endDate)
2024-12-18 01:56:55 +04:00
{
var data = _replenishmentRepository
2024-12-24 20:20:08 +04:00
.ReadFuelReplenishment(dateForm: startDate, dateTo: endDate, fuelId: fuelId)
2024-12-18 01:56:55 +04:00
.Select(x => new {
2024-12-24 20:20:08 +04:00
x.DriverName,
2024-12-18 01:56:55 +04:00
Date = x.ReplenishmentDate,
CountIn = x.FuelFuelReplenishments.
FirstOrDefault(y => y.FuelId == fuelId)?.Amount,
CountOut = (int?)null
})
.Union(
_transportationRepository
2024-12-24 20:20:08 +04:00
.ReadTransportation(dateForm: startDate, dateTo: endDate, fuelId: fuelId)
2024-12-18 01:56:55 +04:00
.Where(x => x.TransportationDate >= startDate &&
x.TransportationDate <= endDate && x.FuelId == fuelId)
.Select(x => new {
2024-12-24 20:20:08 +04:00
x.DriverName,
2024-12-18 01:56:55 +04:00
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[] {
2024-12-24 20:20:08 +04:00
x.DriverName.ToString(), x.Date.ToString("dd.MM.yyyy"), x.CountIn?.ToString("N0") ??
string.Empty, x.CountOut?.ToString("N0") ?? string.Empty}))
2024-12-18 01:56:55 +04:00
.Union(
2024-12-24 20:20:08 +04:00
[["Всего", "", data.Sum(x => x.CountIn ?? 0).ToString("N0"),
data.Sum(x => x.CountOut ?? 0).ToString("N0")]])
2024-12-18 01:56:55 +04:00
.ToList();
}
}