From b685486793197d10251fe4de3921e22641e5482a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D1=91=D0=BD=D0=B0=20=D0=A4=D1=80=D0=BE=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0?= Date: Fri, 20 Dec 2024 00:43:50 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D1=83=D0=BB=D1=83=D1=87=D1=88=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProjectHorseRacing/Entities/BuyHorse.cs | 34 +++++++----- .../Entities/BuyHorseHorse.cs | 3 ++ .../ProjectHorseRacing/Entities/Horse.cs | 5 ++ .../ProjectHorseRacing/Entities/Jockey.cs | 14 +++-- .../ProjectHorseRacing/Entities/Owners.cs | 16 ++++-- .../ProjectHorseRacing/Entities/Race.cs | 3 ++ .../ProjectHorseRacing/Entities/Result.cs | 16 +++++- .../Entities/TempBuyHorseHorse.cs | 16 ------ .../ProjectHorseRacing/Forms/FormBuyHorse.cs | 2 +- .../ProjectHorseRacing/Forms/FormBuyHorses.cs | 9 +++- .../ProjectHorseRacing/Forms/FormHorses.cs | 6 ++- .../ProjectHorseRacing/Forms/FormJockeys.cs | 7 ++- .../Forms/FormOwnerHorsesReport.cs | 2 +- .../ProjectHorseRacing/Forms/FormOwners.cs | 7 ++- .../ProjectHorseRacing/Forms/FormRaces.cs | 7 ++- .../ProjectHorseRacing/Forms/FormResult.cs | 6 +-- .../ProjectHorseRacing/Forms/FormResults.cs | 7 ++- .../Implementation/BuyHorseRepository.cs | 54 +++++++++++++++++-- .../Implementation/QueryBuilder.cs | 34 ++++++++++++ .../Implementation/ResultRepository.cs | 12 ++++- 20 files changed, 205 insertions(+), 55 deletions(-) delete mode 100644 ProjectHorseRacing/ProjectHorseRacing/Entities/TempBuyHorseHorse.cs create mode 100644 ProjectHorseRacing/ProjectHorseRacing/Repositories/Implementation/QueryBuilder.cs diff --git a/ProjectHorseRacing/ProjectHorseRacing/Entities/BuyHorse.cs b/ProjectHorseRacing/ProjectHorseRacing/Entities/BuyHorse.cs index 436354d..2553368 100644 --- a/ProjectHorseRacing/ProjectHorseRacing/Entities/BuyHorse.cs +++ b/ProjectHorseRacing/ProjectHorseRacing/Entities/BuyHorse.cs @@ -1,20 +1,31 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.ComponentModel; + namespace ProjectHorseRacing.Entities; public class BuyHorse { public int Id { get; private set; } + + [Browsable(false)] public int OwnersId { get; private set; } + + [DisplayName("Владелец")] + public string OwnersName { get; private set; } = string.Empty; + + [DisplayName("Дата покупки")] public DateTime DatePurchase { get; private set; } + [DisplayName("Лошадь")] + public string Horse => BuyHorseHorses != null ? + string.Join(", ", BuyHorseHorses.Select(x => $"{x.HorseName} {x.Cost}")) : + string.Empty; + + + [Browsable(false)] public IEnumerable BuyHorseHorses { get; private set; } = []; - public static BuyHorse CreateEntity(int id, int ownerId,DateTime date, IEnumerable buyHorseHorses) + public static BuyHorse CreateEntity(int id, int ownerId, DateTime date, IEnumerable buyHorseHorses) { return new BuyHorse { @@ -25,14 +36,11 @@ public class BuyHorse }; } - public static BuyHorse CreateEntity(TempBuyHorseHorse tempBuyHorseHorse, IEnumerable buyHorseHorses) + public void SetBuyHorseHorses(IEnumerable buyHorseHorses) { - return new BuyHorse + if (buyHorseHorses != null && buyHorseHorses.Any()) { - Id = tempBuyHorseHorse.Id, - OwnersId = tempBuyHorseHorse.OwnersId, - DatePurchase = tempBuyHorseHorse.DatePurchase, - BuyHorseHorses = buyHorseHorses - }; + BuyHorseHorses = buyHorseHorses; + } } } diff --git a/ProjectHorseRacing/ProjectHorseRacing/Entities/BuyHorseHorse.cs b/ProjectHorseRacing/ProjectHorseRacing/Entities/BuyHorseHorse.cs index 39582d4..e6f9621 100644 --- a/ProjectHorseRacing/ProjectHorseRacing/Entities/BuyHorseHorse.cs +++ b/ProjectHorseRacing/ProjectHorseRacing/Entities/BuyHorseHorse.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -13,6 +14,8 @@ public class BuyHorseHorse public int HorseId { get; private set; } + public string HorseName { get; private set; } = string.Empty; + public int Cost { get; private set; } public static BuyHorseHorse CreateElement(int id, int horseId, int cost) diff --git a/ProjectHorseRacing/ProjectHorseRacing/Entities/Horse.cs b/ProjectHorseRacing/ProjectHorseRacing/Entities/Horse.cs index 0829932..981f516 100644 --- a/ProjectHorseRacing/ProjectHorseRacing/Entities/Horse.cs +++ b/ProjectHorseRacing/ProjectHorseRacing/Entities/Horse.cs @@ -1,6 +1,7 @@ using ProjectHorseRacing.Entities.Enums; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -11,12 +12,16 @@ public class Horse { public int Id { get; private set; } + [DisplayName("Кличка")] public string Nickname { get; private set; } = string.Empty; + [DisplayName("Пол")] public HorseGender HorseGender { get; private set; } + [DisplayName("Возраст")] public int AgeHorse { get; private set; } + [DisplayName("Характер")] public HorseCharacters HorseCharacters { get; private set; } diff --git a/ProjectHorseRacing/ProjectHorseRacing/Entities/Jockey.cs b/ProjectHorseRacing/ProjectHorseRacing/Entities/Jockey.cs index 985a034..507dbdf 100644 --- a/ProjectHorseRacing/ProjectHorseRacing/Entities/Jockey.cs +++ b/ProjectHorseRacing/ProjectHorseRacing/Entities/Jockey.cs @@ -1,6 +1,7 @@ using ProjectHorseRacing.Entities.Enums; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -11,12 +12,19 @@ public class Jockey { public int Id { get; private set; } - public string FirstName { get; private set; } = string.Empty; - + [DisplayName("Фамилия")] public string LastName { get; private set; } = string.Empty; - public int Age { get; private set; } + [DisplayName("Имя")] + public string FirstName { get; private set; } = string.Empty; + + public string FullName => $"{LastName} {FirstName}"; + + [DisplayName("Возраст")] + public int Age { get; private set; } + + [DisplayName("Рейтинг")] public double Rating { get; private set; } public static Jockey CreateEntity(int id, string firstName, string lastName, int age, double rating) diff --git a/ProjectHorseRacing/ProjectHorseRacing/Entities/Owners.cs b/ProjectHorseRacing/ProjectHorseRacing/Entities/Owners.cs index 4612c65..c79e979 100644 --- a/ProjectHorseRacing/ProjectHorseRacing/Entities/Owners.cs +++ b/ProjectHorseRacing/ProjectHorseRacing/Entities/Owners.cs @@ -1,14 +1,24 @@ -namespace ProjectHorseRacing.Entities; +using System.ComponentModel; + +namespace ProjectHorseRacing.Entities; public class Owners { - public int Id { get;private set; } - public string FirstName { get; private set; } = string.Empty; + public int Id { get; private set; } + [DisplayName("Фамилия")] public string LastName { get; private set; } = string.Empty; + [DisplayName("Имя")] + public string FirstName { get; private set; } = string.Empty; + + + public string FullName => $"{LastName} {FirstName}"; + + [DisplayName("Адрес")] public string Address { get; private set; } = string.Empty; + [DisplayName("Телефон")] public string PhoneNumber { get; private set; } = string.Empty; diff --git a/ProjectHorseRacing/ProjectHorseRacing/Entities/Race.cs b/ProjectHorseRacing/ProjectHorseRacing/Entities/Race.cs index c2c7b03..834efe3 100644 --- a/ProjectHorseRacing/ProjectHorseRacing/Entities/Race.cs +++ b/ProjectHorseRacing/ProjectHorseRacing/Entities/Race.cs @@ -1,6 +1,7 @@ using ProjectHorseRacing.Entities.Enums; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -11,8 +12,10 @@ public class Race { public int Id { get; private set; } + [DisplayName("Дата скачек")] public DateTime DateTime { get; private set; } + [DisplayName("Место проведения")] public RacePlaceEvent RacePlaceEvent { get; private set; } diff --git a/ProjectHorseRacing/ProjectHorseRacing/Entities/Result.cs b/ProjectHorseRacing/ProjectHorseRacing/Entities/Result.cs index 151b275..7ff1ac9 100644 --- a/ProjectHorseRacing/ProjectHorseRacing/Entities/Result.cs +++ b/ProjectHorseRacing/ProjectHorseRacing/Entities/Result.cs @@ -1,6 +1,7 @@ using ProjectHorseRacing.Entities.Enums; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -10,15 +11,28 @@ namespace ProjectHorseRacing.Entities; public class Result { public int Id { get; private set; } - + [DisplayName("Занятое место")] public int Position { get; private set; } + [Browsable(false)] public int RaceId { get; private set; } + [Browsable(false)] public int JockeyId { get; private set; } + [Browsable(false)] public int HorseId { get; private set; } + [DisplayName("Скачки")] + public string RaceName { get; private set; } = string.Empty; + + [DisplayName("Жокей")] + public string JockeyName { get; private set; } = string.Empty; + + [DisplayName("Лошадь")] + public string HorseName { get; private set; } = string.Empty; + + public static Result CreateEntity(int id, int position, int raceId, int jockeyId, int horseId) { return new Result diff --git a/ProjectHorseRacing/ProjectHorseRacing/Entities/TempBuyHorseHorse.cs b/ProjectHorseRacing/ProjectHorseRacing/Entities/TempBuyHorseHorse.cs deleted file mode 100644 index 42ace9f..0000000 --- a/ProjectHorseRacing/ProjectHorseRacing/Entities/TempBuyHorseHorse.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ProjectHorseRacing.Entities; - -public class TempBuyHorseHorse -{ - public int Id { get; private set; } - public int OwnersId { get; private set; } - public DateTime DatePurchase { get; private set; } - public int HorseId { get; private set; } - public int Cost { get; private set; } -} diff --git a/ProjectHorseRacing/ProjectHorseRacing/Forms/FormBuyHorse.cs b/ProjectHorseRacing/ProjectHorseRacing/Forms/FormBuyHorse.cs index ea9fc87..3fa8e9c 100644 --- a/ProjectHorseRacing/ProjectHorseRacing/Forms/FormBuyHorse.cs +++ b/ProjectHorseRacing/ProjectHorseRacing/Forms/FormBuyHorse.cs @@ -16,7 +16,7 @@ public partial class FormBuyHorse : Form _buyHorseRepository = buyHorseRepository ?? throw new ArgumentNullException(nameof(buyHorseRepository)); comboBoxOwner.DataSource = ownerRepository.ReadOwners(); - comboBoxOwner.DisplayMember = "LastName"; + comboBoxOwner.DisplayMember = "FullName"; comboBoxOwner.ValueMember = "Id"; ColumnHorse.DataSource = horseRepository.ReadHorses(); diff --git a/ProjectHorseRacing/ProjectHorseRacing/Forms/FormBuyHorses.cs b/ProjectHorseRacing/ProjectHorseRacing/Forms/FormBuyHorses.cs index 74f96b7..86e2c5e 100644 --- a/ProjectHorseRacing/ProjectHorseRacing/Forms/FormBuyHorses.cs +++ b/ProjectHorseRacing/ProjectHorseRacing/Forms/FormBuyHorses.cs @@ -54,7 +54,7 @@ public partial class FormBuyHorses : Form } } - + private void buttonDel_Click(object sender, EventArgs e) { if (!TryGetIdentifierFromSelectedRow(out var findId)) @@ -76,7 +76,12 @@ public partial class FormBuyHorses : Form } } - private void LoadList() => dataGridView.DataSource = _buyHorseRepository.ReadBuyHorse(); + private void LoadList() + { + dataGridView.DataSource = _buyHorseRepository.ReadBuyHorse(); + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["DatePurchase"].DefaultCellStyle.Format = "dd MMMM yyyy hh:mm"; + } private bool TryGetIdentifierFromSelectedRow(out int id) { diff --git a/ProjectHorseRacing/ProjectHorseRacing/Forms/FormHorses.cs b/ProjectHorseRacing/ProjectHorseRacing/Forms/FormHorses.cs index e543a2e..6f679e1 100644 --- a/ProjectHorseRacing/ProjectHorseRacing/Forms/FormHorses.cs +++ b/ProjectHorseRacing/ProjectHorseRacing/Forms/FormHorses.cs @@ -94,7 +94,11 @@ public partial class FormHorses : Form } } - private void LoadList() => dataGridView.DataSource = _horseRepository.ReadHorses(); + private void LoadList() + { + dataGridView.DataSource = _horseRepository.ReadHorses(); + dataGridView.Columns["Id"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { diff --git a/ProjectHorseRacing/ProjectHorseRacing/Forms/FormJockeys.cs b/ProjectHorseRacing/ProjectHorseRacing/Forms/FormJockeys.cs index abbdfa4..4675528 100644 --- a/ProjectHorseRacing/ProjectHorseRacing/Forms/FormJockeys.cs +++ b/ProjectHorseRacing/ProjectHorseRacing/Forms/FormJockeys.cs @@ -97,7 +97,12 @@ public partial class FormJockeys : Form } - private void LoadList() => dataGridView.DataSource = _jockeyRepository.ReadJockeys(); + private void LoadList() + { + dataGridView.DataSource = _jockeyRepository.ReadJockeys(); + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["FullName"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { diff --git a/ProjectHorseRacing/ProjectHorseRacing/Forms/FormOwnerHorsesReport.cs b/ProjectHorseRacing/ProjectHorseRacing/Forms/FormOwnerHorsesReport.cs index 14ee0c8..ba1b9f4 100644 --- a/ProjectHorseRacing/ProjectHorseRacing/Forms/FormOwnerHorsesReport.cs +++ b/ProjectHorseRacing/ProjectHorseRacing/Forms/FormOwnerHorsesReport.cs @@ -23,7 +23,7 @@ namespace ProjectHorseRacing.Forms _container = container ?? throw new ArgumentNullException(nameof(container)); comboBoxOwner.DataSource = ownerRepository.ReadOwners(); - comboBoxOwner.DisplayMember = "LastName"; + comboBoxOwner.DisplayMember = "FullName"; comboBoxOwner.ValueMember = "Id"; } diff --git a/ProjectHorseRacing/ProjectHorseRacing/Forms/FormOwners.cs b/ProjectHorseRacing/ProjectHorseRacing/Forms/FormOwners.cs index 5421f91..6940192 100644 --- a/ProjectHorseRacing/ProjectHorseRacing/Forms/FormOwners.cs +++ b/ProjectHorseRacing/ProjectHorseRacing/Forms/FormOwners.cs @@ -78,7 +78,12 @@ public partial class FormOwners : Form } } - private void LoadList() => dataGridView.DataSource = _ownerRepository.ReadOwners(); + private void LoadList() + { + dataGridView.DataSource = _ownerRepository.ReadOwners(); + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["FullName"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { diff --git a/ProjectHorseRacing/ProjectHorseRacing/Forms/FormRaces.cs b/ProjectHorseRacing/ProjectHorseRacing/Forms/FormRaces.cs index 629fa5a..9b534cc 100644 --- a/ProjectHorseRacing/ProjectHorseRacing/Forms/FormRaces.cs +++ b/ProjectHorseRacing/ProjectHorseRacing/Forms/FormRaces.cs @@ -93,7 +93,12 @@ public partial class FormRaces : Form } } - private void LoadList() => dataGridView.DataSource = _raceRepository.ReadRaces(); + private void LoadList() + { + dataGridView.DataSource = _raceRepository.ReadRaces(); + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["DateTime"].DefaultCellStyle.Format = "dd.MM.yyyy"; + } private bool TryGetIdentifierFromSelectedRow(out int id) { diff --git a/ProjectHorseRacing/ProjectHorseRacing/Forms/FormResult.cs b/ProjectHorseRacing/ProjectHorseRacing/Forms/FormResult.cs index 37b2c44..abc23b7 100644 --- a/ProjectHorseRacing/ProjectHorseRacing/Forms/FormResult.cs +++ b/ProjectHorseRacing/ProjectHorseRacing/Forms/FormResult.cs @@ -21,15 +21,15 @@ public partial class FormResult : Form IHorseRepository horseRepository) { InitializeComponent(); - _resultRepository = resultRepository ?? + _resultRepository = resultRepository ?? throw new ArgumentNullException(nameof(resultRepository)); - comboBoxRace.DataSource =raceRepository.ReadRaces(); + comboBoxRace.DataSource = raceRepository.ReadRaces(); comboBoxRace.DisplayMember = "DateTime"; comboBoxRace.ValueMember = "Id"; comboBoxJockey.DataSource = jockeyRepository.ReadJockeys(); - comboBoxJockey.DisplayMember = "FirstName"; + comboBoxJockey.DisplayMember = "FullName"; comboBoxJockey.ValueMember = "Id"; comboBoxHorse.DataSource = horseRepository.ReadHorses(); diff --git a/ProjectHorseRacing/ProjectHorseRacing/Forms/FormResults.cs b/ProjectHorseRacing/ProjectHorseRacing/Forms/FormResults.cs index 53a708f..d5fb461 100644 --- a/ProjectHorseRacing/ProjectHorseRacing/Forms/FormResults.cs +++ b/ProjectHorseRacing/ProjectHorseRacing/Forms/FormResults.cs @@ -57,6 +57,9 @@ public partial class FormResults : Form } - private void LoadList() => dataGridView.DataSource = _resultRepository.ReadResults(); - + private void LoadList() + { + dataGridView.DataSource = _resultRepository.ReadResults(); + dataGridView.Columns["Id"].Visible = false; + } } diff --git a/ProjectHorseRacing/ProjectHorseRacing/Repositories/Implementation/BuyHorseRepository.cs b/ProjectHorseRacing/ProjectHorseRacing/Repositories/Implementation/BuyHorseRepository.cs index 528e398..c36b9ac 100644 --- a/ProjectHorseRacing/ProjectHorseRacing/Repositories/Implementation/BuyHorseRepository.cs +++ b/ProjectHorseRacing/ProjectHorseRacing/Repositories/Implementation/BuyHorseRepository.cs @@ -4,6 +4,7 @@ using Newtonsoft.Json; using Npgsql; using Dapper; + namespace ProjectHorseRacing.Repositories.Implementation; public class BuyHorseRepository : IBuyHorseRepository @@ -82,17 +83,60 @@ public class BuyHorseRepository : IBuyHorseRepository } } - public IEnumerable ReadBuyHorse(DateTime? dateForm = null, DateTime? dateTo = null, int? ownersId = null) + public IEnumerable ReadBuyHorse(DateTime? dateFrom = null, DateTime? dateTo = null, int? ownersId = null) { _logger.LogInformation("Получение всех объектов"); try { + var builder = new QueryBuilder(); + if (dateFrom.HasValue) + { + builder.AddCondition("bh.DatePurchase >= @dateFrom"); + } + if (dateTo.HasValue) + { + builder.AddCondition("bh.DatePurchase <= @dateTo"); + } + if (ownersId.HasValue) + { + builder.AddCondition("bh.ownersId = @ownersId"); + } + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = @"SELECT bh.*, bhh.HorseId, bhh.Cost FROM BuyHorse bh - INNER JOIN BuyHorseHorse bhh on bhh.BuyHorseId = bh.Id"; - var buyHorse = connection.Query(querySelect); + var querySelect = $@"SELECT + bh.*, + CONCAT(o.LastName, ' ', o.FirstName) as OwnersName, + bhh.HorseId, + bhh.Cost, + h.NickName as HorseName + FROM BuyHorse bh + LEFT JOIN Owners o on o.Id = bh.OwnersId + INNER JOIN BuyHorseHorse bhh on bhh.BuyHorseId = bh.Id + LEFT JOIN Horse h on h.Id = bhh.HorseId + {builder.Build()}"; + + var horseDict = new Dictionary>(); + + var buyHorse = connection.Query(querySelect, + (horse, buyHorse) => + { + if (!horseDict.TryGetValue(horse.Id, out var bhh)) + { + bhh = []; + horseDict.Add(horse.Id, bhh); + } + + bhh.Add(buyHorse); + return horse; + }, splitOn: "HorseId", param: new { dateFrom, dateTo, ownersId }); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(buyHorse)); - return buyHorse.GroupBy(x => x.Id, y => y, (key, value) => BuyHorse.CreateEntity(value.First(), value.Select(z => BuyHorseHorse.CreateElement(0, z.HorseId, z.Cost)))).ToList(); + + return horseDict.Select(x => + { + var bh = buyHorse.First(y => y.Id == x.Key); + bh.SetBuyHorseHorses(x.Value); + return bh; + }).ToArray(); } catch (Exception ex) { diff --git a/ProjectHorseRacing/ProjectHorseRacing/Repositories/Implementation/QueryBuilder.cs b/ProjectHorseRacing/ProjectHorseRacing/Repositories/Implementation/QueryBuilder.cs new file mode 100644 index 0000000..50801b5 --- /dev/null +++ b/ProjectHorseRacing/ProjectHorseRacing/Repositories/Implementation/QueryBuilder.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectHorseRacing.Repositories.Implementation; + +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/ProjectHorseRacing/ProjectHorseRacing/Repositories/Implementation/ResultRepository.cs b/ProjectHorseRacing/ProjectHorseRacing/Repositories/Implementation/ResultRepository.cs index 1bf8bfd..191f060 100644 --- a/ProjectHorseRacing/ProjectHorseRacing/Repositories/Implementation/ResultRepository.cs +++ b/ProjectHorseRacing/ProjectHorseRacing/Repositories/Implementation/ResultRepository.cs @@ -10,6 +10,8 @@ using Newtonsoft.Json; using Npgsql; using System.Net.Sockets; using Dapper; +using DocumentFormat.OpenXml.Office2010.Excel; +using DocumentFormat.OpenXml.Drawing.Charts; namespace ProjectHorseRacing.Repositories.Implementation; @@ -51,7 +53,15 @@ public class ResultRepository : IResultRepository try { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = "SELECT * FROM Result"; + var querySelect = @"SELECT + re.*, + r.RacePlaceEvent as RaceName, + CONCAT(j.LastName, ' ', j.FirstName) as JockeyName, + h.NickName as HorseName + FROM Result re + LEFT JOIN Jockey j on j.Id = re.JockeyId + LEFT JOIN Horse h on h.Id = re.HorseId + LEFT JOIN Race r on r.Id = re.RaceId"; var results = connection.Query(querySelect); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(results)); -- 2.25.1 From 67425203f4015cda4981af01e87b2ea705e71be9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D1=91=D0=BD=D0=B0=20=D0=A4=D1=80=D0=BE=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0?= Date: Fri, 20 Dec 2024 00:54:39 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D0=A3=D0=BB=D1=83=D1=87=D1=88=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=202=20=D0=B8=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProjectHorseRacing/Reports/ChartReport.cs | 5 ++--- .../ProjectHorseRacing/Reports/TableReport.cs | 6 +++--- .../Repositories/Implementation/RaceRepository.cs | 15 +++++++++++++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/ProjectHorseRacing/ProjectHorseRacing/Reports/ChartReport.cs b/ProjectHorseRacing/ProjectHorseRacing/Reports/ChartReport.cs index 7d868de..93a0dad 100644 --- a/ProjectHorseRacing/ProjectHorseRacing/Reports/ChartReport.cs +++ b/ProjectHorseRacing/ProjectHorseRacing/Reports/ChartReport.cs @@ -24,7 +24,7 @@ internal class ChartReport { new PdfBuilder(filePath) .AddHeader("Результаты заездов") - .AddPieChart("1 места занятые лошадьми", GetData(dateTime)) + .AddPieChart($"1 места занятые лошадьми на {dateTime:dd.MM.yyyy}", GetData(dateTime)) .Build(); return true; } @@ -40,8 +40,7 @@ internal class ChartReport .ToDictionary(f => f.Id, f => f.Nickname); var racesOnDate = _raceRepository - .ReadRaces() - .Where(r => r.DateTime.Date == dateTime.Date) + .ReadRaces(dateFrom: dateTime.Date, dateTo: dateTime.Date.AddDays(1)) .Select(r => r.Id) .ToHashSet(); diff --git a/ProjectHorseRacing/ProjectHorseRacing/Reports/TableReport.cs b/ProjectHorseRacing/ProjectHorseRacing/Reports/TableReport.cs index 070af7a..ccbddbe 100644 --- a/ProjectHorseRacing/ProjectHorseRacing/Reports/TableReport.cs +++ b/ProjectHorseRacing/ProjectHorseRacing/Reports/TableReport.cs @@ -24,7 +24,7 @@ internal class TableReport { new ExcelBuilder(filePath) .AddHeader("Сводка по покупке лошадей", 0, 2) - .AddParagraph("за период", 0) + .AddParagraph($"за период {startDate:dd.MM.yyyy} по {endDate:dd.MM.yyyy}", 0) .AddTable([10, 10, 10], GetData(ownersId, startDate, endDate)) .Build(); return true; @@ -45,14 +45,14 @@ internal class TableReport { Date = bh.DatePurchase, OwnerId = bh.OwnersId, - HorseId = bhh.HorseId, + HorseName = bhh.HorseName, Cost = bhh.Cost }) .OrderBy(x => x.Date); return new List() { item } .Union(buyHorses - .Select(x => new string[] { x.HorseId.ToString(), x.Date.ToString("dd.MM.yyyy"), x.Cost.ToString()! })) + .Select(x => new string[] { x.HorseName.ToString(), x.Date.ToString("dd.MM.yyyy"), x.Cost.ToString()! })) .Union(new[] { new string[] { "Всего", "", buyHorses.Sum(x => x.Cost).ToString()! } diff --git a/ProjectHorseRacing/ProjectHorseRacing/Repositories/Implementation/RaceRepository.cs b/ProjectHorseRacing/ProjectHorseRacing/Repositories/Implementation/RaceRepository.cs index f122c90..002a744 100644 --- a/ProjectHorseRacing/ProjectHorseRacing/Repositories/Implementation/RaceRepository.cs +++ b/ProjectHorseRacing/ProjectHorseRacing/Repositories/Implementation/RaceRepository.cs @@ -91,9 +91,20 @@ public class RaceRepository : IRaceRepository try { + var builder = new QueryBuilder(); + if (dateFrom.HasValue) + { + builder.AddCondition("r.DateTime >= @dateFrom"); + } + if (dateTo.HasValue) + { + builder.AddCondition("r.DateTime <= @dateTo"); + } + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = "SELECT * FROM Race"; - var races = connection.Query(querySelect); + var querySelect = $@"SELECT r.* FROM Race r + {builder.Build()}"; + var races = connection.Query(querySelect, new { dateFrom, dateTo }); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(races)); return races; -- 2.25.1