diff --git a/app/Services/Cache/CacheInvalidator.php b/app/Services/Cache/CacheInvalidator.php new file mode 100644 index 0000000..749ad9c --- /dev/null +++ b/app/Services/Cache/CacheInvalidator.php @@ -0,0 +1,15 @@ + (string) $part, $parts)); + + return "{$this->version}:{$key}{$suffix}"; + } +} diff --git a/app/Services/OutcomePatientService.php b/app/Services/OutcomePatientService.php new file mode 100644 index 0000000..abcf5fc --- /dev/null +++ b/app/Services/OutcomePatientService.php @@ -0,0 +1,181 @@ + function ($q) { + $q->select([ + 'stt_migrationpatient.MigrationPatientID', + 'stt_migrationpatient.rf_MedicalHistoryID', + 'stt_migrationpatient.DateOut', + 'stt_migrationpatient.rf_DiagnosID', + 'stt_migrationpatient.rf_kl_VisitResultID', + 'stt_migrationpatient.rf_StationarBranchID', + ])->with(['mainDiagnosis' => function ($diagnosisQuery) { + $diagnosisQuery->select([ + 'DiagnosID', + 'rf_MKBID', + ])->with(['mkb' => function ($mkbQuery) { + $mkbQuery->select([ + 'MKBID', + 'DS', + 'NAME', + ]); + }]); + }]); + }, + ]; + } + + public function getOutcomePatients( + int $branchId, + DateRange $dateRange, + string $outcomeType = 'all', + bool $onlyIds = false + ) { + return match ($outcomeType) { + 'deceased' => $this->getBranchDeceasedPatients($branchId, $dateRange, $onlyIds), + 'transferred' => $this->getBranchTransferredPatients($branchId, $dateRange, $onlyIds), + 'discharged' => $this->getBranchDischargedPatients($branchId, $dateRange, $onlyIds), + 'without-transferred' => $this->getBranchOutcomePatientsWithoutTransferred($branchId, $dateRange, $onlyIds), + default => $this->getAllBranchOutcomePatients($branchId, $dateRange, $onlyIds), + }; + } + + // Общий фильтр периода + private function applyPeriod($query, string $column, DateRange $dateRange) + { + return $query + ->where($column, '>=', $dateRange->startSql()) + ->where($column, '<', $dateRange->endSql()); + } + + // Форматирование результата + private function finalizePatientsQuery($query, bool $onlyIds = false) + { + if ($onlyIds) { + return $query->pluck('MedicalHistoryID'); + } + + return $query + ->select($this->patientSelect()) + ->with($this->outcomeRelations()) + ->orderBy('DateRecipient', 'DESC') + ->get(); + } + + // Умершие в отделении. Фильтруем по движению пациента, по дате смерти. Без DateExtract + public function getBranchDeceasedPatients( + int $branchId, + DateRange $dateRange, + bool $onlyIds = false + ) { + $query = MisMedicalHistory::query() + ->where('MedicalHistoryID', '<>', 0) + ->whereHas('migrations', function ($q) use ($branchId) { + $q->where('rf_StationarBranchID', $branchId) + ->whereIn('rf_kl_VisitResultID', [5, 6, 15, 16]); + }); + + $this->applyPeriod($query, 'DateDeath', $dateRange); + + return $this->finalizePatientsQuery($query, $onlyIds); + } + + // Переведенные из отделения + public function getBranchTransferredPatients( + int $branchId, + DateRange $dateRange, + bool $onlyIds = false + ) { + $query = MisMedicalHistory::query() + ->where('MedicalHistoryID', '<>', 0) + ->whereHas('migrations', function ($q) use ($branchId) { + $q->where('rf_StationarBranchID', $branchId) + ->whereIn('rf_kl_VisitResultID', [4, 14]); + }); + + $this->applyPeriod($query, 'DateExtract', $dateRange); + + return $this->finalizePatientsQuery($query, $onlyIds); + } + + // Выписанные из стационара, связанные с отделением + public function getBranchDischargedPatients( + int $branchId, + DateRange $dateRange, + bool $onlyIds = false + ) { + $query = MisMedicalHistory::query() + ->where('MedicalHistoryID', '<>', 0) + ->whereHas('migrations', function ($q) use ($branchId) { + $q->where('rf_StationarBranchID', $branchId) + ->whereIn('rf_kl_VisitResultID', [1, 11, 2, 12, 7, 18, 48]); + }); + + $this->applyPeriod($query, 'DateExtract', $dateRange); + + return $this->finalizePatientsQuery($query, $onlyIds); + } + + // Выбывшие без переведенных + public function getBranchOutcomePatientsWithoutTransferred( + int $branchId, + DateRange $dateRange, + bool $onlyIds = false + ) { + $query = MisMedicalHistory::query() + ->where('MedicalHistoryID', '<>', 0) + ->whereHas('migrations', function ($q) use ($branchId) { + $q->where('rf_StationarBranchID', $branchId) + ->whereNotIn('rf_kl_VisitResultID', [4, 14]) + ->where('rf_kl_VisitResultID', '<>', 0); + }); + + $this->applyPeriod($query, 'DateExtract', $dateRange); + + return $this->finalizePatientsQuery($query, $onlyIds); + } + + // Все выбывшие из отделения + public function getAllBranchOutcomePatients( + int $branchId, + DateRange $dateRange, + bool $onlyIds = false + ) { + $query = MisMedicalHistory::query() + ->where('MedicalHistoryID', '<>', 0) + ->whereHas('migrations', function ($q) use ($branchId) { + $q->where('rf_StationarBranchID', $branchId) + ->where('rf_kl_VisitResultID', '<>', 0); + }); + + $this->applyPeriod($query, 'DateExtract', $dateRange); + + return $this->finalizePatientsQuery($query, $onlyIds); + } +}