2023-04-05 11:47:15 +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;
|
2023-04-05 21:25:26 +04:00
|
|
|
|
using System.Xml.Linq;
|
2023-04-05 11:47:15 +04:00
|
|
|
|
|
|
|
|
|
namespace HospitalDatabaseImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class Procedure : IProcedureModel
|
|
|
|
|
{
|
|
|
|
|
public int Id { get; private set; }
|
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
[MaxLength(50)]
|
|
|
|
|
public string Name { get; private set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
private Dictionary<int, IMedicineModel>? _procedureMedicines = null;
|
|
|
|
|
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, IMedicineModel> ProcedureMedicines { get
|
|
|
|
|
{
|
|
|
|
|
if (_procedureMedicines == null)
|
|
|
|
|
{
|
|
|
|
|
_procedureMedicines = Medicines.ToDictionary(rec => rec.MedicineId, rec =>
|
|
|
|
|
rec.Medicine as IMedicineModel);
|
|
|
|
|
}
|
|
|
|
|
return _procedureMedicines;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ForeignKey("ProcedureId")]
|
|
|
|
|
public virtual List<ProcedureMedicine> Medicines { get; set; } = new();
|
|
|
|
|
|
|
|
|
|
[ForeignKey("ProcedureId")]
|
|
|
|
|
public virtual List<TreatmentProcedure> Treatments { get; set; } = new();
|
|
|
|
|
|
|
|
|
|
public static Procedure Create(HospitalDatabase context, ProcedureBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return new Procedure()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
Name = model.Name,
|
|
|
|
|
Medicines = model.ProcedureMedicines.Select(x => new ProcedureMedicine
|
|
|
|
|
{
|
|
|
|
|
Medicine = context.Medicines.First(y => y.Id == x.Key),
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 16:42:04 +04:00
|
|
|
|
public static Procedure? Create(XElement element)
|
2023-04-05 21:25:26 +04:00
|
|
|
|
{
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return new Procedure()
|
2023-04-07 16:42:04 +04:00
|
|
|
|
{
|
|
|
|
|
Name = element.Element("Name")!.Value,
|
|
|
|
|
// при чтении из файла мы не можем загрузить сведения о лекарствах в процедуре (Medicines),
|
|
|
|
|
// так как на данном этапе нет созданных записей о лекарствах
|
|
|
|
|
// привязка будет осуществляться позднее - на клиенте будет данная возможность на дополнительной формочке
|
|
|
|
|
// Medicines = new()
|
2023-04-05 21:25:26 +04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 11:47:15 +04:00
|
|
|
|
public void Update(ProcedureBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
Name = model.Name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ProcedureViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
2023-04-07 12:55:14 +04:00
|
|
|
|
Name = Name,
|
|
|
|
|
ProcedureMedicines = ProcedureMedicines
|
2023-04-05 11:47:15 +04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|
2023-05-17 17:47:33 +04:00
|
|
|
|
var existingMedicineIds = procedureMedicines?.Select(x => x.MedicineId).ToList();
|
2023-04-05 11:47:15 +04:00
|
|
|
|
foreach (var pm in model.ProcedureMedicines)
|
|
|
|
|
{
|
2023-05-17 17:47:33 +04:00
|
|
|
|
if (existingMedicineIds != null && !existingMedicineIds.Contains(pm.Key))
|
2023-04-05 11:47:15 +04:00
|
|
|
|
{
|
2023-05-17 17:47:33 +04:00
|
|
|
|
context.ProcedureMedicines.Add(new ProcedureMedicine
|
|
|
|
|
{
|
|
|
|
|
Procedure = procedure,
|
|
|
|
|
Medicine = context.Medicines.First(x => x.Id == pm.Key),
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-04-05 11:47:15 +04:00
|
|
|
|
}
|
2023-05-17 17:47:33 +04:00
|
|
|
|
context.SaveChanges();
|
2023-04-05 11:47:15 +04:00
|
|
|
|
_procedureMedicines = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|