Исправление: получение отчета по нескольким лекарствам и пациентам

This commit is contained in:
prodigygirl 2023-04-07 22:18:57 +04:00
parent abd474d67d
commit c823a62a24
4 changed files with 40 additions and 33 deletions

View File

@ -27,37 +27,43 @@ namespace HospitalBusinessLogic
_recipeStorage = recipeStorage;
}
public ReportPatientsMedicinesViewModel GetMedicinePatients(ReportBindingModel model)
public List<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 list = new List<ReportPatientsMedicinesViewModel>();
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<Tuple<string, string, string?, DateTime>>()
};
// пациенты могут получать лекарство как через процедуры, так и через рецепты
// получаем рецепты и процедуры, в которых используется лекарство
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);
// получаем список пациентов, которые связаны с найденными лечениями
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<string>()
};
foreach (var patient in patients)
{
record.Patients.Add($"{patient.Surname} {patient.Name} {patient.Patronymic} {patient.BirthDate}");
// получаем список пациентов, которые связаны с найденными лечениями
record.Patients = patients
.Where(x => x.TreatmentId.HasValue)
.Where(x => treatmentsWithMedicine.Contains(x.TreatmentId!.Value))
.Select(x => new Tuple<string, string, string?, DateTime>(x.Surname, x.Name, x.Patronymic, x.BirthDate)).ToList();
list.Add(record);
}
return record;
return list;
}
public List<ReportPrescriptionProcedureViewModel> GetPrescriptionProcedures(ReportBindingModel model)

View File

@ -1,4 +1,5 @@
using System;
using HospitalDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -11,7 +12,7 @@ namespace HospitalContracts.BindingModels
public string FileName { get; set; } = string.Empty;
public DateTime? DateFrom { get; set; }
public DateTime? DateTo { get; set; }
public int? MedicineId { get; set; }
// выбранные лекарства
public List<int> Medicines { get; set; } = new();
}
}

View File

@ -10,7 +10,7 @@ namespace HospitalContracts.BusinessLogicContracts
/// Получение списка пациентов с указанием, какие они используют лекарства при лечении
/// </summary>
/// <returns></returns>
ReportPatientsMedicinesViewModel GetMedicinePatients(ReportBindingModel reportBinding);
List<ReportPatientsMedicinesViewModel> GetMedicinePatients(ReportBindingModel reportBinding);
/// <summary>
/// Получение списка поступлений лекарств и процедур, в которых использованы данные лекарства
/// </summary>

View File

@ -10,6 +10,6 @@ namespace HospitalContracts.ViewModels
{
public string MedicineName { get; set; } = string.Empty;
public List<string> Patients { get; set; } = new(); // TODO: может как-то по-другому надо хранить информацию по пациенту?
public List<Tuple<string, string, string?, DateTime>> Patients { get; set; } = new();
}
}