Перевод на DDD

This commit is contained in:
brusnitsyn
2026-07-31 17:19:03 +09:00
parent 2dd55b9d11
commit 2c6d4a04cc
48 changed files with 1796 additions and 0 deletions

View File

@@ -0,0 +1,112 @@
<?php
namespace App\Infrastructure\Services;
use App\Domain\ReportDuty\Contracts\PatientSnapshotProviderInterface;
use App\Models\MedicalHistory;
use App\Services\Classification\PatientStatusClassifier;
use App\Services\DateRange;
use Illuminate\Support\Carbon;
use Illuminate\Support\LazyCollection;
/**
* Извлекает пациентов из МИС (Material View)
*/
class MisPatientSnapshotProvider implements PatientSnapshotProviderInterface
{
public function __construct(
protected PatientStatusClassifier $classifier
) {
}
public function getPatients(DateRange $dateRange, int $departmentId): LazyCollection
{
$startYear = Carbon::now()->startOfYear()->format('Y-m-d');
return MedicalHistory::query()
->whereHas('migrations', function ($q) use ($departmentId, $dateRange, $startYear) {
$q->where('department_id', $departmentId)
->where('ingoing_date', '<=', $dateRange->endSql())
->where(function ($sub) use ($dateRange, $startYear) {
$sub->whereNull('out_date')
->where('ingoing_date', '>', $startYear);
$sub->orWhere(function ($sub2) use ($dateRange, $startYear) {
$sub2->whereNotNull('out_date')
->where('out_date', '>', $dateRange->startSql())
->where('out_date', '>', $startYear);
});
});
})
->with([
'latestMigration' => function ($q) use ($departmentId) {
$q->where('department_id', $departmentId);
},
'latestMigration.operations',
'latestMigration.reanimations',
'migrations' => function ($q) use ($departmentId, $dateRange, $startYear) {
$q->where('department_id', $departmentId)
->where('ingoing_date', '<=', $dateRange->endSql())
->where(function ($sub) use ($dateRange, $startYear) {
$sub->whereNull('out_date')
->where('ingoing_date', '>', $startYear);
$sub->orWhere(function ($sub2) use ($dateRange, $startYear) {
$sub2->whereNotNull('out_date')
->where('out_date', '>', $dateRange->startSql())
->where('out_date', '>', $startYear);
});
});
},
'migrations.reanimations' => function ($q) use ($dateRange) {
$q->where(function ($sub) use ($dateRange) {
$sub->whereNull('out_date')
->orWhere('out_date', '>=', $dateRange->startSql());
});
},
'operations' => function ($q) use ($departmentId, $dateRange) {
$q->where('department_id', $departmentId)
->where('start_date', '>=', $dateRange->startSql())
->where('start_date', '<', $dateRange->endSql());
}
])
->lazy()
->map(function (MedicalHistory $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 = $this->classifier::classifyReanimation($h->latestMigration?->reanimations, $dateRange);
}
return new PatientSnapshotData(
id: $h->id,
sourceType: 'mis',
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: $h->migrations->map(fn($m) => $m->toArray())->toArray(),
operations: $h->operations->toArray(),
patientStatus: $patientStatus,
patientUrgency: $patientUrgency,
periodFlags: $periodFlags,
inReanimation: $patientReanimation,
admittedToday: $this->classifier::classifyAdmitted($h->latestMigration?->ingoing_date, $dateRange),
inObservable: $this->classifier::classifyObservable($h->observable, $dateRange),
);
});
}
}

View File

@@ -0,0 +1,106 @@
<?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,
);
});
}
}