nothing
This commit is contained in:
@@ -3,9 +3,15 @@
|
||||
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;
|
||||
|
||||
@@ -41,4 +47,70 @@ class Report extends Model
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user