Обновил метод вывода умерших
This commit is contained in:
@@ -104,58 +104,11 @@ class StatisticController extends Controller
|
||||
|
||||
$dateRange = $this->dateRangeService->getNormalizedDateRange($user, $validated['startAt'], $validated['endAt']);
|
||||
|
||||
$lastMigrations = DB::table('report_duty_migration_patients')
|
||||
->select('id', DB::raw('MAX(ingoing_date) as max_ingoing_date'))
|
||||
->where('ingoing_date', '>=', $dateRange->startSql())
|
||||
->where('ingoing_date', '<=', $dateRange->endSql())
|
||||
->groupBy('id');
|
||||
|
||||
$deadPatients = DB::table('report_duty_migration_patients as rdm')
|
||||
->joinSub($lastMigrations, 'last_rdm', function ($join) {
|
||||
$join->on('rdm.id', '=', 'last_rdm.id')
|
||||
->on('rdm.ingoing_date', '=', 'last_rdm.max_ingoing_date');
|
||||
})
|
||||
->leftJoin('report_duty_patients as rdp', 'rdm.id', '=', 'rdp.id')
|
||||
->join('report_duties as rd', 'rd.id', '=', 'rdp.report_duty_id')
|
||||
->join('departments as dep', 'dep.department_id', '=', 'rd.rf_department_id')
|
||||
->whereIn('rd.rf_department_id', $availableDepartments)
|
||||
->whereNotNull('rdp.death_date')
|
||||
->select(
|
||||
'rdp.id', 'rdp.full_name', 'rdp.birth_date', 'rdp.recipient_date', 'rdm.ingoing_date',
|
||||
'rdm.diagnosis_code', 'rdm.diagnosis_name', 'dep.name_full'
|
||||
)
|
||||
->orderBy('rdm.ingoing_date', 'desc')
|
||||
->orderBy('rdm.id', 'desc')
|
||||
->get()
|
||||
->groupBy('medical_history_id') // Группируем по id пациента
|
||||
->map(fn ($rows) => $rows->first()) // Берем первую (самую последнюю) запись
|
||||
->values()
|
||||
->map(fn ($row) => [
|
||||
'id' => $row->id,
|
||||
'full_name' => $row->full_name,
|
||||
'birth_date' => $row->birth_date,
|
||||
'ingoing_date' => $row->ingoing_date ?? $row->recipient_date,
|
||||
'diagnosis_code' => $row->diagnosis_code,
|
||||
'diagnosis_name' => $row->diagnosis_name,
|
||||
'department_name' => $row->name_full,
|
||||
]);
|
||||
|
||||
$deadPatients = ReportDutyPatient::query()
|
||||
->whereHas('latestMigration', function ($query) use ($dateRange) {
|
||||
$query->where('ingoing_date', '<=', $dateRange->endSql())
|
||||
->where(function ($sub) use ($dateRange) {
|
||||
// Миграции без out_date (еще лежат)
|
||||
$sub->whereNull('out_date');
|
||||
|
||||
// Миграции с out_date (закрытые)
|
||||
$sub->orWhere(function ($sub2) use ($dateRange) {
|
||||
$sub2->whereNotNull('out_date')
|
||||
->where('out_date', '>', $dateRange->startSql());
|
||||
});
|
||||
});
|
||||
})
|
||||
->with('latestMigration')
|
||||
->whereNotNull('death_date')
|
||||
->where('death_date', '>', $dateRange->startSql())
|
||||
->where('death_date', '<=', $dateRange->endSql())
|
||||
->orderBy('death_date', 'desc')
|
||||
->get()->unique('original_id')->values()
|
||||
->map(fn ($row) => [
|
||||
'id' => $row->id,
|
||||
|
||||
Reference in New Issue
Block a user