Files
onboard/app/Http/Controllers/Api/NurseController.php

243 lines
8.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\MedicalHistory;
use App\Models\MedicalHistoryCorrection;
use App\Models\MedicalHistoryNurse;
use App\Models\MigrationPatient;
use App\Models\MigrationPatientCorrection;
use App\Models\MigrationPatientNurse;
use App\Models\MisMKSB;
use App\Models\MisStationarBranch;
use App\Models\ReportNurse;
use App\Models\ReportNurseMigrationPatient;
use App\Models\ReportNursePatient;
use App\Models\UnifiedMedicalHistory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class NurseController extends Controller
{
public function getPatient($id, Request $request)
{
return Cache::remember("nurse_patient:{$id}", 120, function () use ($id, $request) {
return MisMKSB::where('MedicalHistoryID', $id)->select(MisMKSB::workColumns())->first();
});
}
public function searchPatients(Request $request)
{
$search = $request->search;
$results = Cache::remember('patients_search_' . $search, 120, function () use ($search) {
return MisMKSB::select([
'MedicalHistoryID as original_id', 'MedCardNum as medical_card_number', DB::raw('(FAMILY + \' \' + Name + \' \' + OT) as full_name'),
])
->findByFIO($search)
->limit(10)
->orderBy('DateExtract', 'desc')
->get()->map(function ($item) {
$fullName = Str::title($item->full_name);
return [
'label' => "$item->medical_card_number - $fullName",
'value' => $item->original_id
];
});
});
return $results;
}
public function storePatient(Request $request)
{
$user = auth()->user();
$department = auth()->user()->department;
$misUserId = $user->rf_lpudoctor_id;
$data = $request->validate([
'source_type' => 'nullable',
'medical_card_number' => 'nullable',
'full_name' => 'required',
'birth_date' => 'required',
'recipient_date' => 'required',
'extract_date' => 'nullable',
'death_date' => 'nullable',
'male' => 'nullable',
'urgency_id' => 'required',
'hospital_result_id' => 'nullable',
'visit_result_id' => 'required',
'mis_user_id' => 'nullable',
'comment' => 'nullable',
]);
$data['user_id'] = $user->id;
$branch = MisStationarBranch::where('rf_DepartmentID', $department->rf_mis_department_id)
->first();
$migrationData = [
'ingoing_date' => $data['recipient_date'],
'out_date' => $data['extract_date'],
'department_id' => $department->rf_mis_department_id,
'stationar_branch_id' => $branch->StationarBranchID,
'visit_result_id' => $data['visit_result_id'],
'user_id' => $data['user_id'],
'mis_user_id' => $misUserId
];
DB::beginTransaction();
$historyNurse = MedicalHistoryNurse::create($data);
$migrationData['medical_history_id'] = $historyNurse->id;
$migrationNurse = MigrationPatientNurse::create($migrationData);
$historyNurse->update([
'latest_migration_id' => $migrationNurse->id
]);
if ($historyNurse && $migrationNurse) {
DB::commit();
} else {
DB::rollBack();
}
return response()->json([
'data' => $historyNurse,
], 201);
}
public function storeCorrection($id, Request $request)
{
$sourceType = $request->patient_source;
$originalId = $request->original_id;
$data = $request->validate([
'medical_card_number' => 'nullable',
'full_name' => 'nullable',
'birth_date' => 'nullable',
'recipient_date' => 'nullable',
'extract_date' => 'nullable',
'death_date' => 'nullable',
'male' => 'nullable',
'urgency_id' => 'nullable',
'hospital_result_id' => 'nullable',
'visit_result_id' => 'nullable',
'mis_user_id' => 'nullable',
'comment' => 'nullable',
]);
$data['medical_history_id'] = $id;
$data['user_id'] = auth()->user()->id;
$departmentId = auth()->user()->department->rf_mis_department_id;
if ($sourceType === 'mis')
$currentMigration = MigrationPatient::currentMigration($id, $departmentId)->first();
else
$currentMigration = MigrationPatientNurse::currentMigration($originalId, $departmentId)->first();
$migrationData = [
'migration_patient_id' => $currentMigration->id,
'medical_history_id' => $id,
'ingoing_date' => $data['recipient_date'],
'out_date' => $data['extract_date'],
'visit_result_id' => $data['visit_result_id'],
'user_id' => $data['user_id'],
];
DB::beginTransaction();
if ($sourceType === 'mis') {
$historyCorrection = MedicalHistoryCorrection::create($data);
$migrationCorrection = MigrationPatientCorrection::create($migrationData);
} else if ($sourceType === 'manual') {
unset($data['medical_history_id']);
unset($migrationData['migration_patient_id']);
unset($migrationData['medical_history_id']);
$historyCorrection = MedicalHistoryNurse::find($originalId)->update($data);
$migrationCorrection = MigrationPatientNurse::where('medical_history_id', $originalId)->update($migrationData);
}
if ($historyCorrection && $migrationCorrection) {
DB::commit();
// Синхронизируем снапшот, чтобы правки были видны в МИС-вкладке
$reportNurseId = $request->input('report_nurse_id');
$this->syncNurseSnapshot($originalId, $data, $migrationData, $departmentId, $reportNurseId);
return response()->json([
'data' => $historyCorrection,
], 201);
} else {
DB::rollBack();
return response()->json([
'data' => 'Something went wrong',
], 400);
}
}
private function syncNurseSnapshot(
mixed $originalId,
array $data,
array $migrationData,
int $departmentId,
?int $reportNurseId
): void {
$patientUpdate = collect($data)
->only(['recipient_date', 'extract_date', 'death_date', 'urgency_id',
'visit_result_id', 'hospital_result_id', 'full_name', 'birth_date'])
->toArray();
if (empty($patientUpdate)) return;
// Если известен отчёт — обновляем только его и все последующие по этому отделению
$reportNurseQuery = ReportNursePatient::where('original_id', $originalId);
if ($reportNurseId) {
$fromReport = ReportNurse::find($reportNurseId);
if ($fromReport) {
$affectedReportIds = ReportNurse::where('rf_department_id', $fromReport->rf_department_id)
->where('period_start', '>=', $fromReport->period_start)
->pluck('id');
$reportNurseQuery->whereIn('report_nurse_id', $affectedReportIds);
}
}
$snapshotPatientIds = $reportNurseQuery->pluck('id');
if ($snapshotPatientIds->isEmpty()) return;
ReportNursePatient::whereIn('id', $snapshotPatientIds)->update($patientUpdate);
$migrationUpdate = array_filter([
'ingoing_date' => $migrationData['ingoing_date'] ?? null,
'out_date' => $migrationData['out_date'] ?? null,
'visit_result_id' => $migrationData['visit_result_id'] ?? null,
]);
if (!empty($migrationUpdate)) {
ReportNurseMigrationPatient::whereIn('medical_history_id', $snapshotPatientIds)
->where('department_id', $departmentId)
->update($migrationUpdate);
}
}
public function deleteHistory($id, Request $request)
{
$medicalHistory = UnifiedMedicalHistory::where('id', $id)->first();
$originalMedicalHistoryId = $medicalHistory->original_id;
$nurseMedicalHistory = ReportNursePatient::where('original_id', $originalMedicalHistoryId)->first();
$migrationPatientsIds = $medicalHistory->migrations()->pluck('original_id');
foreach ($nurseMedicalHistory->migrations() as $nurseMigrationPatient) {
$nurseMigrationPatient->delete();
}
$nurseMedicalHistory->delete();
return response()->json([]);
}
}