PIbd-22_Filippov_D.S._Cours.../VeterinaryDatabaseImplement/Models/Drug.cs
2024-05-30 07:33:38 +04:00

163 lines
4.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.EntityFrameworkCore.Storage;
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;
using VeterinaryContracts.BindingModels;
using VeterinaryContracts.SearchModels;
using VeterinaryContracts.ViewModels;
using VeterinaryDataModels.Models;
namespace VeterinaryDatabaseImplement.Models
{
public class Drug : IDrugModel
{
public int Id { get; set; }
[Required]
public string DrugName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
[Required]
public int Count { get; set; }
[Required]
public int DoctorId { get; private set; }
public virtual Doctor? Doctor { get; private set; }
[Required]
public DateTime DateCreate { get; private set; }
/*private Dictionary<int, IMedicationModel>? _drugMedications = null;
[NotMapped]
public Dictionary<int, IMedicationModel> DrugMedications
{
get
{
if (_drugMedications == null)
{
_drugMedications = Medications
.ToDictionary(recPC => recPC.MedicationId, recPC =>
(recPC.Medication as IMedicationModel));
}
return _drugMedications;
}
}
[ForeignKey("DrugId")]
public virtual List<DrugMedication> Medications { get; set; } = new();
*/
private Dictionary<int, IServiceModel>? _drugServices = null;
[NotMapped]
public Dictionary<int, IServiceModel> DrugServices
{
get
{
if (_drugServices == null)
{
_drugServices = Services
.ToDictionary(recPC => recPC.ServiceId, recPC =>
(recPC.Service as IServiceModel));
}
return _drugServices;
}
}
[ForeignKey("DrugId")]
public virtual List<DrugService> Services { get; set; } = new();
[ForeignKey("DrugId")]
public virtual List<Purchase> Purchases { get; set; } = new();
public static Drug Create(VeterinaryDatabase context, DrugBindingModel model)
{
var justMedications = model.DrugServices.Select(x => new DrugService
{
Service = context.Services.First(y => y.Id == x.Key)
}).ToList();
return new Drug()
{
Id = model.Id,
DrugName = model.DrugName,
Count = model.Count,
DoctorId = model.DoctorId,
Services = justMedications,
DateCreate= model.DateCreate
};
}
// цену считает неверно
public void Update( DrugBindingModel model)
{
DrugName = model.DrugName;
}
public DrugViewModel GetViewModel => new()
{
Id = Id,
DrugName = DrugName,
Price = Price,
Count= Count,
DoctorId=DoctorId,
DateCreate = DateCreate,
DrugServices = DrugServices
};
public void UpdateMedications(VeterinaryDatabase context, DrugBindingModel model)
{
var drugMedications = context.DrugServices.Where(rec => rec.DrugId == model.Id).ToList();
var list = new List <int>();
foreach (var rec in model.DrugServices)
{
list.Add(rec.Key);
}
//var tmp = model.DrugServices;
if (drugMedications != null && drugMedications.Count > 0)
{
// из таблицы бд удаляем строчки, которые были отменены(которые раньше были а тепперь их в модели нет)
context.DrugServices.RemoveRange(drugMedications.Where(rec => !model.DrugServices.ContainsKey(rec.ServiceId)));
// сохраняемся
context.SaveChanges();
foreach (var updateMedication in drugMedications)
{
// из модели убираем то что осталось( но зачем?)
model.DrugServices.Remove(updateMedication.ServiceId);
}
// сохраняемся
context.SaveChanges();
}
// собственно говоря, драг с которым мы работаем
var drug = context.Drugs.First(x => x.Id == Id);
foreach (var pc in model.DrugServices)
{
// в бдшку добавляем строчки которые остались
context.DrugServices.Add(new DrugService
{
Drug = drug,
Service = context.Services.First(x => x.Id == pc.Key)
});
context.SaveChanges();
}
//TODO DrugServices = null;
var justMedications = list.Select(x => new DrugMedication
{
Medication = context.Medications.First(y => y.Id == x)
}).ToList();
double pric = 0;
foreach (var justMedication in justMedications)
{
pric += justMedication.Medication.Price;
}
Price = pric;
context.SaveChanges();
}
}
}