106 lines
3.9 KiB
PHP
106 lines
3.9 KiB
PHP
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use App\Services\DateRange;
|
||
use Illuminate\Database\Eloquent\Model;
|
||
use Illuminate\Support\Facades\DB;
|
||
|
||
class MigrationPatient extends MaterializedViewModel
|
||
{
|
||
protected $table = 'mv_migrationpatient_details';
|
||
protected $primaryKey = 'id';
|
||
|
||
public function medicalHistory()
|
||
{
|
||
return $this->belongsTo(MedicalHistory::class, 'medical_history_id', 'id');
|
||
}
|
||
|
||
public function operations()
|
||
{
|
||
return $this->hasMany(SurgicalOperation::class, 'migration_patient_id', 'id');
|
||
}
|
||
|
||
// Пересечение с отчетным периодом
|
||
public function scopeDateRange($query, string $from, string $to)
|
||
{
|
||
return $query->where('ingoing_date', '<=', $to)
|
||
->where(function ($q) use ($from) {
|
||
$q->whereNull('out_date')->orWhere('out_date', '>=', $from);
|
||
});
|
||
}
|
||
|
||
// Фильтр по подразделению (получает ID отделений)
|
||
public function scopeDepartment($query, int $departmentId)
|
||
{
|
||
$branchIds = DB::table('stt_stationarbranch')
|
||
->where('rf_DepartmentID', $departmentId)
|
||
->pluck('StationarBranchID');
|
||
|
||
return $query->whereIn('stationar_branch_id', $branchIds);
|
||
}
|
||
|
||
// Добавляет вычисляемый столбец `category` (только для отображения)
|
||
public function scopeWithCategory($query, string $from, string $to)
|
||
{
|
||
$sql = "CASE
|
||
WHEN ingoing_date BETWEEN ? AND ? THEN 'admitted'
|
||
WHEN out_date BETWEEN ? AND ? THEN
|
||
CASE
|
||
WHEN death_date IS NOT NULL AND death_date BETWEEN ? AND ? THEN 'deceased'
|
||
WHEN stat_cure_result_id IN (3,4,7) THEN 'transferred'
|
||
ELSE 'discharged'
|
||
END
|
||
WHEN ingoing_date < ? AND (out_date IS NULL OR out_date > ?) THEN 'current'
|
||
ELSE 'historical'
|
||
END as category";
|
||
|
||
return $query->selectRaw($sql, [$from, $to, $from, $to, $from, $to, $to, $to]);
|
||
}
|
||
|
||
// Быстрые фильтры по статусам (используют индексы MV, а не computed column)
|
||
public function scopeAdmitted($query, string $from, string $to)
|
||
{
|
||
return $query->where('ingoing_date', '>', $from)
|
||
->where('ingoing_date', '<=', $to);
|
||
}
|
||
|
||
public function scopeDischarged($query, string $from, string $to)
|
||
{
|
||
return $query->where('out_date', '>', $from)
|
||
->where('out_date', '<=', $to)
|
||
->whereNotIn('stat_cure_result_id', [3, 4, 7])
|
||
->whereNull('death_date'); // умершие не считаются "выбывшими домой"
|
||
}
|
||
|
||
public function scopeTransferred($query, string $from, string $to)
|
||
{
|
||
return $query->where('out_date', '>', $from)
|
||
->where('out_date', '<=', $to)
|
||
->whereIn('stat_cure_result_id', [3, 4, 7]);
|
||
}
|
||
|
||
public function scopeDeceased($query, string $from, string $to)
|
||
{
|
||
return $query->whereNotNull('death_date')
|
||
->where('death_date', '>', $from)
|
||
->where('death_date', '<=', $to);
|
||
}
|
||
|
||
public function scopeCurrent($query, DateRange $dateRange)
|
||
{
|
||
// return $query->where('ingoing_date', '<', $dateRange->startSql())
|
||
// ->where(function ($q) use ($dateRange) {
|
||
// $q->whereNull('out_date')->orWhere('out_date', '>', $dateRange->endSql());
|
||
// });
|
||
// return $query->where('ingoing_date', '<=', $dateRange->endSql())
|
||
// ->has('medicalHistory')
|
||
// ->where(function ($q) use ($dateRange) {
|
||
// $q->whereNull('out_date')
|
||
// ->orWhere('out_date', '>=', $dateRange->startSql())
|
||
// ->where('out_date', '<=', $dateRange->endSql());
|
||
// });
|
||
return $query->where('is_actually_current', true);
|
||
}
|
||
}
|