81 lines
2.7 KiB
PHP
81 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Infrastructure\Reports\Services;
|
|
|
|
use App\Models\ReanimationPatientIndicator;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Collection;
|
|
|
|
/**
|
|
* Управляет индикаторами пациентов реанимации для отчётного интерфейса.
|
|
*/
|
|
class ReanimationIndicatorService
|
|
{
|
|
public function save(
|
|
User $user,
|
|
int $departmentId,
|
|
int $medicalHistoryId,
|
|
string $indicator,
|
|
?string $comment = null,
|
|
?int $reportId = null
|
|
): ReanimationPatientIndicator {
|
|
return ReanimationPatientIndicator::query()->create([
|
|
'rf_department_id' => $departmentId,
|
|
'rf_report_id' => $reportId,
|
|
'rf_medicalhistory_id' => $medicalHistoryId,
|
|
'indicator' => $indicator,
|
|
'comment' => $comment,
|
|
'created_by' => $user->id,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @param array<int, int> $medicalHistoryIds
|
|
* @return Collection<int, ReanimationPatientIndicator>
|
|
*/
|
|
public function latestByMedicalHistory(int $departmentId, array $medicalHistoryIds): Collection
|
|
{
|
|
if (empty($medicalHistoryIds)) {
|
|
return collect();
|
|
}
|
|
|
|
$subQuery = ReanimationPatientIndicator::query()
|
|
->selectRaw('MAX(reanimation_patient_indicator_id) as max_id, rf_medicalhistory_id')
|
|
->where('rf_department_id', $departmentId)
|
|
->whereIn('rf_medicalhistory_id', $medicalHistoryIds)
|
|
->groupBy('rf_medicalhistory_id');
|
|
|
|
return ReanimationPatientIndicator::query()
|
|
->joinSub($subQuery, 'latest', function ($join) {
|
|
$join->on('reanimation_patient_indicators.reanimation_patient_indicator_id', '=', 'latest.max_id');
|
|
})
|
|
->get([
|
|
'reanimation_patient_indicators.rf_medicalhistory_id',
|
|
'reanimation_patient_indicators.indicator',
|
|
'reanimation_patient_indicators.comment',
|
|
])
|
|
->keyBy('rf_medicalhistory_id');
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, ReanimationPatientIndicator>
|
|
*/
|
|
public function history(int $departmentId, int $medicalHistoryId, int $limit = 50): Collection
|
|
{
|
|
return ReanimationPatientIndicator::query()
|
|
->where('rf_department_id', $departmentId)
|
|
->where('rf_medicalhistory_id', $medicalHistoryId)
|
|
->orderByDesc('reanimation_patient_indicator_id')
|
|
->limit($limit)
|
|
->get([
|
|
'reanimation_patient_indicator_id',
|
|
'rf_report_id',
|
|
'rf_medicalhistory_id',
|
|
'indicator',
|
|
'comment',
|
|
'created_by',
|
|
'created_at',
|
|
]);
|
|
}
|
|
}
|