From c2b44d351ce8bfe64c4973087869307ee7726f3c Mon Sep 17 00:00:00 2001 From: Tonb73 Date: Fri, 6 Dec 2024 13:48:37 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20CLientTo?= =?UTF-8?q?ur=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4,=20=D0=B2=D1=81=D0=B5=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82.=20=20=D0=9D?= =?UTF-8?q?=D0=B0=20-13=20=D0=BC=D0=B8=D0=BD=D1=83=D1=82=D0=B5=20=D0=B2?= =?UTF-8?q?=D0=B8=D0=B4=D0=B5=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProjectTourAgency/Enities/ClientTour.cs | 2 + project/ProjectTourAgency/Enities/Tour.cs | 32 ++++++++++++ .../Implementations/TourRepository.cs | 51 +++++++++++++++---- .../Repositories/ITourRepositiry.cs | 2 +- 4 files changed, 76 insertions(+), 11 deletions(-) diff --git a/project/ProjectTourAgency/Enities/ClientTour.cs b/project/ProjectTourAgency/Enities/ClientTour.cs index a05bd98..ea01ee4 100644 --- a/project/ProjectTourAgency/Enities/ClientTour.cs +++ b/project/ProjectTourAgency/Enities/ClientTour.cs @@ -11,6 +11,8 @@ public class ClientTour { public int Id { get; private set; } public int ClientId { get; private set; } + + public string CLientName { get; private set; } = String.Empty; public int TourId { get; private set; } public int Cost { get; private set; } diff --git a/project/ProjectTourAgency/Enities/Tour.cs b/project/ProjectTourAgency/Enities/Tour.cs index fa545b2..1ef9bcc 100644 --- a/project/ProjectTourAgency/Enities/Tour.cs +++ b/project/ProjectTourAgency/Enities/Tour.cs @@ -1,6 +1,7 @@ using ProjectTourAgency.Enities.Enums; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -9,11 +10,34 @@ namespace ProjectTourAgency.Enities; public class Tour { + + [DisplayName("Id Тура")] public int Id { get; private set; } + + [Browsable(false)] public int EmployeeId { get; private set; } + + [Browsable(false)] public int RouteId { get; private set; } + + [DisplayName("Сотрудник")] + + public string EmployeeName { get; private set; } = string.Empty; + + [DisplayName("Маршрут")] + + public string RouteName { get; private set; } = string.Empty; + + [DisplayName("Дата начала тура")] public DateTime DepartureDate { get; private set; } + + [Browsable(false)] public IEnumerable ClientTours { get; private set; } = []; + + [DisplayName("Клиенты")] + public string Clients => ClientTours != null ? + string.Join(",", ClientTours.Select(x => $"{x.CLientName} {x.Cost}")) : string.Empty; + public static Tour CreateEntity(int id, int employeeId, int routeId, DateTime date,IEnumerable clientTours) { @@ -38,4 +62,12 @@ public class Tour ClientTours = clientTours }; } + + public void SetClientTours(IEnumerable clientTours) + { + if(clientTours != null && clientTours.Any()) + { + ClientTours = clientTours; + } + } } diff --git a/project/ProjectTourAgency/Implementations/TourRepository.cs b/project/ProjectTourAgency/Implementations/TourRepository.cs index f78bf25..bb23682 100644 --- a/project/ProjectTourAgency/Implementations/TourRepository.cs +++ b/project/ProjectTourAgency/Implementations/TourRepository.cs @@ -76,22 +76,53 @@ WHERE Id = @id"; } - public IEnumerable ReadTours() + public IEnumerable ReadTours(DateTime? dateFrom = null, DateTime? dateTo = null, int? tourId = null, int? clientId = null) { _logger.LogInformation("Получение всех объектов"); try { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); var querySelect = @" -SELECT t.*, ct.TourId, ct.Cost, ct.ClientId FROM Tour t -INNER JOIN ClientTour ct ON ct.TourId = t.Id"; - var tour = connection.Query(querySelect); - _logger.LogDebug("Получение объектов {json}", JsonConvert.SerializeObject(tour)); - return tour.GroupBy( - x => x.Id, y => y, - (key, value) => Tour.CreateEntity(value.First(), - value.Select(z => ClientTour.CreateEntity(0,z.ClientId, z.Id,z.Cost)) - )).ToList(); +SELECT + t.*, + CONCAT(r.Departure,'-', r.Destination) AS RouteName, + e.FullName AS EmployeeName, + ct.ClientId, + ct.Cost, + c.FullName AS ClientName +FROM + Tour t +LEFT JOIN + Routes r ON r.Id = t.RouteId +LEFT JOIN + Employees e ON e.Id = t.EmployeeId -- соединяем с таблицей Employees +LEFT JOIN + ClientTour ct ON ct.TourId = t.Id -- соединяем с таблицей ClientTour +LEFT JOIN + Clients c ON c.Id = ct.ClientId; +"; + + var clientTourDict = new Dictionary>(); + + var tours = connection.Query(querySelect, + (clientTour, tours) => + { + if (!clientTourDict.TryGetValue(clientTour.Id, out var ct)) + { + ct = []; + clientTourDict.Add(clientTour.Id, ct); + } + ct.Add(tours); + return clientTour; + }, splitOn: "ClientId", param: new { dateFrom, dateTo, tourId, clientId }); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(tours)); + + return clientTourDict.Select(x => + { + var t = tours.First(y => y.Id == x.Key); + t.SetClientTours(x.Value); + return t; + }).ToArray(); } catch (Exception ex) { diff --git a/project/ProjectTourAgency/Repositories/ITourRepositiry.cs b/project/ProjectTourAgency/Repositories/ITourRepositiry.cs index 60d519e..5207e79 100644 --- a/project/ProjectTourAgency/Repositories/ITourRepositiry.cs +++ b/project/ProjectTourAgency/Repositories/ITourRepositiry.cs @@ -9,7 +9,7 @@ namespace ProjectTourAgency.Repositories; public interface ITourRepository { - IEnumerable ReadTours(); + IEnumerable ReadTours(DateTime? dateFrom = null, DateTime? dateTo = null, int? tourId = null, int? clientId = null); void CreateTour(Tour tour);