From 29371c7f976d9e8bbb783be8793dd9ef5a9aa780 Mon Sep 17 00:00:00 2001 From: Tonb73 Date: Fri, 6 Dec 2024 09:47:16 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D1=83=20=D1=87=D1=82=D0=BE,=20=D0=BE?= =?UTF-8?q?=D0=BD=D0=BE=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= =?UTF-8?q?.=20=D0=A5=D0=B7=20=D0=BA=D0=B0=D0=BA,=20=D0=BD=D0=BE=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Reports/TableReportcs.cs | 150 ++++++++++-------- 1 file changed, 80 insertions(+), 70 deletions(-) diff --git a/project/ProjectTourAgency/Reports/TableReportcs.cs b/project/ProjectTourAgency/Reports/TableReportcs.cs index 44589a0..3268df2 100644 --- a/project/ProjectTourAgency/Reports/TableReportcs.cs +++ b/project/ProjectTourAgency/Reports/TableReportcs.cs @@ -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 _logger; - - internal static readonly string[] item = ["Клиент","Дата","Пополнения","Списания"]; - - public TableReport(IAddMoneyRepository addMoneyRepository, - ITourRepository tourRepository, ILogger 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 _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 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 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() { 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 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() { 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(); - } - - - }