diff --git a/ProjectHotel/ProjectHotel/Entities/Athlete.cs b/ProjectHotel/ProjectHotel/Entities/Athlete.cs index 3347259..75bb71f 100644 --- a/ProjectHotel/ProjectHotel/Entities/Athlete.cs +++ b/ProjectHotel/ProjectHotel/Entities/Athlete.cs @@ -2,6 +2,7 @@ using ProjectHotel.Entities.Enums; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -11,11 +12,19 @@ namespace ProjectHotel.Entities; public class Athlete { public int Id { get; set; } + [DisplayName("Имя")] public string FirstName { get; private set; } = string.Empty; + [DisplayName("Фмилия")] public string LastName { get; private set; } = string.Empty; + [DisplayName("Отчество")] public string FatherName { get; private set; } = string.Empty; + [Browsable(false)] + public string FullName => $"{FirstName} {LastName} {FatherName}" ; + [DisplayName("Вид спорта")] public Sport Sport { get; private set; } + [DisplayName("Разряд")] public AthleteClass AthleteClass { get; private set; } + public static Athlete CreateEntity(int id, string first, string last, string father, Sport sport, AthleteClass athleteClass) { return new Athlete diff --git a/ProjectHotel/ProjectHotel/Entities/AthletePlacingAthlete.cs b/ProjectHotel/ProjectHotel/Entities/AthletePlacingAthlete.cs index 58d04d8..cac7147 100644 --- a/ProjectHotel/ProjectHotel/Entities/AthletePlacingAthlete.cs +++ b/ProjectHotel/ProjectHotel/Entities/AthletePlacingAthlete.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,9 +9,15 @@ namespace ProjectHotel.Entities; public class AthletePlacingAthlete { + public int Id { get; set; } + [Browsable(false)] public int AthleteId { get; private set; } + [DisplayName("Спортсмен")] + public string AthleteName { get; private set; } = string.Empty; + public int PlacingAthleteId{ get; private set; } + [DisplayName("Длительность проживания")] public int LengthOfStay { get; private set; } public static AthletePlacingAthlete CreateElement(int id, int athleteId, int lengthOfStay) { diff --git a/ProjectHotel/ProjectHotel/Entities/CleaningRoom.cs b/ProjectHotel/ProjectHotel/Entities/CleaningRoom.cs index 49a45aa..99bcc90 100644 --- a/ProjectHotel/ProjectHotel/Entities/CleaningRoom.cs +++ b/ProjectHotel/ProjectHotel/Entities/CleaningRoom.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,8 +9,13 @@ namespace ProjectHotel.Entities; public class CleaningRoom { + public int Id { get; set; } + public int RoomId { get; set; } + [DisplayName("Название комнаты")] + public string RoomName { get; set; } = string.Empty; + [DisplayName("Дата уборки")] public DateTime CleaningDate { get; private set; } public static CleaningRoom CreateOperation(int id, int roomId) { diff --git a/ProjectHotel/ProjectHotel/Entities/Hotel.cs b/ProjectHotel/ProjectHotel/Entities/Hotel.cs index 573c8ea..a1c2689 100644 --- a/ProjectHotel/ProjectHotel/Entities/Hotel.cs +++ b/ProjectHotel/ProjectHotel/Entities/Hotel.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,8 +9,11 @@ namespace ProjectHotel.Entities; public class Hotel { + public int Id { get; private set; } + [DisplayName("Название")] public string Name { get; private set; } = string.Empty; + [DisplayName("Адрес")] public string Address { get; private set; } = string.Empty; diff --git a/ProjectHotel/ProjectHotel/Entities/PlacingAthlete.cs b/ProjectHotel/ProjectHotel/Entities/PlacingAthlete.cs index 01de0be..b2b155b 100644 --- a/ProjectHotel/ProjectHotel/Entities/PlacingAthlete.cs +++ b/ProjectHotel/ProjectHotel/Entities/PlacingAthlete.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,9 +9,21 @@ namespace ProjectHotel.Entities; public class PlacingAthlete { + public int Id { get; private set; } + + public int RoomId { get; private set; } + + [DisplayName("Комната")] + public string RoomName { get; private set; } = string.Empty; + [DisplayName("Дата заселения")] public DateTime PlacingDate { get; private set; } + [DisplayName("Корма")] + public string athlete => AthletePlacingAthlete != null ? + string.Join(", ", AthletePlacingAthlete.Select(x => $"{x.AthleteName} {x.Count}")) : string.Empty; + + [Browsable(false)] public IEnumerable AthletePlacingAthletes { get; @@ -27,14 +40,12 @@ public class PlacingAthlete }; } - public static PlacingAthlete CreateOpeartion(TempAthletePlacingAthlete tempAthletePlacingAthlete, IEnumerable athletePlacingAthletes) + + public void SetAthletePlacingAthlete(IEnumerable athletePlacingAthletes) { - return new PlacingAthlete + if (athletePlacingAthletes != null && athletePlacingAthletes.Any()) { - Id = tempAthletePlacingAthlete.Id, - RoomId = tempAthletePlacingAthlete.RoomId, - PlacingDate = DateTime.Now, - AthletePlacingAthletes = athletePlacingAthletes - }; + AthletePlacingAthletes = athletePlacingAthletes; + } } } diff --git a/ProjectHotel/ProjectHotel/Entities/Room.cs b/ProjectHotel/ProjectHotel/Entities/Room.cs index 28abbd9..20f604f 100644 --- a/ProjectHotel/ProjectHotel/Entities/Room.cs +++ b/ProjectHotel/ProjectHotel/Entities/Room.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,15 @@ namespace ProjectHotel.Entities; public class Room { + public int Id { get; private set; } + [Browsable(false)] public int HotelId { get; private set; } - public string Name { get; private set; } = string.Empty; + [DisplayName("Название отеля")] + public string HotelName { get; private set; } = string.Empty; + [DisplayName("Название")] + public string RoomName { get; private set; } = string.Empty; + [DisplayName("Вместимость")] public int Capacity { get; private set; } public static Room CreateEntity(int id, int hotel, string name, int capacity) { @@ -19,7 +26,7 @@ public class Room { Id = id, HotelId = hotel, - Name = name, + RoomName = name, Capacity = capacity }; diff --git a/ProjectHotel/ProjectHotel/Entities/TempAthletePlacingAthlete.cs b/ProjectHotel/ProjectHotel/Entities/TempAthletePlacingAthlete.cs deleted file mode 100644 index fb98623..0000000 --- a/ProjectHotel/ProjectHotel/Entities/TempAthletePlacingAthlete.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ProjectHotel.Entities; - -public class TempAthletePlacingAthlete -{ - public int Id { get; private set; } - public int RoomId { get; private set; } - public DateTime PlacingDate { get; private set; } - public int AthleteId { get; private set; } - public int PlacingAthleteId { get; private set; } - public int LengthOfStay { get; private set; } -} diff --git a/ProjectHotel/ProjectHotel/Forms/FormAthletes.cs b/ProjectHotel/ProjectHotel/Forms/FormAthletes.cs index 105081a..e9082f4 100644 --- a/ProjectHotel/ProjectHotel/Forms/FormAthletes.cs +++ b/ProjectHotel/ProjectHotel/Forms/FormAthletes.cs @@ -89,7 +89,12 @@ public partial class FormAthletes : Form MessageBoxButtons.OK, MessageBoxIcon.Error); } } - private void LoadList() => dataGridViewData.DataSource = _athleteRepository.ReadAthletes(); + private void LoadList() + { + dataGridViewData.DataSource = _athleteRepository.ReadAthletes(); + dataGridViewData.Columns["Id"].Visible = false; + dataGridViewData.Columns["FullName"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { id = 0; diff --git a/ProjectHotel/ProjectHotel/Forms/FormCleaningRooms.cs b/ProjectHotel/ProjectHotel/Forms/FormCleaningRooms.cs index 871b940..0a8de2d 100644 --- a/ProjectHotel/ProjectHotel/Forms/FormCleaningRooms.cs +++ b/ProjectHotel/ProjectHotel/Forms/FormCleaningRooms.cs @@ -48,9 +48,14 @@ public partial class FormCleaningRooms : Form MessageBox.Show(ex.Message, "Ошибка при добавлении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - private void LoadList() => dataGridViewData.DataSource = _cleaningRoomRepository.ReadCleaningRooms(); + private void LoadList() + { + dataGridViewData.DataSource = _cleaningRoomRepository.ReadCleaningRooms(); + dataGridViewData.Columns["Id"].Visible = false; + dataGridViewData.Columns["RoomName"].Visible = false; - + + } } diff --git a/ProjectHotel/ProjectHotel/Forms/FormPlacingAthlete.cs b/ProjectHotel/ProjectHotel/Forms/FormPlacingAthlete.cs index c27d32b..e72acc6 100644 --- a/ProjectHotel/ProjectHotel/Forms/FormPlacingAthlete.cs +++ b/ProjectHotel/ProjectHotel/Forms/FormPlacingAthlete.cs @@ -27,7 +27,7 @@ public partial class FormPlacingAthlete : Form comboBoxRoom.DisplayMember = "Name"; comboBoxRoom.ValueMember = "Id"; ColumnAthlete.DataSource = athleteRepository.ReadAthletes(); - ColumnAthlete.DisplayMember = "FirstName"; + ColumnAthlete.DisplayMember = "FullName"; ColumnAthlete.ValueMember = "Id"; } private void ButtonSave_Click(object sender, EventArgs e) @@ -63,6 +63,8 @@ public partial class FormPlacingAthlete : Form list.Add(AthletePlacingAthlete.CreateElement(0, Convert.ToInt32(row.Cells["ColumnAthlete"].Value), Convert.ToInt32(row.Cells["ColumnLengthOfStay"].Value))); + + } return list; } diff --git a/ProjectHotel/ProjectHotel/Forms/FormPlacingAthletes.cs b/ProjectHotel/ProjectHotel/Forms/FormPlacingAthletes.cs index 3f4e6d0..76fa879 100644 --- a/ProjectHotel/ProjectHotel/Forms/FormPlacingAthletes.cs +++ b/ProjectHotel/ProjectHotel/Forms/FormPlacingAthletes.cs @@ -71,8 +71,12 @@ public partial class FormPlacingAthletes : Form MessageBoxButtons.OK, MessageBoxIcon.Error); } } - private void LoadList() => dataGridViewData.DataSource = - _placingAthleteRepository.ReadPlacingAthlete(); + private void LoadList() + { + dataGridViewData.DataSource = _placingAthleteRepository.ReadPlacingAthlete(); + dataGridViewData.Columns["Id"].Visible = false; + dataGridViewData.Columns["PlacingDate"].DefaultCellStyle.Format = "dd MMMM yyyy hh:mm"; + } private bool TryGetIdentifierFromSelectedRow(out int id) { id = 0; diff --git a/ProjectHotel/ProjectHotel/Forms/FormRoom.cs b/ProjectHotel/ProjectHotel/Forms/FormRoom.cs index 8b866aa..c5cd0bc 100644 --- a/ProjectHotel/ProjectHotel/Forms/FormRoom.cs +++ b/ProjectHotel/ProjectHotel/Forms/FormRoom.cs @@ -28,7 +28,7 @@ namespace ProjectHotel.Forms throw new InvalidDataException(nameof(room)); } - textBoxRoomName.Text = room.Name; + textBoxRoomName.Text = room.RoomName; numericUpDownCapacity.Value = room.Capacity; _roomId = value; } diff --git a/ProjectHotel/ProjectHotel/Reports/DocReport.cs b/ProjectHotel/ProjectHotel/Reports/DocReport.cs index d70e5d8..ea9fcdc 100644 --- a/ProjectHotel/ProjectHotel/Reports/DocReport.cs +++ b/ProjectHotel/ProjectHotel/Reports/DocReport.cs @@ -66,7 +66,7 @@ x.LastName,x.FatherName, x.Sport.ToString(), x.AthleteClass.ToString() }), ["Отель", "Номер", "Вместимость"], .. _roomRepository .ReadRooms() - .Select(x => new string[] { x.HotelId.ToString(), x.Name, + .Select(x => new string[] { x.HotelId.ToString(), x.RoomName, x.Capacity.ToString() }), ]; } diff --git a/ProjectHotel/ProjectHotel/Repositories/Implementations/PlacingAthleteRepository.cs b/ProjectHotel/ProjectHotel/Repositories/Implementations/PlacingAthleteRepository.cs index 1867f25..21c8e23 100644 --- a/ProjectHotel/ProjectHotel/Repositories/Implementations/PlacingAthleteRepository.cs +++ b/ProjectHotel/ProjectHotel/Repositories/Implementations/PlacingAthleteRepository.cs @@ -1,4 +1,5 @@ using Dapper; +using DocumentFormat.OpenXml.Drawing.Spreadsheet; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Npgsql; @@ -53,7 +54,7 @@ internal class PlacingAthleteRepository : IPlacingAthleteRepository transaction.Commit(); } - catch (Exception ex) + catch (Exception ex) { _logger.LogError(ex, "Ошибка при добавлении объекта"); throw; @@ -86,18 +87,48 @@ internal class PlacingAthleteRepository : IPlacingAthleteRepository { _logger.LogInformation("Получение всех объектов"); + + try { + var builder = new QueryBuilder(); + if (placing.HasValue) + { + builder.AddCondition("pa.PlacingDate = @PlacingDate"); + } + + + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = @"SELECT pa.*,apa.* + var querySelect = @"SELECT pa.*, apa.*, r.Name AS RoomName FROM PlacingAthlete pa - INNER JOIN AthletePlacingAthlete apa ON apa.PlacingAthleteId = pa.Id - "; - var placingAthletes = connection.Query(querySelect); - _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(placingAthletes)); - return placingAthletes.GroupBy(x => x.Id, y => y, (key, value) => - PlacingAthlete.CreateOpeartion(value.First(), value.Select(z => - AthletePlacingAthlete.CreateElement(0, z.AthleteId, z.LengthOfStay)))).ToList(); + INNER JOIN AthletePlacingAthlete apa ON apa.PlacingAthleteId = pa.Id + LEFT JOIN Rooms r ON r.Id = pa.RoomId + {builder.Build()};"; + //var placingAthletes = connection.Query(querySelect); + var placingAthletesDict = new Dictionary>(); + + var placingAthlete = connection.Query(querySelect, (placingAthlete, athletePlacingAthlete) => + { + if (!placingAthletesDict.TryGetValue(placingAthlete.Id, out var pa)) + { + pa = []; + placingAthletesDict.Add(placingAthlete.Id, pa); + } + + pa.Add(athletePlacingAthlete); + return placingAthlete; + }, splitOn: "RoomId", param: new + { + placing + }); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(placingAthlete)); + return placingAthletesDict.Select(x => + { + var c = placingAthlete.First(y => y.Id == x.Key); + c.SetAthletePlacingAthlete(x.Value); + return c; + }).ToArray(); } catch (Exception ex) { diff --git a/ProjectHotel/ProjectHotel/Repositories/Implementations/QueryBuilder.cs b/ProjectHotel/ProjectHotel/Repositories/Implementations/QueryBuilder.cs new file mode 100644 index 0000000..4e6f952 --- /dev/null +++ b/ProjectHotel/ProjectHotel/Repositories/Implementations/QueryBuilder.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectHotel.Repositories.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}"; + } +}