Files
onboard/app/Infrastructure/Reports/Sources/MisPatientSource.php
2026-05-06 22:32:11 +09:00

151 lines
5.6 KiB
PHP

<?php
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;
use Illuminate\Support\Collection;
class MisPatientSource
{
public function __construct(
private readonly PatientService $patientService,
) {}
/**
* Загрузить пациентов из МИС и нормализовать их в DTO для отчёта.
*/
public function getDtos(
User $user,
string $status,
DateRange $dateRange,
int $branchId,
?bool $includeCurrent = null,
bool $fillableAuto = false,
bool $forSnapshots = false
): Collection {
return $this->getPatients($user, $status, $dateRange, $branchId, $includeCurrent, $fillableAuto, $forSnapshots)
->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();
}
/**
* Загрузить сырые MIS-модели пациентов для запрошенного статуса отчёта.
*/
public function getPatients(
User $user,
string $status,
DateRange $dateRange,
int $branchId,
?bool $includeCurrent = null,
bool $fillableAuto = false,
bool $forSnapshots = false
): Collection {
$isHeadOrAdmin = $user->isHeadOfDepartment() || $user->isAdmin();
$includeCurrent = $includeCurrent ?? in_array($status, ['plan', 'emergency'], true);
return match ($status) {
'plan', 'emergency' => $this->patientService->getPlanOrEmergencyPatients(
$status,
$isHeadOrAdmin,
$branchId,
$dateRange,
false,
false,
$includeCurrent,
$fillableAuto
),
'current' => $this->patientService->getAllPatientsInDepartment(
$isHeadOrAdmin,
$branchId,
$dateRange,
false,
false,
$fillableAuto
),
'recipient' => $this->patientService->getPlanOrEmergencyPatients(
null,
$isHeadOrAdmin,
$branchId,
$dateRange,
false,
false,
false,
$fillableAuto
),
'outcome' => $this->patientService->getOutcomePatients($branchId, $dateRange, 'without-transferred'),
'outcome-discharged' => $this->patientService->getOutcomePatients($branchId, $dateRange, 'discharged'),
'outcome-transferred' => $this->patientService->getOutcomePatients($branchId, $dateRange, 'transferred'),
'outcome-deceased' => $this->patientService->getOutcomePatients($branchId, $dateRange, 'deceased'),
'reanimation' => $this->patientService->getReanimationPatients($branchId, $dateRange),
default => collect(),
};
}
/**
* Посчитать MIS-пациентов без материализации DTO.
*/
public function getCount(
User $user,
string $status,
DateRange $dateRange,
int $branchId,
?bool $includeCurrent = null,
bool $fillableAuto = false
): int {
$isHeadOrAdmin = $user->isHeadOfDepartment() || $user->isAdmin();
$includeCurrent = $includeCurrent ?? in_array($status, ['plan', 'emergency'], true);
return match ($status) {
'plan', 'emergency' => $includeCurrent
? $this->patientService->getPatientsCountWithCurrent($status, $isHeadOrAdmin, $branchId, $dateRange)
: $this->patientService->getPlanOrEmergencyPatients(
$status,
$isHeadOrAdmin,
$branchId,
$dateRange,
true,
false,
false,
$fillableAuto
),
'current' => $this->patientService->getAllPatientsInDepartment(
$isHeadOrAdmin,
$branchId,
$dateRange,
true,
false,
$fillableAuto
),
'recipient' => $this->patientService->getPlanOrEmergencyPatients(
null,
$isHeadOrAdmin,
$branchId,
$dateRange,
true,
false,
false,
$fillableAuto
),
'outcome' => $this->patientService->getOutcomePatients($branchId, $dateRange, 'without-transferred', true)->count(),
'outcome-discharged' => $this->patientService->getOutcomePatients($branchId, $dateRange, 'discharged', true)->count(),
'outcome-transferred' => $this->patientService->getOutcomePatients($branchId, $dateRange, 'transferred', true)->count(),
'outcome-deceased' => $this->patientService->getOutcomePatients($branchId, $dateRange, 'deceased', true)->count(),
'reanimation' => $this->patientService->getReanimationPatients($branchId, $dateRange, true)->count(),
default => 0,
};
}
}