Этап 4. Бизнес-логика с отчетами

This commit is contained in:
prodigygirl 2023-04-07 12:55:14 +04:00
parent d9c9cf410a
commit c34baa609e
12 changed files with 184 additions and 15 deletions

View File

@ -0,0 +1,95 @@
using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.StorageContracts;
using HospitalContracts.ViewModels;
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 => treatments.Contains(x.TreatmentId));
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}");
}
return record;
}
public List<ReportPrescriptionProcedureViewModel> GetPrescriptionProcedures(ReportBindingModel model)
{
var prescriptions = _prescriptionStorage.GetFilteredList(new PrescriptionSearchModel { DateFrom = model.DateFrom, DateTo = model.DateTo });
var list = new List<ReportPrescriptionProcedureViewModel>();
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();
}
}
}

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalContracts.BindingModels
{
public class ReportBindingModel
{
public string FileName { get; set; } = string.Empty;
public DateTime? DateFrom { get; set; }
public DateTime? DateTo { get; set; }
public int? MedicineId { get; set; }
}
}

View File

@ -0,0 +1,30 @@
using HospitalContracts.BindingModels;
using HospitalContracts.ViewModels;
namespace HospitalContracts.BusinessLogicContracts
{
public interface IReportLogic
{
/// <summary>
/// Получение списка пациентов с указанием, какие они используют лекарства при лечении
/// </summary>
/// <returns></returns>
ReportPatientsMedicinesViewModel GetMedicinePatients(ReportBindingModel reportBinding);
/// <summary>
/// Получение списка поступлений лекарств и процедур, в которых использованы данные лекарства
/// </summary>
/// <returns></returns>
List<ReportPrescriptionProcedureViewModel> GetPrescriptionProcedures(ReportBindingModel reportBinding);
/// <summary>
/// Сохранение пациентов с указанием лекарств в файл-Word
/// </summary>
/// <param name="model"></param>
void SavePatientsToWordFile(ReportBindingModel model);
/// <summary>
/// Сохранение пациентов с указанием лекарств в файл-Excel
/// </summary>
/// <param name="model"></param>
void SavePatientsToExcelFile(ReportBindingModel model);
}
}

View File

@ -4,5 +4,7 @@
{
public int? Id { get; set; }
public int? ApothecaryId { get; set; }
public DateTime? DateFrom { get; set; }
public DateTime? DateTo { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalContracts.ViewModels
{
public class ReportPatientsMedicinesViewModel
{
public string MedicineName { get; set; } = string.Empty;
public List<string> Patients { get; set; } = new(); // TODO: может как-то по-другому надо хранить информацию по пациенту?
}
}

View File

@ -0,0 +1,10 @@
namespace HospitalContracts.ViewModels
{
public class ReportPrescriptionProcedureViewModel
{
public DateTime DateCreate { get; set; }
public string MedicineName { get; set; } = string.Empty;
public int Number { get; set; }
public string ProcedureName { get; set; } = string.Empty;
}
}

View File

@ -23,13 +23,13 @@ namespace HospitalDatabaseImplement.Implements
public List<PrescriptionViewModel> GetFilteredList(PrescriptionSearchModel model)
{
if (!model.Id.HasValue || !model.ApothecaryId.HasValue)
if (!model.Id.HasValue && !model.ApothecaryId.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue)
{
return new();
}
using var context = new HospitalDatabase();
return context.Prescriptions.Include(x => x.Apothecary).Include(x => x.Medicine)
.Where(x => x.Id == model.Id || x.ApothecaryId == model.ApothecaryId)
.Where(x => x.Id == model.Id || x.ApothecaryId == model.ApothecaryId || (model.DateFrom <= x.Date && x.Date <= model.DateTo))
.Select(x => x.GetViewModel)
.ToList();
}

View File

@ -17,7 +17,6 @@ namespace HospitalDatabaseImplement.Implements
}
using var context = new HospitalDatabase();
return context.Treatments
.Include(x => x.Patients) // TODO: проверить, как такое работает - нужно для получения списка пациентов по лекарству
.Include(x => x.Procedures)
.ThenInclude(x => x.Procedure)
.FirstOrDefault(x =>
@ -34,7 +33,6 @@ namespace HospitalDatabaseImplement.Implements
}
using var context = new HospitalDatabase();
return context.Treatments
.Include(x => x.Patients)
.Include(x => x.Procedures)
.ThenInclude(x => x.Procedure)
.Where(x => x.Name.Contains(model.Name))
@ -48,7 +46,7 @@ namespace HospitalDatabaseImplement.Implements
return context.Treatments
.Include(x => x.Patients)
.Include(x => x.Procedures)
.ThenInclude(x => x.Procedure)
.ThenInclude(x => x.Procedure)
.Select(x => x.GetViewModel)
.ToList();
}
@ -63,7 +61,7 @@ namespace HospitalDatabaseImplement.Implements
}
context.Treatments.Add(newElement);
context.SaveChanges();
return context.Treatments.Include(x => x.Patients).FirstOrDefault(x => x.Id == newElement.Id)?.GetViewModel;
return context.Treatments.FirstOrDefault(x => x.Id == newElement.Id)?.GetViewModel;
}
public TreatmentViewModel? Update(TreatmentBindingModel model)

View File

@ -19,15 +19,16 @@ namespace HospitalDatabaseImplement
}
public static void LoadPatients()
{
// TODO return LoadData(PatientFileName, "Patient", x => Patient.Create(x)!)!;
{
using var context = new HospitalDatabase();
if (context.Patients.ToList().Count > 0)
return;
var list = LoadData(PatientFileName, "Patient", x => Patient.Create(x)!)!;
list.ForEach(x =>
{
context.Patients.Add(x);
context.SaveChanges();
context.Patients.Add(x);
});
context.SaveChanges();
}
@ -48,8 +49,7 @@ namespace HospitalDatabaseImplement
{
using var context = new HospitalDatabase();
if (context.Procedures.ToList().Count > 0)
return;
// TODO return LoadData(ProcedureFileName, "Procedure", x => Procedure.Create(context, x)!)!;
return;
var list = LoadData(ProcedureFileName, "Procedure", x => Procedure.Create(context, x)!)!;
list.ForEach(x =>
{

View File

@ -77,7 +77,8 @@ namespace HospitalDatabaseImplement.Models
public ProcedureViewModel GetViewModel => new()
{
Id = Id,
Name = Name
Name = Name,
ProcedureMedicines = ProcedureMedicines
};
public void UpdateMedicines(HospitalDatabase context, ProcedureBindingModel model)

View File

@ -78,7 +78,8 @@ namespace HospitalDatabaseImplement.Models
public TreatmentViewModel GetViewModel => new()
{
Id = Id,
Name = Name
Name = Name,
TreatmentProcedures = TreatmentProcedures
};
public void UpdateProcedures(HospitalDatabase context, TreatmentBindingModel model)

View File

@ -12,6 +12,6 @@
<Name>Имя 2</Name>
<Patronymic>Отчество 1</Patronymic>
<BirthDate>15.04.2001</BirthDate>
<TreatmentId>2</TreatmentId>
<TreatmentId>1</TreatmentId>
</Patient>
</Patients>