ИСЭбд-22. Хусаинов К.С. Четвертая лабораторная работа. #4
@ -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)
|
||||
|
@ -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<StatementStudent> StatementStudents { get; private set; } = [];
|
||||
|
||||
public static Statement CreateOperation(int id, int teacherId, int subjectId, DateTime date, IEnumerable<StatementStudent> statementStudents)
|
||||
@ -20,15 +42,11 @@ public class Statement
|
||||
};
|
||||
}
|
||||
|
||||
public static Statement CreateOperation(TempStatementStudent tempStatementStudent, IEnumerable<StatementStudent> statementStudents)
|
||||
public void SetStatementStudents(IEnumerable<StatementStudent> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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; }
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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] : "Неизвестный предмет",
|
||||
|
@ -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<string[]>() { 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)
|
||||
{
|
||||
|
@ -4,6 +4,7 @@ namespace Project.Repositories;
|
||||
|
||||
public interface IDecreeRepository
|
||||
{
|
||||
IEnumerable<Decree> GetAllDecrees();
|
||||
IEnumerable<Decree> GetAllDecrees(DateTime? dateFrom = null, DateTime? dateTo = null,
|
||||
int? studentId = null);
|
||||
void AddDecree(Decree decree);
|
||||
}
|
@ -5,5 +5,6 @@ namespace Project.Repositories;
|
||||
public interface IStatementRepository
|
||||
{
|
||||
void CreateStatement(Statement statement);
|
||||
IEnumerable<Statement> GetAllStatements();
|
||||
IEnumerable<Statement> GetAllStatements(DateTime? dateFrom = null, DateTime? dateTo = null,
|
||||
int? studentId = null);
|
||||
}
|
||||
|
@ -37,14 +37,33 @@ public class DecreeRepository : IDecreeRepository
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Decree> GetAllDecrees()
|
||||
public IEnumerable<Decree> 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<Decree>(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<Decree>(querySelect, new { dateFrom, dateTo, studentId });
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(decrees));
|
||||
return decrees;
|
||||
|
33
Project/Project/Repositories/Implementations/QueryBuilder.cs
Normal file
33
Project/Project/Repositories/Implementations/QueryBuilder.cs
Normal file
@ -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}";
|
||||
}
|
||||
}
|
@ -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<Statement> GetAllStatements()
|
||||
public IEnumerable<Statement> 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<TempStatementStudent>(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<int, List<StatementStudent>>();
|
||||
|
||||
var statementStudents = connection.Query<Statement, StatementStudent, Statement>(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)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user