Ну что, оно работает. Хз как, но работает

This commit is contained in:
Tonb73 2024-12-06 09:47:16 +03:00
parent 0d6d6dec90
commit 29371c7f97

View File

@ -3,20 +3,16 @@ using ProjectTourAgency.Repositories;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectTourAgency.Reports;
namespace ProjectTourAgency.Reports
{
internal class TableReport internal class TableReport
{ {
private readonly IAddMoneyRepository _addMoneyRepository; private readonly IAddMoneyRepository _addMoneyRepository;
private readonly ITourRepository _tourRepository; private readonly ITourRepository _tourRepository;
private readonly ILogger<TableReport> _logger; private readonly ILogger<TableReport> _logger;
internal static readonly string[] item = ["Клиент","Дата","Пополнения","Списания"]; internal static readonly string[] item = { "Клиент", "Дата", "Пополнения", "Списания" }; // Исправлено на правильный синтаксис
public TableReport(IAddMoneyRepository addMoneyRepository, public TableReport(IAddMoneyRepository addMoneyRepository,
ITourRepository tourRepository, ILogger<TableReport> logger) ITourRepository tourRepository, ILogger<TableReport> logger)
@ -36,8 +32,7 @@ internal class TableReport
new ExcelBuilder(filePath) new ExcelBuilder(filePath)
.AddHeader("Сводка по движению корма", 0, 4) .AddHeader("Сводка по движению корма", 0, 4)
.AddParagraph("за период", 0) .AddParagraph("за период", 0)
.AddTable([10, 10, 15, 15], GetData(tourId, startDate, .AddTable(new[] { 10, 10, 15, 15 }, GetData(tourId, startDate, endDate)) // Исправлено на правильный синтаксис
endDate))
.Build(); .Build();
return true; return true;
} }
@ -48,36 +43,51 @@ internal class TableReport
} }
} }
private List<string[]> GetData(int tourId, DateTime startDate, DateTime private List<string[]> GetData(int tourId, DateTime startDate, DateTime endDate)
endDate)
{ {
var data = _tourRepository.ReadTours().Select(x => new { // Получаем данные о турах
ClientId = (int ?)x.ClientTours.FirstOrDefault(y => y.TourId > 0).ClientId, 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, Date = x.DepartureDate,
CountIn = (int?)null, CountIn = (int?)null,
CountOut = (int?)x.ClientTours.Sum(x => x.Cost) CountOut = (int?)y.Cost
}) }));
.Union(
_addMoneyRepository.ReadAddMoneys().Select(x => new { // Получаем уникальные 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, ClientId = (int?)x.ClientId,
Date = x.Date, Date = x.Date,
CountIn = (int?)x.MoneyAmount, CountIn = (int?)x.MoneyAmount,
CountOut= (int?)null }) CountOut = (int?)null
) });
// Объединяем данные
var data = tourData
.Union(addMoneyData)
.OrderBy(x => x.Date); .OrderBy(x => x.Date);
return
new List<string[]>() { item } return new List<string[]>() { item }
.Union( .Union(data.Select(x => new string[] {
data x.ClientId.ToString(),
.Select(x => new string[] { x.Date.ToString(),
x.ClientId.ToString(), x.Date.ToString(), x.CountIn?.ToString() ?? x.CountIn?.ToString() ?? string.Empty,
string.Empty, x.CountOut?.ToString() ?? string.Empty})) x.CountOut?.ToString() ?? string.Empty }))
.Union( .Union(new[] { new string[] {
[["Всего", "", data.Sum(x => x.CountIn ?? 0).ToString(), "Всего",
data.Sum(x => x.CountOut ?? 0).ToString()]]) "",
data.Sum(x => x.CountIn ?? 0).ToString(),
data.Sum(x => x.CountOut ?? 0).ToString()
}})
.ToList(); .ToList();
} }
}
} }