Сделал CLientTour вывод, все работает. На -13 минуте видео

This commit is contained in:
Tonb73 2024-12-06 13:48:37 +03:00
parent e697397462
commit c2b44d351c
4 changed files with 76 additions and 11 deletions

View File

@ -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; }

View File

@ -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<ClientTour> 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<ClientTour> clientTours)
{
@ -38,4 +62,12 @@ public class Tour
ClientTours = clientTours
};
}
public void SetClientTours(IEnumerable<ClientTour> clientTours)
{
if(clientTours != null && clientTours.Any())
{
ClientTours = clientTours;
}
}
}

View File

@ -76,22 +76,53 @@ WHERE Id = @id";
}
public IEnumerable<Tour> ReadTours()
public IEnumerable<Tour> 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<TempClientTour>(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<int, List<ClientTour>>();
var tours = connection.Query<Tour, ClientTour, Tour>(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)
{

View File

@ -9,7 +9,7 @@ namespace ProjectTourAgency.Repositories;
public interface ITourRepository
{
IEnumerable<Tour> ReadTours();
IEnumerable<Tour> ReadTours(DateTime? dateFrom = null, DateTime? dateTo = null, int? tourId = null, int? clientId = null);
void CreateTour(Tour tour);