From e69739746250a4304d23207acbccc348f5fddc52 Mon Sep 17 00:00:00 2001 From: Tonb73 Date: Fri, 6 Dec 2024 12:17:31 +0300 Subject: [PATCH 1/7] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=B4=D0=BF=D0=B8=D1=81=D0=B8=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=B2=D1=81=D0=B5=20=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BE=D1=87=D0=BD=D0=B8=D0=BA=D0=BE=D0=B2,=20=D1=81=D0=B4?= =?UTF-8?q?=D0=B5=D0=BB=D0=B0=D0=BB=D0=B8=20=D0=B2=D1=81=D0=B5=20=D1=81=20?= =?UTF-8?q?=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=86=D0=B8=D0=B5=D0=B9=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D1=81?= =?UTF-8?q?=D1=87=D0=B5=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- project/ProjectTourAgency/Enities/AddMoney.cs | 14 +++++++++++++- project/ProjectTourAgency/Enities/Client.cs | 12 ++++++++++++ project/ProjectTourAgency/Enities/Employee.cs | 7 +++++++ project/ProjectTourAgency/Enities/Route.cs | 9 +++++++++ project/ProjectTourAgency/Forms/FormClientTour.cs | 4 ++-- .../Implementations/AddMoneyRepository.cs | 8 +++++++- 6 files changed, 50 insertions(+), 4 deletions(-) diff --git a/project/ProjectTourAgency/Enities/AddMoney.cs b/project/ProjectTourAgency/Enities/AddMoney.cs index 3865dfe..fd04059 100644 --- a/project/ProjectTourAgency/Enities/AddMoney.cs +++ b/project/ProjectTourAgency/Enities/AddMoney.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -8,10 +9,21 @@ namespace ProjectTourAgency.Enities { public class AddMoney { + + [Browsable(false)] public int Id { get; private set; } + + [DisplayName("ID Клиента")] public int ClientId { get; private set; } + + [DisplayName("Дата")] public DateTime Date { get; private set; } - public int MoneyAmount{ get; private set; } + + [DisplayName("Размер Пополнение")] + public int MoneyAmount { get; private set; } + + [DisplayName("Клиент")] + public string ClientName { get; private set; } = string.Empty; public static AddMoney CreateEntity(int id,int cId, DateTime date, int money) diff --git a/project/ProjectTourAgency/Enities/Client.cs b/project/ProjectTourAgency/Enities/Client.cs index ab529c4..87c21dc 100644 --- a/project/ProjectTourAgency/Enities/Client.cs +++ b/project/ProjectTourAgency/Enities/Client.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,22 @@ namespace ProjectTourAgency.Enities; public class Client { + public int Id { get; private set; } + + [DisplayName("Полное имя")] public string FullName { get; private set; } = string.Empty; + + [DisplayName("Дата рождения")] public DateTime BirthDate { get; private set; } + + [DisplayName("Номер телефона")] public string PhoneNumber { get; private set; } = string.Empty; + + [DisplayName("Статус клиента")] public ClientStatus ClientStatus { get; private set; } + + [DisplayName("Баланс")] public int Money { get; private set; } public static Client CreateEntity(int id, string fullName, diff --git a/project/ProjectTourAgency/Enities/Employee.cs b/project/ProjectTourAgency/Enities/Employee.cs index 1ee2ff2..7f8a6e8 100644 --- a/project/ProjectTourAgency/Enities/Employee.cs +++ b/project/ProjectTourAgency/Enities/Employee.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; @@ -10,10 +11,16 @@ namespace ProjectTourAgency.Enities; public class Employee { public int Id { get; private set; } + + [DisplayName("Полное имя")] public string FullName { get; private set; } = string.Empty; + public string EmployeeName => $"{EmployeeJob} {FullName}"; + + [DisplayName("Должность")] public EmpoyeeJob EmployeeJob { get; private set; } + public static Employee CreateEntity(int id, string fullName, EmpoyeeJob job) { diff --git a/project/ProjectTourAgency/Enities/Route.cs b/project/ProjectTourAgency/Enities/Route.cs index 09e80b9..fb8aab6 100644 --- a/project/ProjectTourAgency/Enities/Route.cs +++ b/project/ProjectTourAgency/Enities/Route.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -9,9 +10,17 @@ namespace ProjectTourAgency.Enities; public class Route { public int Id { get; private set; } + + [DisplayName("Место назначения")] public string Destination { get; private set; } = string.Empty; + + [DisplayName("Место отбытия")] public string Departure { get; private set; } = string.Empty; + + [DisplayName("Продолжительность")] public int Duration { get; private set; } + + public string DepartureDestination => $"{Departure} - {Destination}"; public static Route CreateEntity(int id, string destination, string departure, int duration) { diff --git a/project/ProjectTourAgency/Forms/FormClientTour.cs b/project/ProjectTourAgency/Forms/FormClientTour.cs index f1cfe34..472b343 100644 --- a/project/ProjectTourAgency/Forms/FormClientTour.cs +++ b/project/ProjectTourAgency/Forms/FormClientTour.cs @@ -25,11 +25,11 @@ namespace ProjectTourAgency.Forms _tourRepository = tourRepository ?? throw new ArgumentNullException(nameof(tourRepository)); comboBoxEmployeeId.DataSource = employeeRepository.ReadEmployees(); - comboBoxEmployeeId.DisplayMember = "FullName"; + comboBoxEmployeeId.DisplayMember = "EmployeeName"; comboBoxEmployeeId.ValueMember = "Id"; comboBoxRouteId.DataSource = routeRepository.ReadRoutes(); - comboBoxRouteId.DisplayMember = "Destination"; + comboBoxRouteId.DisplayMember = "DepartureDestination"; comboBoxRouteId.ValueMember = "Id"; ColumnClient.DataSource = clientRepository.ReadClients(); diff --git a/project/ProjectTourAgency/Implementations/AddMoneyRepository.cs b/project/ProjectTourAgency/Implementations/AddMoneyRepository.cs index 115c497..240ce36 100644 --- a/project/ProjectTourAgency/Implementations/AddMoneyRepository.cs +++ b/project/ProjectTourAgency/Implementations/AddMoneyRepository.cs @@ -109,7 +109,13 @@ WHERE Id = @Id"; try { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = @"SELECT * FROM AddMoneys"; + 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); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(AddMoneys)); return AddMoneys; -- 2.25.1 From c2b44d351ce8bfe64c4973087869307ee7726f3c Mon Sep 17 00:00:00 2001 From: Tonb73 Date: Fri, 6 Dec 2024 13:48:37 +0300 Subject: [PATCH 2/7] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20CLie?= =?UTF-8?q?ntTour=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4,=20=D0=B2=D1=81=D0=B5?= =?UTF-8?q?=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82.=20=20?= =?UTF-8?q?=D0=9D=D0=B0=20-13=20=D0=BC=D0=B8=D0=BD=D1=83=D1=82=D0=B5=20?= =?UTF-8?q?=D0=B2=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); -- 2.25.1 From 9226bb13b9c5567dcd449618aeb78fe1ebfc8aab Mon Sep 17 00:00:00 2001 From: Tonb73 Date: Fri, 6 Dec 2024 14:14:51 +0300 Subject: [PATCH 3/7] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=201=D1=83=D1=8E=20=D1=87=D0=B0=D1=81=D1=82=D1=8C=20=D0=BB?= =?UTF-8?q?=D0=B0=D0=B1=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- project/ProjectTourAgency/Forms/FormAddMoneys.cs | 8 +++++++- project/ProjectTourAgency/Forms/FormClients.cs | 10 ++++++++-- project/ProjectTourAgency/Forms/FormEmployees.cs | 7 ++++++- project/ProjectTourAgency/Forms/FormRoutes.cs | 8 +++++++- .../Implementations/TourRepository.cs | 2 +- 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/project/ProjectTourAgency/Forms/FormAddMoneys.cs b/project/ProjectTourAgency/Forms/FormAddMoneys.cs index 8b9ae68..a7233a6 100644 --- a/project/ProjectTourAgency/Forms/FormAddMoneys.cs +++ b/project/ProjectTourAgency/Forms/FormAddMoneys.cs @@ -51,7 +51,13 @@ namespace ProjectTourAgency.Forms } - private void LoadList() => dataGridViewData.DataSource = _addMoneyRepository.ReadAddMoneys(); + private void LoadList() + { + dataGridViewData.DataSource = _addMoneyRepository.ReadAddMoneys(); + dataGridViewData.Columns["Date"].DefaultCellStyle.Format = "dd.MM.yy"; + + } + private bool TryGetIdentifierFromSelectedRow(out int id) { diff --git a/project/ProjectTourAgency/Forms/FormClients.cs b/project/ProjectTourAgency/Forms/FormClients.cs index b557f14..4eb7ed2 100644 --- a/project/ProjectTourAgency/Forms/FormClients.cs +++ b/project/ProjectTourAgency/Forms/FormClients.cs @@ -1,4 +1,5 @@ -using ProjectTourAgency.Repositories; +using ProjectTourAgency.Implementations; +using ProjectTourAgency.Repositories; using System; using System.Collections.Generic; using System.ComponentModel; @@ -93,8 +94,13 @@ namespace ProjectTourAgency.Forms } } - private void LoadList() => dataGridViewData.DataSource = _clientRepository.ReadClients(); + private void LoadList() + { + dataGridViewData.DataSource = _clientRepository.ReadClients(); + dataGridViewData.Columns["Id"].Visible = false; + + } private bool TryGetIdentifierFromSelectedRow(out int id) { id = 0; diff --git a/project/ProjectTourAgency/Forms/FormEmployees.cs b/project/ProjectTourAgency/Forms/FormEmployees.cs index 2e8bdae..c4cc86d 100644 --- a/project/ProjectTourAgency/Forms/FormEmployees.cs +++ b/project/ProjectTourAgency/Forms/FormEmployees.cs @@ -95,8 +95,13 @@ namespace ProjectTourAgency.Forms } } - private void LoadList() => dataGridViewData.DataSource = _employeeRepository.ReadEmployees(); + private void LoadList() + { + dataGridViewData.DataSource = _employeeRepository.ReadEmployees(); + dataGridViewData.Columns["Id"].Visible = false; + dataGridViewData.Columns["EmployeeName"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { id = 0; diff --git a/project/ProjectTourAgency/Forms/FormRoutes.cs b/project/ProjectTourAgency/Forms/FormRoutes.cs index e9ce87e..b0bbfac 100644 --- a/project/ProjectTourAgency/Forms/FormRoutes.cs +++ b/project/ProjectTourAgency/Forms/FormRoutes.cs @@ -1,4 +1,5 @@ using ProjectRouteAgency.Repositories; +using ProjectTourAgency.Implementations; using ProjectTourAgency.Repositories; using System; using System.Collections.Generic; @@ -94,8 +95,13 @@ namespace ProjectTourAgency.Forms } } - private void LoadList() => dataGridViewData.DataSource = _routeRepository.ReadRoutes(); + private void LoadList() + { + dataGridViewData.DataSource = _routeRepository.ReadRoutes(); + dataGridViewData.Columns["Id"].Visible = false; + dataGridViewData.Columns["DepartureDestination"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { id = 0; diff --git a/project/ProjectTourAgency/Implementations/TourRepository.cs b/project/ProjectTourAgency/Implementations/TourRepository.cs index bb23682..80477ce 100644 --- a/project/ProjectTourAgency/Implementations/TourRepository.cs +++ b/project/ProjectTourAgency/Implementations/TourRepository.cs @@ -114,7 +114,7 @@ LEFT JOIN } ct.Add(tours); return clientTour; - }, splitOn: "ClientId", param: new { dateFrom, dateTo, tourId, clientId }); + }, splitOn: "ClientId"); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(tours)); return clientTourDict.Select(x => -- 2.25.1 From 368960a0f4ab7d0d93f8a86e0e2e38d7afc3f305 Mon Sep 17 00:00:00 2001 From: Tonb73 Date: Sat, 7 Dec 2024 10:47:04 +0300 Subject: [PATCH 4/7] =?UTF-8?q?=D0=92=D1=81=D0=B5=20=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B0=D0=B5=D1=82,=20=D0=BA=D1=80=D0=BE=D0=BC?= =?UTF-8?q?=D0=B5=20=D0=BF=D0=BE=D0=B4=D1=81=D1=87=D0=B5=D1=82=D0=B0=20?= =?UTF-8?q?=D1=81=D1=83=D0=BC=D0=BC=D1=8B=20=D0=B8=20=D0=BD=D1=83=D0=B6?= =?UTF-8?q?=D0=BD=D0=BE=20=D1=84=D0=BE=D1=80=D0=BC=D0=BE=D1=87=D0=BA=D1=83?= =?UTF-8?q?=20=D0=BF=D0=BE=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); -- 2.25.1 From 2e0ba1ba5e2ed7e043e960d24a463f401de42711 Mon Sep 17 00:00:00 2001 From: Tonb73 Date: Sat, 7 Dec 2024 10:51:16 +0300 Subject: [PATCH 5/7] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=20=D1=84=D0=BE=D1=80=D0=BC=D1=83=D1=87=D0=BA=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Forms/FormExcelReport.Designer.cs | 76 +++++++++++++++---- 1 file changed, 62 insertions(+), 14 deletions(-) diff --git a/project/ProjectTourAgency/Forms/FormExcelReport.Designer.cs b/project/ProjectTourAgency/Forms/FormExcelReport.Designer.cs index 2a0c9ed..fbe0ddb 100644 --- a/project/ProjectTourAgency/Forms/FormExcelReport.Designer.cs +++ b/project/ProjectTourAgency/Forms/FormExcelReport.Designer.cs @@ -34,62 +34,106 @@ buttonMakeReport = new Button(); comboBoxTour = new ComboBox(); textBoxFilePath = new TextBox(); + labelFilePAth = new Label(); + label1 = new Label(); + label2 = new Label(); + label3 = new Label(); SuspendLayout(); // // dateTimePickerDateEnd // - dateTimePickerDateEnd.Location = new Point(58, 155); + dateTimePickerDateEnd.Location = new Point(148, 221); dateTimePickerDateEnd.Name = "dateTimePickerDateEnd"; - dateTimePickerDateEnd.Size = new Size(201, 31); + dateTimePickerDateEnd.Size = new Size(280, 31); dateTimePickerDateEnd.TabIndex = 0; // // dateTimePickerDateBegin // - dateTimePickerDateBegin.Location = new Point(58, 105); + dateTimePickerDateBegin.Location = new Point(148, 164); dateTimePickerDateBegin.Name = "dateTimePickerDateBegin"; - dateTimePickerDateBegin.Size = new Size(201, 31); + dateTimePickerDateBegin.Size = new Size(280, 31); dateTimePickerDateBegin.TabIndex = 1; // // buttonSelectFilePath // - buttonSelectFilePath.Location = new Point(148, 47); + buttonSelectFilePath.Location = new Point(396, 59); buttonSelectFilePath.Name = "buttonSelectFilePath"; - buttonSelectFilePath.Size = new Size(112, 34); + buttonSelectFilePath.Size = new Size(32, 34); buttonSelectFilePath.TabIndex = 2; - buttonSelectFilePath.Text = "button1"; + buttonSelectFilePath.Text = ".."; buttonSelectFilePath.UseVisualStyleBackColor = true; buttonSelectFilePath.Click += buttonSelectFilePath_Click; // // buttonMakeReport // - buttonMakeReport.Location = new Point(144, 234); + buttonMakeReport.Location = new Point(148, 278); buttonMakeReport.Name = "buttonMakeReport"; - buttonMakeReport.Size = new Size(112, 34); + buttonMakeReport.Size = new Size(182, 34); buttonMakeReport.TabIndex = 3; - buttonMakeReport.Text = "button2"; + buttonMakeReport.Text = "Сформировать"; buttonMakeReport.UseVisualStyleBackColor = true; buttonMakeReport.Click += buttonMakeReport_Click; // // comboBoxTour // comboBoxTour.FormattingEnabled = true; - comboBoxTour.Location = new Point(298, 157); + comboBoxTour.Location = new Point(148, 110); comboBoxTour.Name = "comboBoxTour"; comboBoxTour.Size = new Size(182, 33); comboBoxTour.TabIndex = 4; // // textBoxFilePath // - textBoxFilePath.Location = new Point(279, 47); + textBoxFilePath.Location = new Point(148, 59); textBoxFilePath.Name = "textBoxFilePath"; - textBoxFilePath.Size = new Size(150, 31); + textBoxFilePath.Size = new Size(242, 31); textBoxFilePath.TabIndex = 5; // + // labelFilePAth + // + labelFilePAth.AutoSize = true; + labelFilePAth.Location = new Point(12, 47); + labelFilePAth.Name = "labelFilePAth"; + labelFilePAth.Size = new Size(118, 25); + labelFilePAth.TabIndex = 6; + labelFilePAth.Text = "Путь к файлу"; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(12, 118); + label1.Name = "label1"; + label1.Size = new Size(73, 25); + label1.TabIndex = 7; + label1.Text = "ID Тура"; + // + // label2 + // + label2.AutoSize = true; + label2.Location = new Point(12, 169); + label2.Name = "label2"; + label2.Size = new Size(110, 25); + label2.TabIndex = 8; + label2.Text = "Дата начала"; + // + // label3 + // + label3.AutoSize = true; + label3.Location = new Point(12, 221); + label3.Name = "label3"; + label3.Size = new Size(105, 25); + label3.TabIndex = 9; + label3.Text = "Дата Конца"; + // // FormExcelReport // AutoScaleDimensions = new SizeF(10F, 25F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); + ClientSize = new Size(507, 342); + Controls.Add(label3); + Controls.Add(label2); + Controls.Add(label1); + Controls.Add(labelFilePAth); Controls.Add(textBoxFilePath); Controls.Add(comboBoxTour); Controls.Add(buttonMakeReport); @@ -110,5 +154,9 @@ private Button buttonMakeReport; private ComboBox comboBoxTour; private TextBox textBoxFilePath; + private Label labelFilePAth; + private Label label1; + private Label label2; + private Label label3; } } \ No newline at end of file -- 2.25.1 From a1a7cad61f7d30f902b0752ccb2b08e66f2ad5d8 Mon Sep 17 00:00:00 2001 From: Tonb73 Date: Sat, 7 Dec 2024 11:11:44 +0300 Subject: [PATCH 6/7] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- project/ProjectTourAgency/Reports/TableReportcs.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/project/ProjectTourAgency/Reports/TableReportcs.cs b/project/ProjectTourAgency/Reports/TableReportcs.cs index 7bb95da..97d1788 100644 --- a/project/ProjectTourAgency/Reports/TableReportcs.cs +++ b/project/ProjectTourAgency/Reports/TableReportcs.cs @@ -79,13 +79,13 @@ namespace ProjectTourAgency.Reports .Union(data.Select(x => new string[] { x.ClientName.ToString(), x.Date.ToString(), - x.CountIn?.ToString() ?? string.Empty, - x.CountOut?.ToString() ?? string.Empty })) + x.CountIn?.ToString() ?? "NO", + x.CountOut?.ToString() ?? "NO"})) .Union(new[] { new string[] { "Всего", "", - data.Sum(x => x.CountIn ?? 0).ToString("NO"), - data.Sum(x => x.CountOut ?? 0).ToString("NO") + data.Sum(x => x.CountIn ?? 0) == 0 ? "NO" : data.Sum(x => x.CountIn ?? 0).ToString(), + data.Sum(x => x.CountOut ?? 0) == 0 ? "NO" : data.Sum(x => x.CountOut ?? 0).ToString() }}) .ToList(); } -- 2.25.1 From b9a375ab27271d9495b31d3b1fbf3a98aad2aac9 Mon Sep 17 00:00:00 2001 From: Tonb73 Date: Fri, 13 Dec 2024 10:05:54 +0300 Subject: [PATCH 7/7] =?UTF-8?q?=D0=A1=D0=B4=D0=B0=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProjectTourAgency/Forms/FormAddMoney.cs | 6 ++-- .../ProjectTourAgency/Reports/ChartReport.cs | 4 +-- .../Reports/TableReportcs.cs | 34 +++++++++++-------- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/project/ProjectTourAgency/Forms/FormAddMoney.cs b/project/ProjectTourAgency/Forms/FormAddMoney.cs index 918074d..bd1382e 100644 --- a/project/ProjectTourAgency/Forms/FormAddMoney.cs +++ b/project/ProjectTourAgency/Forms/FormAddMoney.cs @@ -20,9 +20,9 @@ namespace ProjectTourAgency.Forms InitializeComponent(); _addMoneyRepository = addMoneyRepository ?? throw new ArgumentNullException(nameof(addMoneyRepository)); - comboBoxClientId.DataSource = clientRepository.ReadClients(); - comboBoxClientId.DisplayMember = "Name"; - comboBoxClientId.ValueMember = "Id"; + comboBoxClientId.DataSource = addMoneyRepository.ReadAddMoneys(); + comboBoxClientId.DisplayMember = "ClientName"; + comboBoxClientId.ValueMember = "ClientId"; } private void buttonSave_Click(object sender, EventArgs e) diff --git a/project/ProjectTourAgency/Reports/ChartReport.cs b/project/ProjectTourAgency/Reports/ChartReport.cs index f4c881e..1b890cc 100644 --- a/project/ProjectTourAgency/Reports/ChartReport.cs +++ b/project/ProjectTourAgency/Reports/ChartReport.cs @@ -28,7 +28,7 @@ internal class ChartReport { new PdfBuilder(filePath) .AddHeader("Операции пополнения") - .AddPieChart("Пополнения счетов клиентов", GetData(dateTime)) + .AddPieChart($"Пополнения счетов клиентов за {dateTime.ToString("d MMMM yyyy")}", GetData(dateTime)) .Build(); return true; @@ -44,7 +44,7 @@ internal class ChartReport { return _addMoneyRepository .ReadAddMoneys(dateFrom: dateTime.Date, dateTo: dateTime.Date.AddDays(1)) - .GroupBy(x => x.ClientId, (key, group) => new { Id = key, Count = group.Count() }) + .GroupBy(x => x.ClientName, (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 97d1788..5200dd1 100644 --- a/project/ProjectTourAgency/Reports/TableReportcs.cs +++ b/project/ProjectTourAgency/Reports/TableReportcs.cs @@ -45,7 +45,6 @@ namespace ProjectTourAgency.Reports private List GetData(int tourId, DateTime startDate, DateTime endDate) { - var tourData = _tourRepository.ReadTours(startDate, endDate, tourId) .SelectMany(x => x.ClientTours .Select(y => new { @@ -56,10 +55,8 @@ namespace ProjectTourAgency.Reports CountOut = (int?)y.Cost })); - - var clientId = tourData.Select(x =>x.ClientId).Distinct().ToList(); + var clientId = tourData.Select(x => x.ClientId).Distinct().ToList(); - var addMoneyData = _addMoneyRepository.ReadAddMoneys(startDate, endDate) .Where(x => clientId.Contains(x.ClientId)) .Select(x => new { @@ -70,24 +67,33 @@ namespace ProjectTourAgency.Reports CountOut = (int?)null }); - + // Объединяем данные и группируем по клиенту и дате var data = tourData .Union(addMoneyData) + .GroupBy(x => new { x.ClientId, x.ClientName, x.Date }) + .Select(g => new { + ClientName = g.Key.ClientName, + Date = g.Key.Date, + CountIn = g.Sum(x => x.CountIn ?? 0), + CountOut = g.Sum(x => x.CountOut ?? 0) + }) .OrderBy(x => x.Date); return new List() { item } .Union(data.Select(x => new string[] { - x.ClientName.ToString(), - x.Date.ToString(), - x.CountIn?.ToString() ?? "NO", - x.CountOut?.ToString() ?? "NO"})) + x.ClientName, + x.Date.ToString("dd.MM.yyyy"), // Форматируем дату + x.CountIn == 0 ? "NO" : x.CountIn.ToString(), + x.CountOut == 0 ? "NO" : x.CountOut.ToString() + })) .Union(new[] { new string[] { - "Всего", - "", - data.Sum(x => x.CountIn ?? 0) == 0 ? "NO" : data.Sum(x => x.CountIn ?? 0).ToString(), - data.Sum(x => x.CountOut ?? 0) == 0 ? "NO" : data.Sum(x => x.CountOut ?? 0).ToString() - }}) + "Всего", + "", + data.Sum(x => x.CountIn) == 0 ? "NO" : data.Sum(x => x.CountIn).ToString(), + data.Sum(x => x.CountOut) == 0 ? "NO" : data.Sum(x => x.CountOut).ToString() + }}) .ToList(); } + } } -- 2.25.1