Изменения в основном report

This commit is contained in:
brusnitsyn
2026-05-06 22:32:11 +09:00
parent c5da85763c
commit 723ccee8d3
56 changed files with 1911 additions and 3814 deletions

View File

@@ -2,9 +2,10 @@
namespace App\Services;
use App\Models\MedicalHistory;
use App\Models\MedicalHistorySnapshot;
use App\Models\Report;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
class MetrikaService
{
@@ -21,40 +22,58 @@ class MetrikaService
}
try {
// Получаем снапшоты с операциями
$results = DB::table('medical_history_snapshots as mhs')
->join('reports as r', 'mhs.rf_report_id', '=', 'r.report_id')
->join('stt_migrationpatient as mp', 'mhs.rf_medicalhistory_id', '=', 'mp.rf_MedicalHistoryID')
->join('stt_surgicaloperation as so', 'mhs.rf_medicalhistory_id', '=', 'so.rf_MedicalHistoryID')
->whereIn('r.rf_department_id', $departmentIds)
->where('r.period_start', '>=', $startDate)
->where('r.period_end', '<', $endDate)
->whereIn('mhs.patient_type', ['discharged', 'deceased'])
->select(
'r.rf_department_id',
'mp.rf_MedicalHistoryID',
DB::raw('MIN(mp."DateIngoing") as admission_date'),
DB::raw('MIN(so."Date") as first_operation_date')
)
->groupBy('r.rf_department_id', 'mp.rf_MedicalHistoryID')
->havingRaw('MIN(so."Date") IS NOT NULL')
->get()
$reports = Report::query()
->whereIn('rf_department_id', $departmentIds)
->where('period_start', '>=', $startDate)
->where('period_end', '<', $endDate)
->get(['report_id', 'rf_department_id'])
->groupBy('rf_department_id');
$preoperativeDays = [];
foreach ($departmentIds as $deptId) {
if (! isset($results[$deptId]) || $results[$deptId]->isEmpty()) {
$reportIds = $reports->get($deptId)?->pluck('report_id')->all() ?? [];
if ($reportIds === []) {
$preoperativeDays[$deptId] = 0;
continue;
}
$historyIds = MedicalHistorySnapshot::query()
->whereIn('rf_report_id', $reportIds)
->whereIn('patient_type', ['discharged', 'deceased'])
->pluck('rf_medicalhistory_id')
->filter()
->unique()
->values();
if ($historyIds->isEmpty()) {
$preoperativeDays[$deptId] = 0;
continue;
}
$histories = MedicalHistory::query()
->whereIn('original_id', $historyIds)
->with(['operations'])
->get();
$totalDays = 0;
$count = 0;
foreach ($results[$deptId] as $item) {
$admission = Carbon::parse($item->admission_date);
$operation = Carbon::parse($item->first_operation_date);
foreach ($histories as $history) {
$operationDate = $history->operations
->pluck('operation_date')
->filter()
->sort()
->first();
if (! $history->recipient_date || ! $operationDate) {
continue;
}
$admission = Carbon::parse($history->recipient_date);
$operation = Carbon::parse($operationDate);
$days = $admission->diffInDays($operation);
if ($days >= 0) {