using HospitalContracts.BindingModels;
using HospitalContracts.ViewModels;
using HospitalDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalDatabaseImplement.Models
{
///
/// Сущность "Пациент"
///
public class Patient : IPatientModel
{
///
/// Идентификатор
///
public int Id { get; private set; }
///
/// ФИО пациента
///
[Required]
[MaxLength(100)]
public string FullName { get; private set; } = string.Empty;
///
/// Дата рождения пациента
///
[Required]
public DateTime BirthDate { get; private set; }
///
/// Номер телефона пациента
///
[Required]
[MaxLength(16)]
public string Phone { get; private set; } = string.Empty;
///
/// Идентификатор лечащего врача
///
[ForeignKey("DoctorId")]
public int DoctorId { get; private set; }
///
/// Лечащий врач
///
public virtual Doctor Doctor { get; private set; } = new();
///
/// Связь с таблицей связи для сущностей "Пациент" и "Рецепт"
///
[ForeignKey("PatientId")]
public virtual List Recipes { get; set; } = new();
///
/// Список рецептов пациента
///
private Dictionary? _patientRecipes = null;
///
/// Список рецептов пациента
///
[NotMapped]
public Dictionary PatientRecipes
{
get
{
if (_patientRecipes == null)
{
_patientRecipes = Recipes
.ToDictionary(recPR => recPR.RecipeId, recPR => (recPR.Recipe as IRecipeModel));
}
return _patientRecipes;
}
}
///
/// Связь с таблицей связи для сущностей "Пациент" и "Процедура"
///
[ForeignKey("PatientId")]
public virtual List Procedures { get; set; } = new();
///
/// Список процедур пациента
///
private Dictionary? _patientProcedures = null;
///
/// Список процедур пациента
///
[NotMapped]
public Dictionary PatientProcedures
{
get
{
if (_patientProcedures == null)
{
_patientProcedures = Procedures
.ToDictionary(recPP => recPP.ProcedureId, recPP => (recPP.Procedure as IProcedureModel));
}
return _patientProcedures;
}
}
///
/// Cоздать сущность
///
///
///
///
public static Patient? Create(HospitalDatabase context, PatientBindingModel model)
{
if (model == null)
{
return null;
}
return new Patient()
{
Id = model.Id,
FullName = model.FullName,
BirthDate = model.BirthDate,
Phone = model.Phone,
DoctorId = model.DoctorId,
Doctor = context.Doctors
.First(x => x.Id == model.DoctorId),
Recipes = model.PatientRecipes.Select(x => new PatientRecipe
{
Recipe = context.Recipes.First(y => y.Id == x.Key)
}).ToList(),
Procedures = model.PatientProcedures.Select(x => new PatientProcedure
{
Procedure = context.Procedures.First(y => y.Id == x.Key)
}).ToList()
};
}
///
/// Изменить сущность
///
///
public void Update(PatientBindingModel model)
{
if (model == null)
{
return;
}
FullName = model.FullName;
BirthDate = model.BirthDate;
Phone = model.Phone;
}
///
/// Получить модель представления
///
public PatientViewModel GetViewModel => new()
{
Id = Id,
FullName = FullName,
BirthDate = BirthDate,
Phone = Phone,
DoctorId = DoctorId,
DoctorFullName = Doctor.FullName,
PatientRecipes = PatientRecipes,
PatientProcedures = PatientProcedures
};
///
/// Обновить связи с рецептами
///
///
///
public void UpdateRecipes(HospitalDatabase context, PatientBindingModel model)
{
var patientRecipes = context.PatientRecipes.Where(rec => rec.PatientId == model.Id).ToList();
if (patientRecipes != null && patientRecipes.Count > 0)
{
// Удаление рецептов, не относящихся к пациенту
context.PatientRecipes.RemoveRange(patientRecipes.Where(rec => !model.PatientRecipes.ContainsKey(rec.RecipeId)));
context.SaveChanges();
}
var patient = context.Patients.First(x => x.Id == Id);
foreach (var pr in model.PatientRecipes)
{
if (patientRecipes!.Any(x => x.RecipeId == pr.Key))
{
continue;
}
context.PatientRecipes.Add(new PatientRecipe
{
Patient = patient,
Recipe = context.Recipes.First(x => x.Id == pr.Key)
});
context.SaveChanges();
}
_patientRecipes = null;
}
///
/// Обновить связи с процедурами
///
///
///
public void UpdateProcedures(HospitalDatabase context, PatientBindingModel model)
{
var patientProcedures = context.PatientProcedures.Where(rec => rec.PatientId == model.Id).ToList();
if (patientProcedures != null && patientProcedures.Count > 0)
{
// Удаление процедур, не относящихся к пациенту
context.PatientProcedures.RemoveRange(patientProcedures.Where(rec => !model.PatientProcedures.ContainsKey(rec.ProcedureId)));
context.SaveChanges();
}
var patient = context.Patients.First(x => x.Id == Id);
foreach (var pp in model.PatientProcedures)
{
if (patientProcedures!.Any(x => x.ProcedureId == pp.Key))
{
continue;
}
context.PatientProcedures.Add(new PatientProcedure
{
Patient = patient,
Procedure = context.Procedures.First(x => x.Id == pp.Key)
});
context.SaveChanges();
}
_patientProcedures = null;
}
}
}