123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php
- declare (strict_types = 1);
- namespace app\bug\listener;
- use app\bug\model\Work;use app\bug\model\WorkUser;
- use app\user\model\AccountCompany;use app\user\model\Headquarters;
- use think\facade\Log;
- class UserEvent
- {
- private $model;
- private $type=['on'=>'启用','off'=>'禁用','delete'=>'删除'];
- /**
- * 事件监听处理
- * @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'];
- $info = WorkUser::withTrashed()->with(['WorkRole','Account'=>['userInfo']])->where('id', $id)
- ->findOrEmpty();
- if ($info->isEmpty()) {
- return false;
- }
- switch ($info->WorkRole->belong) {
- case 1:
- $this->model = new \app\admin\model\UserRole();
- break;
- case 2:
- $this->model = new \app\cxinv\model\UserRole();
- break;
- case 3:
- $this->model = new \app\bug\model\UserRole();
- break;
- default:
- // 处理未知的belong值
- throw new \InvalidArgumentException('Unsupported belong value.');
- }
- $this->model->startTrans();
- try {
-
- $method = 'user'.ucfirst($type);
- $data=[
- 'uid'=>$info->account_id,
- 'nickname'=>$info->Account->nickname,
- 'roleid'=>$info->belong_role_id,
- 'companyNo'=>$info->companyNo,
- 'status'=>$info->WorkRole->status,
- 'id'=>$info->id,
- ];
- $result = $this->$method($data);
- if ($result === false) {
- $date = date('Y-m-d H:i:s');
- $sys = Work::$blongCn[$info->WorkRole->belong];
- $typeCn =$this->type[$type];
- Log::error("[$date]: {$sys} {$info->WorkRole->role_name}角色信息{$typeCn}失败");
- return false;
- }
- $this->model->commit();
- }catch (\Exception $e) {
- $this->model->rollback();
- // 日志记录异常信息
- Log::error('Error handling event: ' . $e->getMessage());
- return false;
- }
- }
-
- private function userOn($event){
- $info=$this->model->where([['uid','=',$event['uid']],['companyNo','=',$event['companyNo']],['is_del','=',0]])
- ->findOrEmpty();
- $company = Headquarters::where('code',$event['companyNo'])->value('type',0);
- $info->uid = $event['uid'];
- $info->nickname = $event['nickname'];
- $info->roleid = $event['roleid'];
- $info->status = $event['status'];
- $info->is_main = AccountCompany::where(['account_id'=>$event['uid'],"companyCode"=> $event['companyNo']])->value('is_main',0);
- $info->company_type =$company==1?2:1;
- $info->companyNo = $event['companyNo'];
- $save=$info->save();
- if($save){
- $work=WorkUser::where('id',$event['id'])->update(['status'=>2]);//系统同步成功
- if($work==false)throw new \Exception('系统同步失败');
- }
- return $save;
- }
- private function userOff($event){
- $info=$this->model->where([['uid','=',$event['uid']],['companyNo','=',$event['companyNo']],['roleid','=',
- $event['roleid']],['is_del','=',0]])->findOrEmpty();
- if($info->isEmpty())return false;
- $info->status = 0;
- $save=$info->save();
- return $save;
- }
-
- private function userDelete($event){
- $info=$this->model->where([['uid','=',$event['uid']],['companyNo','=',$event['companyNo']],['roleid','=',$event['roleid']],['is_del','=',0]])->findOrEmpty();
- if($info->isEmpty())return false;
- $info->is_del = 1;
- $save=$info->save();
- return $save;
- }
- }
|