where('id', $userid)->first(); if (!$user || !$user->department_id) { return \Yz::Return(true, '查询成功', ['list' => [], 'count' => 0]); } $list = DB::table('s_devices') ->join('s_department_resources_device', 's_devices.id', '=', 's_department_resources_device.device_id') ->where('s_department_resources_device.department_id', $user->department_id) ->where('s_devices.is_del', 0); if (!empty($searchInfo['name'])) { $list->where('s_devices.device_name', 'like', '%' . $searchInfo['name'] . '%'); } if (isset($searchInfo['status']) && $searchInfo['status'] !== '') { $list->where('s_devices.status', $searchInfo['status']); } $count = $list->count(); $items = $list->select('s_devices.*') ->orderBy('s_devices.id', 'desc') ->skip(($page - 1) * $pageSize) ->take($pageSize) ->get(); return \Yz::Return(true, '查询成功', ['list' => $items, 'count' => $count]); } //保存设备(新建时同时关联到当前科室) public function Save($data, $userid) { $user = DB::table('users')->where('id', $userid)->first(); if (!$user || !$user->department_id) { return \Yz::Return(false, '用户科室信息不存在'); } DB::beginTransaction(); try { if (empty($data['id'])) { //新建设备 $deviceId = DB::table('s_devices')->insertGetId([ 'device_name' => $data['device_name'], 'status' => $data['status'] ?? 1, 'is_del' => 0, ]); //关联到当前科室 DB::table('s_department_resources_device')->insert([ 'department_id' => $user->department_id, 'device_id' => $deviceId, ]); } else { //更新设备 DB::table('s_devices')->where('id', $data['id'])->update([ 'device_name' => $data['device_name'], 'status' => $data['status'] ?? 1, ]); } DB::commit(); return \Yz::Return(true, '保存成功'); } catch (\Exception $e) { DB::rollBack(); return \Yz::Return(false, '保存失败'); } } //删除设备(软删除 + 解除关联) public function Del($id) { DB::beginTransaction(); try { DB::table('s_devices')->where('id', $id)->update(['is_del' => 1]); DB::table('s_department_resources_device')->where('device_id', $id)->delete(); DB::commit(); return \Yz::Return(true, '删除成功'); } catch (\Exception $e) { DB::rollBack(); return \Yz::Return(false, '删除失败'); } } }