From 2becaa20452e14558b459afd3c505ffd38c04cf7 Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Thu, 16 May 2024 00:33:36 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A7=D0=B5=D1=82=D0=BE=20=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B0=D0=B5=D1=82,=20=D1=87=D0=B5=D1=82=D0=BE=20?= =?UTF-8?q?=D0=BD=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/AbstractLogic.cs | 10 +- .../BusinessLogics/DiagnoseLogic.cs | 4 +- .../BusinessLogics/DoctorLogic.cs | 2 +- .../BusinessLogics/PatientLogic.cs | 2 +- .../BusinessLogics/SpecializationLogic.cs | 4 +- .../BusinessLogics/VisitLogic.cs | 2 +- Medical/MedicalDatabaseContracts/ILogic.cs | 4 +- .../ViewModels/SpecializationViewModel.cs | 4 + .../Doctors/FormDoctor.Designer.cs | 3 + Medical/MedicalView/Doctors/FormDoctor.cs | 102 +++++++++++++++++- Medical/MedicalView/FormAbstractList.cs | 2 +- Medical/MedicalView/FormMain.cs | 2 +- .../FormSpecialization.Designer.cs | 3 + .../Specializations/FormSpecialization.cs | 75 ++++++++++++- 14 files changed, 199 insertions(+), 20 deletions(-) diff --git a/Medical/MedicalBusinessLogic/BusinessLogics/AbstractLogic.cs b/Medical/MedicalBusinessLogic/BusinessLogics/AbstractLogic.cs index d5d2947..af00003 100644 --- a/Medical/MedicalBusinessLogic/BusinessLogics/AbstractLogic.cs +++ b/Medical/MedicalBusinessLogic/BusinessLogics/AbstractLogic.cs @@ -20,7 +20,7 @@ namespace MedicalBusinessLogic.BusinessLogics _storage = storage; } - protected abstract void CheckModelIsValid(M model); + protected abstract void CheckModelIsValid(M model, bool modelUpdate = false); protected abstract bool CheckModelBySearchModel(M model, S searchModel); @@ -41,9 +41,9 @@ namespace MedicalBusinessLogic.BusinessLogics return ReadElement(id, out _); } - public List ReadList(S? searchModel) + public List ReadList(S? searchModel = null) { - return ReadList(searchModel, out _); + return ReadList(out _, searchModel); } public bool Update(M model) @@ -51,7 +51,7 @@ namespace MedicalBusinessLogic.BusinessLogics return Update(model, out _); } - public virtual List ReadList(S? searchModel, out long elapsedMilliseconds) + public virtual List ReadList(out long elapsedMilliseconds, S? searchModel = null) { var elements = _storage.GetAll(out elapsedMilliseconds); @@ -98,7 +98,7 @@ namespace MedicalBusinessLogic.BusinessLogics elapsedMilliseconds = 0; try { - CheckModelIsValid(model); + CheckModelIsValid(model, true); _storage.Update(model, out elapsedMilliseconds); _logger.LogInformation($"Update record id:{model.Id} operation success"); return true; diff --git a/Medical/MedicalBusinessLogic/BusinessLogics/DiagnoseLogic.cs b/Medical/MedicalBusinessLogic/BusinessLogics/DiagnoseLogic.cs index c9ab261..8e10140 100644 --- a/Medical/MedicalBusinessLogic/BusinessLogics/DiagnoseLogic.cs +++ b/Medical/MedicalBusinessLogic/BusinessLogics/DiagnoseLogic.cs @@ -21,7 +21,7 @@ namespace MedicalBusinessLogic.BusinessLogics return true; } - protected override void CheckModelIsValid(Diagnose model) + protected override void CheckModelIsValid(Diagnose model, bool modelUpdate = false) { if (model == null) { @@ -31,7 +31,7 @@ namespace MedicalBusinessLogic.BusinessLogics { throw new ArgumentNullException(nameof(model.Name)); } - else if (ReadList(new DiagnoseSearchModel { Name = model.Name }).Count != 0) + if (!modelUpdate && ReadList(new DiagnoseSearchModel { Name = model.Name }).Count != 0) { throw new InvalidOperationException($"Диагноз с таким названием уже есть: \"{model.Name}\""); } diff --git a/Medical/MedicalBusinessLogic/BusinessLogics/DoctorLogic.cs b/Medical/MedicalBusinessLogic/BusinessLogics/DoctorLogic.cs index c032a24..342c135 100644 --- a/Medical/MedicalBusinessLogic/BusinessLogics/DoctorLogic.cs +++ b/Medical/MedicalBusinessLogic/BusinessLogics/DoctorLogic.cs @@ -32,7 +32,7 @@ namespace MedicalBusinessLogic.BusinessLogics return true; } - protected override void CheckModelIsValid(Doctor model) + protected override void CheckModelIsValid(Doctor model, bool modelUpdate = false) { if (string.IsNullOrEmpty(model.Name)) { diff --git a/Medical/MedicalBusinessLogic/BusinessLogics/PatientLogic.cs b/Medical/MedicalBusinessLogic/BusinessLogics/PatientLogic.cs index f60475e..ea5d7f4 100644 --- a/Medical/MedicalBusinessLogic/BusinessLogics/PatientLogic.cs +++ b/Medical/MedicalBusinessLogic/BusinessLogics/PatientLogic.cs @@ -25,7 +25,7 @@ namespace MedicalBusinessLogic.BusinessLogics return true; } - protected override void CheckModelIsValid(Patient model) + protected override void CheckModelIsValid(Patient model, bool modelUpdate = false) { if (string.IsNullOrEmpty(model.Name)) { diff --git a/Medical/MedicalBusinessLogic/BusinessLogics/SpecializationLogic.cs b/Medical/MedicalBusinessLogic/BusinessLogics/SpecializationLogic.cs index 6129c96..f570b50 100644 --- a/Medical/MedicalBusinessLogic/BusinessLogics/SpecializationLogic.cs +++ b/Medical/MedicalBusinessLogic/BusinessLogics/SpecializationLogic.cs @@ -25,7 +25,7 @@ namespace MedicalBusinessLogic.BusinessLogics return true; } - protected override void CheckModelIsValid(Specialization model) + protected override void CheckModelIsValid(Specialization model, bool modelUpdate = false) { if (model == null) { @@ -35,7 +35,7 @@ namespace MedicalBusinessLogic.BusinessLogics { throw new ArgumentNullException(nameof(model.Name)); } - else if (ReadList(new SpecializationSearchModel { Name = model.Name }).Count != 0) + if (!modelUpdate && ReadList(new SpecializationSearchModel { Name = model.Name }).Count != 0) { throw new InvalidOperationException($"Специальность с таким названием уже есть: \"{model.Name}\""); } diff --git a/Medical/MedicalBusinessLogic/BusinessLogics/VisitLogic.cs b/Medical/MedicalBusinessLogic/BusinessLogics/VisitLogic.cs index fe78c79..910b83f 100644 --- a/Medical/MedicalBusinessLogic/BusinessLogics/VisitLogic.cs +++ b/Medical/MedicalBusinessLogic/BusinessLogics/VisitLogic.cs @@ -47,7 +47,7 @@ namespace MedicalBusinessLogic.BusinessLogics return true; } - protected override void CheckModelIsValid(Visit model) + protected override void CheckModelIsValid(Visit model, bool modelUpdate = false) { if (model == null) { diff --git a/Medical/MedicalDatabaseContracts/ILogic.cs b/Medical/MedicalDatabaseContracts/ILogic.cs index 2b9f298..8a53c1a 100644 --- a/Medical/MedicalDatabaseContracts/ILogic.cs +++ b/Medical/MedicalDatabaseContracts/ILogic.cs @@ -9,8 +9,8 @@ namespace MedicalDatabaseContracts where V : AbstractViewModel where S : AbstractSearchModel { - List ReadList(S? searchModel); - List ReadList(S? searchModel, out long elapsedMilliseconds); + List ReadList(S? searchModel = null); + List ReadList(out long elapsedMilliseconds, S? searchModel = null); V? ReadElement(int id); V? ReadElement(int id, out long elapsedMilliseconds); bool Create(M model); diff --git a/Medical/MedicalDatabaseContracts/ViewModels/SpecializationViewModel.cs b/Medical/MedicalDatabaseContracts/ViewModels/SpecializationViewModel.cs index c0d1259..366c4d1 100644 --- a/Medical/MedicalDatabaseContracts/ViewModels/SpecializationViewModel.cs +++ b/Medical/MedicalDatabaseContracts/ViewModels/SpecializationViewModel.cs @@ -10,5 +10,9 @@ namespace MedicalDatabaseContracts.ViewModels public bool IsPediatric { get; set; } [DisplayName("Терапевт")] public bool IsTherapeutic { get; set; } + public override string ToString() + { + return Name; + } } } diff --git a/Medical/MedicalView/Doctors/FormDoctor.Designer.cs b/Medical/MedicalView/Doctors/FormDoctor.Designer.cs index 3934840..6154a5c 100644 --- a/Medical/MedicalView/Doctors/FormDoctor.Designer.cs +++ b/Medical/MedicalView/Doctors/FormDoctor.Designer.cs @@ -145,12 +145,14 @@ ApplyToolStripMenuItem.Name = "ApplyToolStripMenuItem"; ApplyToolStripMenuItem.Size = new Size(97, 24); ApplyToolStripMenuItem.Text = "Сохранить"; + ApplyToolStripMenuItem.Click += ApplyToolStripMenuItem_Click; // // CancelToolStripMenuItem // CancelToolStripMenuItem.Name = "CancelToolStripMenuItem"; CancelToolStripMenuItem.Size = new Size(76, 24); CancelToolStripMenuItem.Text = "Отмена"; + CancelToolStripMenuItem.Click += CancelToolStripMenuItem_Click; // // FormDoctor // @@ -172,6 +174,7 @@ MinimumSize = new Size(0, 255); Name = "FormDoctor"; Text = "Редактировать врача"; + Load += FormDoctor_Load; menuStrip.ResumeLayout(false); menuStrip.PerformLayout(); ResumeLayout(false); diff --git a/Medical/MedicalView/Doctors/FormDoctor.cs b/Medical/MedicalView/Doctors/FormDoctor.cs index 5b08cc4..26b3537 100644 --- a/Medical/MedicalView/Doctors/FormDoctor.cs +++ b/Medical/MedicalView/Doctors/FormDoctor.cs @@ -1,10 +1,108 @@ -namespace MedicalView.Doctors +using MedicalDatabaseContracts; +using MedicalDatabaseContracts.Models; +using MedicalDatabaseContracts.SearchModels; +using MedicalDatabaseContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace MedicalView.Doctors { public partial class FormDoctor : FormAbstractDetail { - public FormDoctor() + private readonly ILogger _logger; + private readonly ILogic _doctorLogic; + private readonly ILogic _specializationLogic; + public FormDoctor( + ILogger logger, + ILogic logic, + ILogic specializationLogic) { InitializeComponent(); + _logger = logger; + _doctorLogic = logic; + _specializationLogic = specializationLogic; + } + + private void ApplyToolStripMenuItem_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxName.Text)) + { + MessageBox.Show("Заполните имя", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + else if (string.IsNullOrEmpty(textBoxSurname.Text)) + { + MessageBox.Show("Заполните фамилию", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + else if (string.IsNullOrEmpty(textBoxPhoneNumber.Text)) + { + MessageBox.Show("Заполните номер телефона", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + else if (comboBoxSpecialization.SelectedIndex == -1 || comboBoxSpecialization.SelectedItem == null) + { + MessageBox.Show("Выберите специальность", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Сохранение модели врача"); + try + { + var model = new Doctor + { + Id = ModelId ?? 0, + Name = textBoxName.Text, + Surname = textBoxSurname.Text, + Patronymic = textBoxPatronymic.Text, + PhoneNumber = textBoxPhoneNumber.Text, + SpecializationId = (comboBoxSpecialization.SelectedItem as SpecializationViewModel).Id + }; + var operationResult = ModelId.HasValue ? _doctorLogic.Update(model) : _doctorLogic.Create(model); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка сохранения специальности"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void CancelToolStripMenuItem_Click(object sender, EventArgs e) + { + Cancel(); + } + + private void FormDoctor_Load(object sender, EventArgs e) + { + if (ModelId.HasValue) + { + try + { + _logger.LogInformation("Получение модели врача"); + var view = _doctorLogic.ReadElement(ModelId.Value); + if (view != null) + { + textBoxName.Text = view.Name; + textBoxSurname.Text = view.Surname; + textBoxPatronymic.Text = view.Patronymic; + textBoxPhoneNumber.Text = view.PhoneNumber; + + var specs = _specializationLogic.ReadList(); + comboBoxSpecialization.DataSource = specs; + comboBoxSpecialization.SelectedIndex = specs.FindIndex(x => x.Id == view.SpecializationId); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения модели врача"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } } } } diff --git a/Medical/MedicalView/FormAbstractList.cs b/Medical/MedicalView/FormAbstractList.cs index 64a986c..33cb96d 100644 --- a/Medical/MedicalView/FormAbstractList.cs +++ b/Medical/MedicalView/FormAbstractList.cs @@ -92,7 +92,7 @@ namespace MedicalView try { long elapsed; - var items = _logic.ReadList(null, out elapsed); + var items = _logic.ReadList(out elapsed); dataGridView.DataSource = items; dataGridView.Columns["Id"].DisplayIndex = 0; diff --git a/Medical/MedicalView/FormMain.cs b/Medical/MedicalView/FormMain.cs index 4277217..ee98d82 100644 --- a/Medical/MedicalView/FormMain.cs +++ b/Medical/MedicalView/FormMain.cs @@ -68,7 +68,7 @@ namespace MedicalView.Visits try { long elapsed; - var items = _visitLogic.ReadList(null, out elapsed); + var items = _visitLogic.ReadList(out elapsed); dataGridView.DataSource = items; foreach (DataGridViewTextBoxColumn column in dataGridView.Columns) diff --git a/Medical/MedicalView/Specializations/FormSpecialization.Designer.cs b/Medical/MedicalView/Specializations/FormSpecialization.Designer.cs index 3fd35d3..3e5f682 100644 --- a/Medical/MedicalView/Specializations/FormSpecialization.Designer.cs +++ b/Medical/MedicalView/Specializations/FormSpecialization.Designer.cs @@ -53,12 +53,14 @@ ApplyToolStripMenuItem.Name = "ApplyToolStripMenuItem"; ApplyToolStripMenuItem.Size = new Size(97, 24); ApplyToolStripMenuItem.Text = "Сохранить"; + ApplyToolStripMenuItem.Click += ApplyToolStripMenuItem_Click; // // CancelToolStripMenuItem // CancelToolStripMenuItem.Name = "CancelToolStripMenuItem"; CancelToolStripMenuItem.Size = new Size(76, 24); CancelToolStripMenuItem.Text = "Отмена"; + CancelToolStripMenuItem.Click += CancelToolStripMenuItem_Click; // // textBoxName // @@ -110,6 +112,7 @@ MinimumSize = new Size(0, 155); Name = "FormSpecialization"; Text = "Редактировать специализацию"; + Load += FormSpecialization_Load; menuStrip.ResumeLayout(false); menuStrip.PerformLayout(); ResumeLayout(false); diff --git a/Medical/MedicalView/Specializations/FormSpecialization.cs b/Medical/MedicalView/Specializations/FormSpecialization.cs index cdb7ced..4512f0f 100644 --- a/Medical/MedicalView/Specializations/FormSpecialization.cs +++ b/Medical/MedicalView/Specializations/FormSpecialization.cs @@ -1,10 +1,81 @@ -namespace MedicalView.Specializations +using MedicalDatabaseContracts; +using MedicalDatabaseContracts.Models; +using MedicalDatabaseContracts.SearchModels; +using MedicalDatabaseContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace MedicalView.Specializations { public partial class FormSpecialization : FormAbstractDetail { - public FormSpecialization() + private readonly ILogger _logger; + private readonly ILogic _logic; + public FormSpecialization(ILogger logger, ILogic logic) { InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void ApplyToolStripMenuItem_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxName.Text)) + { + MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Сохранение специальности"); + try + { + var model = new Specialization + { + Id = ModelId ?? 0, + Name = textBoxName.Text, + IsPediatric = checkBoxIsPediatric.Checked, + IsTherapeutic = checkBoxIsTherapeutic.Checked + }; + var operationResult = ModelId.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) + { + _logger.LogError(ex, "Ошибка сохранения специальности"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void CancelToolStripMenuItem_Click(object sender, EventArgs e) + { + Cancel(); + } + + private void FormSpecialization_Load(object sender, EventArgs e) + { + if (ModelId.HasValue) + { + try + { + _logger.LogInformation("Получение диагноза"); + var view = _logic.ReadElement(ModelId.Value); + if (view != null) + { + textBoxName.Text = view.Name; + checkBoxIsPediatric.Checked = view.IsPediatric; + checkBoxIsTherapeutic.Checked = view.IsTherapeutic; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения специальности"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } } } }