From 368960a0f4ab7d0d93f8a86e0e2e38d7afc3f305 Mon Sep 17 00:00:00 2001 From: Tonb73 Date: Sat, 7 Dec 2024 10:47:04 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=81=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=B5=D1=82,=20=D0=BA=D1=80=D0=BE=D0=BC=D0=B5=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=B4=D1=81=D1=87=D0=B5=D1=82=D0=B0=20=D1=81=D1=83?= =?UTF-8?q?=D0=BC=D0=BC=D1=8B=20=D0=B8=20=D0=BD=D1=83=D0=B6=D0=BD=D0=BE=20?= =?UTF-8?q?=D1=84=D0=BE=D1=80=D0=BC=D0=BE=D1=87=D0=BA=D1=83=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D1=8F=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Enities/{TempClientTour.cs => .cs} | 0 .../Implementations/AddMoneyRepository.cs | 26 +++++++++++-- .../Implementations/QueryBuilder.cs | 37 +++++++++++++++++++ .../Implementations/TourRepository.cs | 28 +++++++++++--- .../ProjectTourAgency/Reports/ChartReport.cs | 3 +- .../Reports/TableReportcs.cs | 24 ++++++------ .../Repositories/IAddMoneyRepository.cs | 2 +- 7 files changed, 95 insertions(+), 25 deletions(-) rename project/ProjectTourAgency/Enities/{TempClientTour.cs => .cs} (100%) create mode 100644 project/ProjectTourAgency/Implementations/QueryBuilder.cs diff --git a/project/ProjectTourAgency/Enities/TempClientTour.cs b/project/ProjectTourAgency/Enities/.cs similarity index 100% rename from project/ProjectTourAgency/Enities/TempClientTour.cs rename to project/ProjectTourAgency/Enities/.cs diff --git a/project/ProjectTourAgency/Implementations/AddMoneyRepository.cs b/project/ProjectTourAgency/Implementations/AddMoneyRepository.cs index 240ce36..bc02dda 100644 --- a/project/ProjectTourAgency/Implementations/AddMoneyRepository.cs +++ b/project/ProjectTourAgency/Implementations/AddMoneyRepository.cs @@ -103,20 +103,38 @@ WHERE Id = @Id"; } } - public IEnumerable ReadAddMoneys() + public IEnumerable ReadAddMoneys(DateTime? dateFrom = null, DateTime? dateTo = null, int? addMoneyId = null, int? clientId = null) { _logger.LogInformation("Получение всех объектов"); try { + var builder = new QueryBuilder(); + if (dateFrom.HasValue) + { + builder.AddCondition("am.Date >= @dateFrom"); + } + if (dateTo.HasValue) + { + builder.AddCondition("am.Date <= @dateTo"); + } + if (addMoneyId.HasValue) + { + builder.AddCondition("am.Id =@addMoneyId"); + } + if (clientId.HasValue) + { + builder.AddCondition("am.ClientId =@clientId"); + } using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = @"SELECT + var querySelect = $@"SELECT am.*, c.FullName AS ClientName FROM AddMoneys am LEFT JOIN - Clients c ON c.Id = am.ClientId"; - var AddMoneys = connection.Query(querySelect); + Clients c ON c.Id = am.ClientId +{builder.Build()}"; + var AddMoneys = connection.Query(querySelect, new {dateFrom, dateTo, addMoneyId, clientId}); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(AddMoneys)); return AddMoneys; diff --git a/project/ProjectTourAgency/Implementations/QueryBuilder.cs b/project/ProjectTourAgency/Implementations/QueryBuilder.cs new file mode 100644 index 0000000..2f984fc --- /dev/null +++ b/project/ProjectTourAgency/Implementations/QueryBuilder.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectTourAgency.Implementations; + +internal class QueryBuilder +{ + private readonly StringBuilder _builder; + + public QueryBuilder() + { + _builder = new(); + } + + public QueryBuilder AddCondition(string condition) + { + if(_builder.Length > 0) + { + _builder.Append(" AND "); + } + _builder.Append(condition); + return this; + } + + public string Build() + { + if (_builder.Length == 0) + { + return string.Empty; + } + return $"WHERE {_builder}"; + } +} + diff --git a/project/ProjectTourAgency/Implementations/TourRepository.cs b/project/ProjectTourAgency/Implementations/TourRepository.cs index 80477ce..92e4687 100644 --- a/project/ProjectTourAgency/Implementations/TourRepository.cs +++ b/project/ProjectTourAgency/Implementations/TourRepository.cs @@ -81,8 +81,24 @@ WHERE Id = @id"; _logger.LogInformation("Получение всех объектов"); try { + var builder = new QueryBuilder(); + if (dateFrom.HasValue) + { + builder.AddCondition("t.DepartureDate >= @dateFrom"); + } + if (dateTo.HasValue) + { + builder.AddCondition("t.DepartureDate <= @dateTo"); + } + if(tourId.HasValue) + { + builder.AddCondition("t.Id = @tourId"); + } + + + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = @" + var querySelect = $@" SELECT t.*, CONCAT(r.Departure,'-', r.Destination) AS RouteName, @@ -95,12 +111,12 @@ FROM LEFT JOIN Routes r ON r.Id = t.RouteId LEFT JOIN - Employees e ON e.Id = t.EmployeeId -- соединяем с таблицей Employees + Employees e ON e.Id = t.EmployeeId LEFT JOIN - ClientTour ct ON ct.TourId = t.Id -- соединяем с таблицей ClientTour + ClientTour ct ON ct.TourId = t.Id LEFT JOIN - Clients c ON c.Id = ct.ClientId; -"; + Clients c ON c.Id = ct.ClientId +{builder.Build()}"; var clientTourDict = new Dictionary>(); @@ -114,7 +130,7 @@ LEFT JOIN } ct.Add(tours); return clientTour; - }, splitOn: "ClientId"); + }, splitOn: "ClientId",param: new {dateFrom, dateTo, tourId, clientId}); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(tours)); return clientTourDict.Select(x => diff --git a/project/ProjectTourAgency/Reports/ChartReport.cs b/project/ProjectTourAgency/Reports/ChartReport.cs index ac2b5d2..f4c881e 100644 --- a/project/ProjectTourAgency/Reports/ChartReport.cs +++ b/project/ProjectTourAgency/Reports/ChartReport.cs @@ -43,8 +43,7 @@ internal class ChartReport private List<(string Caption, double Value)> GetData(DateTime dateTime) { return _addMoneyRepository - .ReadAddMoneys() - .Where(x => x.Date.Date == dateTime.Date) + .ReadAddMoneys(dateFrom: dateTime.Date, dateTo: dateTime.Date.AddDays(1)) .GroupBy(x => x.ClientId, (key, group) => new { Id = key, Count = group.Count() }) .Select(x => (x.Id.ToString(), (double)x.Count)) .ToList(); diff --git a/project/ProjectTourAgency/Reports/TableReportcs.cs b/project/ProjectTourAgency/Reports/TableReportcs.cs index c388620..7bb95da 100644 --- a/project/ProjectTourAgency/Reports/TableReportcs.cs +++ b/project/ProjectTourAgency/Reports/TableReportcs.cs @@ -31,7 +31,7 @@ namespace ProjectTourAgency.Reports { new ExcelBuilder(filePath) .AddHeader("Сводка по движению корма", 0, 4) - .AddParagraph("за период", 0) + .AddParagraph($"за период с {startDate:dd.MM.yyyy} по {endDate:dd.MM.yyyy}", 0) .AddTable(new[] { 10, 10, 15, 15 }, GetData(tourId, startDate, endDate)) .Build(); return true; @@ -46,25 +46,25 @@ namespace ProjectTourAgency.Reports 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)) + var tourData = _tourRepository.ReadTours(startDate, endDate, tourId) .SelectMany(x => x.ClientTours - .Where(y => y.TourId == tourId) .Select(y => new { - ClientId = (int?)y.ClientId, + ClientId = y.ClientId, + ClientName = y.CLientName, Date = x.DepartureDate, CountIn = (int?)null, CountOut = (int?)y.Cost })); - var clientIds = tourData.Select(x => x.ClientId).Distinct().ToList(); + var clientId = tourData.Select(x =>x.ClientId).Distinct().ToList(); - var addMoneyData = _addMoneyRepository.ReadAddMoneys() - .Where(x => x.Date >= startDate && x.Date <= endDate && clientIds.Contains(x.ClientId)) + var addMoneyData = _addMoneyRepository.ReadAddMoneys(startDate, endDate) + .Where(x => clientId.Contains(x.ClientId)) .Select(x => new { - ClientId = (int?)x.ClientId, + ClientId = x.ClientId, + ClientName = x.ClientName, Date = x.Date, CountIn = (int?)x.MoneyAmount, CountOut = (int?)null @@ -77,15 +77,15 @@ namespace ProjectTourAgency.Reports return new List() { item } .Union(data.Select(x => new string[] { - x.ClientId.ToString(), + x.ClientName.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() + data.Sum(x => x.CountIn ?? 0).ToString("NO"), + data.Sum(x => x.CountOut ?? 0).ToString("NO") }}) .ToList(); } diff --git a/project/ProjectTourAgency/Repositories/IAddMoneyRepository.cs b/project/ProjectTourAgency/Repositories/IAddMoneyRepository.cs index 9f8e7b6..8d64a5b 100644 --- a/project/ProjectTourAgency/Repositories/IAddMoneyRepository.cs +++ b/project/ProjectTourAgency/Repositories/IAddMoneyRepository.cs @@ -9,7 +9,7 @@ namespace ProjectTourAgency.Repositories; public interface IAddMoneyRepository { - IEnumerable ReadAddMoneys(); + IEnumerable ReadAddMoneys(DateTime? dateFrom = null, DateTime? dateTo = null, int? addMoneyId = null, int? clientId = null); AddMoney ReadAddMoneyById(int id);