123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- declare (strict_types = 1);
- namespace app\bug\listener;
- use app\admin\model\Role;
- use app\bug\model\Work;
- use app\bug\model\WorkAction;
- use app\bug\model\WorkRole;
- use think\facade\Log;
- class RoleEvent
- {
- private $model;
- private $type=["on"=>"启用","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'],
- ]);
- }
- }
|