117 lines
3.3 KiB
PHP
117 lines
3.3 KiB
PHP
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use Illuminate\Database\Eloquent\Model;
|
||
use Illuminate\Support\Carbon;
|
||
|
||
class Report extends Model
|
||
{
|
||
/**
|
||
* ID метрики для среднего койко-дня
|
||
*/
|
||
const METRIC_BED_DAYS_ID = 18;
|
||
|
||
protected $primaryKey = 'report_id';
|
||
public $timestamps = false;
|
||
|
||
protected $fillable = [
|
||
'created_at',
|
||
'sent_at',
|
||
'rf_department_id',
|
||
'rf_user_id',
|
||
'rf_lpudoctor_id'
|
||
];
|
||
|
||
public function metrikaResults(): \Illuminate\Database\Eloquent\Relations\HasMany
|
||
{
|
||
return $this->hasMany(MetrikaResult::class, 'rf_report_id', 'report_id');
|
||
}
|
||
|
||
public function observationPatients(): \Illuminate\Database\Eloquent\Relations\HasMany
|
||
{
|
||
return $this->hasMany(ObservationPatient::class, 'rf_report_id', 'report_id');
|
||
}
|
||
|
||
public function unwantedEvents()
|
||
{
|
||
return $this->hasMany(UnwantedEvent::class, 'rf_report_id', 'report_id');
|
||
}
|
||
|
||
public function user()
|
||
{
|
||
return $this->belongsTo(User::class, 'rf_user_id');
|
||
}
|
||
|
||
public function lpuDoctor()
|
||
{
|
||
return $this->belongsTo(MisLpuDoctor::class, 'rf_lpudoctor_id');
|
||
}
|
||
|
||
/**
|
||
* Связь со снапшотами
|
||
*/
|
||
public function snapshots()
|
||
{
|
||
return $this->hasMany(MedicalHistorySnapshot::class, 'rf_report_id', 'report_id');
|
||
}
|
||
|
||
/**
|
||
* Получить выписанных пациентов из снапшотов
|
||
*/
|
||
public function getDischargedPatientsOnSnapshots()
|
||
{
|
||
return $this->snapshots()
|
||
->where('patient_type', MedicalHistorySnapshot::PATIENT_TYPE_DISCHARGED)
|
||
->distinct()
|
||
->with('medicalHistory')
|
||
->get();
|
||
}
|
||
|
||
/**
|
||
* Получить текущих пациентов из снапшотов
|
||
*/
|
||
public function getCurrentPatientsOnSnapshots()
|
||
{
|
||
return $this->snapshots()
|
||
->where('patient_type', MedicalHistorySnapshot::PATIENT_TYPE_CURRENT)
|
||
->with('medicalHistory')
|
||
->get();
|
||
}
|
||
|
||
/**
|
||
* Рассчитать и сохранить средний койко-день на основе снапшотов
|
||
*/
|
||
public function calculateAndSaveAverageBedDays()
|
||
{
|
||
$dischargedPatients = $this->getDischargedPatientsOnSnapshots();
|
||
|
||
if ($dischargedPatients->isEmpty()) {
|
||
$avgBedDays = 0;
|
||
} else {
|
||
$totalDays = 0;
|
||
foreach ($dischargedPatients as $snapshot) {
|
||
$history = $snapshot->medicalHistory;
|
||
if ($history && $history->DateRecipient && $history->DateExtract) {
|
||
$start = Carbon::parse($history->DateRecipient);
|
||
$end = Carbon::parse($history->DateExtract);
|
||
$days = $start->diffInDays($end);
|
||
$totalDays += $days;
|
||
}
|
||
}
|
||
$avgBedDays = round($totalDays / $dischargedPatients->count(), 1);
|
||
}
|
||
|
||
// Сохраняем результат как метрику
|
||
MetrikaResult::updateOrCreate([
|
||
'rf_report_id' => $this->report_id,
|
||
'rf_metrika_item_id' => self::METRIC_BED_DAYS_ID,
|
||
],
|
||
[
|
||
'value' => $avgBedDays,
|
||
]);
|
||
|
||
return $avgBedDays;
|
||
}
|
||
}
|