163 lines
7.5 KiB
C#
163 lines
7.5 KiB
C#
using HospitalBusinessLogic.MailWorker;
|
||
using HospitalBusinessLogic.OfficePackage;
|
||
using HospitalBusinessLogic.OfficePackage.HelperModels;
|
||
using HospitalBusinessLogic.OfficePackage.Implements;
|
||
using HospitalContracts.BindingModels;
|
||
using HospitalContracts.BusinessLogicContracts;
|
||
using HospitalContracts.SearchModels;
|
||
using HospitalContracts.StorageContracts;
|
||
using HospitalContracts.ViewModels;
|
||
using System.Linq;
|
||
using System.Net.Mail;
|
||
|
||
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;
|
||
|
||
private readonly AbstractSaveToExcel _saveToExcel;
|
||
private readonly AbstractSaveToWord _saveToWord;
|
||
private readonly AbstractSaveToPdf _saveToPdf;
|
||
|
||
private readonly AbstractMailWorker _mailWorker;
|
||
|
||
public ReportLogic(IMedicineStorage medicineStorage, IPatientStorage patientStorage,
|
||
IPrescriptionStorage prescriptionStorage, ITreatmentStorage treatmentStorage,
|
||
IProcedureStorage procedureStorage, IRecipeStorage recipeStorage,
|
||
AbstractSaveToExcel abstractSaveToExcel, AbstractSaveToWord abstractSaveToWord, AbstractSaveToPdf abstractSaveToPdf, AbstractMailWorker abstractMailWorker)
|
||
{
|
||
_medicineStorage = medicineStorage;
|
||
_patientStorage = patientStorage;
|
||
_prescriptionStorage = prescriptionStorage;
|
||
_treatmentStorage = treatmentStorage;
|
||
_procedureStorage = procedureStorage;
|
||
_recipeStorage = recipeStorage;
|
||
|
||
_saveToExcel = abstractSaveToExcel;
|
||
_saveToWord = abstractSaveToWord;
|
||
_saveToPdf = abstractSaveToPdf;
|
||
|
||
_mailWorker = abstractMailWorker;
|
||
}
|
||
|
||
public List<ReportPatientsMedicinesViewModel> GetMedicinePatients(ReportBindingModel model)
|
||
{
|
||
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);
|
||
|
||
// получаем список пациентов, которые связаны с найденными лечениями
|
||
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 list;
|
||
}
|
||
|
||
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 MemoryStream SavePatientsToExcelFile(ReportBindingModel model)
|
||
{
|
||
return _saveToExcel.CreateReport(new ExcelInfo
|
||
{
|
||
FileName = model.FileName,
|
||
Title = "Список пациентов",
|
||
PatientsMedicines = GetMedicinePatients(model),
|
||
Headers = {"№","Фамилия", "Имя", "Отчество", "Дата рождения"}
|
||
});
|
||
}
|
||
|
||
public MemoryStream SavePatientsToWordFile(ReportBindingModel model)
|
||
{
|
||
return _saveToWord.CreateDoc(new WordInfo
|
||
{
|
||
FileName = model.FileName,
|
||
Title = "Список пациентов",
|
||
PatientsMedicines = GetMedicinePatients(model)
|
||
});
|
||
}
|
||
|
||
public MemoryStream SavePrescriptionsToPdfFile(ReportBindingModel model)
|
||
{
|
||
return _saveToPdf.CreateDoc(new PdfInfo
|
||
{
|
||
FileName = model.FileName,
|
||
Title = "Список поступлений и процедур",
|
||
Prescriptions = GetPrescriptionProcedures(model),
|
||
DateFrom = model.DateFrom!.Value,
|
||
DateTo = model.DateTo!.Value
|
||
});
|
||
}
|
||
|
||
public void SendMailWithReportAttachments(ReportBindingModel model)
|
||
{
|
||
var stream = SavePrescriptionsToPdfFile(model);
|
||
if (stream == null)
|
||
{
|
||
throw new Exception("Pdf-document is not created");
|
||
}
|
||
var attachments = new List<Attachment>();
|
||
attachments.Add(new Attachment(stream, $"report-{DateTime.Now.ToShortDateString()}.pdf"));
|
||
_mailWorker.MailSendAsync(new MailSendInfoBindingModel()
|
||
{
|
||
Subject = "Отчет \"Поступления\"",
|
||
Text = $"Отчет по поступлениям лекарств, требующихся для выполнения процедур, за период с {model.DateFrom} по {model.DateTo}",
|
||
MailAddress = model.Email,
|
||
Attachments = attachments
|
||
});
|
||
}
|
||
}
|
||
}
|