107 lines
4.6 KiB
PHP
107 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Infrastructure\Services;
|
|
|
|
use App\Domain\ReportDuty\Contracts\PatientSnapshotProviderInterface;
|
|
use App\Domain\ReportDuty\DTO\PatientSnapshotData;
|
|
use App\Models\ReportNurse;
|
|
use App\Models\ReportNursePatient;
|
|
use App\Services\Classification\PatientStatusClassifier;
|
|
use App\Services\DateRange;
|
|
use Illuminate\Support\LazyCollection;
|
|
|
|
/**
|
|
* Извлекает пациентов из отчёта медсестры (ReportNurse)
|
|
*/
|
|
class NurseReportPatientSnapshotProvider implements PatientSnapshotProviderInterface
|
|
{
|
|
public function __construct(
|
|
protected PatientStatusClassifier $classifier
|
|
) {
|
|
}
|
|
|
|
public function getPatients(DateRange $dateRange, int $departmentId): LazyCollection
|
|
{
|
|
// Здесь нужно получить ReportNurse для данного отделения и периода
|
|
$nurseReport = ReportNurse::where('rf_department_id', $departmentId)
|
|
->where('period_start', $dateRange->startSql())
|
|
->where('period_end', $dateRange->endSql())
|
|
->latest('id')
|
|
->first();
|
|
|
|
if (!$nurseReport) {
|
|
return LazyCollection::make([]);
|
|
}
|
|
|
|
return ReportNursePatient::query()
|
|
->where('report_nurse_id', $nurseReport->id)
|
|
->with([
|
|
'migrations',
|
|
'operations' => function ($q) use ($dateRange) {
|
|
$q->where('start_date', '>=', $dateRange->startSql())
|
|
->where('start_date', '<', $dateRange->endSql());
|
|
},
|
|
'latestMigration',
|
|
])
|
|
->lazy()
|
|
->map(function (ReportNursePatient $h) use ($dateRange) {
|
|
$patientStatus = $this->classifier::classify($h, $dateRange);
|
|
$periodFlags = $this->classifier::classifyPeriodFlags($h, $dateRange);
|
|
$patientUrgency = null;
|
|
$patientReanimation = null;
|
|
if (!in_array($patientStatus, [
|
|
PatientStatusClassifier::STATUS_DECEASED,
|
|
PatientStatusClassifier::STATUS_DISCHARGED,
|
|
PatientStatusClassifier::STATUS_TRANSFERRED
|
|
])) {
|
|
$patientUrgency = $this->classifier::classifyUrgency($h->urgency_id);
|
|
$patientReanimation = false; // в отчёте медсестры нет реанимации
|
|
}
|
|
|
|
// Преобразуем миграции в массив (как в оригинале)
|
|
$migrations = $h->migrations->map(fn($m) => [
|
|
'id' => $m->original_id,
|
|
'ingoing_date' => $m->ingoing_date,
|
|
'out_date' => $m->out_date,
|
|
'diagnosis_id' => $m->diagnosis_id,
|
|
'diagnosis_code' => $m->diagnosis_code,
|
|
'diagnosis_name' => $m->diagnosis_name,
|
|
'interrupted_event_id' => $m->interrupted_event_id,
|
|
'stationar_branch_id' => $m->stationar_branch_id,
|
|
'department_id' => $m->department_id,
|
|
'visit_result_id' => $m->visit_result_id,
|
|
'stat_cure_result_id' => $m->stat_cure_result_id,
|
|
'user_id' => $m->user_id,
|
|
'mis_user_id' => $m->mis_user_id,
|
|
'comment' => $m->comment,
|
|
'reanimations' => [],
|
|
])->toArray();
|
|
|
|
return new PatientSnapshotData(
|
|
id: $h->original_id,
|
|
sourceType: 'nurse_report',
|
|
medicalCardNumber: $h->medical_card_number,
|
|
fullName: $h->full_name,
|
|
birthDate: $h->birth_date,
|
|
recipientDate: $h->recipient_date,
|
|
extractDate: $h->extract_date,
|
|
deathDate: $h->death_date,
|
|
male: $h->male,
|
|
urgencyId: $h->urgency_id,
|
|
visitResultId: $h->visit_result_id,
|
|
hospitalResultId: $h->hospital_result_id,
|
|
comment: $h->comment,
|
|
profitTypeId: $h->profit_type_id,
|
|
migrations: $migrations,
|
|
operations: $h->operations->toArray(),
|
|
patientStatus: $patientStatus,
|
|
patientUrgency: $patientUrgency,
|
|
periodFlags: $periodFlags,
|
|
inReanimation: $patientReanimation,
|
|
admittedToday: $this->classifier::classifyAdmitted($h->latestMigration?->ingoing_date, $dateRange),
|
|
inObservable: false,
|
|
);
|
|
});
|
|
}
|
|
}
|