66 lines
2.8 KiB
C#
66 lines
2.8 KiB
C#
using Microsoft.Extensions.Logging;
|
||
using ProjectCompRepair.Entities;
|
||
using ProjectCompRepair.Repositories;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace ProjectCompRepair.Reports;
|
||
|
||
internal class TableReport
|
||
{
|
||
private readonly IOrderRepository _orderRepository;
|
||
private readonly IAccessoriesRepository _accessoriesRepository;
|
||
private readonly ILogger<TableReport> _logger;
|
||
|
||
internal static readonly string[] item = ["Дата", "вход", "выход"];
|
||
|
||
public TableReport(IOrderRepository orderRepository, IAccessoriesRepository accessoriesRepository, ILogger<TableReport> logger)
|
||
{
|
||
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
|
||
_accessoriesRepository = accessoriesRepository ?? throw new ArgumentNullException(nameof(accessoriesRepository));
|
||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||
}
|
||
|
||
public bool CreateTable(string filePath, int accessoriesId, DateTime startDate, DateTime endDate)
|
||
{
|
||
try
|
||
{
|
||
new ExcelBuilder(filePath)
|
||
.AddHeader("Поставка комплектующих", 0, 3)
|
||
.AddParagraph("за период", 0)
|
||
.AddTable([10, 15, 15], GetData(accessoriesId, startDate, endDate))
|
||
.Build();
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Ошибка при формировании документа");
|
||
return false;
|
||
}
|
||
}
|
||
|
||
private List<string[]> GetData(int accessoriesId, DateTime startDate, DateTime endDate)
|
||
{
|
||
var data = _orderRepository.ReadOrder()
|
||
.Where(x => x.Date >= startDate && x.Date <= endDate && x.AccessoiresOrders.Any(y => y.AccessoriesId == accessoriesId))
|
||
.Select(x => new {Date = x.Date, CountIn = (int?)null, CountOut = x.AccessoiresOrders.FirstOrDefault(y => y.AccessoriesId == accessoriesId)?.Count, })
|
||
.Union(
|
||
_accessoriesRepository
|
||
.ReadAccessories()
|
||
.Where(x => x.Date >= startDate && x.Date <= endDate && x.Id == accessoriesId)
|
||
.Select(x => new {Date = x.Date, 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("yyyy-MM-dd"), 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();
|
||
}
|
||
}
|