using HospitalContracts.BindingModels; using HospitalContracts.BusinessLogicContracts; using HospitalContracts.SearchModels; using HospitalContracts.StorageContracts; using HospitalContracts.ViewModels; using System.Linq; namespace HospitalBusinessLogic { public class ReportLogic : IReportLogic { private readonly IMedicineStorage _medicineStorage; private readonly IPatientStorage _patientStorage; private readonly ITreatmentStorage _treatmentStorage; private readonly IPrescriptionStorage _prescriptionStorage; private readonly IProcedureStorage _procedureStorage; private readonly IRecipeStorage _recipeStorage; public ReportLogic(IMedicineStorage medicineStorage, IPatientStorage patientStorage, IPrescriptionStorage prescriptionStorage, ITreatmentStorage treatmentStorage, IProcedureStorage procedureStorage, IRecipeStorage recipeStorage) { _medicineStorage = medicineStorage; _patientStorage = patientStorage; _prescriptionStorage = prescriptionStorage; _treatmentStorage = treatmentStorage; _procedureStorage = procedureStorage; _recipeStorage = recipeStorage; } public ReportPatientsMedicinesViewModel GetMedicinePatients(ReportBindingModel model) { if (!model.MedicineId.HasValue) { return new(); } var medicine = _medicineStorage.GetElement(new MedicineSearchModel { Id = model.MedicineId}); if (medicine == null) { return new(); } // получаем рецепты и процедуры, в которых используется лекарство var treatmentsInRecipes = _recipeStorage.GetFullList().Where(x => x.RecipeMedicines.ContainsKey(medicine.Id)).SelectMany(x => x.RecipeTreatments.Keys).Distinct(); var procedures = _procedureStorage.GetFullList().Where(x => x.ProcedureMedicines.ContainsKey(medicine.Id)).Select(x => x.Id); // пациенты могут получать лекарство как через процедуры, так и через рецепты, поэтому объединим оба варианта var treatments = _treatmentStorage.GetFullList().Where(x => (x.TreatmentProcedures.Keys.Intersect(procedures).Any() || treatmentsInRecipes.Contains(x.Id))).Select(x => x.Id); // получаем список пациентов, которые связаны с найденными лечениями var patients = _patientStorage.GetFullList().Where(x => x.TreatmentId.HasValue).Where(x => treatments.Contains(x.TreatmentId!.Value)); var record = new ReportPatientsMedicinesViewModel { MedicineName = medicine.Name, Patients = new List() }; foreach (var patient in patients) { record.Patients.Add($"{patient.Surname} {patient.Name} {patient.Patronymic} {patient.BirthDate}"); } return record; } public List GetPrescriptionProcedures(ReportBindingModel model) { var prescriptions = _prescriptionStorage.GetFilteredList(new PrescriptionSearchModel { DateFrom = model.DateFrom, DateTo = model.DateTo }); var list = new List(); foreach (var prescription in prescriptions) { var procedures = _procedureStorage.GetFullList().Where((x) => x.ProcedureMedicines.ContainsKey(prescription.MedicineId)); foreach (var procedure in procedures) { var record = new ReportPrescriptionProcedureViewModel { DateCreate = prescription.Date, MedicineName = prescription.MedicineName, Number = prescription.Number, ProcedureName = procedure.Name }; list.Add(record); } } return list; } public void SavePatientsToExcelFile(ReportBindingModel model) { throw new NotImplementedException(); } public void SavePatientsToWordFile(ReportBindingModel model) { throw new NotImplementedException(); } } }