Изменения в основном 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

@@ -6,11 +6,13 @@ use App\Services\DateRange;
use App\Services\OutcomePatientService;
use App\Services\RecipientPatientService;
use App\Services\CurrentPatientService;
use App\Models\MedicalHistory;
use App\Models\MisMedicalHistory;
use App\Models\MisMigrationPatient;
use App\Models\MisReanimation;
use App\Models\MisSurgicalOperation;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Schema;
/**
* Query-источник для пациентских выборок из МИС и связанных клинических агрегатов.
@@ -70,7 +72,10 @@ class MisClinicalDataSource
return $this->buildPatientCardsQuery($medicalHistoryIds, $branchId)
->get()
->map(function ($patient) use ($recipientIds) {
$patient->is_recipient_today = in_array($patient->MedicalHistoryID, $recipientIds, true);
$patientId = $patient instanceof MedicalHistory
? (int) ($patient->original_id ?? $patient->id)
: (int) $patient->MedicalHistoryID;
$patient->is_recipient_today = in_array($patientId, $recipientIds, true);
return $patient;
});
@@ -94,7 +99,9 @@ class MisClinicalDataSource
$currentIds = $fillableAuto
? $this->currentPatientService->getHistoricalCurrentMedicalHistoryIds(null, $branchId, $dateRange)
: MisMigrationPatient::currentlyInTreatment($branchId)->pluck('rf_MedicalHistoryID')->toArray();
: ($this->useMaterializedViews()
? $this->currentPatientService->getCurrentMedicalHistoryIds('all', $branchId, $dateRange, false)
: MisMigrationPatient::currentlyInTreatment($branchId)->pluck('rf_MedicalHistoryID')->toArray());
$allIds = array_unique(array_merge($recipientIds, $currentIds));
@@ -113,7 +120,10 @@ class MisClinicalDataSource
return $this->buildPatientCardsQuery($allIds, $branchId)
->get()
->map(function ($patient) use ($recipientIds) {
$patient->is_recipient_today = in_array($patient->MedicalHistoryID, $recipientIds, true);
$patientId = $patient instanceof MedicalHistory
? (int) ($patient->original_id ?? $patient->id)
: (int) $patient->MedicalHistoryID;
$patient->is_recipient_today = in_array($patientId, $recipientIds, true);
return $patient;
});
@@ -169,20 +179,42 @@ class MisClinicalDataSource
$reanimationDateByMedicalHistory = $reanimationByMedicalHistory->pluck('reanimation_date_in', 'medical_history_id');
$reanimationCompleteByMedicalHistory = $reanimationByMedicalHistory->pluck('reanimation_is_complete', 'medical_history_id');
return MisMedicalHistory::whereIn('MedicalHistoryID', $medicalHistoryIds)
->select($this->patientSelect())
->with($this->patientRelations($branchId))
->orderBy('DateRecipient', 'DESC')
->get()
->map(function ($patient) use ($reanimationDateByMedicalHistory, $reanimationCompleteByMedicalHistory) {
$reanimationDateIn = $reanimationDateByMedicalHistory->get($patient->MedicalHistoryID);
if ($reanimationDateIn) {
$patients = $this->useMaterializedViews()
? MedicalHistory::query()
->whereIn('original_id', $medicalHistoryIds)
->with([
'operations',
'latestMigration' => fn ($query) => $query->where('stationar_branch_id', $branchId),
'migrations' => fn ($query) => $query
->where('stationar_branch_id', $branchId)
->orderByDesc('ingoing_date'),
])
->orderByDesc('recipient_date')
->get()
: MisMedicalHistory::whereIn('MedicalHistoryID', $medicalHistoryIds)
->select($this->patientSelect())
->with($this->patientRelations($branchId))
->orderBy('DateRecipient', 'DESC')
->get();
return $patients->map(function ($patient) use ($reanimationDateByMedicalHistory, $reanimationCompleteByMedicalHistory) {
$patientId = $patient instanceof MedicalHistory
? (int) ($patient->original_id ?? $patient->id)
: (int) $patient->MedicalHistoryID;
$reanimationDateIn = $reanimationDateByMedicalHistory->get($patientId);
if ($reanimationDateIn) {
if ($patient instanceof MedicalHistory) {
$patient->recipient_date = $reanimationDateIn;
} else {
$patient->DateRecipient = $reanimationDateIn;
}
$patient->reanimation_is_complete = (bool) $reanimationCompleteByMedicalHistory->get($patient->MedicalHistoryID, false);
}
return $patient;
});
$patient->reanimation_is_complete = (bool) $reanimationCompleteByMedicalHistory->get($patientId, false);
return $patient;
});
}
/**
@@ -233,6 +265,19 @@ class MisClinicalDataSource
*/
public function buildPatientCardsQuery(array $medicalHistoryIds, int $branchId)
{
if ($this->useMaterializedViews()) {
return MedicalHistory::query()
->whereIn('original_id', $medicalHistoryIds)
->with([
'operations',
'latestMigration' => fn ($query) => $query->where('stationar_branch_id', $branchId),
'migrations' => fn ($query) => $query
->where('stationar_branch_id', $branchId)
->orderByDesc('ingoing_date'),
])
->orderByDesc('recipient_date');
}
return MisMedicalHistory::query()
->whereIn('MedicalHistoryID', $medicalHistoryIds)
->select($this->patientSelect())
@@ -324,4 +369,9 @@ class MisClinicalDataSource
},
];
}
private function useMaterializedViews(): bool
{
return Schema::hasTable('mv_medicalhistory_summary') && Schema::hasTable('mv_migrationpatient_details');
}
}