From 754c59426d4e7f7ad5db5e250cd9cf5f3a0af410 Mon Sep 17 00:00:00 2001 From: Kirill <39030726+Kirill3455@users.noreply.github.com> Date: Mon, 23 Dec 2024 02:44:35 +0400 Subject: [PATCH] 4 --- Project/Project/Entities/Decree.cs | 15 ++++- Project/Project/Entities/Statement.cs | 36 +++++++++--- Project/Project/Entities/StatementStudent.cs | 1 + Project/Project/Entities/Student.cs | 12 +++- Project/Project/Entities/Subject.cs | 5 ++ Project/Project/Entities/Teacher.cs | 10 +++- .../Project/Entities/TempStatementStudent.cs | 18 ------ Project/Project/Forms/DecreesForm.cs | 6 +- Project/Project/Forms/StatementListForm.cs | 8 ++- Project/Project/Forms/StudentsListForm.cs | 6 +- Project/Project/Forms/SubjectsForm.cs | 7 ++- Project/Project/Forms/TeachersListForm.cs | 8 ++- Project/Project/Reports/ChartReport.cs | 7 +-- Project/Project/Reports/TableReport.cs | 8 +-- .../Project/Repositories/IDecreeRepository.cs | 3 +- .../Repositories/IStatementRepository.cs | 3 +- .../Implementations/DecreeRepository.cs | 25 +++++++- .../Implementations/QueryBuilder.cs | 33 +++++++++++ .../Implementations/StatementRepository.cs | 57 +++++++++++++++++-- 19 files changed, 209 insertions(+), 59 deletions(-) delete mode 100644 Project/Project/Entities/TempStatementStudent.cs create mode 100644 Project/Project/Repositories/Implementations/QueryBuilder.cs diff --git a/Project/Project/Entities/Decree.cs b/Project/Project/Entities/Decree.cs index 6b20fae..64f81a5 100644 --- a/Project/Project/Entities/Decree.cs +++ b/Project/Project/Entities/Decree.cs @@ -1,11 +1,24 @@ -namespace Project.Entities; +using System.ComponentModel; + +namespace Project.Entities; public class Decree { public int Id { get; private set; } + + [Browsable(false)] public int StudentId { get; private set; } + + [DisplayName("Студент")] + public string StudentName { get; private set; } = string.Empty; + + [DisplayName("Название")] public string Name { get; private set; } = string.Empty; + + [DisplayName("Дата")] public DateTime Date { get; private set; } + + [DisplayName("Описание")] public string Info { get; private set; } = string.Empty; public static Decree CreateOperation(int id, int studentId, string name, DateTime date, string info) diff --git a/Project/Project/Entities/Statement.cs b/Project/Project/Entities/Statement.cs index 864a78d..fbb19ff 100644 --- a/Project/Project/Entities/Statement.cs +++ b/Project/Project/Entities/Statement.cs @@ -1,11 +1,33 @@ -namespace Project.Entities; +using Project.Entities.Enums; +using System.ComponentModel; + +namespace Project.Entities; public class Statement { public int Id { get; private set; } + + [Browsable(false)] public int TeacherId { get; private set; } + + [Browsable(false)] public int SubjectId { get; private set; } + + [DisplayName("Преподаватель")] + public string TeacherName{ get; private set; } = string.Empty; + + [DisplayName("Предмет")] + public string SubjectName { get; private set; } = string.Empty; + + [DisplayName("Средний балл")] public DateTime Date { get; private set; } + + [DisplayName("Результаты")] + public string StatementStudent => StatementStudents != null ? + string.Join(", ", StatementStudents.Select(x => $"{x.StudentName}:{(int)x.Mark}")) : + string.Empty; + + [Browsable(false)] public IEnumerable StatementStudents { get; private set; } = []; public static Statement CreateOperation(int id, int teacherId, int subjectId, DateTime date, IEnumerable statementStudents) @@ -20,15 +42,11 @@ public class Statement }; } - public static Statement CreateOperation(TempStatementStudent tempStatementStudent, IEnumerable statementStudents) + public void SetStatementStudents(IEnumerable statementStudents) { - return new Statement + if (statementStudents != null && statementStudents.Any()) { - Id = tempStatementStudent.Id, - TeacherId = tempStatementStudent.TeacherId, - SubjectId = tempStatementStudent.SubjectId, - Date = tempStatementStudent.Date, - StatementStudents = statementStudents - }; + StatementStudents = statementStudents; + } } } diff --git a/Project/Project/Entities/StatementStudent.cs b/Project/Project/Entities/StatementStudent.cs index b2ddbc8..9c62d11 100644 --- a/Project/Project/Entities/StatementStudent.cs +++ b/Project/Project/Entities/StatementStudent.cs @@ -6,6 +6,7 @@ public class StatementStudent { public int Id { get; private set; } public int StudentId { get; private set; } + public string StudentName { get; private set; } = string.Empty; public Mark Mark { get; private set; } public static StatementStudent CreateOperation(int id, int studentId, Mark mark) diff --git a/Project/Project/Entities/Student.cs b/Project/Project/Entities/Student.cs index f3e8de6..7886b4d 100644 --- a/Project/Project/Entities/Student.cs +++ b/Project/Project/Entities/Student.cs @@ -1,11 +1,21 @@ -namespace Project.Entities; +using System.ComponentModel; + +namespace Project.Entities; public class Student { public int Id { get; private set; } + + [DisplayName("Имя")] public string FirstName { get; private set; } = string.Empty; + + [DisplayName("Фамилия")] public string LastName { get; private set; } = string.Empty; + + [DisplayName("Средний балл")] public float Score { get; private set; } + + [Browsable(false)] public string DisplayName => $"{FirstName} {LastName}"; public static Student CreateOperation(int id, string firstName, string lastName, float score) diff --git a/Project/Project/Entities/Subject.cs b/Project/Project/Entities/Subject.cs index 0092b3f..377d089 100644 --- a/Project/Project/Entities/Subject.cs +++ b/Project/Project/Entities/Subject.cs @@ -1,11 +1,16 @@ using Project.Entities.Enums; +using System.ComponentModel; namespace Project.Entities; public class Subject { public int Id { get; private set; } + + [DisplayName("Название")] public string Name { get; private set; } = string.Empty; + + [DisplayName("Курсы")] public Course Courses { get; private set; } public static Subject CreateOperation(int id, string name, Course courses) diff --git a/Project/Project/Entities/Teacher.cs b/Project/Project/Entities/Teacher.cs index e9ada10..c27aa7f 100644 --- a/Project/Project/Entities/Teacher.cs +++ b/Project/Project/Entities/Teacher.cs @@ -1,10 +1,18 @@ -namespace Project.Entities; +using System.ComponentModel; + +namespace Project.Entities; public class Teacher { public int Id { get; private set; } + + [DisplayName("Имя")] public string FirstName { get; private set; } = string.Empty; + + [DisplayName("Фамилия")] public string LastName { get; private set; } = string.Empty; + + [Browsable(false)] public string DisplayName => $"{LastName} {FirstName}"; public static Teacher CreateOperation(int id, string firstName, string lastName) diff --git a/Project/Project/Entities/TempStatementStudent.cs b/Project/Project/Entities/TempStatementStudent.cs deleted file mode 100644 index 63e185d..0000000 --- a/Project/Project/Entities/TempStatementStudent.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Project.Entities.Enums; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Project.Entities; - -public class TempStatementStudent -{ - public int Id { get; private set; } - public int TeacherId { get; private set; } - public int SubjectId { get; private set; } - public DateTime Date { get; private set; } - public int StudentId { get; private set; } - public Mark Mark { get; private set; } -} diff --git a/Project/Project/Forms/DecreesForm.cs b/Project/Project/Forms/DecreesForm.cs index b6e08ad..80f8c3f 100644 --- a/Project/Project/Forms/DecreesForm.cs +++ b/Project/Project/Forms/DecreesForm.cs @@ -42,5 +42,9 @@ public partial class DecreesForm : Form } } - private void LoadList() => dataGridView.DataSource = _decreeRepository.GetAllDecrees(); + private void LoadList() + { + dataGridView.DataSource = _decreeRepository.GetAllDecrees(); + dataGridView.Columns["Id"].Visible = false; + } } diff --git a/Project/Project/Forms/StatementListForm.cs b/Project/Project/Forms/StatementListForm.cs index e76e56e..fbaa12a 100644 --- a/Project/Project/Forms/StatementListForm.cs +++ b/Project/Project/Forms/StatementListForm.cs @@ -1,4 +1,5 @@ using Project.Repositories; +using System.Windows.Forms; using Unity; namespace Project.Forms; @@ -28,8 +29,11 @@ public partial class StatementListForm : Form } } - private void LoadList() => dataGridViewData.DataSource = _statementRepository.GetAllStatements(); - + private void LoadList() + { + dataGridViewData.DataSource = _statementRepository.GetAllStatements(); + dataGridViewData.Columns["Id"].Visible = false; + } private void StatementListForm_Load(object sender, EventArgs e) { try diff --git a/Project/Project/Forms/StudentsListForm.cs b/Project/Project/Forms/StudentsListForm.cs index 4635dfd..7229989 100644 --- a/Project/Project/Forms/StudentsListForm.cs +++ b/Project/Project/Forms/StudentsListForm.cs @@ -83,8 +83,12 @@ public partial class StudentsListForm : Form } } - private void LoadList() => dataGridViewStudents.DataSource = _studentRepository.GetAllStudents(); + private void LoadList() + { + dataGridViewStudents.DataSource = _studentRepository.GetAllStudents(); + dataGridViewStudents.Columns["Id"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { id = 0; diff --git a/Project/Project/Forms/SubjectsForm.cs b/Project/Project/Forms/SubjectsForm.cs index cb6fc38..e7ad55b 100644 --- a/Project/Project/Forms/SubjectsForm.cs +++ b/Project/Project/Forms/SubjectsForm.cs @@ -70,8 +70,11 @@ public partial class SubjectsForm : Form } } - private void LoadList() => dataGridView.DataSource = _subjectRepository.GetAllSubjects(); - + private void LoadList() + { + dataGridView.DataSource = _subjectRepository.GetAllSubjects(); + dataGridView.Columns["Id"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { id = 0; diff --git a/Project/Project/Forms/TeachersListForm.cs b/Project/Project/Forms/TeachersListForm.cs index af840be..5ed28d2 100644 --- a/Project/Project/Forms/TeachersListForm.cs +++ b/Project/Project/Forms/TeachersListForm.cs @@ -1,4 +1,5 @@ using Project.Repositories; +using System.Windows.Forms; using Unity; namespace Project.Forms; @@ -81,8 +82,11 @@ public partial class TeachersListForm : Form } } - private void LoadList() => dataGridViewTeachers.DataSource = _teacherRepository.GetAllTeachers(); - + private void LoadList() + { + dataGridViewTeachers.DataSource = _teacherRepository.GetAllTeachers(); + dataGridViewTeachers.Columns["Id"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) { id = 0; diff --git a/Project/Project/Reports/ChartReport.cs b/Project/Project/Reports/ChartReport.cs index c517c52..683b95f 100644 --- a/Project/Project/Reports/ChartReport.cs +++ b/Project/Project/Reports/ChartReport.cs @@ -39,16 +39,13 @@ internal class ChartReport private List<(string Caption, double Value)> GetData(DateTime dateTime) { - // Получаем все предметы var subjects = _subjectRepository .GetAllSubjects() .ToDictionary(s => s.Id, s => s.Name); - // Группируем оценки по предметам var groupedData = _statementRepository - .GetAllStatements() - .Where(s => s.Date.Date == dateTime.Date) // Фильтруем по дате - .GroupBy(s => s.SubjectId) // Группируем по ID предмета + .GetAllStatements(dateFrom: dateTime.Date, dateTo: dateTime.Date.AddDays(1)) + .GroupBy(s => s.SubjectId) .Select(g => ( Caption: subjects.ContainsKey(g.Key) ? subjects[g.Key] : "Неизвестный предмет", diff --git a/Project/Project/Reports/TableReport.cs b/Project/Project/Reports/TableReport.cs index ba1482b..2e0c615 100644 --- a/Project/Project/Reports/TableReport.cs +++ b/Project/Project/Reports/TableReport.cs @@ -34,7 +34,7 @@ internal class TableReport { new ExcelBuilder(filePath) .AddHeader("Сводка по студенту", 0, 3) - .AddParagraph("за период", 0) + .AddParagraph($"за период {startDate:dd.MM.yyyy} по {endDate:dd.MM.yyyy}", 0) .AddTable([10, 15, 15], GetData(studentId, startDate, endDate)) .Build(); return true; @@ -51,8 +51,7 @@ internal class TableReport var result = new List() { item }; // Получаем оценки студента за период - var statements = _statementRepository.GetAllStatements() - .Where(s => s.StatementStudents.Any(ss => ss.StudentId == studentId) && s.Date >= startDate && s.Date <= endDate); + var statements = _statementRepository.GetAllStatements(startDate, endDate, studentId); foreach (var statement in statements) { @@ -71,8 +70,7 @@ internal class TableReport } // Получаем приказы студента за период - var decrees = _decreeRepository.GetAllDecrees() - .Where(d => d.StudentId == studentId && d.Date >= startDate && d.Date <= endDate); + var decrees = _decreeRepository.GetAllDecrees(startDate, endDate, studentId); foreach (var decree in decrees) { diff --git a/Project/Project/Repositories/IDecreeRepository.cs b/Project/Project/Repositories/IDecreeRepository.cs index 4a3071f..9cc5cd9 100644 --- a/Project/Project/Repositories/IDecreeRepository.cs +++ b/Project/Project/Repositories/IDecreeRepository.cs @@ -4,6 +4,7 @@ namespace Project.Repositories; public interface IDecreeRepository { - IEnumerable GetAllDecrees(); + IEnumerable GetAllDecrees(DateTime? dateFrom = null, DateTime? dateTo = null, + int? studentId = null); void AddDecree(Decree decree); } \ No newline at end of file diff --git a/Project/Project/Repositories/IStatementRepository.cs b/Project/Project/Repositories/IStatementRepository.cs index ac4cc50..89a9d08 100644 --- a/Project/Project/Repositories/IStatementRepository.cs +++ b/Project/Project/Repositories/IStatementRepository.cs @@ -5,5 +5,6 @@ namespace Project.Repositories; public interface IStatementRepository { void CreateStatement(Statement statement); - IEnumerable GetAllStatements(); + IEnumerable GetAllStatements(DateTime? dateFrom = null, DateTime? dateTo = null, + int? studentId = null); } diff --git a/Project/Project/Repositories/Implementations/DecreeRepository.cs b/Project/Project/Repositories/Implementations/DecreeRepository.cs index 065e678..97bf7d8 100644 --- a/Project/Project/Repositories/Implementations/DecreeRepository.cs +++ b/Project/Project/Repositories/Implementations/DecreeRepository.cs @@ -37,14 +37,33 @@ public class DecreeRepository : IDecreeRepository } } - public IEnumerable GetAllDecrees() + public IEnumerable GetAllDecrees(DateTime? dateFrom = null, DateTime? dateTo = null, + int? studentId = null) { _logger.LogInformation("Получение всех объектов"); try { + var builder = new QueryBuilder(); + if (dateFrom.HasValue) + { + builder.AddCondition("de.Date >= @dateFrom"); + } + if (dateTo.HasValue) + { + builder.AddCondition("de.Date <= @dateTo"); + } + if (studentId.HasValue) + { + builder.AddCondition("de.StudentId = @studentId"); + } using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = "SELECT * FROM Decree"; - var decrees = connection.Query(querySelect); + var querySelect = $@"SELECT + de.*, + CONCAT(st.FirstName, ' ', st.LastName) as StudentName + FROM Decree de + LEFT JOIN Student st on st.Id = de.StudentId + {builder.Build()}"; + var decrees = connection.Query(querySelect, new { dateFrom, dateTo, studentId }); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(decrees)); return decrees; diff --git a/Project/Project/Repositories/Implementations/QueryBuilder.cs b/Project/Project/Repositories/Implementations/QueryBuilder.cs new file mode 100644 index 0000000..f0acc32 --- /dev/null +++ b/Project/Project/Repositories/Implementations/QueryBuilder.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Project.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}"; + } +} diff --git a/Project/Project/Repositories/Implementations/StatementRepository.cs b/Project/Project/Repositories/Implementations/StatementRepository.cs index 284492b..807219d 100644 --- a/Project/Project/Repositories/Implementations/StatementRepository.cs +++ b/Project/Project/Repositories/Implementations/StatementRepository.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Npgsql; using Project.Entities; +using static System.Runtime.InteropServices.JavaScript.JSType; namespace Project.Repositories.Implementations; @@ -55,18 +56,62 @@ public class StatementRepository : IStatementRepository } } - public IEnumerable GetAllStatements() + public IEnumerable GetAllStatements(DateTime? dateFrom = null, DateTime? dateTo = null, + int? studentId = null) { _logger.LogInformation("Получение всех объектов"); try { + var builder = new QueryBuilder(); + if (dateFrom.HasValue) + { + builder.AddCondition("st.Date >= @dateFrom"); + } + if (dateTo.HasValue) + { + builder.AddCondition("st.Date <= @dateTo"); + } + if (studentId.HasValue) + { + builder.AddCondition("stt.StudentId = @studentId"); + } using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = @"SELECT st.*, stt.StudentId, stt.Mark FROM Statement st - INNER JOIN StatementStudent stt on stt.StatementId = st.Id"; - var statements = connection.Query(querySelect); + var querySelect = $@"SELECT + st.*, + CONCAT(te.FirstName, ' ', te.LastName) as TeacherName, + sb.Name as SubjectName, + stt.StudentId, + stt.Mark, + CONCAT(stu.FirstName, ' ', stu.LastName) as StudentName + FROM Statement st + LEFT JOIN Teacher te on te.Id = st.TeacherId + LEFT JOIN Subject sb on sb.Id = st.SubjectId + INNER JOIN StatementStudent stt on stt.StatementId = st.Id + LEFT JOIN Student stu on stu.Id = stt.StudentId + {builder.Build()}"; + var statementsDict = new Dictionary>(); + + var statementStudents = connection.Query(querySelect, + (statement, statementStudent) => + { + if (!statementsDict.TryGetValue(statement.Id, out var stt)) + { + stt = []; + statementsDict.Add(statement.Id, stt); + } + + stt.Add(statementStudent); + return statement; + }, splitOn: "StudentId", param: new { dateFrom, dateTo, studentId }); _logger.LogDebug("Полученные объекты: {json}", - JsonConvert.SerializeObject(statements)); - return statements.GroupBy(x => x.Id, y => y, (key, value) => Statement.CreateOperation(value.First(), value.Select(z => StatementStudent.CreateOperation(0, z.StudentId, z.Mark)))).ToList(); + JsonConvert.SerializeObject(statementStudents)); + + return statementsDict.Select(x => + { + var st = statementStudents.First(y => y.Id == x.Key); + st.SetStatementStudents(x.Value); + return st; + }).ToArray(); } catch (Exception ex) { -- 2.25.1