Перевод на доменную архитектуру

This commit is contained in:
brusnitsyn
2026-04-26 23:37:50 +09:00
parent 75ca01ffd8
commit f107ebd167
70 changed files with 4656 additions and 2070 deletions

View File

@@ -2,21 +2,25 @@
namespace App\Services;
use App\Infrastructure\Reports\Sources\MisClinicalDataSource;
use App\Models\MisMedicalHistory;
use App\Models\MisMigrationPatient;
use App\Models\MisReanimation;
use App\Models\MisSurgicalOperation;
class PatientService
{
public function __construct(
protected OutcomePatientService $outcomePatientService,
protected RecipientPatientService $recipientPatientService,
protected CurrentPatientService $currentPatientService
) {}
protected CurrentPatientService $currentPatientService,
?MisClinicalDataSource $misClinicalDataSource = null,
) {
$this->misClinicalDataSource = $misClinicalDataSource ?? app(MisClinicalDataSource::class);
}
protected MisClinicalDataSource $misClinicalDataSource;
/**
* Получить плановых или экстренных пациентов
* Получить плановых или экстренных пациентов из МИС,
* при необходимости объединяя их с уже текущими в отделении.
*/
public function getPlanOrEmergencyPatients(
?string $type,
@@ -28,47 +32,21 @@ class PatientService
bool $includeCurrent = false,
bool $fillableAuto = false
) {
$medicalHistoryIds = $this->recipientPatientService->resolvePlanOrEmergencyMedicalHistoryIds(
return $this->misClinicalDataSource->getPlanOrEmergencyPatients(
$type,
$isHeadOrAdmin,
$branchId,
$dateRange,
$countOnly,
$onlyIds,
$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;
});
}
/**
* Получить всех пациентов в отделении (поступившие сегодня + уже лечащиеся)
* Получить всех MIS-пациентов в отделении:
* поступившие за период плюс уже текущие в отделении.
*/
public function getAllPatientsInDepartment(
bool $isHeadOrAdmin,
@@ -78,115 +56,14 @@ class PatientService
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;
}
return collect();
}
if ($countOnly) {
return count($allIds);
}
if ($onlyIds) {
return collect($allIds);
}
$res = 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([
'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');
return $res->get()
->map(function ($patient) use ($recipientIds) {
$patient->is_recipient_today = in_array($patient->MedicalHistoryID, $recipientIds);
return $patient;
});
return $this->misClinicalDataSource->getAllPatientsInDepartment(
$isHeadOrAdmin,
$branchId,
$dateRange,
$countOnly,
$onlyIds,
$fillableAuto
);
}
/**
@@ -226,7 +103,7 @@ class PatientService
}
/**
* Получить выбывших пациентов
* Получить выбывших MIS-пациентов по типу исхода.
*/
public function getOutcomePatients(
int $branchId,
@@ -238,129 +115,18 @@ class PatientService
}
/**
* Получить пациентов с записями в реанимации по отделению
* Получить пациентов с записями в реанимации по отделению.
*/
public function getReanimationPatients(
int $branchId,
DateRange $dateRange,
bool $onlyIds = false
) {
$currentIds = $this->getAllPatientsInDepartment(
true,
$branchId,
$dateRange,
false,
true,
false
)->all();
$outcomeIds = $this->getOutcomePatients(
$branchId,
$dateRange,
'all',
true
)->all();
$reportCohortIds = array_values(array_unique(array_merge($currentIds, $outcomeIds)));
if (empty($reportCohortIds)) {
return collect();
}
$reanimationByMedicalHistory = MisReanimation::query()
->join('stt_migrationpatient as mp', 'mp.MigrationPatientID', '=', 'stt_reanimation.rf_MigrationPatientID')
->where('mp.rf_StationarBranchID', $branchId)
->where('mp.rf_MedicalHistoryID', '<>', 0)
->whereIn('mp.rf_MedicalHistoryID', $reportCohortIds)
->selectRaw('
mp."rf_MedicalHistoryID" as medical_history_id,
MAX(stt_reanimation."DateIn") as reanimation_date_in,
BOOL_OR(COALESCE(stt_reanimation."isComplete", false)) as reanimation_is_complete
')
->groupBy('mp.rf_MedicalHistoryID')
->get();
$medicalHistoryIds = $reanimationByMedicalHistory
->pluck('medical_history_id')
->map(fn ($id) => (int) $id)
->values()
->all();
$reanimationDateByMedicalHistory = $reanimationByMedicalHistory
->pluck('reanimation_date_in', 'medical_history_id');
$reanimationCompleteByMedicalHistory = $reanimationByMedicalHistory
->pluck('reanimation_is_complete', 'medical_history_id');
if (empty($medicalHistoryIds)) {
return collect();
}
if ($onlyIds) {
return collect($medicalHistoryIds);
}
return MisMedicalHistory::whereIn('MedicalHistoryID', $medicalHistoryIds)
->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([
'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',
]);
}]);
}]);
},
])
->orderBy('DateRecipient', 'DESC')
->get()
->map(function ($patient) use ($reanimationDateByMedicalHistory, $reanimationCompleteByMedicalHistory) {
$reanimationDateIn = $reanimationDateByMedicalHistory->get($patient->MedicalHistoryID);
if ($reanimationDateIn) {
$patient->DateRecipient = $reanimationDateIn;
}
$patient->reanimation_is_complete = (bool) $reanimationCompleteByMedicalHistory->get($patient->MedicalHistoryID, false);
return $patient;
});
return $this->misClinicalDataSource->getReanimationPatients($branchId, $dateRange, $onlyIds);
}
/**
* Получить пациентов с операциями
* Получить операции пациентов по типу срочности.
*/
public function getSurgicalPatients(
string $type,
@@ -368,26 +134,11 @@ class PatientService
DateRange $dateRange,
bool $countOnly = false
) {
$query = MisSurgicalOperation::where('rf_StationarBranchID', $branchId)
->completed()
->where('Date', '>=', $dateRange->startSql())
->where('Date', '<=', $dateRange->endSql());
if ($type === 'plan') {
$query->where('rf_TypeSurgOperationInTimeID', 6);
} else {
$query->whereIn('rf_TypeSurgOperationInTimeID', [4, 5]);
}
if ($countOnly) {
return $query->count();
}
return $query->get();
return $this->misClinicalDataSource->getSurgicalPatients($type, $branchId, $dateRange, $countOnly);
}
/**
* Получить количество пациентов по типу с учетом уже находящихся в отделении
* Получить количество пациентов по типу с учетом уже находящихся в отделении.
*/
public function getPatientsCountWithCurrent(
?string $type,
@@ -395,92 +146,6 @@ class PatientService
int $branchId,
DateRange $dateRange
): int {
return $this->getPlanOrEmergencyPatients(
$type,
$isHeadOrAdmin,
$branchId,
$dateRange,
true,
false,
true
);
}
private function buildPatientCardsQuery(array $medicalHistoryIds, int $branchId)
{
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');
return $this->misClinicalDataSource->getPatientsCountWithCurrent($type, $isHeadOrAdmin, $branchId, $dateRange);
}
}