181 lines
6.7 KiB
C#
181 lines
6.7 KiB
C#
using MedicalView.Doctors;
|
||
using MedicalView.Specializations;
|
||
using MedicalView.Diagnoses;
|
||
using MedicalView.Patients;
|
||
using Microsoft.Extensions.Logging;
|
||
using MedicalDatabaseContracts.Models;
|
||
using MedicalDatabaseContracts.ViewModels;
|
||
using MedicalDatabaseContracts.SearchModels;
|
||
using MedicalDatabaseContracts;
|
||
|
||
namespace MedicalView.Visits
|
||
{
|
||
public partial class FormMain : Form
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly ILogic<Visit, VisitViewModel, VisitSearchModel> _visitLogic;
|
||
public FormMain(ILogger<FormMain> logger, ILogic<Visit, VisitViewModel, VisitSearchModel> visitLogic)
|
||
{
|
||
InitializeComponent();
|
||
dataGridView.CellDoubleClick += buttonEdit_Click;
|
||
dataGridView.CellContentDoubleClick += buttonEdit_Click;
|
||
_logger = logger;
|
||
_visitLogic = visitLogic;
|
||
}
|
||
|
||
private void FormMain_Load(object sender, EventArgs e)
|
||
{
|
||
LoadData();
|
||
}
|
||
|
||
private void DiagnosesToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
var service = Program.ServiceProvider?.GetService(typeof(FormDiagnoses));
|
||
if (service is FormDiagnoses form)
|
||
{
|
||
form.ShowDialog();
|
||
}
|
||
}
|
||
|
||
private void SpecializationsToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
var service = Program.ServiceProvider?.GetService(typeof(FormSpecializations));
|
||
if (service is FormSpecializations form)
|
||
{
|
||
form.ShowDialog();
|
||
}
|
||
}
|
||
|
||
private void DoctorsToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
var service = Program.ServiceProvider?.GetService(typeof(FormDoctors));
|
||
if (service is FormDoctors form)
|
||
{
|
||
form.ShowDialog();
|
||
}
|
||
}
|
||
|
||
private void PatientsToolStripMenuItem1_Click(object sender, EventArgs e)
|
||
{
|
||
var service = Program.ServiceProvider?.GetService(typeof(FormPatients));
|
||
if (service is FormPatients form)
|
||
{
|
||
form.ShowDialog();
|
||
}
|
||
}
|
||
|
||
protected virtual void LoadData()
|
||
{
|
||
SetStatusStripText("Загрузка списка...");
|
||
try
|
||
{
|
||
double elapsed;
|
||
var items = _visitLogic.ReadList(out elapsed);
|
||
|
||
dataGridView.DataSource = items;
|
||
foreach (DataGridViewTextBoxColumn column in dataGridView.Columns)
|
||
{
|
||
if (column.Name.Contains("Id"))
|
||
{
|
||
column.Visible = false;
|
||
}
|
||
}
|
||
dataGridView.ReadOnly = true;
|
||
|
||
_logger.LogInformation("Список загружен успешно");
|
||
SetStatusStripText($"Готово. Загружено записей: {items.Count}, время загрузки {elapsed} мск");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
string errorMessage = "Ошибка загрузки списка";
|
||
_logger.LogError(string.Join(", ", errorMessage, $"\"{ex.Message}\""));
|
||
MessageBox.Show(string.Join("\n\n", errorMessage, $"{ex.Message}"), "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
SetStatusStripText($"Ошибка загрузки");
|
||
}
|
||
}
|
||
protected virtual void SetStatusStripText(string text)
|
||
{
|
||
toolStripStatusLabel.Text = text;
|
||
}
|
||
|
||
private void buttonAdd_Click(object sender, EventArgs e)
|
||
{
|
||
var service = Program.ServiceProvider?.GetService(typeof(FormVisit));
|
||
if (service is FormVisit form)
|
||
{
|
||
if (form.ShowDialog() == DialogResult.OK)
|
||
{
|
||
LoadData();
|
||
}
|
||
}
|
||
}
|
||
|
||
private void buttonEdit_Click(object sender, EventArgs e)
|
||
{
|
||
if (dataGridView.SelectedRows.Count == 1)
|
||
{
|
||
var service = Program.ServiceProvider?.GetService(typeof(FormVisit));
|
||
if (service is FormVisit form)
|
||
{
|
||
form.ModelId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
|
||
if (form.ShowDialog() == DialogResult.OK)
|
||
{
|
||
LoadData();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private void buttonDelete_Click(object sender, EventArgs e)
|
||
{
|
||
if (dataGridView.SelectedRows.Count == 1)
|
||
{
|
||
if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
||
{
|
||
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
|
||
_logger.LogInformation("Удаление элемента");
|
||
try
|
||
{
|
||
if (!_visitLogic.Delete(id))
|
||
{
|
||
throw new Exception("Ошибка при удалении. Дополнительная информация в логах.");
|
||
}
|
||
LoadData();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Ошибка удаления элемента");
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private void buttonGenerateRandomData_Click(object sender, EventArgs e)
|
||
{
|
||
if (MessageBox.Show("Сшенерировать случайные данные?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
||
{
|
||
var service = Program.ServiceProvider?.GetService(typeof(FormVisit));
|
||
if (service is FormVisit form)
|
||
{
|
||
form.GenerateRandomDatabaseData();
|
||
LoadData();
|
||
}
|
||
}
|
||
}
|
||
|
||
private void buttonDeleteAllData_Click(object sender, EventArgs e)
|
||
{
|
||
if (MessageBox.Show("Удалить ВСЕ данные?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
||
{
|
||
var service = Program.ServiceProvider?.GetService(typeof(FormVisit));
|
||
if (service is FormVisit form)
|
||
{
|
||
form.DeleteAllDatabaseData();
|
||
LoadData();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|