143 lines
5.5 KiB
C#
Raw Normal View History

2023-04-05 23:24:58 +04:00
using HospitalContracts.BindingModels;
using HospitalContracts.ViewModels;
using HospitalDataModels.Models;
2023-04-07 00:53:55 +04:00
using Microsoft.EntityFrameworkCore.Query.Internal;
using Microsoft.Identity.Client;
2023-04-05 23:24:58 +04:00
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
2023-04-07 00:53:55 +04:00
using System.Dynamic;
2023-04-05 23:24:58 +04:00
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalDataBaseImplements.Models
{
public class Recipes : IRecipesModel
{
public int Id { get; private set; }
[Required]
public string Dose { get; private set; } = string.Empty;
[Required]
public DateTime Date { get; private set; } = DateTime.Now;
2023-04-07 00:53:55 +04:00
public int ClientId { get; set; }
public virtual Medicines Medicines { get; set; } = new();
2023-04-05 23:24:58 +04:00
public int MedicinesId { get; private set; }
[Required]
public string ModeOfApplication { get; private set; } = string.Empty;
2023-04-07 00:53:55 +04:00
public Client Client { get; set; }
private Dictionary<int, IProceduresModel>? _recipeProcedures = null;
2023-04-05 23:24:58 +04:00
[NotMapped]
public Dictionary<int, IProceduresModel> RecipeProcedures
2023-04-05 23:24:58 +04:00
{
get
{
if (_recipeProcedures == null)
{
2023-05-20 06:49:33 +04:00
_recipeProcedures = Procedures.ToDictionary(recPC => recPC.ProcedureId, recPC =>
recPC.Procedure as IProceduresModel);
2023-04-05 23:24:58 +04:00
}
return _recipeProcedures;
}
}
2023-05-20 06:49:33 +04:00
[ForeignKey("RecipesId")]
2023-04-05 23:24:58 +04:00
public virtual List<RecipesProcedures> Procedures { get; set; } = new();
2023-05-20 06:49:33 +04:00
//private Dictionary<int, ISymptomsModel>? _recipeSymptoms = null;
//[NotMapped]
//public Dictionary<int, ISymptomsModel> RecipeSymptoms
//{
// get
// {
// if (_recipeSymptoms == null)
// {
// _recipeSymptoms = Symptoms.ToDictionary(recPC => recPC.SymptomsId, recPC => (recPC.Symptoms as ISymptomsModel));
// }
// return _recipeSymptoms;
// }
//}
//public virtual List<RecipesSymptoms> Symptoms { get; set; } = new();
2023-04-05 23:24:58 +04:00
2023-05-20 06:49:33 +04:00
public static Recipes? Create(RecipesBindingModel model)
{
2023-04-05 23:24:58 +04:00
return new Recipes()
{
Id = model.Id,
2023-05-20 06:49:33 +04:00
Dose = model.Dose,
2023-04-05 23:24:58 +04:00
Date = model.Date,
2023-05-20 06:49:33 +04:00
ModeOfApplication = model.ModeOfApplication,
ClientId = model.ClientId
//Symptoms = model.RecipeSymptoms.Select(x => new RecipesSymptoms
//{
// Symptoms = context.Symptomses.First(y => y.Id == x.Key),
//}).ToList()
2023-04-05 23:24:58 +04:00
};
}
public void Update(RecipesBindingModel model)
2023-04-05 23:24:58 +04:00
{
Date = model.Date;
MedicinesId = model.MedicinesId;
2023-04-05 23:24:58 +04:00
}
2023-04-07 00:53:55 +04:00
public RecipesViewModel GetViewModel
2023-04-05 23:24:58 +04:00
{
2023-05-20 06:49:33 +04:00
get
{
2023-04-07 00:53:55 +04:00
using var context = new HospitalDatabase();
return new RecipesViewModel
{
Id = Id,
Date = Date,
ClientId = ClientId,
ClientFIO = context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty,
2023-05-20 06:49:33 +04:00
MedicinesId = MedicinesId,
2023-04-07 00:53:55 +04:00
RecipeProcedures = RecipeProcedures,
2023-05-20 06:49:33 +04:00
//RecipeSymptoms = RecipeSymptoms
2023-04-07 00:53:55 +04:00
};
}
}
public void UpdateProcedures(HospitalDatabase context, RecipesBindingModel model)
{
2023-04-07 00:53:55 +04:00
var recipeProcedures = context.RecipesProcedures.Where(rec => rec.RecipesId == model.Id).ToList();
if (recipeProcedures != null && recipeProcedures.Count > 0)
{ // удалили те, которых нет в модели
context.RecipesProcedures.RemoveRange(recipeProcedures.Where(rec
=> !model.RecipeProcedures.ContainsKey(rec.ProcedureId)));
context.SaveChanges();
}
var recipe = context.Recipes.First(x => x.Id == Id);
foreach (var pc in model.RecipeProcedures)
{
context.RecipesProcedures.Add(new RecipesProcedures
{
2023-04-07 00:53:55 +04:00
Recipe = recipe,
Procedure = context.Procedures.First(x => x.Id == pc.Key)
});
context.SaveChanges();
}
_recipeProcedures = null;
}
2023-05-20 06:49:33 +04:00
//public void UpdateSymptomses(HospitalDatabase context, RecipesBindingModel model)
//{
// var recipeSymptomses = context.RecipesSymptoms.Where(rec => rec.RecipesId == model.Id).ToList();
// if (recipeSymptomses != null && recipeSymptomses.Count > 0)
// { // удалили те, которых нет в модели
// context.RecipesSymptoms.RemoveRange(recipeSymptomses.Where(rec
// => !model.RecipeSymptoms.ContainsKey(rec.SymptomsId)));
// context.SaveChanges();
// }
// var recipe = context.Recipes.First(x => x.Id == Id);
// foreach (var pc in model.RecipeSymptoms)
// {
// context.RecipesSymptoms.Add(new RecipesSymptoms
// {
// Recipe = recipe,
// Symptoms = context.Symptomses.First(x => x.Id == pc.Key)
// });
// context.SaveChanges();
// }
// _recipeSymptoms = null;
//}
2023-04-05 23:24:58 +04:00
}
}