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 List GetMedicinePatients(ReportBindingModel model) { var list = new List(); var medicines = _medicineStorage.GetFullList().Where(x => model.Medicines.Contains(x.Id)); var recipes = _recipeStorage.GetFullList(); var procedures = _procedureStorage.GetFullList(); var treatments = _treatmentStorage.GetFullList(); var patients = _patientStorage.GetFullList(); foreach (var medicine in medicines) { var record = new ReportPatientsMedicinesViewModel { MedicineName = medicine.Name, Patients = new List>() }; // пациенты могут получать лекарство как через процедуры, так и через рецепты // получаем рецепты и процедуры, в которых используется лекарство var treatmentsInRecipesWithMedicine = recipes .Where(x => x.RecipeMedicines.ContainsKey(medicine.Id)) .SelectMany(x => x.RecipeTreatments.Keys).Distinct(); var proceduresWithMedicine = procedures .Where(x => x.ProcedureMedicines.ContainsKey(medicine.Id)) .Select(x => x.Id); // объединим оба варианта var treatmentsWithMedicine = treatments .Where(x => x.TreatmentProcedures.Keys.Intersect(proceduresWithMedicine).Any() || treatmentsInRecipesWithMedicine.Contains(x.Id)) .Select(x => x.Id); // получаем список пациентов, которые связаны с найденными лечениями record.Patients = patients .Where(x => x.TreatmentId.HasValue) .Where(x => treatmentsWithMedicine.Contains(x.TreatmentId!.Value)) .Select(x => new Tuple(x.Surname, x.Name, x.Patronymic, x.BirthDate)).ToList(); list.Add(record); } return list; } 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(); } } }