248 lines
10 KiB
C#
Raw Normal View History

using MedicalDatabaseContracts;
using MedicalDatabaseContracts.Models;
using MedicalDatabaseContracts.SearchModels;
using MedicalDatabaseContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System.Windows.Forms;
namespace MedicalView.Visits
{
public partial class FormVisit : FormAbstractDetail
{
private readonly ILogger _logger;
private readonly ILogic<Visit, VisitViewModel, VisitSearchModel> _visitLogic;
private readonly ILogic<Diagnose, DiagnoseViewModel, DiagnoseSearchModel> _diagnoseLogic;
private readonly ILogic<Doctor, DoctorViewModel, DoctorSearchModel> _doctorLogic;
private readonly ILogic<Patient, PatientViewModel, PatientSearchModel> _patientLogic;
private readonly ILogic<Specialization, SpecializationViewModel, SpecializationSearchModel> _specLogic;
public FormVisit(
ILogger<FormVisit> logger,
ILogic<Visit, VisitViewModel, VisitSearchModel> visitLogic,
ILogic<Diagnose, DiagnoseViewModel, DiagnoseSearchModel> diagnoseLogic,
ILogic<Doctor, DoctorViewModel, DoctorSearchModel> doctorLogic,
ILogic<Patient, PatientViewModel, PatientSearchModel> patientLogic,
ILogic<Specialization, SpecializationViewModel, SpecializationSearchModel> specLogic)
{
InitializeComponent();
_logger = logger;
_visitLogic = visitLogic;
_diagnoseLogic = diagnoseLogic;
_doctorLogic = doctorLogic;
_patientLogic = patientLogic;
_specLogic = specLogic;
}
private void ApplyToolStripMenuItem_Click(object sender, EventArgs e)
{
if (comboBoxPatient.SelectedItem == null)
{
MessageBox.Show("Выберите пациента", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else if (comboBoxDoctor.SelectedItem == null)
{
MessageBox.Show("Выберите врача", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Сохранение модели приема");
try
{
var model = new Visit
{
Id = ModelId ?? 0,
Comment = textBoxComment.Text,
PatientId = (comboBoxPatient.SelectedItem as PatientViewModel).Id,
DoctorId = (comboBoxDoctor.SelectedItem as DoctorViewModel).Id,
DiagnoseId = comboBoxDiagnose.SelectedItem != null ? (comboBoxDiagnose.SelectedItem as DiagnoseViewModel).Id : null,
Date = DateOnly.FromDateTime(datePicker.Value),
Time = TimeOnly.FromDateTime(timePicker.Value)
};
var operationResult = ModelId.HasValue ? _visitLogic.Update(model) : _visitLogic.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 FormVisit_Load(object sender, EventArgs e)
{
var patients = _patientLogic.ReadList();
var doctors = _doctorLogic.ReadList();
var diagnoses = _diagnoseLogic.ReadList();
comboBoxPatient.DataSource = patients;
comboBoxPatient.SelectedIndex = -1;
comboBoxDoctor.DataSource = doctors;
comboBoxDoctor.SelectedIndex = -1;
comboBoxDiagnose.DataSource = diagnoses;
comboBoxDiagnose.SelectedIndex = -1;
if (ModelId.HasValue)
{
try
{
_logger.LogInformation("Получение модели приема");
var view = _visitLogic.ReadElement(ModelId.Value);
if (view != null)
{
textBoxComment.Text = view.Comment;
datePicker.Value = view.Date.ToDateTime(view.Time);
timePicker.Value = view.Date.ToDateTime(view.Time);
if (view.DiagnoseId != null)
{
comboBoxDiagnose.SelectedIndex = diagnoses.FindIndex(x => x.Id == view.DiagnoseId);
}
comboBoxDoctor.SelectedIndex = doctors.FindIndex(x => x.Id == view.DoctorId);
comboBoxPatient.SelectedIndex = patients.FindIndex(x => x.Id == view.PatientId);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения модели приема");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
public void DeleteAllDatabaseData()
{
try
{
_logger.LogInformation("Удаление всех данных БД");
_visitLogic.DeleteAll();
_diagnoseLogic.DeleteAll();
_doctorLogic.DeleteAll();
_specLogic.DeleteAll();
_patientLogic.DeleteAll();
MessageBox.Show("Все данные успешно удалены", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения модели приема");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public void GenerateRandomDatabaseData()
{
int specsCount = 100;
int diagnosesCount = 100;
int doctorsCount = 100;
int patientsCount = 100;
int visitsCount = 100;
double avgInsertSpecsTimer = 0;
double avgInsertDiagnosesTimer = 0;
double avgInsertDoctorsTimer = 0;
double avgInsertPatientsTimer = 0;
double avgInsertVisitsTimer = 0;
double temp = 0;
Random rnd = new();
for (int i = 0; i < specsCount; i++)
{
_specLogic.Create(new Specialization
{
Name = i.ToString(),
IsPediatric = Convert.ToBoolean(rnd.Next(2)),
IsTherapeutic = Convert.ToBoolean(rnd.Next(2))
}, out temp);
avgInsertSpecsTimer += temp;
}
avgInsertSpecsTimer /= specsCount;
var specs = _specLogic.ReadList();
for (int i = 0; i < diagnosesCount; i++)
{
_diagnoseLogic.Create(new Diagnose
{
Name = i.ToString()
}, out temp);
avgInsertDiagnosesTimer += temp;
}
avgInsertDiagnosesTimer /= diagnosesCount;
var diagnoses = _diagnoseLogic.ReadList();
for (int i = 0; i < doctorsCount; i++)
{
_doctorLogic.Create(new Doctor {
Name = i.ToString(),
Surname = i.ToString(),
Patronymic = i.ToString(),
PhoneNumber = i.ToString(),
SpecializationId = specs[rnd.Next(specs.Count)].Id
}, out temp);
avgInsertDoctorsTimer += temp;
}
avgInsertDoctorsTimer /= doctorsCount;
var doctors = _doctorLogic.ReadList();
for (int i = 0; i < patientsCount; i++)
{
_patientLogic.Create(new Patient {
Name = i.ToString(),
Surname = i.ToString(),
Patronymic = i.ToString(),
PhoneNumber = i.ToString(),
Birthday = new DateTime(2021, 12, 21),
Gender = rnd.Next(2) == 0 ? "М" : "Ж",
Height = rnd.Next(100, 250),
Weight = rnd.Next(50, 100)
}, out temp);
avgInsertPatientsTimer += temp;
}
avgInsertPatientsTimer /= patientsCount;
var patients = _patientLogic.ReadList();
for (int i = 0; i < visitsCount; i++)
{
_visitLogic.Create(new Visit {
Date = new DateOnly(2021, 12, 12),
Time = new TimeOnly(12, 34),
PatientId = patients[rnd.Next(patients.Count)].Id,
DoctorId = doctors[rnd.Next(doctors.Count)].Id,
DiagnoseId = diagnoses[rnd.Next(diagnoses.Count)].Id,
Comment = "iusnefiuesnfes yf ey fiuewfwe fhweiufiuefiuweh fwehf uewyiufwe fuewoiuf oweyiuf weuif ioewfuyewuif yweufoiwefuywef"
}, out temp);
avgInsertVisitsTimer += temp;
}
avgInsertVisitsTimer /= visitsCount;
MessageBox.Show(
"Данные успешно сгенерированы!\n\n" +
"Среднее время вставки:\n" +
$"Специализации: {specsCount} шт : {avgInsertSpecsTimer} мск\n" +
$"Диагнозы: {diagnosesCount} шт : {avgInsertDiagnosesTimer} мск\n" +
$"Врачи: {doctorsCount} шт : {avgInsertDoctorsTimer} мск\n" +
$"Пациенты: {patientsCount} шт : {avgInsertPatientsTimer} мск\n" +
$"Приемы: {visitsCount} шт : {avgInsertVisitsTimer} мск\n",
"Успех",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
}