"启用","off"=>"禁用","delete"=>"删除"]; /** * 事件监听处理 * @param $event * @param $type * @return mixed */ public function handle($event) { // 验证输入是否存在且类型正确 if (!isset($event['type'], $event['info_id'],$this->type[$event['type']]) || !is_string($event['type']) || !is_int ($event['info_id'])) { throw new \InvalidArgumentException('Invalid event data.'); } $type=$event["type"]; $id=$event["info_id"]; try { $info = WorkRole::withTrashed()->where('id', $id)->findOrEmpty(); if ($info->isEmpty()) { return false; } // 使用switch-case优化多分支逻辑 switch ($info->belong) { case 1: $this->model = new Role(); break; case 2: $this->model = new \app\cxinv\model\Role(); break; case 3: //修改为报表 $this->model = new \app\bug\model\Role(); break; default: // 处理未知的belong值 throw new \Exception('Unsupported belong value.'); } // 使用方法常量代替硬编码字符串 $method = "role".ucfirst($type); $result = $this->$method($type === 'off' || $type === 'delete' ? $info->belong_role_id : $info); if ($result === false) { $date = date('Y-m-d H:i:s'); $sys = Work::$blongCn[$info->belong]; $typeCn =$this->type[$type]; Log::error("[$date]: {$sys} {$info->role_name}角色信息{$typeCn}失败"); return false; } }catch (\Exception $e) { // 日志记录异常信息 Log::error('Error handling event: ' . $e->getMessage()); return false; } } private function roleOn($event){ $action = (new WorkAction)->GetBelongActionByIdArrs($event->action); $info=$this->model->with(["RoleInfo","ProcessInfo"])->where('id',$event->belong_role_id)->findOrEmpty(); $data = [ 'role_name' => $event->role_name, 'role_level' => $event->level, 'companyNo' => $event->companyNo, 'status' => 1 ]; if($info->isEmpty()){ $info=$this->model->create($data); if($info->isEmpty())return false; $this->saveRoleAction($info,$action); $this->saveRoleProcess($info,$action,$event); }else{ $isSave = $info->save($data); if($isSave){ $this->saveRoleAction($info,$action); $this->saveRoleProcess($info,$action,$event); } } if($event->belong_role_id==0){ $event->belong_role_id =$info->id; $event->save(); } return true; } private function roleOff($eventId){ $info=$this->model->where('id',$eventId)->findOrEmpty(); if($info->isEmpty())return false; $info->status = 0; $isSave = $info->save(); return $isSave; } private function roleDelete($eventId){ $info=$this->model->with(["RoleInfo","ProcessInfo"])->where('id',$eventId)->findOrEmpty(); if($info->isEmpty())return false; $isSave = $info->together(['RoleInfo','ProcessInfo'])->delete(); return $isSave; } private function saveRoleAction($info,$action){ if(is_null($info->RoleInfo)||!isset($info->RoleInfo)){ $this->model->RoleInfo()->save([ 'action_conllect' => $action['action']??[], 'action_data' => [], 'private_data' => $action['private']??[], 'role_id' => $info->id, 'status' => 1 ]); }else $info->RoleInfo->save([ 'action_conllect' => $action['action']??[], 'action_data' => [], 'private_data' => $action['private']??[], ]); } private function saveRoleProcess($info,$action,$event){ if(is_null($info->ProcessInfo)||!isset($info->ProcessInfo)){ $this->model->ProcessInfo()->save([ 'action_data' => $action['process'], 'role_id' => $info->id, 'createrid' => $event->apply_id, 'creater' => $event->apply_name, 'updaterid' => $event->apply_id, 'updater' => $event->apply_name, ]); }else $info->ProcessInfo->save([ 'action_data' => $action['process'], ]); } }