ИСЭбд-22. Фролова А.С. Лабораторная работа №4 #14
@ -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<BuyHorseHorse> BuyHorseHorses { get; private set; } = [];
|
||||
|
||||
public static BuyHorse CreateEntity(int id, int ownerId,DateTime date, IEnumerable<BuyHorseHorse> buyHorseHorses)
|
||||
public static BuyHorse CreateEntity(int id, int ownerId, DateTime date, IEnumerable<BuyHorseHorse> buyHorseHorses)
|
||||
{
|
||||
return new BuyHorse
|
||||
{
|
||||
@ -25,14 +36,11 @@ public class BuyHorse
|
||||
};
|
||||
}
|
||||
|
||||
public static BuyHorse CreateEntity(TempBuyHorseHorse tempBuyHorseHorse, IEnumerable<BuyHorseHorse> buyHorseHorses)
|
||||
public void SetBuyHorseHorses(IEnumerable<BuyHorseHorse> buyHorseHorses)
|
||||
{
|
||||
return new BuyHorse
|
||||
if (buyHorseHorses != null && buyHorseHorses.Any())
|
||||
{
|
||||
Id = tempBuyHorseHorse.Id,
|
||||
OwnersId = tempBuyHorseHorse.OwnersId,
|
||||
DatePurchase = tempBuyHorseHorse.DatePurchase,
|
||||
BuyHorseHorses = buyHorseHorses
|
||||
};
|
||||
BuyHorseHorses = buyHorseHorses;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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; }
|
||||
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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;
|
||||
|
||||
|
||||
|
@ -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; }
|
||||
|
||||
|
||||
|
@ -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
|
||||
|
@ -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; }
|
||||
}
|
@ -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();
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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";
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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();
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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<string[]>() { 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()! }
|
||||
|
@ -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<BuyHorse> ReadBuyHorse(DateTime? dateForm = null, DateTime? dateTo = null, int? ownersId = null)
|
||||
public IEnumerable<BuyHorse> 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<TempBuyHorseHorse>(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<int, List<BuyHorseHorse>>();
|
||||
|
||||
var buyHorse = connection.Query<BuyHorse, BuyHorseHorse, BuyHorse>(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)
|
||||
{
|
||||
|
@ -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}";
|
||||
}
|
||||
}
|
@ -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<Race>(querySelect);
|
||||
var querySelect = $@"SELECT r.* FROM Race r
|
||||
{builder.Build()}";
|
||||
var races = connection.Query<Race>(querySelect, new { dateFrom, dateTo });
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(races));
|
||||
return races;
|
||||
|
@ -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<Result>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(results));
|
||||
|
Loading…
x
Reference in New Issue
Block a user