150 lines
4.8 KiB
C#
150 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 Procedure : IProcedureModel
|
||
{
|
||
/// <summary>
|
||
/// Идентификатор
|
||
/// </summary>
|
||
public int Id { get; private set; }
|
||
|
||
/// <summary>
|
||
/// Название процедуры
|
||
/// </summary>
|
||
[Required]
|
||
[MaxLength(30)]
|
||
public string Name { get; private set; } = string.Empty;
|
||
|
||
/// <summary>
|
||
/// Описание процедуры
|
||
/// </summary>
|
||
[MaxLength(100)]
|
||
public string? Description { get; private set; }
|
||
|
||
/// <summary>
|
||
/// Связь с таблицей связи для сущностей "Процедура" и "Лекарство"
|
||
/// </summary>
|
||
[ForeignKey("ProcedureId")]
|
||
public virtual List<ProcedureMedicine> Medicines { get; set; } = new();
|
||
|
||
/// <summary>
|
||
/// Список лекарств для процедуры
|
||
/// </summary>
|
||
private Dictionary<int, IMedicineModel>? _procedureMedicines = null;
|
||
|
||
/// <summary>
|
||
/// Список лекарств для процедуры
|
||
/// </summary>
|
||
[NotMapped]
|
||
public Dictionary<int, IMedicineModel> ProcedureMedicines
|
||
{
|
||
get
|
||
{
|
||
if (_procedureMedicines == null)
|
||
{
|
||
_procedureMedicines = Medicines
|
||
.ToDictionary(recPM => recPM.MedicineId, recPM => (recPM.Medicine as IMedicineModel));
|
||
}
|
||
return _procedureMedicines;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Создать сущность
|
||
/// </summary>
|
||
/// <param name="context"></param>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
public static Procedure? Create(HospitalDatabase context, ProcedureBindingModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
return new Procedure()
|
||
{
|
||
Id = model.Id,
|
||
Name = model.Name,
|
||
Description = model.Description,
|
||
Medicines = model.ProcedureMedicines.Select(x => new ProcedureMedicine
|
||
{
|
||
Medicine = context.Medicines.First(y => y.Id == x.Key)
|
||
}).ToList()
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// Изменить сущность
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
public void Update(ProcedureBindingModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
Name = model.Name;
|
||
Description = model.Description;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Получить модель представления
|
||
/// </summary>
|
||
public ProcedureViewModel GetViewModel => new()
|
||
{
|
||
Id = Id,
|
||
Name = Name,
|
||
Description = Description,
|
||
ProcedureMedicines = ProcedureMedicines
|
||
};
|
||
|
||
/// <summary>
|
||
/// Обновить связи с лекарствами
|
||
/// </summary>
|
||
/// <param name="context"></param>
|
||
/// <param name="model"></param>
|
||
public void UpdateMedicines(HospitalDatabase context, ProcedureBindingModel model)
|
||
{
|
||
var procedureMedicines = context.ProcedureMedicines.Where(rec => rec.ProcedureId == model.Id).ToList();
|
||
if (procedureMedicines != null && procedureMedicines.Count > 0)
|
||
{
|
||
// Удаление лекарств, не относящихся к процедуре
|
||
context.ProcedureMedicines.RemoveRange(procedureMedicines.Where(rec => !model.ProcedureMedicines.ContainsKey(rec.MedicineId)));
|
||
context.SaveChanges();
|
||
}
|
||
|
||
var procedure = context.Procedures.First(x => x.Id == Id);
|
||
foreach (var pm in model.ProcedureMedicines)
|
||
{
|
||
if (procedureMedicines!.Any(x => x.MedicineId == pm.Key))
|
||
{
|
||
continue;
|
||
}
|
||
|
||
context.ProcedureMedicines.Add(new ProcedureMedicine
|
||
{
|
||
Procedure = procedure,
|
||
Medicine = context.Medicines.First(x => x.Id == pm.Key)
|
||
});
|
||
context.SaveChanges();
|
||
}
|
||
_procedureMedicines = null;
|
||
}
|
||
}
|
||
}
|