Добавил ограничение по отказникам
This commit is contained in:
@@ -53,6 +53,24 @@ class MedicalHistory extends MaterializedViewModel
|
||||
->latest('observable_in');
|
||||
}
|
||||
|
||||
public function denial()
|
||||
{
|
||||
return $this->hasOne(MisDenial::class, 'rf_MedicalHistoryID', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Исключить отказников от госпитализации.
|
||||
* Безопасно: пока таблицы отказов нет в реплике — no-op (см. MisDenial::tableAvailable()).
|
||||
*/
|
||||
public function scopeWithoutDenials($query)
|
||||
{
|
||||
if (MisDenial::tableAvailable()) {
|
||||
$query->whereDoesntHave('denial');
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
public function operationsInDepartment($query, $departmentId)
|
||||
{
|
||||
return $this->operations()->where('department_id', $departmentId);
|
||||
|
||||
@@ -72,4 +72,25 @@ class MedicalHistorySnapshot extends Model
|
||||
$q->department($departmentId);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Исключить из снимка карты, по которым в МИС оформлен отказ от госпитализации.
|
||||
* Снимок остаётся неизменным (immutable) — отсекаем только при чтении.
|
||||
* Безопасно: пока таблицы отказов нет в реплике — no-op (см. MisDenial::tableAvailable()).
|
||||
*/
|
||||
public function scopeWithoutDenials($query)
|
||||
{
|
||||
if (! MisDenial::tableAvailable()) {
|
||||
return $query;
|
||||
}
|
||||
|
||||
$denialTable = (new MisDenial)->getTable();
|
||||
$snapshotTable = $this->getTable();
|
||||
|
||||
return $query->whereNotExists(function ($sub) use ($denialTable, $snapshotTable) {
|
||||
$sub->selectRaw('1')
|
||||
->from($denialTable)
|
||||
->whereColumn("{$denialTable}.rf_MedicalHistoryID", "{$snapshotTable}.rf_medicalhistory_id");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
41
app/Models/MisDenial.php
Normal file
41
app/Models/MisDenial.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
/**
|
||||
* Отказ от госпитализации (МИС).
|
||||
*
|
||||
* ВНИМАНИЕ: ЗАГЛУШКА. На момент написания таблицы отказов ещё нет в реплике.
|
||||
* Когда репликатор её добавит — поменять $table на реальное имя.
|
||||
* Пока таблицы нет, MisDenial::tableAvailable() === false и вся
|
||||
* фильтрация отказников превращается в no-op (отчёты не ломаются).
|
||||
*
|
||||
* Структура в МИС:
|
||||
* DenialID (PK), rf_MedicalHistoryID -> mv_medicalhistory_summary.id,
|
||||
* rf_kl_HospRefusalID, DateTime, FAM/Name/OT, rf_StationarTypeID, ...
|
||||
* Признак отказа = сам факт наличия строки с rf_MedicalHistoryID
|
||||
* (по причинам/типам не фильтруем).
|
||||
*/
|
||||
class MisDenial extends MaterializedViewModel
|
||||
{
|
||||
protected $table = 'stt_denial'; // TODO: подставить реальное имя таблицы в реплике
|
||||
protected $primaryKey = 'DenialID';
|
||||
|
||||
private static ?bool $tableExists = null;
|
||||
|
||||
public function medicalHistory()
|
||||
{
|
||||
return $this->belongsTo(MedicalHistory::class, 'rf_MedicalHistoryID', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Доступна ли таблица отказов в текущей БД (реплике).
|
||||
* Мемоизируем на процесс, чтобы не дёргать information_schema на каждый запрос.
|
||||
*/
|
||||
public static function tableAvailable(): bool
|
||||
{
|
||||
return self::$tableExists ??= Schema::hasTable((new self)->getTable());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user