151 lines
4.8 KiB
C#
151 lines
4.8 KiB
C#
|
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 Recipe : IRecipeModel
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Идентификатор
|
|||
|
/// </summary>
|
|||
|
public int Id { get; private set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Дата выписки рецепта
|
|||
|
/// </summary>
|
|||
|
[Required]
|
|||
|
public DateTime IssueDate { get; private set; } = DateTime.Now;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Идентификатор доктора
|
|||
|
/// </summary>
|
|||
|
[ForeignKey("DoctorId")]
|
|||
|
public int DoctorId { get; private set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Сущность "Доктор"
|
|||
|
/// </summary>
|
|||
|
public virtual Doctor Doctor { get; private set; } = new();
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Связь с таблицей связи для сущностей "Рецепт" и "Лекарство"
|
|||
|
/// </summary>
|
|||
|
[ForeignKey("RecipeId")]
|
|||
|
public virtual List<RecipeMedicine> Medicines { get; set; } = new();
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Список лекарств в рецепте
|
|||
|
/// </summary>
|
|||
|
private Dictionary<int, IMedicineModel>? _recipeMedicines = null;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Список лекарств в рецепте
|
|||
|
/// </summary>
|
|||
|
[NotMapped]
|
|||
|
public Dictionary<int, IMedicineModel> RecipeMedicines
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
if (_recipeMedicines == null)
|
|||
|
{
|
|||
|
_recipeMedicines = Medicines
|
|||
|
.ToDictionary(recRM => recRM.MedicineId, recRM => (recRM.Medicine as IMedicineModel));
|
|||
|
}
|
|||
|
return _recipeMedicines;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Cоздать сущность
|
|||
|
/// </summary>
|
|||
|
/// <param name="context"></param>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static Recipe? Create(HospitalDatabase context, RecipeBindingModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
return new Recipe()
|
|||
|
{
|
|||
|
Id = model.Id,
|
|||
|
IssueDate = model.IssueDate,
|
|||
|
DoctorId = model.DoctorId,
|
|||
|
Doctor = context.Doctors
|
|||
|
.First(x => x.Id == model.DoctorId),
|
|||
|
Medicines = model.RecipeMedicines.Select(x => new RecipeMedicine
|
|||
|
{
|
|||
|
Medicine = context.Medicines.First(y => y.Id == x.Key)
|
|||
|
}).ToList()
|
|||
|
};
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Изменить сущность
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
public void Update(RecipeBindingModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
IssueDate = model.IssueDate;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Получить модель представления
|
|||
|
/// </summary>
|
|||
|
public RecipeViewModel GetViewModel => new()
|
|||
|
{
|
|||
|
Id = Id,
|
|||
|
IssueDate = IssueDate,
|
|||
|
DoctorId = DoctorId,
|
|||
|
DoctorFullName = Doctor.FullName,
|
|||
|
RecipeMedicines = RecipeMedicines
|
|||
|
};
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Обновить связи с лекарствами
|
|||
|
/// </summary>
|
|||
|
/// <param name="context"></param>
|
|||
|
/// <param name="model"></param>
|
|||
|
public void UpdateMedicines(HospitalDatabase context, RecipeBindingModel model)
|
|||
|
{
|
|||
|
var recipeMedicines = context.RecipeMedicines.Where(rec => rec.RecipeId == model.Id).ToList();
|
|||
|
if (recipeMedicines != null && recipeMedicines.Count > 0)
|
|||
|
{
|
|||
|
// Удаление лекарств, не относящихся к рецепту
|
|||
|
context.RecipeMedicines.RemoveRange(recipeMedicines.Where(rec => !model.RecipeMedicines.ContainsKey(rec.MedicineId)));
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
|
|||
|
var recipe = context.Recipes.First(x => x.Id == Id);
|
|||
|
foreach (var rm in model.RecipeMedicines)
|
|||
|
{
|
|||
|
context.RecipeMedicines.Add(new RecipeMedicine
|
|||
|
{
|
|||
|
Recipe = recipe,
|
|||
|
Medicine = context.Medicines.First(x => x.Id == rm.Key)
|
|||
|
});
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
_recipeMedicines = null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|