ПИбд-22 Морозов Д.В. Лабораторная 3 #8
112
ProjectRepairCompany/ProjectRepairCompany/Reports/TableReport.cs
Normal file
112
ProjectRepairCompany/ProjectRepairCompany/Reports/TableReport.cs
Normal file
@ -0,0 +1,112 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ProjectRepairCompany.Entities;
|
||||
using ProjectRepairCompany.Repositories;
|
||||
using ProjectRepairCompany.Repositories.Implementations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;
|
||||
|
||||
namespace ProjectRepairCompany.Reports;
|
||||
|
||||
internal class TableReport
|
||||
{
|
||||
private readonly IStorageDetailRepository _storageDetailRepository;
|
||||
private readonly IOrderRepository _orderRepository;
|
||||
private readonly ILogger<TableReport> _logger;
|
||||
|
||||
internal static readonly string[] item = { "Деталь", "Дата", "Количество пришло", "Количество ушло" };
|
||||
|
||||
public TableReport(IStorageDetailRepository storageDetailRepository, IOrderRepository orderRepository, ILogger<TableReport> logger)
|
||||
{
|
||||
_storageDetailRepository = storageDetailRepository ?? throw new ArgumentNullException(nameof(storageDetailRepository));
|
||||
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
|
||||
public bool CreateTable(string filePath, int detailId, DateTime startDate, DateTime endDate)
|
||||
{
|
||||
try
|
||||
{
|
||||
new ExcelBuilder(filePath)
|
||||
.AddHeader("Сводка по движению деталей", 1, 4)
|
||||
.AddParagraph($"за период с {startDate:dd.MM.yyyy} по {endDate:dd.MM.yyyy}", 1)
|
||||
.AddTable(new[] { 20, 15, 15, 15 }, GetData(detailId, startDate, endDate))
|
||||
.Build();
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при формировании документа");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private List<string[]> GetData(int detailId, DateTime startDate, DateTime endDate)
|
||||
{
|
||||
// Получаем данные о пополнении склада
|
||||
var storageData = _storageDetailRepository
|
||||
.ReadStorageDetails()
|
||||
.Where(x => x.SupplyDate >= startDate && x.SupplyDate <= endDate && x.DetailId == detailId)
|
||||
.Select(x => new
|
||||
{
|
||||
x.DetailId,
|
||||
Date = x.SupplyDate, // Дата поступления
|
||||
CountIn = x.DetailCount, // Количество пришло
|
||||
CountOut = (int?)null // Нет исходящих
|
||||
})
|
||||
.OrderBy(x => x.Date); // Сортировка по дате поступления
|
||||
|
||||
// Получаем данные о заказах
|
||||
var orderData = _orderRepository
|
||||
.ReadOrders()
|
||||
.Where(x => x.DateIssue >= startDate && x.DateIssue <= endDate)
|
||||
.SelectMany(x => x.OrderDetails) // Извлекаем все детали из заказов
|
||||
.Where(orderDetail => orderDetail.DetailId == detailId)
|
||||
.Select(orderDetail => new
|
||||
{
|
||||
DetailId = orderDetail.DetailId,
|
||||
Date = orderDetail.Order.DateIssue, // Дата заказа
|
||||
CountIn = (int?)null, // Нет поступлений
|
||||
CountOut = orderDetail.DetailCount // Количество ушло
|
||||
})
|
||||
.OrderBy(x => x.Date); // Сортировка по дате заказа
|
||||
|
||||
// Объединяем данные из двух источников
|
||||
var combinedData = storageData
|
||||
.Union(orderData)
|
||||
.OrderBy(x => x.Date) // Сортируем все данные по дате
|
||||
.ToList();
|
||||
|
||||
// Формируем итоговую таблицу
|
||||
var result = new List<string[]> { item }
|
||||
.Union(combinedData.Select(x => new string[]
|
||||
{
|
||||
x.DetailId.ToString(), // Деталь
|
||||
x.Date.ToString("dd.MM.yyyy"), // Дата
|
||||
x.CountIn?.ToString() ?? string.Empty, // Количество пришло
|
||||
x.CountOut?.ToString() ?? string.Empty // Количество ушло
|
||||
}))
|
||||
.Union(new[]
|
||||
{
|
||||
|
||||
new string[]
|
||||
{
|
||||
"Всего",
|
||||
"",
|
||||
combinedData.Sum(x => x.CountIn ?? 0).ToString(), // Суммируем все поступления
|
||||
combinedData.Sum(x => x.CountOut ?? 0).ToString() // Суммируем все исходящие
|
||||
}
|
||||
})
|
||||
.ToList();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user
Много пустых строк