Изменения в основном report

This commit is contained in:
brusnitsyn
2026-05-06 22:32:11 +09:00
parent c5da85763c
commit 723ccee8d3
56 changed files with 1911 additions and 3814 deletions

View File

@@ -2,7 +2,10 @@
namespace App\Services;
use App\Models\MedicalHistory;
use App\Models\MigrationPatient;
use App\Models\MisMedicalHistory;
use Illuminate\Support\Facades\Schema;
class OutcomePatientService
{
@@ -57,6 +60,10 @@ class OutcomePatientService
string $outcomeType = 'all',
bool $onlyIds = false
) {
if ($this->useMaterializedViews()) {
return $this->getOutcomePatientsFromMaterializedViews($branchId, $dateRange, $outcomeType, $onlyIds);
}
return match ($outcomeType) {
'deceased' => $this->getBranchDeceasedPatients($branchId, $dateRange, $onlyIds),
'transferred' => $this->getBranchTransferredPatients($branchId, $dateRange, $onlyIds),
@@ -178,4 +185,62 @@ class OutcomePatientService
return $this->finalizePatientsQuery($query, $onlyIds);
}
private function getOutcomePatientsFromMaterializedViews(
int $branchId,
DateRange $dateRange,
string $outcomeType,
bool $onlyIds
) {
$migrationQuery = MigrationPatient::query()
->where('stationar_branch_id', $branchId);
$migrationQuery = match ($outcomeType) {
'deceased' => $migrationQuery->deceased($dateRange->startSql(), $dateRange->endSql()),
'transferred' => $migrationQuery->transferred($dateRange->startSql(), $dateRange->endSql()),
'discharged' => $migrationQuery->discharged($dateRange->startSql(), $dateRange->endSql()),
'without-transferred' => $migrationQuery->where(function ($query) use ($dateRange) {
$query->discharged($dateRange->startSql(), $dateRange->endSql())
->orWhere(fn ($orQuery) => $orQuery->deceased($dateRange->startSql(), $dateRange->endSql()));
}),
default => $migrationQuery->where(function ($query) use ($dateRange) {
$query->discharged($dateRange->startSql(), $dateRange->endSql())
->orWhere(fn ($orQuery) => $orQuery->deceased($dateRange->startSql(), $dateRange->endSql()))
->orWhere(fn ($orQuery) => $orQuery->transferred($dateRange->startSql(), $dateRange->endSql()));
}),
};
$historyIds = $migrationQuery
->distinct()
->pluck('medical_history_id')
->filter()
->values()
->all();
if (empty($historyIds)) {
return $onlyIds ? collect() : collect();
}
$query = MedicalHistory::query()
->whereIn('id', $historyIds)
->with([
'latestMigration' => fn ($builder) => $builder->where('stationar_branch_id', $branchId),
'migrations' => fn ($builder) => $builder
->where('stationar_branch_id', $branchId)
->orderByDesc('ingoing_date'),
'operations',
])
->orderByDesc('recipient_date');
if ($onlyIds) {
return $query->pluck('original_id');
}
return $query->get();
}
private function useMaterializedViews(): bool
{
return Schema::hasTable('mv_medicalhistory_summary') && Schema::hasTable('mv_migrationpatient_details');
}
}