diff --git a/app/Services/DutyReportService.php b/app/Services/DutyReportService.php index ac6a888..a3240c7 100644 --- a/app/Services/DutyReportService.php +++ b/app/Services/DutyReportService.php @@ -10,6 +10,7 @@ use App\Models\MetrikaResult; use App\Models\ObservableMedicalHistory; use App\Models\ReportDuty; use App\Models\ReportNurse; +use App\Models\ReportNursePatient; use App\Models\UnifiedMedicalHistory; use App\Models\User; use App\Services\Classification\PatientStatusClassifier; @@ -80,9 +81,37 @@ class DutyReportService { $departmentId = $departmentId ?? $reportDuty->department->rf_mis_department_id; $userId = $userId ?? $reportDuty->rf_user_id; + + // Если за этот же период есть отчет медсестры - берём пациентов (и метрики) из него + $nurseReport = ReportNurse::where('rf_department_id', $reportDuty->rf_department_id) + ->where('period_start', $reportDuty->period_start) + ->where('period_end', $reportDuty->period_end) + ->latest('id') + ->first(); + + if ($nurseReport) { + $patients = $this->getPatientsFromNurseReport($nurseReport, $dateRange); + } else { + $patients = $this->getPatientsFromMis($dateRange, $departmentId); + } + + $savedStats = $this->saveReportSnapshot($reportDuty->id, $patients, $userId, $departmentId, $dateRange); + + return [ + ...$savedStats, + 'report_date' => $dateRange->startSql(), + 'department_id' => $departmentId, + ]; + } + + /** + * Снимок пациентов из МИС (используется, если нет отчета медсестры за период) + */ + private function getPatientsFromMis(DateRange $dateRange, int $departmentId) + { $startYear = Carbon::now()->startOfYear()->format('Y-m-d'); - $patients = MedicalHistory::query() + return MedicalHistory::query() ->whereHas('migrations', function ($q) use ($departmentId, $dateRange, $startYear) { $q->where('department_id', $departmentId) ->where('ingoing_date', '<=', $dateRange->endSql()) @@ -160,14 +189,75 @@ class DutyReportService 'in_observable' => PatientStatusClassifier::classifyObservable($h->observable, $dateRange), ]; }); + } - $savedStats = $this->saveReportSnapshot($reportDuty->id, $patients, $userId, $departmentId, $dateRange); + /** + * Снимок пациентов из отчета медсестры за тот же период/отделение + */ + private function getPatientsFromNurseReport(ReportNurse $nurseReport, DateRange $dateRange) + { + return ReportNursePatient::query() + ->where('report_nurse_id', $nurseReport->id) + ->with(['migrations', 'operations', 'latestMigration']) + ->get() + ->map(function (ReportNursePatient $h) use ($dateRange) { + $patientStatus = PatientStatusClassifier::classify($h, $dateRange); + $periodFlags = PatientStatusClassifier::classifyPeriodFlags($h, $dateRange); + $patientUrgency = null; + $patientReanimation = null; + if (!in_array($patientStatus, [ + PatientStatusClassifier::STATUS_DECEASED, + PatientStatusClassifier::STATUS_DISCHARGED, + PatientStatusClassifier::STATUS_TRANSFERRED + ])) { + $patientUrgency = PatientStatusClassifier::classifyUrgency($h->urgency_id); + $patientReanimation = false; // в отчете медсестры данные о реанимации не фиксируются + } - return [ - ...$savedStats, - 'report_date' => $dateRange->startSql(), - 'department_id' => $departmentId, - ]; + return [ + 'id' => $h->original_id, + 'source_type' => $h->source_type, + 'medical_card_number' => $h->medical_card_number, + 'full_name' => $h->full_name, + 'birth_date' => $h->birth_date, + 'recipient_date' => $h->recipient_date, + 'extract_date' => $h->extract_date, + 'death_date' => $h->death_date, + 'male' => $h->male, + 'urgency_id' => $h->urgency_id, + 'hospital_result_id' => $h->hospital_result_id, + 'visit_result_id' => $h->visit_result_id, + 'comment' => $h->comment, + + '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(), + + 'operations' => $h->operations->toArray(), + + // + вычисляемые мета-поля для фронтенда + 'patient_status' => $patientStatus, + 'patient_urgency' => $patientUrgency, + 'period_flags' => $periodFlags, + 'in_reanimation' => $patientReanimation, + 'admitted_today' => PatientStatusClassifier::classifyAdmitted($h->latestMigration?->ingoing_date, $dateRange), + 'in_observable' => false, + ]; + }); } public function saveReportSnapshot( @@ -239,7 +329,7 @@ class DutyReportService $patientData = [ 'report_duty_id' => $reportDutyId, - 'source_type' => 'mis', + 'source_type' => $patient['source_type'] ?? 'mis', 'original_id' => $patient['id'], 'medical_card_number' => $patient['medical_card_number'], 'full_name' => $patient['full_name'], @@ -270,7 +360,7 @@ class DutyReportService $migrationItem = [ '_temp_key' => [ 'report_duty_id' => $reportDutyId, - 'source_type' => 'mis', + 'source_type' => $patient['source_type'] ?? 'mis', 'original_id' => $patient['id'], ], 'original_id' => $migration['id'], diff --git a/app/Services/NurseReportService.php b/app/Services/NurseReportService.php index e1ce24e..4dbdc53 100644 --- a/app/Services/NurseReportService.php +++ b/app/Services/NurseReportService.php @@ -159,6 +159,7 @@ class NurseReportService 'source_type' => $patient->source_type, 'original_id' => $patient->original_id, ], + 'original_id' => $migration->id, 'ingoing_date' => $migration->ingoing_date, 'out_date' => $migration->out_date, 'diagnosis_id' => $migration->diagnosis_id, @@ -235,6 +236,7 @@ class NurseReportService if (isset($patientIds[$originalId])) { $finalMigrations[] = [ 'medical_history_id' => $patientIds[$originalId], // Реальный ID + 'original_id' => $m['original_id'], 'ingoing_date' => $m['ingoing_date'], 'out_date' => $m['out_date'], 'diagnosis_id' => $m['diagnosis_id'],