Files
onboard/app/Domain/ReportDuty/Actions/SaveReportMetricsAction.php
2026-07-31 17:19:03 +09:00

80 lines
4.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Domain\ReportDuty\Actions;
use App\Domain\ReportDuty\Contracts\DutyMetricRepositoryInterface;
use App\Domain\ReportDuty\DTO\SnapshotStatistics;
use App\Domain\ReportDuty\Models\ReportDuty;
use App\Models\DepartmentMetrikaDefault;
use Illuminate\Support\Carbon;
class SaveReportMetricsAction
{
public function __construct(
protected DutyMetricRepositoryInterface $metricRepository,
) {}
public function execute(ReportDuty $report, SnapshotStatistics $stats, int $staff = 0, array $statsOverride = []): void
{
$byStatus = $stats->byStatus;
$byUrgency = $stats->byUrgency;
$admitted = $stats->admitted;
// === Базовые счётчики ===
$patientsIsRecipient = $statsOverride['recipient'] ?? ($byStatus['recipient'] ?? 0);
$patientsInDepartment = $statsOverride['in_department'] ?? ($byStatus['in_department'] ?? 0);
$patientsIsDischarged = $statsOverride['outcome'] ?? $stats->outcome;
$patientsIsTransferred = $stats->transferred;
$patientsIsDeceased = $byStatus['deceased'] ?? 0;
// Ср. койко-день
$totalPatients = $stats->totalPatients;
$totalBedDays = $stats->totalBedDays;
$avgBedDay = $totalPatients > 0 ? round($totalBedDays / $totalPatients, 2) : 0;
// Пред. опер. койко-день
$patientsWithOps = $stats->patientsWithOperations;
$totalPreOpDays = $stats->totalPreopBedDays;
$avgPreOpBedDay = $patientsWithOps > 0 ? round($totalPreOpDays / $patientsWithOps, 2) : 0;
// % загруженности
$bedsInDepartment = DepartmentMetrikaDefault::where('rf_department_id', $report->rf_department_id)
->where('rf_metrika_item_id', 1)
->where('date_end', '>', Carbon::now())
->value('value') ?? 0;
$occupancyPercent = $bedsInDepartment > 0 ? round(($patientsInDepartment * 100) / $bedsInDepartment, 2) : 0;
// % летальности
$mortalityPercent = $totalPatients > 0 ? round(($patientsIsDeceased * 100) / $totalPatients, 2) : 0;
// Операции
$totalOperations = $stats->totalOperations;
$plannedOperations = $stats->plannedOperations;
$urgentOperations = $stats->urgentOperations;
// === Сохранение метрик ===
$this->metricRepository->saveMetric($report->id, 1, $bedsInDepartment); // Кол-во коек
$this->metricRepository->saveMetric($report->id, 8, $patientsInDepartment); // Пациентов в отделении
$this->metricRepository->saveMetric($report->id, 3, $patientsIsRecipient); // Поступило
$this->metricRepository->saveMetric($report->id, 15, $patientsIsDischarged); // Выписано
$this->metricRepository->saveMetric($report->id, 7, $patientsIsDischarged); // Выписано (дубль?)
$this->metricRepository->saveMetric($report->id, 13, $patientsIsTransferred); // Переведено
$this->metricRepository->saveMetric($report->id, 9, $patientsIsDeceased); // Умерло
$this->metricRepository->saveMetric($report->id, 4, $admitted['planned'] ?? 0); // Планово поступило
$this->metricRepository->saveMetric($report->id, 12, $admitted['urgent'] ?? 0); // Экстренно поступило
$this->metricRepository->saveMetric($report->id, 22, $occupancyPercent); // % загруженности
$this->metricRepository->saveMetric($report->id, 25, round($totalBedDays, 2)); // Всего койко-дней
$this->metricRepository->saveMetric($report->id, 18, $avgBedDay); // Ср. койко-день
$this->metricRepository->saveMetric($report->id, 26, round($totalPreOpDays, 2)); // Пред. опер. койко-день (сумма)
$this->metricRepository->saveMetric($report->id, 27, $patientsWithOps); // Пациентов с операциями (знаменатель)
$this->metricRepository->saveMetric($report->id, 21, $avgPreOpBedDay); // Ср. Пред. опер. койко-день
$this->metricRepository->saveMetric($report->id, 19, $mortalityPercent); // % летальности
$this->metricRepository->saveMetric($report->id, 11, $plannedOperations); // Плановых операций
$this->metricRepository->saveMetric($report->id, 10, $urgentOperations); // Экстренных операций
$this->metricRepository->saveMetric($report->id, 17, $staff); // Мед. персонал
}
}