RoleEvent.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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","RoleProcess"])->where('id',$event->belong_role_id)->findOrEmpty();
  67. $info->role_name = $event->role_name;
  68. $info->role_level = $event->level;
  69. $info->level = $event->level;
  70. $info->companyNo = $event->companyNo;
  71. $info->status = 1;
  72. $isSave = $info->save();
  73. if($isSave){
  74. if(is_null($info->RoleInfo)){
  75. $info->RoleInfo()->save( [
  76. 'action_conllect' => $action['action']??[],
  77. 'action_data' => [],
  78. 'private_data' => $action['private']??[],
  79. 'role_id' => $info->id,
  80. 'status' => 1
  81. ]);
  82. }else$info->RoleInfo->save( [
  83. 'action_conllect' => $action['action']??[],
  84. 'action_data' => [],
  85. 'private_data' => $action['private']??[],
  86. ]);
  87. if(is_null($info->RoleProcess)){
  88. $info->RoleProcess()->save( [
  89. 'action_data' => $action['process']??[],
  90. 'role_id' => $info->id,
  91. "createrid"=>$info->apply_id,
  92. "creater"=>$info->apply_name,
  93. "updaterid"=>$info->apply_id,
  94. "updater"=>$info->apply_name,
  95. ]);
  96. }else$info->RoleProcess->save( [
  97. 'action_data' =>$action['process'],
  98. ]);
  99. if($event->belong_role_id==0){
  100. $event->belong_role_id =$info->id;
  101. $event->save();
  102. }
  103. }
  104. return $isSave;
  105. }
  106. private function roleOff($eventId){
  107. $info=$this->model->where('id',$eventId)->findOrEmpty();
  108. if($info->isEmpty())return false;
  109. $info->status = 0;
  110. $isSave = $info->save();
  111. return $isSave;
  112. }
  113. private function roleDelete($eventId){
  114. $info=$this->model->with(["RoleInfo","RoleProcess"])->where('id',$eventId)->findOrEmpty();
  115. if($info->isEmpty())return false;
  116. $isSave = $info->together(['RoleInfo','RoleProcess'])->delete();
  117. return $isSave;
  118. }
  119. }