Перевод на доменную архитектуру
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
namespace App\Services;
|
||||
|
||||
use App\Data\UnifiedPatientData;
|
||||
use App\Infrastructure\Reports\Sources\MisPatientSource;
|
||||
use App\Infrastructure\Reports\Sources\SpecialPatientSource;
|
||||
use App\Models\Department;
|
||||
use App\Models\DepartmentPatient;
|
||||
use App\Models\MisMedicalHistory;
|
||||
@@ -17,7 +19,16 @@ class UnifiedPatientService
|
||||
|
||||
public function __construct(
|
||||
protected PatientService $patientService,
|
||||
) {}
|
||||
?MisPatientSource $misPatientSource = null,
|
||||
?SpecialPatientSource $specialPatientSource = null,
|
||||
) {
|
||||
$this->misPatientSource = $misPatientSource ?? app(MisPatientSource::class);
|
||||
$this->specialPatientSource = $specialPatientSource ?? app(SpecialPatientSource::class);
|
||||
}
|
||||
|
||||
protected MisPatientSource $misPatientSource;
|
||||
|
||||
protected SpecialPatientSource $specialPatientSource;
|
||||
|
||||
public function getLivePatientsByStatus(
|
||||
Department $department,
|
||||
@@ -37,8 +48,8 @@ class UnifiedPatientService
|
||||
}
|
||||
|
||||
$patients = match ($sourceScope) {
|
||||
'mis' => $this->getMisPatientDtos($user, $baseStatus, $dateRange, $branchId, $includeCurrent, $fillableAuto, $forSnapshots),
|
||||
'special' => $this->getSpecialPatientDtos($department, $baseStatus, $dateRange, $forSnapshots),
|
||||
'mis' => $this->misPatientSource->getDtos($user, $baseStatus, $dateRange, $branchId, $includeCurrent, $fillableAuto, $forSnapshots),
|
||||
'special' => $this->specialPatientSource->getDtos($department, $baseStatus, $dateRange, self::SPECIAL_SOURCE_TYPES, $forSnapshots),
|
||||
default => $this->getAggregatedPatientDtos($department, $user, $baseStatus, $dateRange, $branchId, $includeCurrent, $fillableAuto, $forSnapshots),
|
||||
};
|
||||
|
||||
@@ -76,10 +87,10 @@ class UnifiedPatientService
|
||||
}
|
||||
|
||||
if ($sourceScope === 'special') {
|
||||
return $this->getManualPatientsCount($department, $baseStatus, $dateRange, self::SPECIAL_SOURCE_TYPES);
|
||||
return $this->specialPatientSource->getCount($department, $baseStatus, $dateRange, self::SPECIAL_SOURCE_TYPES);
|
||||
}
|
||||
|
||||
$misCount = $this->getMisPatientsCount(
|
||||
$misCount = $this->misPatientSource->getCount(
|
||||
$user,
|
||||
$baseStatus,
|
||||
$dateRange,
|
||||
@@ -92,7 +103,7 @@ class UnifiedPatientService
|
||||
return $misCount;
|
||||
}
|
||||
|
||||
$specialCount = $this->getManualPatientsCount($department, $baseStatus, $dateRange, self::SPECIAL_SOURCE_TYPES);
|
||||
$specialCount = $this->specialPatientSource->getCount($department, $baseStatus, $dateRange, self::SPECIAL_SOURCE_TYPES);
|
||||
|
||||
return $misCount + $specialCount;
|
||||
}
|
||||
@@ -117,7 +128,7 @@ class UnifiedPatientService
|
||||
$fillableAuto
|
||||
);
|
||||
|
||||
$manualIds = $this->buildManualPatientsQuery($department, $dateRange, self::SPECIAL_SOURCE_TYPES, false)
|
||||
$manualIds = $this->specialPatientSource->getPatients($department, 'recipient', $dateRange, self::SPECIAL_SOURCE_TYPES, false)
|
||||
->whereBetween('admitted_at', [$dateRange->startSql(), $dateRange->endSql()])
|
||||
->pluck('department_patient_id');
|
||||
|
||||
@@ -275,25 +286,8 @@ class UnifiedPatientService
|
||||
bool $fillableAuto = false,
|
||||
bool $forSnapshots = false
|
||||
): Collection {
|
||||
$misPatients = $this->getMisPatients($user, $status, $dateRange, $branchId, $includeCurrent, $fillableAuto, $forSnapshots);
|
||||
$manualPatients = $this->getManualPatients($department, $status, $dateRange, self::SPECIAL_SOURCE_TYPES, ! $forSnapshots);
|
||||
$reportIds = $this->getReportIdsForDepartmentPeriod($department, $dateRange);
|
||||
|
||||
$linkedManualPatients = DepartmentPatient::query()
|
||||
->where(function ($builder) use ($department, $reportIds) {
|
||||
if (! empty($reportIds)) {
|
||||
$builder->whereIn('rf_report_id', $reportIds);
|
||||
}
|
||||
|
||||
$builder->orWhere(function ($legacyQuery) use ($department) {
|
||||
$legacyQuery->whereNull('rf_report_id')
|
||||
->where('rf_department_id', $department->department_id);
|
||||
});
|
||||
})
|
||||
->whereIn('source_type', self::SPECIAL_SOURCE_TYPES)
|
||||
->whereNotNull('rf_medicalhistory_id')
|
||||
->get()
|
||||
->keyBy('rf_medicalhistory_id');
|
||||
$misPatients = $this->misPatientSource->getPatients($user, $status, $dateRange, $branchId, $includeCurrent, $fillableAuto, $forSnapshots);
|
||||
$linkedManualPatients = $this->specialPatientSource->getLinkedManualPatientsForPeriod($department, $dateRange);
|
||||
|
||||
$mergedMisPatients = $misPatients->map(function ($patient) use ($linkedManualPatients) {
|
||||
$linkedManual = $linkedManualPatients->get($patient->MedicalHistoryID);
|
||||
@@ -306,281 +300,13 @@ class UnifiedPatientService
|
||||
);
|
||||
});
|
||||
|
||||
$manualDtos = $this->mapManualPatients($manualPatients, $dateRange);
|
||||
$manualDtos = $this->specialPatientSource->getDtos($department, $status, $dateRange, self::SPECIAL_SOURCE_TYPES, $forSnapshots);
|
||||
|
||||
return UnifiedPatientData::unique($mergedMisPatients->concat($manualDtos))
|
||||
->sortByDesc(fn (UnifiedPatientData $patient) => $patient->admittedAt ?? '')
|
||||
->values();
|
||||
}
|
||||
|
||||
private function getMisPatientDtos(
|
||||
User $user,
|
||||
string $status,
|
||||
DateRange $dateRange,
|
||||
int $branchId,
|
||||
?bool $includeCurrent = null,
|
||||
bool $fillableAuto = false,
|
||||
bool $forSnapshots = false
|
||||
): Collection {
|
||||
return $this->getMisPatients($user, $status, $dateRange, $branchId, $includeCurrent, $fillableAuto, $forSnapshots)
|
||||
->map(fn ($patient) => UnifiedPatientData::fromMisMedicalHistory(
|
||||
$patient,
|
||||
(bool) ($patient->is_recipient_today ?? false),
|
||||
))
|
||||
->sortByDesc(fn (UnifiedPatientData $patient) => $patient->admittedAt ?? '')
|
||||
->values();
|
||||
}
|
||||
|
||||
private function getSpecialPatientDtos(
|
||||
Department $department,
|
||||
string $status,
|
||||
DateRange $dateRange,
|
||||
bool $forSnapshots = false
|
||||
): Collection {
|
||||
return $this->mapManualPatients(
|
||||
$this->getManualPatients($department, $status, $dateRange, self::SPECIAL_SOURCE_TYPES, ! $forSnapshots),
|
||||
$dateRange
|
||||
);
|
||||
}
|
||||
|
||||
private function getMisPatients(
|
||||
User $user,
|
||||
string $status,
|
||||
DateRange $dateRange,
|
||||
int $branchId,
|
||||
?bool $includeCurrent = null,
|
||||
bool $fillableAuto = false,
|
||||
bool $forSnapshots = false
|
||||
): Collection {
|
||||
$isHeadOrAdmin = $user->isHeadOfDepartment() || $user->isAdmin();
|
||||
$includeCurrent = $includeCurrent ?? in_array($status, ['plan', 'emergency'], true);
|
||||
|
||||
return match ($status) {
|
||||
'plan', 'emergency' => $this->patientService->getPlanOrEmergencyPatients(
|
||||
$status,
|
||||
$isHeadOrAdmin,
|
||||
$branchId,
|
||||
$dateRange,
|
||||
false,
|
||||
false,
|
||||
$includeCurrent,
|
||||
$fillableAuto
|
||||
),
|
||||
'current' => $this->patientService->getAllPatientsInDepartment(
|
||||
$isHeadOrAdmin,
|
||||
$branchId,
|
||||
$dateRange,
|
||||
false,
|
||||
false,
|
||||
$fillableAuto
|
||||
),
|
||||
'recipient' => $this->patientService->getPlanOrEmergencyPatients(
|
||||
null,
|
||||
$isHeadOrAdmin,
|
||||
$branchId,
|
||||
$dateRange,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
$fillableAuto
|
||||
),
|
||||
'outcome' => $this->patientService->getOutcomePatients($branchId, $dateRange, 'without-transferred'),
|
||||
'outcome-discharged' => $this->patientService->getOutcomePatients($branchId, $dateRange, 'discharged'),
|
||||
'outcome-transferred' => $this->patientService->getOutcomePatients($branchId, $dateRange, 'transferred'),
|
||||
'outcome-deceased' => $this->patientService->getOutcomePatients($branchId, $dateRange, 'deceased'),
|
||||
'reanimation' => $this->patientService->getReanimationPatients($branchId, $dateRange),
|
||||
default => collect(),
|
||||
};
|
||||
}
|
||||
|
||||
private function getMisPatientsCount(
|
||||
User $user,
|
||||
string $status,
|
||||
DateRange $dateRange,
|
||||
int $branchId,
|
||||
?bool $includeCurrent = null,
|
||||
bool $fillableAuto = false
|
||||
): int {
|
||||
$isHeadOrAdmin = $user->isHeadOfDepartment() || $user->isAdmin();
|
||||
$includeCurrent = $includeCurrent ?? in_array($status, ['plan', 'emergency'], true);
|
||||
|
||||
return match ($status) {
|
||||
'plan', 'emergency' => $includeCurrent
|
||||
? $this->patientService->getPatientsCountWithCurrent($status, $isHeadOrAdmin, $branchId, $dateRange)
|
||||
: $this->patientService->getPlanOrEmergencyPatients(
|
||||
$status,
|
||||
$isHeadOrAdmin,
|
||||
$branchId,
|
||||
$dateRange,
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
$fillableAuto
|
||||
),
|
||||
'current' => $this->patientService->getAllPatientsInDepartment(
|
||||
$isHeadOrAdmin,
|
||||
$branchId,
|
||||
$dateRange,
|
||||
true,
|
||||
false,
|
||||
$fillableAuto
|
||||
),
|
||||
'recipient' => $this->patientService->getPlanOrEmergencyPatients(
|
||||
null,
|
||||
$isHeadOrAdmin,
|
||||
$branchId,
|
||||
$dateRange,
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
$fillableAuto
|
||||
),
|
||||
'outcome' => $this->patientService->getOutcomePatients($branchId, $dateRange, 'without-transferred', true)->count(),
|
||||
'outcome-discharged' => $this->patientService->getOutcomePatients($branchId, $dateRange, 'discharged', true)->count(),
|
||||
'outcome-transferred' => $this->patientService->getOutcomePatients($branchId, $dateRange, 'transferred', true)->count(),
|
||||
'outcome-deceased' => $this->patientService->getOutcomePatients($branchId, $dateRange, 'deceased', true)->count(),
|
||||
'reanimation' => $this->patientService->getReanimationPatients($branchId, $dateRange, true)->count(),
|
||||
default => 0,
|
||||
};
|
||||
}
|
||||
|
||||
private function getManualPatients(
|
||||
Department $department,
|
||||
string $status,
|
||||
DateRange $dateRange,
|
||||
?array $sourceTypes = self::SPECIAL_SOURCE_TYPES,
|
||||
bool $withOperations = true
|
||||
): Collection {
|
||||
$query = $this->buildManualPatientsQuery($department, $dateRange, $sourceTypes, $withOperations);
|
||||
|
||||
return match ($status) {
|
||||
'plan', 'emergency' => $query
|
||||
->current()
|
||||
->where('patient_kind', $status)
|
||||
->get(),
|
||||
'current' => $query
|
||||
->current()
|
||||
->get(),
|
||||
'recipient' => $query
|
||||
->whereBetween('admitted_at', [$dateRange->startSql(), $dateRange->endSql()])
|
||||
->get(),
|
||||
'outcome' => $query
|
||||
->whereNotNull('outcome_type')
|
||||
->whereIn('outcome_type', ['discharged', 'deceased'])
|
||||
->whereBetween('outcome_at', [$dateRange->startSql(), $dateRange->endSql()])
|
||||
->get(),
|
||||
'outcome-discharged', 'outcome-transferred', 'outcome-deceased' => $query
|
||||
->where('outcome_type', str_replace('outcome-', '', $status))
|
||||
->whereBetween('outcome_at', [$dateRange->startSql(), $dateRange->endSql()])
|
||||
->get(),
|
||||
'reanimation' => collect(),
|
||||
default => collect(),
|
||||
};
|
||||
}
|
||||
|
||||
private function getManualPatientsCount(
|
||||
Department $department,
|
||||
string $status,
|
||||
DateRange $dateRange,
|
||||
?array $sourceTypes = self::SPECIAL_SOURCE_TYPES
|
||||
): int {
|
||||
$query = $this->buildManualPatientsQuery($department, $dateRange, $sourceTypes, false);
|
||||
|
||||
return match ($status) {
|
||||
'plan', 'emergency' => $query
|
||||
->current()
|
||||
->where('patient_kind', $status)
|
||||
->count(),
|
||||
'current' => $query
|
||||
->current()
|
||||
->count(),
|
||||
'recipient' => $query
|
||||
->whereBetween('admitted_at', [$dateRange->startSql(), $dateRange->endSql()])
|
||||
->count(),
|
||||
'outcome' => $query
|
||||
->whereNotNull('outcome_type')
|
||||
->whereIn('outcome_type', ['discharged', 'deceased'])
|
||||
->whereBetween('outcome_at', [$dateRange->startSql(), $dateRange->endSql()])
|
||||
->count(),
|
||||
'outcome-discharged', 'outcome-transferred', 'outcome-deceased' => $query
|
||||
->where('outcome_type', str_replace('outcome-', '', $status))
|
||||
->whereBetween('outcome_at', [$dateRange->startSql(), $dateRange->endSql()])
|
||||
->count(),
|
||||
default => 0,
|
||||
};
|
||||
}
|
||||
|
||||
private function buildManualPatientsQuery(
|
||||
Department $department,
|
||||
DateRange $dateRange,
|
||||
?array $sourceTypes,
|
||||
bool $withOperations
|
||||
) {
|
||||
$reportIds = $this->getReportIdsForDepartmentPeriod($department, $dateRange);
|
||||
|
||||
$query = DepartmentPatient::query()
|
||||
->where(function ($builder) use ($department, $reportIds) {
|
||||
if (! empty($reportIds)) {
|
||||
$builder->whereIn('rf_report_id', $reportIds);
|
||||
}
|
||||
|
||||
$builder->orWhere(function ($legacyQuery) use ($department) {
|
||||
$legacyQuery->whereNull('rf_report_id')
|
||||
->where('rf_department_id', $department->department_id);
|
||||
});
|
||||
});
|
||||
|
||||
if ($withOperations) {
|
||||
$query->with(['operations.serviceMedical']);
|
||||
}
|
||||
|
||||
if ($sourceTypes !== null) {
|
||||
$query->whereIn('source_type', $sourceTypes);
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
private function getReportIdsForDepartmentPeriod(Department $department, DateRange $dateRange): array
|
||||
{
|
||||
return Report::query()
|
||||
->where('rf_department_id', $department->department_id)
|
||||
->when(
|
||||
$dateRange->isOneDay,
|
||||
fn ($query) => $query->exactPeriod($dateRange->startSql(), $dateRange->endSql()),
|
||||
fn ($query) => $query->withinPeriod($dateRange->startSql(), $dateRange->endSql()),
|
||||
)
|
||||
->pluck('report_id')
|
||||
->all();
|
||||
}
|
||||
|
||||
private function mapManualPatients(Collection $manualPatients, DateRange $dateRange): Collection
|
||||
{
|
||||
return $manualPatients
|
||||
->map(function (DepartmentPatient $patient) use ($dateRange) {
|
||||
$operationsRelation = $patient->relationLoaded('operations')
|
||||
? $patient->operations
|
||||
: collect();
|
||||
|
||||
$operations = $operationsRelation->map(fn ($operation) => [
|
||||
'id' => $operation->department_patient_operation_id,
|
||||
'code' => $operation->serviceMedical?->ServiceMedicalCode ?? $operation->service_code,
|
||||
'name' => $operation->serviceMedical?->ServiceMedicalName ?? $operation->service_name,
|
||||
'startAt' => $operation->started_at?->toIso8601String(),
|
||||
'endAt' => $operation->ended_at?->toIso8601String(),
|
||||
])->filter(fn ($operation) => $operation['code'] || $operation['name'])->values()->all();
|
||||
|
||||
return UnifiedPatientData::fromDepartmentPatient(
|
||||
$patient,
|
||||
$patient->admitted_at?->betweenIncluded($dateRange->startDate, $dateRange->endDate) ?? false,
|
||||
$operations,
|
||||
$this->resolveObservationComment(null, $patient->department_patient_id)
|
||||
);
|
||||
})
|
||||
->sortByDesc(fn (UnifiedPatientData $patient) => $patient->admittedAt ?? '')
|
||||
->values();
|
||||
}
|
||||
|
||||
private function parseScopedStatus(string $status): array
|
||||
{
|
||||
foreach (['mis', 'special'] as $scope) {
|
||||
|
||||
Reference in New Issue
Block a user