* добавил исход спец контингенту

* оптимизация обновления при редактировании спец контингента
* добавил поддержку заключительных диагнозов
* изменил определение законченной операции
* добавил поддержку исхода операции
* добавил определение отмены для операции через назначение
* работа над диапазонами календарей, подсчет статистики
* добавил статусы отчетов и подкорректировал привязку спец контингента к отчету
* добавил новые сервисы для будущего кеширования
* частичное разделение логики подсчета пациентов
This commit is contained in:
brusnitsyn
2026-04-22 20:35:39 +09:00
parent 2041ab54ea
commit 719eb1403f
39 changed files with 1458 additions and 763 deletions

View File

@@ -7,11 +7,16 @@ use App\Models\MisMigrationPatient;
use App\Models\MisReanimation;
use App\Models\MisSurgicalOperation;
use App\Models\ObservationPatient;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
class PatientService
{
public function __construct(
protected OutcomePatientService $outcomePatientService,
protected RecipientPatientService $recipientPatientService,
protected CurrentPatientService $currentPatientService
)
{ }
/**
* Получить плановых или экстренных пациентов
*/
@@ -25,36 +30,83 @@ class PatientService
bool $includeCurrent = false,
bool $fillableAuto = false
) {
// Получаем поступивших сегодня
$recipientQuery = $this->buildRecipientQuery($type, $isHeadOrAdmin, $branchId, $dateRange, $fillableAuto);
if ($fillableAuto)
$recipientIds = $recipientQuery->distinct()->pluck('rf_MedicalHistoryID')->toArray();
else
$recipientIds = $recipientQuery->pluck('rf_MedicalHistoryID')->toArray();
// Если нужно добавить уже находящихся в отделении
if ($includeCurrent) {
if ($fillableAuto) {
$currentIds = $this->getHistoricalCurrentMedicalHistoryIds($branchId, $dateRange);
} else {
$currentIds = MisMigrationPatient::currentlyInTreatment($branchId)
->pluck('rf_MedicalHistoryID')
->toArray();
}
$medicalHistoryIds = array_unique(array_merge($recipientIds, $currentIds));
} else {
$medicalHistoryIds = $recipientIds;
}
$medicalHistoryIds = $this->recipientPatientService->resolvePlanOrEmergencyMedicalHistoryIds(
$type,
$isHeadOrAdmin,
$branchId,
$dateRange,
$includeCurrent,
$fillableAuto
);
if (empty($medicalHistoryIds)) {
return $countOnly ? 0 : collect();
}
if ($countOnly) {
return count($medicalHistoryIds);
}
if ($onlyIds) {
return collect($medicalHistoryIds);
}
$recipientIds = $this->recipientPatientService->getRecipientMedicalHistoryIds(
$type,
$isHeadOrAdmin,
$branchId,
$dateRange
);
$res = $this->buildPatientCardsQuery($medicalHistoryIds, $branchId);
return $res->get()
->map(function ($patient) use ($recipientIds) {
$patient->is_recipient_today = in_array($patient->MedicalHistoryID, $recipientIds, true);
return $patient;
});
}
/**
* Получить всех пациентов в отделении (поступившие сегодня + уже лечащиеся)
*/
public function getAllPatientsInDepartment(
bool $isHeadOrAdmin,
int $branchId,
DateRange $dateRange,
bool $countOnly = false,
bool $onlyIds = false,
bool $fillableAuto = false
) {
$recipientIds = $this->recipientPatientService->buildRecipientQuery(null, $isHeadOrAdmin, $branchId, $dateRange, $fillableAuto)
->pluck('rf_MedicalHistoryID')
->toArray();
if ($fillableAuto) {
$currentIds = $this->currentPatientService->getHistoricalCurrentMedicalHistoryIds(null, $branchId, $dateRange);
} else {
$currentIds = MisMigrationPatient::currentlyInTreatment($branchId)
->pluck('rf_MedicalHistoryID')
->toArray();
}
$allIds = array_unique(array_merge($recipientIds, $currentIds));
if (empty($allIds)) {
if ($countOnly) return 0;
if ($onlyIds) return collect();
return collect();
}
// Получаем истории
$query = MisMedicalHistory::whereIn('MedicalHistoryID', $medicalHistoryIds)
if ($countOnly) {
return count($allIds);
}
if ($onlyIds) {
return collect($allIds);
}
$res = MisMedicalHistory::whereIn('MedicalHistoryID', $allIds)
->select([
'MedicalHistoryID',
'FAMILY',
@@ -83,10 +135,10 @@ class PatientService
},
'outcomeMigration' => function ($q) {
$q->select([
'MigrationPatientID',
'rf_MedicalHistoryID',
'DateOut',
'rf_DiagnosID',
'stt_migrationpatient.MigrationPatientID',
'stt_migrationpatient.rf_MedicalHistoryID',
'stt_migrationpatient.DateOut',
'stt_migrationpatient.rf_DiagnosID',
])->with(['mainDiagnosis' => function ($diagnosisQuery) {
$diagnosisQuery->select([
'DiagnosID',
@@ -127,145 +179,10 @@ class PatientService
])
->orderBy('DateRecipient', 'DESC');
// Фильтруем по типу (план/экстренные)
if ($type === 'plan') {
$query->plan();
} elseif ($type === 'emergency') {
$query->emergency();
}
if ($countOnly) {
return $query->count();
}
if ($onlyIds) {
return $query->pluck('MedicalHistoryID');
}
return $query->get()->map(function ($patient) use ($recipientIds) {
// Добавляем флаг "поступил сегодня"
$patient->is_recipient_today = in_array($patient->MedicalHistoryID, $recipientIds);
return $patient;
});
}
/**
* Получить всех пациентов в отделении (поступившие сегодня + уже лечащиеся)
*/
public function getAllPatientsInDepartment(
bool $isHeadOrAdmin,
int $branchId,
DateRange $dateRange,
bool $countOnly = false,
bool $onlyIds = false,
bool $fillableAuto = false
) {
// Поступившие сегодня
$recipientIds = $this->buildRecipientQuery(null, $isHeadOrAdmin, $branchId, $dateRange, $fillableAuto)
->pluck('rf_MedicalHistoryID')
->toArray();
// Уже находящиеся на лечении
if ($fillableAuto) {
$currentIds = $this->getHistoricalCurrentMedicalHistoryIds($branchId, $dateRange);
} else {
$currentIds = MisMigrationPatient::currentlyInTreatment($branchId)
->pluck('rf_MedicalHistoryID')
->toArray();
}
// Объединяем и убираем дубли
$allIds = array_unique(array_merge($recipientIds, $currentIds));
if (empty($allIds)) {
if ($countOnly) return 0;
return collect();
}
if ($countOnly) {
return count($allIds);
}
if ($onlyIds) {
return collect($allIds);
}
return MisMedicalHistory::whereIn('MedicalHistoryID', $allIds)
->select([
'MedicalHistoryID',
'FAMILY',
'Name',
'OT',
'BD',
'DateRecipient',
'DateExtract',
'rf_EmerSignID',
'rf_kl_VisitResultID',
])
->with([
'surgicalOperations' => function ($q) {
$q->select([
'SurgicalOperationID',
'rf_MedicalHistoryID',
'rf_kl_ServiceMedicalID',
'Date',
])->with(['serviceMedical' => function ($serviceQuery) {
$serviceQuery->select([
'ServiceMedicalID',
'ServiceMedicalCode',
'ServiceMedicalName',
]);
}]);
},
'outcomeMigration' => function ($q) {
$q->select([
'MigrationPatientID',
'rf_MedicalHistoryID',
'DateOut',
'rf_DiagnosID',
])->with(['mainDiagnosis' => function ($diagnosisQuery) {
$diagnosisQuery->select([
'DiagnosID',
'rf_MKBID',
])->with(['mkb' => function ($mkbQuery) {
$mkbQuery->select([
'MKBID',
'DS',
'NAME',
]);
}]);
}]);
},
'migrations' => function ($q) use ($branchId) {
$q->where('rf_StationarBranchID', $branchId)
->select([
'MigrationPatientID',
'rf_MedicalHistoryID',
'rf_DiagnosID',
'DateIngoing',
'rf_StationarBranchID',
])
->orderByDesc('DateIngoing')
->with(['mainDiagnosis' => function ($diagnosisQuery) {
$diagnosisQuery->select([
'DiagnosID',
'rf_MKBID',
'rf_MigrationPatientID',
])->with(['mkb' => function ($mkbQuery) {
$mkbQuery->select([
'MKBID',
'DS',
'NAME',
]);
}]);
}]);
},
])
->orderBy('DateRecipient', 'DESC')
->get()
->map(function ($patient) use ($recipientIds) {
$patient->is_recipient_today = in_array($patient->MedicalHistoryID, $recipientIds);
return $patient;
return $res->get()
->map(function ($patient) use ($recipientIds) {
$patient->is_recipient_today = in_array($patient->MedicalHistoryID, $recipientIds);
return $patient;
});
}
@@ -288,9 +205,8 @@ class PatientService
if ($onlyIds) $patients = $query->pluck('MedicalHistoryID');
else {
// Загрузка отношений, необходимых для FormattedPatientResource
$query->with([
'outcomeMigration.mainDiagnosis.mkb', // mkb через диагноз
'outcomeMigration.mainDiagnosis.mkb',
]);
$patients = $query->get();
}
@@ -313,73 +229,7 @@ class PatientService
string $outcomeType = 'all',
bool $onlyIds = false
) {
$query = MisMedicalHistory::query()
->where('MedicalHistoryID', '<>', 0)
->whereHas('migrations', function ($migrationQuery) use ($branchId, $outcomeType) {
$migrationQuery->where('rf_StationarBranchID', $branchId);
if ($outcomeType === 'deceased') {
$migrationQuery->whereIn('rf_kl_VisitResultID', [5, 6, 15, 16]);
} elseif ($outcomeType === 'transferred') {
$migrationQuery->whereIn('rf_kl_VisitResultID', [4, 14]);
} elseif ($outcomeType === 'discharged') {
$migrationQuery->whereIn('rf_kl_VisitResultID', [1, 11, 2, 12, 7, 18, 48]);
} elseif ($outcomeType === 'without-transferred') {
$migrationQuery->whereNotIn('rf_kl_VisitResultID', [4, 14])
->where('rf_kl_VisitResultID', '<>', 0);
}
});
if ($dateRange->isOneDay) {
$query->where('DateExtract', '>', $dateRange->startSql())
->where('DateExtract', '<=', $dateRange->endSql());
} else {
$startAt = $dateRange->startSql();
$endDate = $dateRange->end()->toDateString();
$query->where('DateExtract', '>', $startAt)
->whereDate('DateExtract', '<=', $endDate);
}
if ($onlyIds) {
return $query->pluck('MedicalHistoryID');
}
return $query
->select([
'MedicalHistoryID',
'FAMILY',
'Name',
'OT',
'BD',
'DateRecipient',
'DateExtract',
'rf_EmerSignID',
'rf_kl_VisitResultID',
])
->with([
'outcomeMigration' => function ($q) {
$q->select([
'MigrationPatientID',
'rf_MedicalHistoryID',
'DateOut',
'rf_DiagnosID',
])->with(['mainDiagnosis' => function ($diagnosisQuery) {
$diagnosisQuery->select([
'DiagnosID',
'rf_MKBID',
])->with(['mkb' => function ($mkbQuery) {
$mkbQuery->select([
'MKBID',
'DS',
'NAME',
]);
}]);
}]);
},
])
->orderBy('DateRecipient', 'DESC')
->get()
->map(fn ($patient) => $this->addOutcomeInfo($patient));
return $this->outcomePatientService->getOutcomePatients($branchId, $dateRange, $outcomeType, $onlyIds);
}
/**
@@ -443,10 +293,10 @@ class PatientService
},
'outcomeMigration' => function ($q) {
$q->select([
'MigrationPatientID',
'rf_MedicalHistoryID',
'DateOut',
'rf_DiagnosID',
'stt_migrationpatient.MigrationPatientID',
'stt_migrationpatient.rf_MedicalHistoryID',
'stt_migrationpatient.DateOut',
'stt_migrationpatient.rf_DiagnosID',
])->with(['mainDiagnosis' => function ($diagnosisQuery) {
$diagnosisQuery->select([
'DiagnosID',
@@ -478,7 +328,6 @@ class PatientService
->completed()
->where('Date', '>=', $dateRange->startSql())
->where('Date', '<=', $dateRange->endSql());
// ->whereBetween('Date', [$dateRange->startSql(), $dateRange->endSql()]);
if ($type === 'plan') {
$query->where('rf_TypeSurgOperationInTimeID', 6);
@@ -493,164 +342,6 @@ class PatientService
return $query->get();
}
/**
* Получить текущих пациентов
*/
public function getCurrentPatients(int $branchId, bool $countOnly = false)
{
$medicalHistoryIds = MisMigrationPatient::currentlyInTreatment($branchId)
->pluck('rf_MedicalHistoryID')
->unique()
->toArray();
if (empty($medicalHistoryIds)) {
return $countOnly ? 0 : collect();
}
$query = MisMedicalHistory::whereIn('MedicalHistoryID', $medicalHistoryIds)
->currentlyHospitalized()
->with(['surgicalOperations'])
->orderBy('DateRecipient', 'DESC');
if ($countOnly) {
return $query->count();
}
return $query->get();
}
/**
* Собрать базовый запрос для пациентов
*/
private function buildPatientQuery(
?string $type,
bool $isHeadOrAdmin,
int $branchId,
DateRange $dateRange
) {
if ($isHeadOrAdmin) {
$query = MisMigrationPatient::whereInDepartment($branchId)
->where('DateIngoing', '>=', $dateRange->startSql())
->where('DateIngoing', '<=', $dateRange->endSql());
// ->whereBetween('DateIngoing', [$dateRange->startSql(), $dateRange->endSql()]);
} else {
$query = MisMigrationPatient::currentlyInTreatment($branchId)
->where('DateIngoing', '>=', $dateRange->startSql())
->where('DateIngoing', '<=', $dateRange->endSql());
// ->whereBetween('DateIngoing', [$dateRange->startSql(), $dateRange->endSql()]);
}
$medicalHistoryIds = $query->pluck('rf_MedicalHistoryID')->toArray();
if (empty($medicalHistoryIds)) {
return MisMedicalHistory::whereRaw('1 = 0');
}
$query = MisMedicalHistory::whereIn('MedicalHistoryID', $medicalHistoryIds);
if ($type === 'plan') {
$query->plan();
} elseif ($type === 'emergency') {
$query->emergency();
}
if (!$isHeadOrAdmin && !in_array($type, ['discharged', 'transferred', 'deceased'])) {
$query->currentlyHospitalized();
}
return $query;
}
/**
* Построить запрос для поступивших пациентов
*/
private function buildRecipientQuery(
?string $type,
bool $isHeadOrAdmin,
int $branchId,
DateRange $dateRange,
bool $fillableAuto = false
) {
$startAt = $dateRange->start()->copy()->subDay()->format('Y-m-d H:i:s');
$endAt = $dateRange->end()->copy()->addDay()->format('Y-m-d H:i:s');
if ($dateRange->isOneDay) {
$startAt = $dateRange->startSql();
$endAt = $dateRange->endSql();
}
$query = DB::table('stt_medicalhistory as mh')
->selectRaw('mh."MedicalHistoryID" as "rf_MedicalHistoryID"')
->where('mh.MedicalHistoryID', '<>', 0);
$query->whereExists(function ($subQuery) use ($branchId, $startAt, $endAt) {
$subQuery->select(DB::raw(1))
->from('stt_migrationpatient as mp')
->whereColumn('mp.rf_MedicalHistoryID', 'mh.MedicalHistoryID')
->where('mp.rf_StationarBranchID', $branchId)
->where('mp.DateIngoing', '>', $startAt)
->where('mp.DateIngoing', '<=', $endAt);
});
if ($type === 'plan') {
$query->where('mh.rf_EmerSignID', 1);
} elseif ($type === 'emergency') {
$query->whereIn('mh.rf_EmerSignID', [2, 4]);
}
return $query->distinct();
}
private function getHistoricalCurrentMedicalHistoryIds(int $branchId, DateRange $dateRange): array
{
// Исторический срез по основной таблице миграций:
// для каждой истории болезни берём последнюю миграцию в отделении
// на момент конца периода и проверяем, что пациент числился в отделении.
$latestRows = DB::table('stt_migrationpatient')
->select('rf_MedicalHistoryID', DB::raw('MAX("MigrationPatientID") as max_migration_patient_id'))
->where('rf_StationarBranchID', $branchId)
->where('rf_MedicalHistoryID', '<>', 0)
->where('DateIngoing', '<=', $dateRange->endSql())
->groupBy('rf_MedicalHistoryID');
return DB::table('stt_migrationpatient as mp')
->joinSub($latestRows, 'latest', function ($join) {
$join->on('mp.rf_MedicalHistoryID', '=', 'latest.rf_MedicalHistoryID')
->on('mp.MigrationPatientID', '=', 'latest.max_migration_patient_id');
})
->join('stt_medicalhistory as mh', 'mh.MedicalHistoryID', '=', 'mp.rf_MedicalHistoryID')
->where('mp.rf_StationarBranchID', $branchId)
->where('mh.DateRecipient', '<=', $dateRange->endSql())
->where('mp.DateOut', '>=', $dateRange->endSql())
->where(function ($query) use ($dateRange) {
$query->where('mh.DateExtract', '>', $dateRange->endSql())
->orWhereDate('mh.DateExtract', '1900-01-01');
})
->distinct()
->pluck('mp.rf_MedicalHistoryID')
->toArray();
}
/**
* Добавить информацию об исходе пациенту
*/
private function addOutcomeInfo(MisMedicalHistory $patient)
{
$latestMigration = $patient->migrations
->whereNotNull('DateOut')
->where('DateOut', '<>', '1900-01-01')
->sortByDesc('DateOut')
->first();
if ($latestMigration) {
$patient->outcome_type = $this->getOutcomeTypeName($latestMigration->rf_kl_VisitResultID);
$patient->outcome_date = $latestMigration->DateOut;
$patient->visit_result_id = $latestMigration->rf_kl_VisitResultID;
}
return $patient;
}
/**
* Получить количество пациентов по типу с учетом уже находящихся в отделении
*/
@@ -671,16 +362,81 @@ class PatientService
);
}
/**
* Получить название типа исхода
*/
private function getOutcomeTypeName(int $visitResultId): string
private function buildPatientCardsQuery(array $medicalHistoryIds, int $branchId)
{
return match($visitResultId) {
1, 7, 8, 9, 10, 11, 48, 49, 124 => 'Выписка',
2, 3, 4, 12, 13, 14 => 'Перевод',
5, 6, 15, 16 => 'Умер',
default => 'Другое (' . $visitResultId . ')'
};
return MisMedicalHistory::query()
->whereIn('MedicalHistoryID', $medicalHistoryIds)
->select([
'MedicalHistoryID',
'FAMILY',
'Name',
'OT',
'BD',
'DateRecipient',
'DateExtract',
'DateDeath',
'rf_EmerSignID',
'rf_kl_VisitResultID',
])
->with([
'surgicalOperations' => function ($q) {
$q->select([
'SurgicalOperationID',
'rf_MedicalHistoryID',
'rf_kl_ServiceMedicalID',
'Date',
])->with(['serviceMedical' => function ($serviceQuery) {
$serviceQuery->select([
'ServiceMedicalID',
'ServiceMedicalCode',
'ServiceMedicalName',
]);
}]);
},
'outcomeMigration' => function ($q) {
$q->select([
'stt_migrationpatient.MigrationPatientID',
'stt_migrationpatient.rf_MedicalHistoryID',
'stt_migrationpatient.DateOut',
'stt_migrationpatient.rf_DiagnosID',
])->with(['mainDiagnosis' => function ($diagnosisQuery) {
$diagnosisQuery->select([
'DiagnosID',
'rf_MKBID',
])->with(['mkb' => function ($mkbQuery) {
$mkbQuery->select([
'MKBID',
'DS',
'NAME',
]);
}]);
}]);
},
'migrations' => function ($q) use ($branchId) {
$q->where('rf_StationarBranchID', $branchId)
->select([
'MigrationPatientID',
'rf_MedicalHistoryID',
'rf_DiagnosID',
'DateIngoing',
'rf_StationarBranchID',
])
->orderByDesc('DateIngoing')
->with(['mainDiagnosis' => function ($diagnosisQuery) {
$diagnosisQuery->select([
'DiagnosID',
'rf_MKBID',
'rf_MigrationPatientID',
])->with(['mkb' => function ($mkbQuery) {
$mkbQuery->select([
'MKBID',
'DS',
'NAME',
]);
}]);
}]);
},
])
->orderBy('DateRecipient', 'DESC');
}
}