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);