Перевод на доменную архитектуру
This commit is contained in:
154
app/Infrastructure/Reports/Services/ReportMetricsFinalizer.php
Normal file
154
app/Infrastructure/Reports/Services/ReportMetricsFinalizer.php
Normal file
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
|
||||
namespace App\Infrastructure\Reports\Services;
|
||||
|
||||
use App\Domain\Reports\Calculators\BedDaysCalculator;
|
||||
use App\Domain\Reports\Calculators\DepartmentLoadCalculator;
|
||||
use App\Domain\Reports\Calculators\PreoperativeDaysCalculator;
|
||||
use App\Domain\Reports\Models\OperationInterval;
|
||||
use App\Domain\Reports\Models\StayInterval;
|
||||
use App\Domain\Reports\ValueObjects\MetrikaConfig;
|
||||
use App\Models\MedicalHistorySnapshot;
|
||||
use App\Models\Report;
|
||||
use DateTimeImmutable;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ReportMetricsFinalizer
|
||||
{
|
||||
public function __construct(
|
||||
private readonly BedDaysCalculator $bedDaysCalculator,
|
||||
private readonly PreoperativeDaysCalculator $preoperativeDaysCalculator,
|
||||
private readonly DepartmentLoadCalculator $departmentLoadCalculator,
|
||||
private readonly ReportStorageService $reportStorageService,
|
||||
) {}
|
||||
|
||||
public function finalize(Report $report): void
|
||||
{
|
||||
$this->saveBedDaysMetrics($report);
|
||||
$this->savePreoperativeMetrics($report);
|
||||
$this->saveDepartmentLoadMetric($report);
|
||||
}
|
||||
|
||||
private function saveBedDaysMetrics(Report $report): void
|
||||
{
|
||||
$result = $this->bedDaysCalculator->calculate($this->buildStayIntervals($report));
|
||||
|
||||
$this->reportStorageService->saveMetric($report, MetrikaConfig::TOTAL_BED_DAYS, $result->total);
|
||||
$this->reportStorageService->saveMetric($report, MetrikaConfig::AVERAGE_BED_DAYS, $result->average);
|
||||
}
|
||||
|
||||
private function savePreoperativeMetrics(Report $report): void
|
||||
{
|
||||
$result = $this->preoperativeDaysCalculator->calculate($this->buildOperationIntervals($report));
|
||||
|
||||
$this->reportStorageService->saveMetric($report, MetrikaConfig::TOTAL_PREOPERATIVE_DAYS, $result->total);
|
||||
$this->reportStorageService->saveMetric($report, MetrikaConfig::PREOPERATIVE_PATIENT_COUNT, $result->count);
|
||||
$this->reportStorageService->saveMetric($report, MetrikaConfig::PREOPERATIVE_AVERAGE_DAYS, $result->average);
|
||||
}
|
||||
|
||||
private function saveDepartmentLoadMetric(Report $report): void
|
||||
{
|
||||
$currentCount = (float) ($report->metrikaResults()->where('rf_metrika_item_id', MetrikaConfig::CURRENT)->value('value') ?? 0);
|
||||
$bedsCount = (float) ($report->metrikaResults()->where('rf_metrika_item_id', MetrikaConfig::BEDS)->value('value') ?? 0);
|
||||
|
||||
$this->reportStorageService->saveMetric(
|
||||
$report,
|
||||
MetrikaConfig::DEPARTMENT_LOADED,
|
||||
$this->departmentLoadCalculator->calculate($currentCount, $bedsCount),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, StayInterval>
|
||||
*/
|
||||
private function buildStayIntervals(Report $report): array
|
||||
{
|
||||
$snapshots = MedicalHistorySnapshot::query()
|
||||
->where('rf_report_id', $report->report_id)
|
||||
->whereIn('patient_type', ['discharged', 'deceased'])
|
||||
->with('medicalHistory')
|
||||
->get();
|
||||
|
||||
$intervals = [];
|
||||
|
||||
foreach ($snapshots as $snapshot) {
|
||||
$history = $snapshot->medicalHistory;
|
||||
|
||||
if (! $history) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$startRaw = $history->DateRecipientHS ?? $history->DateRecipient ?? null;
|
||||
$endRaw = null;
|
||||
|
||||
if ($snapshot->patient_type === 'deceased') {
|
||||
if ($history->DateDeath && ! in_array($history->DateDeath->format('Y-m-d'), ['1900-01-01', '2222-01-01'], true)) {
|
||||
$endRaw = $history->DateDeath;
|
||||
} elseif ($history->DateExtract && ! in_array($history->DateExtract->format('Y-m-d'), ['1900-01-01', '2222-01-01'], true)) {
|
||||
$endRaw = $history->DateExtract;
|
||||
}
|
||||
} elseif ($history->DateExtract && ! in_array($history->DateExtract->format('Y-m-d'), ['1900-01-01', '2222-01-01'], true)) {
|
||||
$endRaw = $history->DateExtract;
|
||||
}
|
||||
|
||||
if (! $startRaw || ! $endRaw) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$intervals[] = new StayInterval(
|
||||
startAt: new DateTimeImmutable((string) $startRaw),
|
||||
endAt: new DateTimeImmutable((string) $endRaw),
|
||||
);
|
||||
}
|
||||
|
||||
return $intervals;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, OperationInterval>
|
||||
*/
|
||||
private function buildOperationIntervals(Report $report): array
|
||||
{
|
||||
$patientIds = MedicalHistorySnapshot::query()
|
||||
->where('rf_report_id', $report->report_id)
|
||||
->whereIn('patient_type', ['discharged', 'deceased'])
|
||||
->pluck('rf_medicalhistory_id')
|
||||
->unique()
|
||||
->values();
|
||||
|
||||
if ($patientIds->isEmpty()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$rows = DB::table('stt_medicalhistory as mh')
|
||||
->join('stt_surgicaloperation as so', 'so.rf_MedicalHistoryID', '=', 'mh.MedicalHistoryID')
|
||||
->whereIn('mh.MedicalHistoryID', $patientIds)
|
||||
->whereNotNull('so.Date')
|
||||
->select(
|
||||
'mh.MedicalHistoryID',
|
||||
DB::raw('MIN(so."Date") as first_operation'),
|
||||
'mh.DateRecipientHS',
|
||||
'mh.DateRecipient'
|
||||
)
|
||||
->groupBy('mh.MedicalHistoryID', 'mh.DateRecipientHS', 'mh.DateRecipient')
|
||||
->get();
|
||||
|
||||
$intervals = [];
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$startRaw = $row->DateRecipientHS ?? $row->DateRecipient ?? null;
|
||||
$operationRaw = $row->first_operation ?? null;
|
||||
|
||||
if (! $startRaw || ! $operationRaw) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$intervals[] = new OperationInterval(
|
||||
admittedAt: new DateTimeImmutable((string) $startRaw),
|
||||
operationAt: new DateTimeImmutable((string) $operationRaw),
|
||||
);
|
||||
}
|
||||
|
||||
return $intervals;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user