100 lines
3.0 KiB
PHP
100 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class RecipientPatientService
|
|
{
|
|
public function __construct(
|
|
protected CurrentPatientService $currentPatientService
|
|
) {}
|
|
|
|
// Получить IDs поступивших
|
|
public function getRecipientMedicalHistoryIds(
|
|
?string $type,
|
|
bool $isHeadOrAdmin,
|
|
int $branchId,
|
|
DateRange $dateRange
|
|
): array {
|
|
return $this->buildRecipientQuery($type, $isHeadOrAdmin, $branchId, $dateRange, false)
|
|
->distinct()
|
|
->pluck('rf_MedicalHistoryID')
|
|
->toArray();
|
|
}
|
|
|
|
/**
|
|
* Построить запрос для поступивших пациентов
|
|
*/
|
|
public function buildRecipientQuery(
|
|
?string $type,
|
|
bool $isHeadOrAdmin,
|
|
int $branchId,
|
|
DateRange $dateRange,
|
|
bool $fillableAuto = false
|
|
) {
|
|
$startAt = $dateRange->start()->copy()->format('Y-m-d H:i:s');
|
|
$endAt = $dateRange->end()->copy()->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();
|
|
}
|
|
|
|
// Получить IDs состоящих
|
|
private function getCurrentMedicalHistoryIds(
|
|
string $type,
|
|
int $branchId,
|
|
DateRange $dateRange,
|
|
bool $fillableAuto,
|
|
): array {
|
|
return $this->currentPatientService->getCurrentMedicalHistoryIds($type, $branchId, $dateRange, $fillableAuto);
|
|
}
|
|
|
|
public function resolvePlanOrEmergencyMedicalHistoryIds(
|
|
?string $type,
|
|
bool $isHeadOrAdmin,
|
|
int $branchId,
|
|
DateRange $dateRange,
|
|
bool $includeCurrent,
|
|
bool $fillableAuto
|
|
): array {
|
|
$recipientIds = $this->getRecipientMedicalHistoryIds(
|
|
$type,
|
|
$isHeadOrAdmin,
|
|
$branchId,
|
|
$dateRange
|
|
);
|
|
|
|
if (! $includeCurrent) {
|
|
return array_values(array_unique($recipientIds));
|
|
}
|
|
|
|
$currentIds = $this->getCurrentMedicalHistoryIds($type, $branchId, $dateRange, $fillableAuto);
|
|
|
|
return array_values(array_unique(array_merge($recipientIds, $currentIds)));
|
|
}
|
|
}
|