Ну что, оно работает. Хз как, но работает
This commit is contained in:
parent
0d6d6dec90
commit
29371c7f97
@ -3,81 +3,91 @@ using ProjectTourAgency.Repositories;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectTourAgency.Reports;
|
||||
|
||||
internal class TableReport
|
||||
namespace ProjectTourAgency.Reports
|
||||
{
|
||||
private readonly IAddMoneyRepository _addMoneyRepository;
|
||||
|
||||
private readonly ITourRepository _tourRepository;
|
||||
|
||||
private readonly ILogger<TableReport> _logger;
|
||||
|
||||
internal static readonly string[] item = ["Клиент","Дата","Пополнения","Списания"];
|
||||
|
||||
public TableReport(IAddMoneyRepository addMoneyRepository,
|
||||
ITourRepository tourRepository, ILogger<TableReport> logger)
|
||||
internal class TableReport
|
||||
{
|
||||
_addMoneyRepository = addMoneyRepository ??
|
||||
throw new ArgumentNullException(nameof(addMoneyRepository));
|
||||
_tourRepository = tourRepository ??
|
||||
throw new ArgumentNullException(nameof(tourRepository));
|
||||
_logger = logger ??
|
||||
throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
private readonly IAddMoneyRepository _addMoneyRepository;
|
||||
private readonly ITourRepository _tourRepository;
|
||||
private readonly ILogger<TableReport> _logger;
|
||||
|
||||
public bool CreateTable(string filePath, int tourId, DateTime startDate, DateTime endDate)
|
||||
{
|
||||
try
|
||||
{
|
||||
new ExcelBuilder(filePath)
|
||||
.AddHeader("Сводка по движению корма", 0, 4)
|
||||
.AddParagraph("за период", 0)
|
||||
.AddTable([10, 10, 15, 15], GetData(tourId, startDate,
|
||||
endDate))
|
||||
.Build();
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
internal static readonly string[] item = { "Клиент", "Дата", "Пополнения", "Списания" }; // Исправлено на правильный синтаксис
|
||||
|
||||
public TableReport(IAddMoneyRepository addMoneyRepository,
|
||||
ITourRepository tourRepository, ILogger<TableReport> logger)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при формировании документа");
|
||||
return false;
|
||||
_addMoneyRepository = addMoneyRepository ??
|
||||
throw new ArgumentNullException(nameof(addMoneyRepository));
|
||||
_tourRepository = tourRepository ??
|
||||
throw new ArgumentNullException(nameof(tourRepository));
|
||||
_logger = logger ??
|
||||
throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
|
||||
public bool CreateTable(string filePath, int tourId, DateTime startDate, DateTime endDate)
|
||||
{
|
||||
try
|
||||
{
|
||||
new ExcelBuilder(filePath)
|
||||
.AddHeader("Сводка по движению корма", 0, 4)
|
||||
.AddParagraph("за период", 0)
|
||||
.AddTable(new[] { 10, 10, 15, 15 }, GetData(tourId, startDate, endDate)) // Исправлено на правильный синтаксис
|
||||
.Build();
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при формировании документа");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private List<string[]> GetData(int tourId, DateTime startDate, DateTime endDate)
|
||||
{
|
||||
// Получаем данные о турах
|
||||
var tourData = _tourRepository.ReadTours()
|
||||
.Where(x => x.DepartureDate >= startDate && x.DepartureDate <= endDate && x.ClientTours.Any(y => y.TourId == tourId))
|
||||
.SelectMany(x => x.ClientTours
|
||||
.Where(y => y.TourId == tourId)
|
||||
.Select(y => new {
|
||||
ClientId = (int?)y.ClientId,
|
||||
Date = x.DepartureDate,
|
||||
CountIn = (int?)null,
|
||||
CountOut = (int?)y.Cost
|
||||
}));
|
||||
|
||||
// Получаем уникальные ClientId из tourData
|
||||
var clientIds = tourData.Select(x => x.ClientId).Distinct().ToList();
|
||||
|
||||
// Получаем данные о пополнениях, фильтруя по ClientId
|
||||
var addMoneyData = _addMoneyRepository.ReadAddMoneys()
|
||||
.Where(x => x.Date >= startDate && x.Date <= endDate && clientIds.Contains(x.ClientId))
|
||||
.Select(x => new {
|
||||
ClientId = (int?)x.ClientId,
|
||||
Date = x.Date,
|
||||
CountIn = (int?)x.MoneyAmount,
|
||||
CountOut = (int?)null
|
||||
});
|
||||
|
||||
// Объединяем данные
|
||||
var data = tourData
|
||||
.Union(addMoneyData)
|
||||
.OrderBy(x => x.Date);
|
||||
|
||||
return new List<string[]>() { item }
|
||||
.Union(data.Select(x => new string[] {
|
||||
x.ClientId.ToString(),
|
||||
x.Date.ToString(),
|
||||
x.CountIn?.ToString() ?? string.Empty,
|
||||
x.CountOut?.ToString() ?? string.Empty }))
|
||||
.Union(new[] { new string[] {
|
||||
"Всего",
|
||||
"",
|
||||
data.Sum(x => x.CountIn ?? 0).ToString(),
|
||||
data.Sum(x => x.CountOut ?? 0).ToString()
|
||||
}})
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
private List<string[]> GetData(int tourId, DateTime startDate, DateTime
|
||||
endDate)
|
||||
{
|
||||
var data = _tourRepository.ReadTours().Select(x => new {
|
||||
ClientId = (int ?)x.ClientTours.FirstOrDefault(y => y.TourId > 0).ClientId,
|
||||
Date = x.DepartureDate,
|
||||
CountIn = (int?)null,
|
||||
CountOut = (int?)x.ClientTours.Sum(x => x.Cost)
|
||||
})
|
||||
.Union(
|
||||
_addMoneyRepository.ReadAddMoneys().Select(x => new {
|
||||
ClientId = (int?)x.ClientId,
|
||||
Date = x.Date,
|
||||
CountIn = (int?)x.MoneyAmount,
|
||||
CountOut= (int?)null })
|
||||
)
|
||||
.OrderBy(x => x.Date);
|
||||
return
|
||||
new List<string[]>() { item }
|
||||
.Union(
|
||||
data
|
||||
.Select(x => new string[] {
|
||||
x.ClientId.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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user