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

@@ -1,42 +0,0 @@
<?php
namespace App\Infrastructure\Reports\Sources;
use App\Domain\Reports\Contracts\PatientSource;
use App\Domain\Reports\Models\PatientCollection;
use App\Domain\Reports\Models\ReportContext;
use App\Models\Department;
use App\Models\User;
use App\Services\DateRange;
use App\Infrastructure\Reports\Adapters\LegacyReportServiceAdapter;
use Carbon\Carbon;
final readonly class LegacyAutoFillPatientSource implements PatientSource
{
public function __construct(
private LegacyReportServiceAdapter $legacyAdapter,
) {}
public function load(ReportContext $context): PatientCollection
{
$department = Department::query()->findOrFail($context->departmentId);
$user = User::query()->findOrFail($context->actorUserId ?? $context->userId);
$scopedUser = clone $user;
$scopedUser->rf_department_id = $department->department_id;
$scopedUser->setRelation('department', $department);
$dateRange = new DateRange(
startDate: Carbon::parse($context->periodStart->format('Y-m-d H:i:s'), 'Asia/Yakutsk'),
endDate: Carbon::parse($context->periodEnd->format('Y-m-d H:i:s'), 'Asia/Yakutsk'),
startDateRaw: $context->periodStart->format('Y-m-d H:i:s'),
endDateRaw: $context->periodEnd->format('Y-m-d H:i:s'),
isOneDay: $context->periodStart->diff($context->periodEnd)->days <= 1,
);
return new PatientCollection(
items: [],
metadata: [
'payload' => $this->legacyAdapter->buildAutoFillPayload($scopedUser, $department, $dateRange),
],
);
}
}

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');
}
}

View File

@@ -3,6 +3,7 @@
namespace App\Infrastructure\Reports\Sources;
use App\Data\UnifiedPatientData;
use App\Models\MedicalHistory;
use App\Models\User;
use App\Services\DateRange;
use App\Services\PatientService;
@@ -27,10 +28,15 @@ class MisPatientSource
bool $forSnapshots = false
): Collection {
return $this->getPatients($user, $status, $dateRange, $branchId, $includeCurrent, $fillableAuto, $forSnapshots)
->map(fn ($patient) => UnifiedPatientData::fromMisMedicalHistory(
$patient,
(bool) ($patient->is_recipient_today ?? false),
))
->map(fn ($patient) => $patient instanceof MedicalHistory
? UnifiedPatientData::fromMedicalHistory(
$patient,
(bool) ($patient->is_recipient_today ?? false),
)
: UnifiedPatientData::fromMisMedicalHistory(
$patient,
(bool) ($patient->is_recipient_today ?? false),
))
->sortByDesc(fn (UnifiedPatientData $patient) => $patient->admittedAt ?? '')
->values();
}

View File

@@ -4,8 +4,8 @@ namespace App\Infrastructure\Reports\Sources;
use App\Data\UnifiedPatientData;
use App\Models\DepartmentPatientOperation;
use App\Models\MedicalHistory;
use App\Models\MedicalHistorySnapshot;
use App\Models\MisMedicalHistory;
use Illuminate\Support\Collection;
class SnapshotPatientSource
@@ -181,15 +181,15 @@ class SnapshotPatientSource
return [];
}
return MisMedicalHistory::query()
->whereIn('MedicalHistoryID', $historyIds)
->with(['surgicalOperations.serviceMedical'])
return MedicalHistory::query()
->whereIn('original_id', $historyIds)
->with(['operations'])
->get()
->mapWithKeys(function (MisMedicalHistory $history) {
->mapWithKeys(function (MedicalHistory $history) {
return [
$history->MedicalHistoryID => $history->surgicalOperations->map(fn ($operation) => [
'code' => $operation->serviceMedical?->ServiceMedicalCode,
'name' => $operation->serviceMedical?->ServiceMedicalName,
$history->original_id => $history->operations->map(fn ($operation) => [
'code' => $operation->service_medical_code ?? null,
'name' => $operation->service_medical_name ?? null,
])->values()->all(),
];
})