UserEvent.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\bug\listener;
  4. use app\bug\model\Work;use app\bug\model\WorkUser;
  5. use app\user\model\AccountCompany;use app\user\model\Headquarters;
  6. use think\facade\Log;
  7. class UserEvent
  8. {
  9. private $model;
  10. private $type=['on'=>'启用','off'=>'禁用','delete'=>'删除'];
  11. /**
  12. * 事件监听处理
  13. * @return mixed
  14. */
  15. public function handle($event)
  16. {
  17. if (!isset($event['type'], $event['info_id'],$this->type[$event['type']]) || !is_string($event['type']) || !is_int
  18. ($event['info_id'])) {
  19. throw new \InvalidArgumentException('Invalid event data.');
  20. }
  21. $type=$event['type'];
  22. $id=$event['info_id'];
  23. $info = WorkUser::withTrashed()->with(['WorkRole','Account'=>['userInfo']])->where('id', $id)
  24. ->findOrEmpty();
  25. if ($info->isEmpty()) {
  26. return false;
  27. }
  28. switch ($info->WorkRole->belong) {
  29. case 1:
  30. $this->model = new \app\admin\model\UserRole();
  31. break;
  32. case 2:
  33. $this->model = new \app\cxinv\model\UserRole();
  34. break;
  35. case 3:
  36. $this->model = new \app\bug\model\UserRole();
  37. break;
  38. default:
  39. // 处理未知的belong值
  40. throw new \InvalidArgumentException('Unsupported belong value.');
  41. }
  42. $this->model->startTrans();
  43. try {
  44. $method = 'user'.ucfirst($type);
  45. $data=[
  46. 'uid'=>$info->account_id,
  47. 'nickname'=>$info->Account->nickname,
  48. 'roleid'=>$info->belong_role_id,
  49. 'companyNo'=>$info->companyNo,
  50. 'status'=>$info->WorkRole->status,
  51. 'id'=>$info->id,
  52. ];
  53. $result = $this->$method($data);
  54. if ($result === false) {
  55. $date = date('Y-m-d H:i:s');
  56. $sys = Work::$blongCn[$info->WorkRole->belong];
  57. $typeCn =$this->type[$type];
  58. Log::error("[$date]: {$sys} {$info->WorkRole->role_name}角色信息{$typeCn}失败");
  59. return false;
  60. }
  61. $this->model->commit();
  62. }catch (\Exception $e) {
  63. $this->model->rollback();
  64. // 日志记录异常信息
  65. Log::error('Error handling event: ' . $e->getMessage());
  66. return false;
  67. }
  68. }
  69. private function userOn($event){
  70. $info=$this->model->where([['uid','=',$event['uid']],['companyNo','=',$event['companyNo']],['is_del','=',0]])
  71. ->findOrEmpty();
  72. $company = Headquarters::where('code',$event['companyNo'])->value('type',0);
  73. $info->uid = $event['uid'];
  74. $info->nickname = $event['nickname'];
  75. $info->roleid = $event['roleid'];
  76. $info->status = $event['status'];
  77. $info->is_main = AccountCompany::where(['account_id'=>$event['uid'],"companyCode"=> $event['companyNo']])->value('is_main',0);
  78. $info->company_type =$company==1?2:1;
  79. $info->companyNo = $event['companyNo'];
  80. $save=$info->save();
  81. if($save){
  82. $work=WorkUser::where('id',$event['id'])->update(['status'=>2]);//系统同步成功
  83. if($work==false)throw new \Exception('系统同步失败');
  84. }
  85. return $save;
  86. }
  87. private function userOff($event){
  88. $info=$this->model->where([['uid','=',$event['uid']],['companyNo','=',$event['companyNo']],['roleid','=',
  89. $event['roleid']],['is_del','=',0]])->findOrEmpty();
  90. if($info->isEmpty())return false;
  91. $info->status = 0;
  92. $save=$info->save();
  93. return $save;
  94. }
  95. private function userDelete($event){
  96. $info=$this->model->where([['uid','=',$event['uid']],['companyNo','=',$event['companyNo']],['roleid','=',$event['roleid']],['is_del','=',0]])->findOrEmpty();
  97. if($info->isEmpty())return false;
  98. $info->is_del = 1;
  99. $save=$info->save();
  100. return $save;
  101. }
  102. }