diff --git a/app/Http/Controllers/Web/DutyReportController.php b/app/Http/Controllers/Web/DutyReportController.php index 0b4d7ba..ddd6407 100644 --- a/app/Http/Controllers/Web/DutyReportController.php +++ b/app/Http/Controllers/Web/DutyReportController.php @@ -14,12 +14,14 @@ use App\Services\DateRangeService; use App\Services\DutyMedicalHistoryService; use App\Services\DutyReportService; use App\Services\MedicalHistoryService; +use App\Services\MetrikaService; use App\Services\NurseMedicalHistoryService; use App\Services\NurseReportService; use App\Services\UnifiedMedicalHistoryService; use Barryvdh\DomPDF\Facade\Pdf; use Illuminate\Http\Request; use Illuminate\Support\Carbon; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\Auth; use Inertia\Inertia; @@ -31,6 +33,7 @@ class DutyReportController extends Controller protected DutyMedicalHistoryService $dutyMedicalHistoryService, protected DutyReportService $dutyReportService, protected NurseMedicalHistoryService $nurseMedicalHistoryService, + protected MetrikaService $metrikaService, ) {} @@ -59,7 +62,7 @@ class DutyReportController extends Controller ->where('period_start', '>=', $dateRange->startSql()) ->where('period_end', '<=', $dateRange->endSql()) ->orderBy('period_end', 'desc') - ->with(['unwantedEvents', 'doctor']) + ->with(['unwantedEvents', 'doctor', 'metrikaResult']) ->get(); $reportsNurse = ReportNurse::where('rf_department_id', $departmentId) @@ -109,7 +112,8 @@ class DutyReportController extends Controller $latestReport = $reportsDuty->first(); - } else { + } + else { // Для прошедшего периода - данные из отчета $patients = $hasDutyReport ? $this->dutyMedicalHistoryService->getGroupedHistories( @@ -146,7 +150,7 @@ class DutyReportController extends Controller 'canSaveReport' => $isRangeOneDay && $user->currentRoleCan('report.create'), 'canEditPastReport' => $user->currentRoleCan('report.edit.past'), 'canSaveNurseReport' => $isRangeOneDay && $user->currentRoleCan('nurse.report.create'), - 'stats' => $this->prepareStats($patients, $nursePatients, $loaded, $bedsInDepartment), + 'stats' => $this->prepareStats($reportsDuty, $patients, $nursePatients, $loaded, $bedsInDepartment), 'dates' => [ $dateRange->startDate->getTimestampMs(), $dateRange->endDate->getTimestampMs(), @@ -281,7 +285,7 @@ class DutyReportController extends Controller /** * Подготавливает статистику для отображения на фронтенде */ - private function prepareStats(array $patients, array $nursePatients, int $loaded, ?int $bedsInDepartment): array + private function prepareStats(Collection $reportsDuty, array $patients, array $nursePatients, int $loaded, ?int $bedsInDepartment): array { $deceased = $patients['meta']['counts']['deceased'] ?? 0; // Умершие $discharged = $patients['meta']['counts']['discharged'] ?? 0; // Выписанные @@ -290,6 +294,8 @@ class DutyReportController extends Controller ? round(($deceased / $discharged) * 100, 2) : 0; + $baseMetrics = $this->metrikaService->calculateBaseStatistics($reportsDuty); + return [ 'nurse' => [ 'current' => !empty($nursePatients) @@ -305,13 +311,14 @@ class DutyReportController extends Controller 'duty' => [ 'beds' => $bedsInDepartment ?? 0, 'loaded' => $loaded, - 'current' => $patients['meta']['counts']['in_department'] ?? 0, - 'recipient' => $patients['meta']['counts']['recipient'] ?? 0, - 'discharged' => ($patients['meta']['counts']['discharged'] ?? 0) + ($patients['meta']['counts']['deceased'] ?? 0), + 'current' => $baseMetrics['current'],// $patients['meta']['counts']['in_department'] ?? 0, + 'recipient' => $baseMetrics['recipient'] ?? 0, + 'discharged' => $baseMetrics['outcome'] + ($patients['meta']['counts']['deceased'] ?? 0), //($patients['meta']['counts']['discharged'] ?? 0) + ($patients['meta']['counts']['deceased'] ?? 0), 'deceased' => $patients['meta']['counts']['deceased'] ?? 0, 'lethality' => $lethality, 'surgical_planned' => $patients['meta']['counts']['surgical_planned'] ?? 0, 'surgical_urgent' => $patients['meta']['counts']['surgical_urgent'] ?? 0, + 'staff' => $baseMetrics['staff'] ] ]; } @@ -369,6 +376,8 @@ class DutyReportController extends Controller 'observable_out' => $dateRange->endDate, 'out_reason' => 'Закрыто пользователем' ]); + + return response()->json()->setStatusCode(200); } /** diff --git a/app/Models/ReportDuty.php b/app/Models/ReportDuty.php index 9fb0596..8793dc8 100644 --- a/app/Models/ReportDuty.php +++ b/app/Models/ReportDuty.php @@ -70,6 +70,11 @@ class ReportDuty extends Model return $this->hasMany(DutyUnwantedEvent::class, 'report_duty_id', 'id'); } + public function metrikaResult() + { + return $this->hasMany(DutyReportMetricResult::class, 'rf_report_id', 'id'); + } + public function getLoadedDepartmentAttribute(int $patientsInDepartment) { $beds = DutyReportMetricResult::where('rf_report_id', $this->id) diff --git a/app/Services/DutyReportService.php b/app/Services/DutyReportService.php index d83fc22..92412ed 100644 --- a/app/Services/DutyReportService.php +++ b/app/Services/DutyReportService.php @@ -928,11 +928,11 @@ class DutyReportService $admitted = $stats['admitted'] ?? []; // === Базовые счётчики === - $patientsIsRecipient = array_key_exists('recipient', $statsOverride) + $patientsIsRecipient = array_key_exists('recipient', $statsOverride) && !empty($statsOverride['recipient']) ? $statsOverride['recipient'] : $byStatus['recipient'] ?? 0; - $patientsInDepartment = array_key_exists('in_department', $statsOverride) + $patientsInDepartment = array_key_exists('in_department', $statsOverride) && !empty($statsOverride['in_department']) ? $statsOverride['in_department'] : $byStatus['in_department'] ?? 0; - $patientsIsDischarged = array_key_exists('outcome', $statsOverride) + $patientsIsDischarged = array_key_exists('outcome', $statsOverride) && !empty($statsOverride['outcome']) ? $statsOverride['outcome'] : $byStatus['outcome'] ?? 0; $patientsIsTransferred = $stats['transferred'] ?? 0; $patientsIsDeceased = $byStatus['deceased'] ?? 0; @@ -977,7 +977,7 @@ class DutyReportService $this->saveMetric($reportDuty->id, 8, $patientsInDepartment); // Пациентов в отделении $this->saveMetric($reportDuty->id, 3, $patientsIsRecipient); // Поступило $this->saveMetric($reportDuty->id, 15, $patientsIsDischarged); // Выписано - $this->saveMetric($reportDuty->id, 7, $patientsIsDischarged); // Выписано + $this->saveMetric($reportDuty->id, 7, $patientsIsDischarged); // Выписано $this->saveMetric($reportDuty->id, 13, $patientsIsTransferred); // Переведено $this->saveMetric($reportDuty->id, 9, $patientsIsDeceased); // Умерло diff --git a/app/Services/MetrikaService.php b/app/Services/MetrikaService.php index 041f87d..7b5ab17 100644 --- a/app/Services/MetrikaService.php +++ b/app/Services/MetrikaService.php @@ -6,6 +6,8 @@ use App\Models\MedicalHistory; use App\Models\MedicalHistorySnapshot; use App\Models\Report; use Illuminate\Support\Carbon; +use Illuminate\Support\Collection; +use Illuminate\Support\Facades\DB; class MetrikaService { @@ -110,4 +112,32 @@ class MetrikaService return $metrics; } + + /** + * Подсчитывает базовую статистику для отчетов дежурного врача + * Поступило, выбыло, состоит и мед. персонал + * @param Collection $reportsDuty + * @return array + */ + public function calculateBaseStatistics(Collection $reportsDuty): array + { + $reportIds = $reportsDuty->pluck('id'); // получаем ID всех отчётов + + // Один запрос, который сразу возвращает суммы по каждому типу + $totals = DB::table('duty_report_metric_results') + ->whereIn('rf_report_id', $reportIds) // предполагаем внешний ключ `report_id` + ->whereIn('rf_metrika_item_id', [3, 7, 8, 17]) + ->select('rf_metrika_item_id', DB::raw('SUM(value::integer) as total')) + ->groupBy('rf_metrika_item_id') + ->pluck('total', 'rf_metrika_item_id') + ->toArray(); + + // Формируем итоговый массив с ключами, как у вас + return [ + 'recipient' => $totals[3] ?? 0, + 'outcome' => $totals[7] ?? 0, + 'current' => $totals[8] ?? 0, + 'staff' => $totals[17] ?? 0, + ]; + } } diff --git a/resources/js/Pages/Report/Index.vue b/resources/js/Pages/Report/Index.vue index a26ee54..bf43d98 100644 --- a/resources/js/Pages/Report/Index.vue +++ b/resources/js/Pages/Report/Index.vue @@ -120,10 +120,10 @@ const latestReportObj = ref(props.latestReport ?? { const loading = ref(false) const showUnwantedEventModal = ref(false) const reportForm = ref({ - recipient: props.stats.nurse.recipient, - discharged: props.stats.nurse.discharged, - current: props.stats.nurse.current, - staff: 0, + recipient: props.stats.duty.recipient, + discharged: props.stats.duty.discharged, + current: props.stats.duty.current, + staff: props.stats.duty.staff, observables: [] }) const updateReportForm = (form) => { @@ -199,9 +199,9 @@ const syncPageProps = (pageProps = props) => { latestReportObj.value = pageProps.latestReport ?? { unwanted_events: [] } reportForm.value = { ...reportForm.value, - recipient: pageProps.stats.nurse.recipient, - discharged: pageProps.stats.nurse.discharged, - current: pageProps.stats.nurse.current, + recipient: pageProps.stats.duty.recipient, + discharged: pageProps.stats.duty.discharged, + current: pageProps.stats.duty.current, } }