Докинул к 719eb1403f
This commit is contained in:
15
app/Services/Cache/CacheInvalidator.php
Normal file
15
app/Services/Cache/CacheInvalidator.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cache;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class CacheInvalidator
|
||||
{
|
||||
public function forget(array $keys): void
|
||||
{
|
||||
foreach ($keys as $key) {
|
||||
Cache::forget($key);
|
||||
}
|
||||
}
|
||||
}
|
||||
17
app/Services/Cache/CacheKeyBuilder.php
Normal file
17
app/Services/Cache/CacheKeyBuilder.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cache;
|
||||
|
||||
class CacheKeyBuilder
|
||||
{
|
||||
public function __construct(
|
||||
private readonly string $version = 'v1'
|
||||
) {}
|
||||
|
||||
public function make(string $key, array $parts = []): string
|
||||
{
|
||||
$suffix = empty($parts) ? '' : ':' . implode(':', array_map(static fn ($part) => (string) $part, $parts));
|
||||
|
||||
return "{$this->version}:{$key}{$suffix}";
|
||||
}
|
||||
}
|
||||
181
app/Services/OutcomePatientService.php
Normal file
181
app/Services/OutcomePatientService.php
Normal file
@@ -0,0 +1,181 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\MisMedicalHistory;
|
||||
|
||||
class OutcomePatientService
|
||||
{
|
||||
// Базовый select по пациенту
|
||||
private function patientSelect(): array
|
||||
{
|
||||
return [
|
||||
'MedicalHistoryID',
|
||||
'FAMILY',
|
||||
'Name',
|
||||
'OT',
|
||||
'BD',
|
||||
'DateRecipient',
|
||||
'DateExtract',
|
||||
'DateDeath',
|
||||
'rf_EmerSignID',
|
||||
'rf_kl_VisitResultID',
|
||||
];
|
||||
}
|
||||
|
||||
// Базовые relation для выбытия
|
||||
private function outcomeRelations(): array
|
||||
{
|
||||
return [
|
||||
'outcomeMigration' => 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user