using DocumentFormat.OpenXml.Office2010.Excel;
using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.ViewModels;
using HospitalDataModels.Models;
using Microsoft.AspNetCore.Mvc;
using System.Numerics;
namespace HospitalWebApp.Controllers
{
///
/// Контроллер для сущности "Пациент"
///
public class PatientController : Controller
{
///
/// Логгер
///
private readonly ILogger _logger;
///
/// Бизнес-логика для сущности "Пациент"
///
private readonly IPatientLogic _patientLogic;
///
/// Бизнес-логика для сущности "Рецепт"
///
private readonly IRecipeLogic _recipeLogic;
///
/// Бизнес-логика для сущности "Процедура"
///
private readonly IProcedureLogic _procedureLogic;
///
/// Конструктор
///
///
///
///
///
public PatientController(ILogger logger, IPatientLogic patientLogic, IRecipeLogic recipeLogic, IProcedureLogic procedureLogic)
{
_logger = logger;
_patientLogic = patientLogic;
_recipeLogic = recipeLogic;
_procedureLogic = procedureLogic;
}
///
/// Вывести список пациентов
///
///
[HttpGet]
public IActionResult Patients()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
return View(_patientLogic.ReadList(new PatientSearchModel
{
DoctorId = APIClient.Doctor.Id,
}));
}
///
/// Создать пациента
///
///
[HttpGet]
public IActionResult CreatePatient()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Procedures = _procedureLogic.ReadList(null);
return View();
}
///
/// Создать пациента
///
///
///
///
///
///
[HttpPost]
public void CreatePatient(string fullname, DateTime birthdate, string phone, List procedures)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(fullname) || birthdate == DateTime.MinValue || string.IsNullOrEmpty(phone) || procedures == null)
{
throw new Exception("Введены не все данные!");
}
Dictionary patientProcedures = new Dictionary();
foreach (var procedureId in procedures)
{
patientProcedures.Add(procedureId, _procedureLogic.ReadElement(new ProcedureSearchModel { Id = procedureId })!);
}
_patientLogic.Create(new PatientBindingModel
{
FullName = fullname,
BirthDate = birthdate,
Phone = phone,
DoctorId = APIClient.Doctor.Id,
PatientProcedures = patientProcedures
});
Response.Redirect("/Patient/Patients");
}
///
/// Редактировать пациента
///
///
[HttpGet]
public IActionResult UpdatePatient(int id)
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Procedures = _procedureLogic.ReadList(null);
return View(_patientLogic.ReadElement(new PatientSearchModel
{
Id = id
}));
}
///
/// Редактировать пациента
///
///
///
///
///
///
[HttpPost]
public void UpdatePatient(int id, string fullname, DateTime birthdate, string phone, List procedures)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(fullname) || birthdate == DateTime.MinValue || string.IsNullOrEmpty(phone) || procedures == null)
{
throw new Exception("Введены не все данные!");
}
Dictionary patientProcedures = new Dictionary();
foreach (var procedureId in procedures)
{
patientProcedures.Add(procedureId, _procedureLogic.ReadElement(new ProcedureSearchModel { Id = procedureId })!);
}
var patient = _patientLogic.ReadElement(new PatientSearchModel { Id = id });
_patientLogic.Update(new PatientBindingModel
{
Id = id,
FullName = fullname,
BirthDate = birthdate,
Phone = phone,
DoctorId = APIClient.Doctor.Id,
PatientRecipes = patient!.PatientRecipes,
PatientProcedures = patientProcedures
});
Response.Redirect("/Patient/Patients");
}
///
/// Удалить пациента
///
///
[HttpPost]
public void DeletePatient(int id)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
_patientLogic.Delete(new PatientBindingModel
{
Id = id
});
Response.Redirect("/Patient/Patients");
}
///
/// Выписать рецепт пациенту
///
///
///
[HttpGet]
public IActionResult CreatePatientRecipe()
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
ViewBag.Patients = _patientLogic.ReadList(new PatientSearchModel
{
DoctorId = APIClient.Doctor.Id
});
ViewBag.Recipes = _recipeLogic.ReadList(new RecipeSearchModel
{
DoctorId = APIClient.Doctor.Id
});
return View();
}
///
/// Выписать рецепт пациенту
///
///
///
///
[HttpPost]
public void CreatePatientRecipe(int patientId, List recipes)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (patientId <= 0 || recipes == null)
{
throw new Exception("Введены не все данные!");
}
Dictionary patientRecipes = new Dictionary();
foreach (var recipeId in recipes)
{
patientRecipes.Add(recipeId, _recipeLogic.ReadElement(new RecipeSearchModel { Id = recipeId })!);
}
var patient = _patientLogic.ReadElement(new PatientSearchModel { Id = patientId });
_patientLogic.Update(new PatientBindingModel
{
Id = patient!.Id,
FullName = patient!.FullName,
BirthDate = patient!.BirthDate,
Phone = patient!.Phone,
DoctorId = APIClient.Doctor.Id,
PatientProcedures = patient!.PatientProcedures,
PatientRecipes = patientRecipes
});
Response.Redirect("/Patient/Patients");
}
///
/// Получить список рецептов пациента
///
///
///
///
[HttpGet]
public JsonResult GetPatientRecipes(int patientId)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
var allRecipes = _recipeLogic.ReadList(new RecipeSearchModel
{
DoctorId = APIClient.Doctor.Id
});
var patient = _patientLogic.ReadElement(new PatientSearchModel { Id = patientId });
var patientRecipeIds = patient?.PatientRecipes?.Select(x => x.Key).ToList() ?? new List();
var result = new
{
allRecipes = allRecipes.Select(r => new { id = r.Id, issueDate = r.IssueDate.ToShortDateString() }),
patientRecipeIds = patientRecipeIds
};
return Json(result);
}
}
}