RoleEvent.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\bug\listener;
  4. use app\admin\model\Role;
  5. use app\bug\model\Work;
  6. use app\bug\model\WorkAction;
  7. use app\bug\model\WorkRole;
  8. use think\facade\Log;
  9. class RoleEvent
  10. {
  11. private $model;
  12. private $type=["on"=>"启用","off"=>"禁用","delete"=>"删除"];
  13. /**
  14. * 事件监听处理
  15. * @param $event
  16. * @param $type
  17. * @return mixed
  18. */
  19. public function handle($event)
  20. {
  21. // 验证输入是否存在且类型正确
  22. if (!isset($event['type'], $event['info_id'],$this->type[$event['type']]) || !is_string($event['type']) || !is_int
  23. ($event['info_id'])) {
  24. throw new \InvalidArgumentException('Invalid event data.');
  25. }
  26. $type=$event["type"];
  27. $id=$event["info_id"];
  28. try {
  29. $info = WorkRole::withTrashed()->where('id', $id)->findOrEmpty();
  30. if ($info->isEmpty()) {
  31. return false;
  32. }
  33. // 使用switch-case优化多分支逻辑
  34. switch ($info->belong) {
  35. case 1:
  36. $this->model = new Role();
  37. break;
  38. case 2:
  39. $this->model = new \app\cxinv\model\Role();
  40. break;
  41. case 3: //修改为报表
  42. $this->model = new \app\bug\model\Role();
  43. break;
  44. default:
  45. // 处理未知的belong值
  46. throw new \Exception('Unsupported belong value.');
  47. }
  48. // 使用方法常量代替硬编码字符串
  49. $method = "role".ucfirst($type);
  50. $result = $this->$method($type === 'off' || $type === 'delete' ? $info->belong_role_id : $info);
  51. if ($result === false) {
  52. $date = date('Y-m-d H:i:s');
  53. $sys = Work::$blongCn[$info->belong];
  54. $typeCn =$this->type[$type];
  55. Log::error("[$date]: {$sys} {$info->role_name}角色信息{$typeCn}失败");
  56. return false;
  57. }
  58. }catch (\Exception $e) {
  59. // 日志记录异常信息
  60. Log::error('Error handling event: ' . $e->getMessage());
  61. return false;
  62. }
  63. }
  64. private function roleOn($event){
  65. $action = (new WorkAction)->GetBelongActionByIdArrs($event->action);
  66. $info=$this->model->with(["RoleInfo","ProcessInfo"])->where('id',$event->belong_role_id)->findOrEmpty();
  67. $data = [
  68. 'role_name' => $event->role_name,
  69. 'role_level' => $event->level,
  70. 'companyNo' => $event->companyNo,
  71. 'status' => 1
  72. ];
  73. if($info->isEmpty()){
  74. $info=$this->model->create($data);
  75. if($info->isEmpty())return false;
  76. $this->saveRoleAction($info,$action);
  77. $this->saveRoleProcess($info,$action,$event);
  78. }else{
  79. $isSave = $info->save($data);
  80. if($isSave){
  81. $this->saveRoleAction($info,$action);
  82. $this->saveRoleProcess($info,$action,$event);
  83. }
  84. }
  85. if($event->belong_role_id==0){
  86. $event->belong_role_id =$info->id;
  87. $event->save();
  88. }
  89. return true;
  90. }
  91. private function roleOff($eventId){
  92. $info=$this->model->where('id',$eventId)->findOrEmpty();
  93. if($info->isEmpty())return false;
  94. $info->status = 0;
  95. $isSave = $info->save();
  96. return $isSave;
  97. }
  98. private function roleDelete($eventId){
  99. $info=$this->model->with(["RoleInfo","ProcessInfo"])->where('id',$eventId)->findOrEmpty();
  100. if($info->isEmpty())return false;
  101. $isSave = $info->together(['RoleInfo','ProcessInfo'])->delete();
  102. return $isSave;
  103. }
  104. private function saveRoleAction($info,$action){
  105. if(is_null($info->RoleInfo)||!isset($info->RoleInfo)){
  106. $this->model->RoleInfo()->save([
  107. 'action_conllect' => $action['action']??[],
  108. 'action_data' => [],
  109. 'private_data' => $action['private']??[],
  110. 'role_id' => $info->id,
  111. 'status' => 1
  112. ]);
  113. }else $info->RoleInfo->save([
  114. 'action_conllect' => $action['action']??[],
  115. 'action_data' => [],
  116. 'private_data' => $action['private']??[],
  117. ]);
  118. }
  119. private function saveRoleProcess($info,$action,$event){
  120. if(is_null($info->ProcessInfo)||!isset($info->ProcessInfo)){
  121. $this->model->ProcessInfo()->save([
  122. 'action_data' => $action['process'],
  123. 'role_id' => $info->id,
  124. 'createrid' => $event->apply_id,
  125. 'creater' => $event->apply_name,
  126. 'updaterid' => $event->apply_id,
  127. 'updater' => $event->apply_name,
  128. ]);
  129. }else $info->ProcessInfo->save([
  130. 'action_data' => $action['process'],
  131. ]);
  132. }
  133. }