model = new OpmDsDevice; $this->repository = DeviceRepository::new(); } public function index(): Response { return raw_view('device/index'); } public function insert(Request $request): Response { if ($request->method() === 'POST') { return parent::insert($request); } return raw_view('device/insert'); } public function update(Request $request): Response { if ($request->method() === 'POST') { return parent::update($request); } return raw_view('device/update'); } /** * 设备下拉选项(可按 hospital_id 筛选) */ public function options(Request $request): Response { $query = $this->repository->scopedQuery(); if ($hospitalId = $request->get('hospital_id')) { $query->where('hospital_id', $hospitalId); } $data = $query->select(['id', 'name'])->get() ->map(fn($item) => ['id' => $item->id, 'name' => $item->name]) ->toArray(); return json(['code' => 0, 'msg' => 'ok', 'data' => $data]); } protected function afterQuery($items) { $statusMap = [1 => '正常', 2 => '离线', 3 => '报警']; $typeMap = [1 => '网关', 2 => '消毒灭菌器']; foreach ($items as &$item) { $hospital = OpmMwHospital::find($item->hospital_id); $department = OpmMwDepartment::find($item->department_id); $item->hospital_name = $hospital->organ_name ?? '未知'; $item->department_name = $department->dept_name ?? '未知'; $item->status_text = $statusMap[$item->status] ?? '未知'; $item->type_text = $typeMap[$item->type] ?? '未知'; } return $items; } }