487 lines
17 KiB
PHP
487 lines
17 KiB
PHP
<?php
|
||
|
||
namespace App\Services;
|
||
|
||
use App\Models\MisMedicalHistory;
|
||
use App\Models\MisMigrationPatient;
|
||
use App\Models\MisReanimation;
|
||
use App\Models\MisSurgicalOperation;
|
||
|
||
class PatientService
|
||
{
|
||
public function __construct(
|
||
protected OutcomePatientService $outcomePatientService,
|
||
protected RecipientPatientService $recipientPatientService,
|
||
protected CurrentPatientService $currentPatientService
|
||
) {}
|
||
|
||
/**
|
||
* Получить плановых или экстренных пациентов
|
||
*/
|
||
public function getPlanOrEmergencyPatients(
|
||
?string $type,
|
||
bool $isHeadOrAdmin,
|
||
int $branchId,
|
||
DateRange $dateRange,
|
||
bool $countOnly = false,
|
||
bool $onlyIds = false,
|
||
bool $includeCurrent = false,
|
||
bool $fillableAuto = false
|
||
) {
|
||
$medicalHistoryIds = $this->recipientPatientService->resolvePlanOrEmergencyMedicalHistoryIds(
|
||
$type,
|
||
$isHeadOrAdmin,
|
||
$branchId,
|
||
$dateRange,
|
||
$includeCurrent,
|
||
$fillableAuto
|
||
);
|
||
|
||
if (empty($medicalHistoryIds)) {
|
||
return $countOnly ? 0 : collect();
|
||
}
|
||
|
||
if ($countOnly) {
|
||
return count($medicalHistoryIds);
|
||
}
|
||
|
||
if ($onlyIds) {
|
||
return collect($medicalHistoryIds);
|
||
}
|
||
|
||
$recipientIds = $this->recipientPatientService->getRecipientMedicalHistoryIds(
|
||
$type,
|
||
$isHeadOrAdmin,
|
||
$branchId,
|
||
$dateRange
|
||
);
|
||
|
||
$res = $this->buildPatientCardsQuery($medicalHistoryIds, $branchId);
|
||
|
||
return $res->get()
|
||
->map(function ($patient) use ($recipientIds) {
|
||
$patient->is_recipient_today = in_array($patient->MedicalHistoryID, $recipientIds, true);
|
||
|
||
return $patient;
|
||
});
|
||
|
||
}
|
||
|
||
/**
|
||
* Получить всех пациентов в отделении (поступившие сегодня + уже лечащиеся)
|
||
*/
|
||
public function getAllPatientsInDepartment(
|
||
bool $isHeadOrAdmin,
|
||
int $branchId,
|
||
DateRange $dateRange,
|
||
bool $countOnly = false,
|
||
bool $onlyIds = false,
|
||
bool $fillableAuto = false
|
||
) {
|
||
$recipientIds = $this->recipientPatientService->buildRecipientQuery(null, $isHeadOrAdmin, $branchId, $dateRange, $fillableAuto)
|
||
->pluck('rf_MedicalHistoryID')
|
||
->toArray();
|
||
|
||
if ($fillableAuto) {
|
||
$currentIds = $this->currentPatientService->getHistoricalCurrentMedicalHistoryIds(null, $branchId, $dateRange);
|
||
} else {
|
||
$currentIds = MisMigrationPatient::currentlyInTreatment($branchId)
|
||
->pluck('rf_MedicalHistoryID')
|
||
->toArray();
|
||
}
|
||
|
||
$allIds = array_unique(array_merge($recipientIds, $currentIds));
|
||
|
||
if (empty($allIds)) {
|
||
if ($countOnly) {
|
||
return 0;
|
||
}
|
||
|
||
return collect();
|
||
}
|
||
|
||
if ($countOnly) {
|
||
return count($allIds);
|
||
}
|
||
|
||
if ($onlyIds) {
|
||
return collect($allIds);
|
||
}
|
||
|
||
$res = MisMedicalHistory::whereIn('MedicalHistoryID', $allIds)
|
||
->select([
|
||
'MedicalHistoryID',
|
||
'FAMILY',
|
||
'Name',
|
||
'OT',
|
||
'BD',
|
||
'DateRecipient',
|
||
'DateExtract',
|
||
'rf_EmerSignID',
|
||
'rf_kl_VisitResultID',
|
||
])
|
||
->with([
|
||
'surgicalOperations' => function ($q) {
|
||
$q->select([
|
||
'SurgicalOperationID',
|
||
'rf_MedicalHistoryID',
|
||
'rf_kl_ServiceMedicalID',
|
||
'Date',
|
||
])->with(['serviceMedical' => function ($serviceQuery) {
|
||
$serviceQuery->select([
|
||
'ServiceMedicalID',
|
||
'ServiceMedicalCode',
|
||
'ServiceMedicalName',
|
||
]);
|
||
}]);
|
||
},
|
||
'outcomeMigration' => function ($q) {
|
||
$q->select([
|
||
'stt_migrationpatient.MigrationPatientID',
|
||
'stt_migrationpatient.rf_MedicalHistoryID',
|
||
'stt_migrationpatient.DateOut',
|
||
'stt_migrationpatient.rf_DiagnosID',
|
||
])->with(['mainDiagnosis' => function ($diagnosisQuery) {
|
||
$diagnosisQuery->select([
|
||
'DiagnosID',
|
||
'rf_MKBID',
|
||
])->with(['mkb' => function ($mkbQuery) {
|
||
$mkbQuery->select([
|
||
'MKBID',
|
||
'DS',
|
||
'NAME',
|
||
]);
|
||
}]);
|
||
}]);
|
||
},
|
||
'migrations' => function ($q) use ($branchId) {
|
||
$q->where('rf_StationarBranchID', $branchId)
|
||
->select([
|
||
'MigrationPatientID',
|
||
'rf_MedicalHistoryID',
|
||
'rf_DiagnosID',
|
||
'DateIngoing',
|
||
'rf_StationarBranchID',
|
||
])
|
||
->orderByDesc('DateIngoing')
|
||
->with(['mainDiagnosis' => function ($diagnosisQuery) {
|
||
$diagnosisQuery->select([
|
||
'DiagnosID',
|
||
'rf_MKBID',
|
||
'rf_MigrationPatientID',
|
||
])->with(['mkb' => function ($mkbQuery) {
|
||
$mkbQuery->select([
|
||
'MKBID',
|
||
'DS',
|
||
'NAME',
|
||
]);
|
||
}]);
|
||
}]);
|
||
},
|
||
])
|
||
->orderBy('DateRecipient', 'DESC');
|
||
|
||
return $res->get()
|
||
->map(function ($patient) use ($recipientIds) {
|
||
$patient->is_recipient_today = in_array($patient->MedicalHistoryID, $recipientIds);
|
||
|
||
return $patient;
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Получить пациентов под наблюдением
|
||
*/
|
||
public function getObservationPatients(int $departmentId, bool $onlyIds = false)
|
||
{
|
||
$query = MisMedicalHistory::whereHas('observationPatient', function ($q) use ($departmentId) {
|
||
$q->where('rf_department_id', $departmentId);
|
||
});
|
||
|
||
if (! $onlyIds) {
|
||
$query->with(['observationPatient' => function ($query) use ($departmentId) {
|
||
$query->where('rf_department_id', $departmentId)
|
||
->select(['rf_medicalhistory_id', 'observation_patient_id', 'comment']);
|
||
}])
|
||
->orderBy('DateRecipient', 'DESC');
|
||
}
|
||
|
||
if ($onlyIds) {
|
||
$patients = $query->pluck('MedicalHistoryID');
|
||
} else {
|
||
$query->with([
|
||
'outcomeMigration.mainDiagnosis.mkb',
|
||
]);
|
||
$patients = $query->get();
|
||
}
|
||
|
||
return $patients->map(function ($patient) {
|
||
$patient->comment = $patient->observationPatient
|
||
->pluck('comment')
|
||
->filter()
|
||
->implode('; ');
|
||
|
||
return $patient;
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Получить выбывших пациентов
|
||
*/
|
||
public function getOutcomePatients(
|
||
int $branchId,
|
||
DateRange $dateRange,
|
||
string $outcomeType = 'all',
|
||
bool $onlyIds = false
|
||
) {
|
||
return $this->outcomePatientService->getOutcomePatients($branchId, $dateRange, $outcomeType, $onlyIds);
|
||
}
|
||
|
||
/**
|
||
* Получить пациентов с записями в реанимации по отделению
|
||
*/
|
||
public function getReanimationPatients(
|
||
int $branchId,
|
||
DateRange $dateRange,
|
||
bool $onlyIds = false
|
||
) {
|
||
$currentIds = $this->getAllPatientsInDepartment(
|
||
true,
|
||
$branchId,
|
||
$dateRange,
|
||
false,
|
||
true,
|
||
false
|
||
)->all();
|
||
|
||
$outcomeIds = $this->getOutcomePatients(
|
||
$branchId,
|
||
$dateRange,
|
||
'all',
|
||
true
|
||
)->all();
|
||
|
||
$reportCohortIds = array_values(array_unique(array_merge($currentIds, $outcomeIds)));
|
||
|
||
if (empty($reportCohortIds)) {
|
||
return collect();
|
||
}
|
||
|
||
$reanimationByMedicalHistory = MisReanimation::query()
|
||
->join('stt_migrationpatient as mp', 'mp.MigrationPatientID', '=', 'stt_reanimation.rf_MigrationPatientID')
|
||
->where('mp.rf_StationarBranchID', $branchId)
|
||
->where('mp.rf_MedicalHistoryID', '<>', 0)
|
||
->whereIn('mp.rf_MedicalHistoryID', $reportCohortIds)
|
||
->selectRaw('
|
||
mp."rf_MedicalHistoryID" as medical_history_id,
|
||
MAX(stt_reanimation."DateIn") as reanimation_date_in,
|
||
BOOL_OR(COALESCE(stt_reanimation."isComplete", false)) as reanimation_is_complete
|
||
')
|
||
->groupBy('mp.rf_MedicalHistoryID')
|
||
->get();
|
||
|
||
$medicalHistoryIds = $reanimationByMedicalHistory
|
||
->pluck('medical_history_id')
|
||
->map(fn ($id) => (int) $id)
|
||
->values()
|
||
->all();
|
||
|
||
$reanimationDateByMedicalHistory = $reanimationByMedicalHistory
|
||
->pluck('reanimation_date_in', 'medical_history_id');
|
||
$reanimationCompleteByMedicalHistory = $reanimationByMedicalHistory
|
||
->pluck('reanimation_is_complete', 'medical_history_id');
|
||
|
||
if (empty($medicalHistoryIds)) {
|
||
return collect();
|
||
}
|
||
|
||
if ($onlyIds) {
|
||
return collect($medicalHistoryIds);
|
||
}
|
||
|
||
return MisMedicalHistory::whereIn('MedicalHistoryID', $medicalHistoryIds)
|
||
->select([
|
||
'MedicalHistoryID',
|
||
'FAMILY',
|
||
'Name',
|
||
'OT',
|
||
'BD',
|
||
'DateRecipient',
|
||
'DateExtract',
|
||
'rf_EmerSignID',
|
||
'rf_kl_VisitResultID',
|
||
])
|
||
->with([
|
||
'surgicalOperations' => function ($q) {
|
||
$q->select([
|
||
'SurgicalOperationID',
|
||
'rf_MedicalHistoryID',
|
||
'rf_kl_ServiceMedicalID',
|
||
'Date',
|
||
])->with(['serviceMedical' => function ($serviceQuery) {
|
||
$serviceQuery->select([
|
||
'ServiceMedicalID',
|
||
'ServiceMedicalCode',
|
||
'ServiceMedicalName',
|
||
]);
|
||
}]);
|
||
},
|
||
'outcomeMigration' => function ($q) {
|
||
$q->select([
|
||
'stt_migrationpatient.MigrationPatientID',
|
||
'stt_migrationpatient.rf_MedicalHistoryID',
|
||
'stt_migrationpatient.DateOut',
|
||
'stt_migrationpatient.rf_DiagnosID',
|
||
])->with(['mainDiagnosis' => function ($diagnosisQuery) {
|
||
$diagnosisQuery->select([
|
||
'DiagnosID',
|
||
'rf_MKBID',
|
||
])->with(['mkb' => function ($mkbQuery) {
|
||
$mkbQuery->select([
|
||
'MKBID',
|
||
'DS',
|
||
'NAME',
|
||
]);
|
||
}]);
|
||
}]);
|
||
},
|
||
])
|
||
->orderBy('DateRecipient', 'DESC')
|
||
->get()
|
||
->map(function ($patient) use ($reanimationDateByMedicalHistory, $reanimationCompleteByMedicalHistory) {
|
||
$reanimationDateIn = $reanimationDateByMedicalHistory->get($patient->MedicalHistoryID);
|
||
if ($reanimationDateIn) {
|
||
$patient->DateRecipient = $reanimationDateIn;
|
||
}
|
||
$patient->reanimation_is_complete = (bool) $reanimationCompleteByMedicalHistory->get($patient->MedicalHistoryID, false);
|
||
|
||
return $patient;
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Получить пациентов с операциями
|
||
*/
|
||
public function getSurgicalPatients(
|
||
string $type,
|
||
int $branchId,
|
||
DateRange $dateRange,
|
||
bool $countOnly = false
|
||
) {
|
||
$query = MisSurgicalOperation::where('rf_StationarBranchID', $branchId)
|
||
->completed()
|
||
->where('Date', '>=', $dateRange->startSql())
|
||
->where('Date', '<=', $dateRange->endSql());
|
||
|
||
if ($type === 'plan') {
|
||
$query->where('rf_TypeSurgOperationInTimeID', 6);
|
||
} else {
|
||
$query->whereIn('rf_TypeSurgOperationInTimeID', [4, 5]);
|
||
}
|
||
|
||
if ($countOnly) {
|
||
return $query->count();
|
||
}
|
||
|
||
return $query->get();
|
||
}
|
||
|
||
/**
|
||
* Получить количество пациентов по типу с учетом уже находящихся в отделении
|
||
*/
|
||
public function getPatientsCountWithCurrent(
|
||
?string $type,
|
||
bool $isHeadOrAdmin,
|
||
int $branchId,
|
||
DateRange $dateRange
|
||
): int {
|
||
return $this->getPlanOrEmergencyPatients(
|
||
$type,
|
||
$isHeadOrAdmin,
|
||
$branchId,
|
||
$dateRange,
|
||
true,
|
||
false,
|
||
true
|
||
);
|
||
}
|
||
|
||
private function buildPatientCardsQuery(array $medicalHistoryIds, int $branchId)
|
||
{
|
||
return MisMedicalHistory::query()
|
||
->whereIn('MedicalHistoryID', $medicalHistoryIds)
|
||
->select([
|
||
'MedicalHistoryID',
|
||
'FAMILY',
|
||
'Name',
|
||
'OT',
|
||
'BD',
|
||
'DateRecipient',
|
||
'DateExtract',
|
||
'DateDeath',
|
||
'rf_EmerSignID',
|
||
'rf_kl_VisitResultID',
|
||
])
|
||
->with([
|
||
'surgicalOperations' => function ($q) {
|
||
$q->select([
|
||
'SurgicalOperationID',
|
||
'rf_MedicalHistoryID',
|
||
'rf_kl_ServiceMedicalID',
|
||
'Date',
|
||
])->with(['serviceMedical' => function ($serviceQuery) {
|
||
$serviceQuery->select([
|
||
'ServiceMedicalID',
|
||
'ServiceMedicalCode',
|
||
'ServiceMedicalName',
|
||
]);
|
||
}]);
|
||
},
|
||
'outcomeMigration' => function ($q) {
|
||
$q->select([
|
||
'stt_migrationpatient.MigrationPatientID',
|
||
'stt_migrationpatient.rf_MedicalHistoryID',
|
||
'stt_migrationpatient.DateOut',
|
||
'stt_migrationpatient.rf_DiagnosID',
|
||
])->with(['mainDiagnosis' => function ($diagnosisQuery) {
|
||
$diagnosisQuery->select([
|
||
'DiagnosID',
|
||
'rf_MKBID',
|
||
])->with(['mkb' => function ($mkbQuery) {
|
||
$mkbQuery->select([
|
||
'MKBID',
|
||
'DS',
|
||
'NAME',
|
||
]);
|
||
}]);
|
||
}]);
|
||
},
|
||
'migrations' => function ($q) use ($branchId) {
|
||
$q->where('rf_StationarBranchID', $branchId)
|
||
->select([
|
||
'MigrationPatientID',
|
||
'rf_MedicalHistoryID',
|
||
'rf_DiagnosID',
|
||
'DateIngoing',
|
||
'rf_StationarBranchID',
|
||
])
|
||
->orderByDesc('DateIngoing')
|
||
->with(['mainDiagnosis' => function ($diagnosisQuery) {
|
||
$diagnosisQuery->select([
|
||
'DiagnosID',
|
||
'rf_MKBID',
|
||
'rf_MigrationPatientID',
|
||
])->with(['mkb' => function ($mkbQuery) {
|
||
$mkbQuery->select([
|
||
'MKBID',
|
||
'DS',
|
||
'NAME',
|
||
]);
|
||
}]);
|
||
}]);
|
||
},
|
||
])
|
||
->orderBy('DateRecipient', 'DESC');
|
||
}
|
||
}
|