2024-05-06 20:26:16 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using StudentEnrollmentContracts.BindingModels;
|
|
|
|
|
using StudentEnrollmentContracts.BusinessLogicContracts;
|
|
|
|
|
using StudentEnrollmentContracts.SearchModels;
|
|
|
|
|
using StudentEnrollmentDataModels.Models;
|
|
|
|
|
|
|
|
|
|
namespace StudentEnrollmentView
|
|
|
|
|
{
|
|
|
|
|
public partial class FormStudent : Form
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IStudentLogic _logic;
|
2024-05-07 23:34:50 +04:00
|
|
|
|
private readonly IExamPointsLogic _examPointsLogic;
|
2024-05-06 20:26:16 +04:00
|
|
|
|
private int? _id;
|
|
|
|
|
private Dictionary<int, ICourseModel> _studentCourses;
|
|
|
|
|
private IExamPointsModel _examPoints;
|
|
|
|
|
public int Id { set { _id = value; } }
|
2024-05-07 23:34:50 +04:00
|
|
|
|
public FormStudent(ILogger<FormStudent> logger, IStudentLogic logic, IExamPointsLogic examPointsLogic)
|
2024-05-06 20:26:16 +04:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
dataGridView.AllowUserToAddRows = false;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_logic = logic;
|
2024-05-07 23:34:50 +04:00
|
|
|
|
_examPointsLogic = examPointsLogic;
|
2024-05-06 20:26:16 +04:00
|
|
|
|
_studentCourses = new Dictionary<int, ICourseModel>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FormStudent_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (_id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Загрузка студента");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var view = _logic.ReadElement(new StudentSearchModel
|
|
|
|
|
{
|
|
|
|
|
student_id = _id.Value
|
|
|
|
|
});
|
|
|
|
|
if (view != null)
|
|
|
|
|
{
|
|
|
|
|
textBoxLastName.Text = view.LastName;
|
|
|
|
|
textBoxFirstName.Text = view.FirstName;
|
|
|
|
|
textBoxMiddleName.Text = view.MiddleName;
|
|
|
|
|
textBoxEmail.Text = view.Email;
|
|
|
|
|
textBoxTIN.Text = view.TIN.ToString();
|
|
|
|
|
_studentCourses = view.StudentCourse ?? new Dictionary<int, ICourseModel>();
|
2024-05-07 23:34:50 +04:00
|
|
|
|
var test = _examPointsLogic.ReadElement(new ExamPointsSearchModel
|
|
|
|
|
{
|
|
|
|
|
exampoints_id = view.ExamPointsId,
|
|
|
|
|
});
|
|
|
|
|
_examPoints = test;
|
2024-05-06 20:26:16 +04:00
|
|
|
|
LoadData();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка загрузки студента");
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private void LoadData()
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Загрузка направлений студента");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_studentCourses != null)
|
|
|
|
|
{
|
|
|
|
|
dataGridView.Rows.Clear();
|
|
|
|
|
foreach (var pc in _studentCourses)
|
|
|
|
|
{
|
2024-05-07 23:34:50 +04:00
|
|
|
|
dataGridView.Rows.Add(new object[] { pc.Key, pc.Value.name });
|
2024-05-06 20:26:16 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка загрузки направлений студента");
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private void buttonAdd_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var service = Program.ServiceProvider?.GetService(typeof(FormStudentCourse));
|
|
|
|
|
if (service is FormStudentCourse form)
|
|
|
|
|
{
|
|
|
|
|
if (form.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
if (form.CourseModel == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-05-07 23:34:50 +04:00
|
|
|
|
_logger.LogInformation("Добавление нового направления:{ CourseName}", form.CourseModel.name);
|
2024-05-06 20:26:16 +04:00
|
|
|
|
if (_studentCourses.ContainsKey(form.Id))
|
|
|
|
|
{
|
|
|
|
|
_studentCourses[form.Id] = form.CourseModel;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_studentCourses.Add(form.Id, form.CourseModel);
|
|
|
|
|
}
|
|
|
|
|
LoadData();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonUpd_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (dataGridView.SelectedRows.Count == 1)
|
|
|
|
|
{
|
|
|
|
|
var service = Program.ServiceProvider?.GetService(typeof(FormStudentCourse));
|
|
|
|
|
if (service is FormStudentCourse form)
|
|
|
|
|
{
|
|
|
|
|
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value);
|
|
|
|
|
form.Id = id;
|
|
|
|
|
if (form.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
if (form.CourseModel == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-05-07 23:34:50 +04:00
|
|
|
|
_logger.LogInformation("Изменение направления:{ CourseName }", form.CourseModel.name);
|
2024-05-06 20:26:16 +04:00
|
|
|
|
_studentCourses[form.Id] = form.CourseModel;
|
|
|
|
|
LoadData();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonDel_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (dataGridView.SelectedRows.Count == 1)
|
|
|
|
|
{
|
|
|
|
|
if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Удаление направления:{ CourseName}", dataGridView.SelectedRows[0].Cells[1].Value);
|
|
|
|
|
_studentCourses?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
LoadData();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonRef_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
LoadData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonSave_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(textBoxFirstName.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Заполните имя", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(textBoxLastName.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Заполните фамилию", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(textBoxEmail.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Заполните почту", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(textBoxTIN.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Заполните ИНН", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (_studentCourses.Count == 0 || _studentCourses == null)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Заполните направления", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (_examPoints == null
|
|
|
|
|
|| (_examPoints.FirstExamPoints == 0
|
|
|
|
|
&& _examPoints.SecondExamPoints == 0
|
|
|
|
|
&& _examPoints.ThirdExamPoints == 0))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Заполните баллы студента", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("Сохранение студента");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var model = new StudentBindingModel
|
|
|
|
|
{
|
|
|
|
|
Id = _id ?? 0,
|
|
|
|
|
FirstName = textBoxFirstName.Text,
|
|
|
|
|
LastName = textBoxLastName.Text,
|
|
|
|
|
MiddleName = !string.IsNullOrEmpty(textBoxMiddleName.Text) ? textBoxMiddleName.Text : string.Empty,
|
|
|
|
|
Email = textBoxEmail.Text,
|
|
|
|
|
TIN = Convert.ToInt64(textBoxTIN.Text),
|
|
|
|
|
StudentCourse = _studentCourses,
|
|
|
|
|
ExamPointsId = _examPoints.Id,
|
|
|
|
|
};
|
|
|
|
|
var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model);
|
|
|
|
|
if (!operationResult)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
|
|
|
|
|
}
|
|
|
|
|
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
|
|
|
DialogResult = DialogResult.OK;
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-05-07 23:34:50 +04:00
|
|
|
|
_logger.LogError(ex, "Ошибка сохранения студента");
|
2024-05-06 20:26:16 +04:00
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonCancel_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
DialogResult = DialogResult.Cancel;
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonAddPoints_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var service = Program.ServiceProvider?.GetService(typeof(FormExamPoints));
|
|
|
|
|
if (service is FormExamPoints form)
|
|
|
|
|
{
|
|
|
|
|
form.Id = _id ?? 0;
|
|
|
|
|
if (form.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
_examPoints = form.ExamPoints;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|