PIbd-21_MasenkinMS_Coursewo.../Hospital/HospitalDatabaseImplement/Models/Patient.cs

242 lines
8.0 KiB
C#
Raw Permalink Normal View History

2024-04-25 02:00:30 +04:00
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
{
/// <summary>
/// Сущность "Пациент"
/// </summary>
public class Patient : IPatientModel
{
/// <summary>
/// Идентификатор
/// </summary>
public int Id { get; private set; }
/// <summary>
/// ФИО пациента
/// </summary>
[Required]
[MaxLength(100)]
public string FullName { get; private set; } = string.Empty;
/// <summary>
/// Дата рождения пациента
/// </summary>
[Required]
public DateTime BirthDate { get; private set; }
/// <summary>
/// Номер телефона пациента
/// </summary>
[Required]
[MaxLength(16)]
public string Phone { get; private set; } = string.Empty;
/// <summary>
/// Идентификатор лечащего врача
/// </summary>
[ForeignKey("DoctorId")]
public int DoctorId { get; private set; }
/// <summary>
/// Лечащий врач
/// </summary>
public virtual Doctor Doctor { get; private set; } = new();
/// <summary>
/// Связь с таблицей связи для сущностей "Пациент" и "Рецепт"
/// </summary>
[ForeignKey("PatientId")]
public virtual List<PatientRecipe> Recipes { get; set; } = new();
/// <summary>
/// Список рецептов пациента
/// </summary>
private Dictionary<int, IRecipeModel>? _patientRecipes = null;
/// <summary>
/// Список рецептов пациента
/// </summary>
[NotMapped]
public Dictionary<int, IRecipeModel> PatientRecipes
{
get
{
if (_patientRecipes == null)
{
_patientRecipes = Recipes
.ToDictionary(recPR => recPR.RecipeId, recPR => (recPR.Recipe as IRecipeModel));
}
return _patientRecipes;
}
}
/// <summary>
/// Связь с таблицей связи для сущностей "Пациент" и "Процедура"
/// </summary>
[ForeignKey("PatientId")]
public virtual List<PatientProcedure> Procedures { get; set; } = new();
/// <summary>
/// Список процедур пациента
/// </summary>
private Dictionary<int, IProcedureModel>? _patientProcedures = null;
/// <summary>
/// Список процедур пациента
/// </summary>
[NotMapped]
public Dictionary<int, IProcedureModel> PatientProcedures
{
get
{
if (_patientProcedures == null)
{
_patientProcedures = Procedures
.ToDictionary(recPP => recPP.ProcedureId, recPP => (recPP.Procedure as IProcedureModel));
}
return _patientProcedures;
}
}
/// <summary>
/// Cоздать сущность
/// </summary>
/// <param name="context"></param>
/// <param name="model"></param>
/// <returns></returns>
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()
};
}
/// <summary>
/// Изменить сущность
/// </summary>
/// <param name="model"></param>
public void Update(PatientBindingModel model)
{
if (model == null)
{
return;
}
FullName = model.FullName;
BirthDate = model.BirthDate;
Phone = model.Phone;
}
/// <summary>
/// Получить модель представления
/// </summary>
public PatientViewModel GetViewModel => new()
{
Id = Id,
FullName = FullName,
BirthDate = BirthDate,
Phone = Phone,
DoctorId = DoctorId,
DoctorFullName = Doctor.FullName,
PatientRecipes = PatientRecipes,
PatientProcedures = PatientProcedures
};
/// <summary>
/// Обновить связи с рецептами
/// </summary>
/// <param name="context"></param>
/// <param name="model"></param>
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
2024-04-25 02:00:30 +04:00
{
Patient = patient,
Recipe = context.Recipes.First(x => x.Id == pr.Key)
});
context.SaveChanges();
}
_patientRecipes = null;
}
/// <summary>
/// Обновить связи с процедурами
/// </summary>
/// <param name="context"></param>
/// <param name="model"></param>
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
2024-04-25 02:00:30 +04:00
{
Patient = patient,
Procedure = context.Procedures.First(x => x.Id == pp.Key)
});
context.SaveChanges();
}
_patientProcedures = null;
}
}
}