* работа над функционалом автоматического заполнения

* исправил фантомный сдвиг даты
* переделал получение ФИО врачей из отделений
* добавил возможность поиска врача
* переписал сохранение отчета
This commit is contained in:
brusnitsyn
2026-02-05 17:11:43 +09:00
parent eab78a0291
commit 10fb138c30
22 changed files with 1192 additions and 654 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Services;
use App\Models\LifeMisMigrationPatient;
use App\Models\MisMedicalHistory;
use App\Models\MisMigrationPatient;
use App\Models\ObservationPatient;
@@ -19,17 +20,28 @@ class PatientService
DateRange $dateRange,
bool $countOnly = false,
bool $onlyIds = false,
bool $includeCurrent = false
bool $includeCurrent = false,
bool $fillableAuto = false
) {
// Получаем поступивших сегодня
$recipientQuery = $this->buildRecipientQuery($type, $isHeadOrAdmin, $branchId, $dateRange);
$recipientIds = $recipientQuery->pluck('rf_MedicalHistoryID')->toArray();
$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) {
$currentIds = MisMigrationPatient::currentlyInTreatment($branchId)
->pluck('rf_MedicalHistoryID')
->toArray();
if ($fillableAuto) {
$currentIds = LifeMisMigrationPatient::currentlyInTreatment($branchId, $dateRange)
->distinct()
->pluck('rf_MedicalHistoryID')
->toArray();
} else {
$currentIds = MisMigrationPatient::currentlyInTreatment($branchId)
->pluck('rf_MedicalHistoryID')
->toArray();
}
$medicalHistoryIds = array_unique(array_merge($recipientIds, $currentIds));
} else {
@@ -38,14 +50,10 @@ class PatientService
if (empty($medicalHistoryIds)) {
if ($countOnly) return 0;
if ($onlyIds) return [];
if ($onlyIds) return collect();
return collect();
}
if ($onlyIds) {
return $medicalHistoryIds;
}
// Получаем истории
$query = MisMedicalHistory::whereIn('MedicalHistoryID', $medicalHistoryIds)
->with([
@@ -53,7 +61,7 @@ class PatientService
$q->whereBetween('Date', [$dateRange->startSql(), $dateRange->endSql()]);
},
'migrations' => function ($q) use ($branchId) {
$q->where('rf_StationarBranchID', $branchId) // укажите поле сортировки
$q->where('rf_StationarBranchID', $branchId)
->take(1) // берем только одну последнюю
->with(['diagnosis' => function ($q) {
$q->where('rf_DiagnosTypeID', 3)
@@ -75,6 +83,10 @@ class PatientService
return $query->count();
}
if ($onlyIds) {
return $query->pluck('MedicalHistoryID');
}
return $query->get()->map(function ($patient) use ($recipientIds, $branchId) {
// Добавляем флаг "поступил сегодня"
$patient->is_recipient_today = in_array($patient->MedicalHistoryID, $recipientIds);
@@ -89,7 +101,8 @@ class PatientService
bool $isHeadOrAdmin,
int $branchId,
DateRange $dateRange,
bool $countOnly = false
bool $countOnly = false,
bool $onlyIds = false
) {
// Поступившие сегодня
$recipientIds = $this->buildRecipientQuery(null, $isHeadOrAdmin, $branchId, $dateRange)
@@ -113,9 +126,13 @@ class PatientService
return count($allIds);
}
if ($onlyIds) {
return collect($allIds);
}
return MisMedicalHistory::whereIn('MedicalHistoryID', $allIds)
->with(['surgicalOperations' => function ($q) use ($startDate, $endDate) {
$q->whereBetween('Date', [$startDate, $endDate]);
->with(['surgicalOperations' => function ($q) use ($dateRange) {
$q->whereBetween('Date', [$dateRange->startSql(), $dateRange->endSql()]);
}])
->orderBy('DateRecipient', 'DESC')
->get()
@@ -128,17 +145,22 @@ class PatientService
/**
* Получить пациентов под наблюдением
*/
public function getObservationPatients(int $departmentId)
public function getObservationPatients(int $departmentId, bool $onlyIds = false)
{
$patients = MisMedicalHistory::whereHas('observationPatient', function ($q) use ($departmentId) {
$query = MisMedicalHistory::whereHas('observationPatient', function ($q) use ($departmentId) {
$q->where('rf_department_id', $departmentId);
})
->with(['observationPatient' => function($query) use ($departmentId) {
});
if (!$onlyIds) {
$query->with(['observationPatient' => function ($query) use ($departmentId) {
$query->where('rf_department_id', $departmentId)
->select(['rf_medicalhistory_id', 'observation_patient_id', 'comment']);
}])
->orderBy('DateRecipient', 'DESC')
->get();
->orderBy('DateRecipient', 'DESC');
}
if ($onlyIds) $patients = $query->pluck('MedicalHistoryID');
else $patients = $query->get();
return $patients->map(function ($patient) {
$patient->comment = $patient->observationPatient
@@ -155,11 +177,13 @@ class PatientService
public function getOutcomePatients(
int $branchId,
DateRange $dateRange,
string $outcomeType = 'all'
string $outcomeType = 'all',
bool $onlyIds = false
) {
$methodMap = [
'discharged' => 'discharged',
'transferred' => 'transferred',
'discharged' => 'outcomeDischarged',
'transferred' => 'outcomeTransferred',
'without-transferred' => 'outcomeWithoutTransferred',
'deceased' => 'deceasedOutcome',
'all' => 'outcomePatients',
];
@@ -175,6 +199,8 @@ class PatientService
return collect();
}
if ($onlyIds) return collect($medicalHistoryIds);
return MisMedicalHistory::whereIn('MedicalHistoryID', $medicalHistoryIds)
->with(['surgicalOperations'])
->orderBy('DateRecipient', 'DESC')
@@ -282,17 +308,28 @@ class PatientService
?string $type,
bool $isHeadOrAdmin,
int $branchId,
DateRange $dateRange
DateRange $dateRange,
bool $fillableAuto = false
) {
// Разная логика для заведующего и врача
if ($isHeadOrAdmin) {
// Заведующий: все поступившие за период
$query = MisMigrationPatient::whereInDepartment($branchId)
->whereBetween('DateIngoing', [$dateRange->startSql(), $dateRange->endSql()]);
if ($fillableAuto) {
$query = LifeMisMigrationPatient::whereInDepartment($branchId)
->whereBetween('DateIngoing', [$dateRange->startSql(), $dateRange->endSql()]);
} else {
$query = MisMigrationPatient::whereInDepartment($branchId)
->whereBetween('DateIngoing', [$dateRange->startSql(), $dateRange->endSql()]);
}
} else {
// Врач: только поступившие за сутки
$query = MisMigrationPatient::whereInDepartment($branchId)
->whereBetween('DateIngoing', [$dateRange->startSql(), $dateRange->endSql()]);
if ($fillableAuto) {
$query = LifeMisMigrationPatient::whereInDepartment($branchId)
->whereBetween('DateIngoing', [$dateRange->startSql(), $dateRange->endSql()]);
} else {
$query = MisMigrationPatient::whereInDepartment($branchId)
->whereBetween('DateIngoing', [$dateRange->startSql(), $dateRange->endSql()]);
};
}
return $query;