123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- namespace App\Jobs;
- use App\Facades\Servers\Logs\Log;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\ShouldBeUnique;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Bus\Dispatchable;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Queue\SerializesModels;
- use App\Facades\Servers\WeiBan\OpenApi;
- use App\Models\WeiBan\Sync;
- use App\Models\Custom;
- use App\Models\WeiBan\External as WeiBanExternal;
- use App\Models\WeiBan\Follow as WeiBanFollow;
- use App\Models\WeiBan\Tags as WeiBanTags;
- class WeiBanSync implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- /**
- * 任务可尝试的次数
- *
- * @var int
- */
- public $tries = 3;
- /**
- * 队列参数
- * @var array|mixed
- */
- protected $extUser = [];
-
- /**
- * Create a new job instance.
- *
- * @return void
- */
- public function __construct($extUser=[])
- {
- // 获取参数赋值
- $this->extUser = $extUser;
- }
- /**
- * Execute the job.
- *
- * @return void
- */
- public function handle()
- {
- // 如果存在需要更新的的客户
- if( $this->extUser['id'] ){
- // 实例化
- $Sync = new Sync();
- // 尝试执行
- try{
- // 记录错误信息
- Log::error('weiban_sync','进行更新',$this->extUser);
- // 通过id查询详情信息
- $extUser = OpenApi::getUserDetail($this->extUser['id']);
- // 不存在客户信息,再次获取
- if( !$extUser ) $extUser = OpenApi::getUserDetail($this->extUser['id']);
- // 存在客户信息,则继续
- if( $extUser ) $this->sync_user($extUser);
- // 解除锁定
- $Sync->unlockSyncExtidMark($this->extUser['id']);
- // 记录错误信息
- Log::error('weiban_sync','更新成功',$this->extUser);
- } catch (\Exception $exception) {
- // 解除锁定
- $Sync->unlockSyncExtidMark($this->extUser['id']);
- // 记录错误信息
- Log::error('weiban_sync_error',$exception->getMessage().'尝试执行第'.$this->attempts().'次',$this->extUser);
- // 每次尝试执行 时间间隔
- $this->release($this->attempts() * 5);
- }
- }
- }
- /**
- * 同步
- *
- * */
- public function sync_user($extUser){
- // 实例
- $Custom = New Custom();
- $External = New WeiBanExternal();
- $Follow = New WeiBanFollow();
- $Tags = New WeiBanTags();
- // 获取结果
- $followList = $extUser['follow_staffs'];
- // 获取结构数据
- $extUser = ['id'=>$extUser['id'],'name'=>$extUser['name'],'avatar'=>str_ireplace('http://','https://',(string)$extUser['avatar']),'gender'=>$extUser['gender'],'type'=>$extUser['type'],'corp_name'=>(string)$extUser['corp_name'],'corp_full_name'=>(string)$extUser['corp_full_name'],'insert_time'=>$extUser['created_at'],'update_time'=>time(),'custom_uid'=>0];
- // 手机号
- $phone = '';
- // 循环跟进客服
- foreach ( $followList as $follow ) {
- // 标签处理
- $this->tagsHandle($extUser['id'],$follow['staff_id'],$follow['tags'],$Tags);
- // 客服处理
- $this->staffHandle($follow,$extUser,$Follow);
- // 有手机号才获取手机号
- if( $follow['phone_number'] ) $phone = $follow['phone_number'];
- }
- // 如果没有客服,状态流失
- if( !$followList ) $extUser['status'] = 4;
- // 判断客户是否存在
- $oldExtUser = $External->query()->find($extUser['id'],['custom_uid']);
- // 如果存在账号的话获取客户UID
- if( $oldExtUser ) $extUser['custom_uid'] = ($oldExtUser->custom_uid);
- // 存在手机号,才创建账号
- if( $phone ) $extUser['custom_uid'] = $this->customHandle($phone,$extUser,$Custom);
- // 新增或者修改
- $External->query()->upsert($extUser,'id',['name','avatar','gender','type','corp_name','corp_full_name','status','custom_uid','update_time']);
- }
- /**
- * 标签处理
- */
- private function tagsHandle($extId,$staffId,$tagList,WeiBanTags $Tags){
- // 如果标签不存在,删除客服给客户的标签
- if( !$tagList ) return $Tags->query()->where([['weiban_extid','=',$extId],['staff_id','=',$staffId]])->delete();
- // 查询客户的标签
- $oldTags = $Tags->getListByExtStaff($extId,$staffId);
- // 循环标签数据
- foreach ($tagList as $k=>$tag) {
- // 标签数据
- $tag['id'] = 0;
- // 获取结果
- foreach ($oldTags as $oldtag) {
- // 如果有相同的话,获取ID
- if( $oldtag['name'] == $tag['name'] && $oldtag['group'] == $tag['group'] ) $tag['id'] = $oldtag['id'];
- }
- // 如果没有ID
- if( !$tag['id'] ) {
- // 返回结果
- $tag['id'] = $Tags->add(['name'=>$tag['name'],'group'=>$tag['group'],'weiban_extid'=>$extId,'staff_id'=>$staffId]);
- }
- // 重组
- $follow['tags'][$k] = $tag;
- }
- // 如果不在标签内的,删除
- if( $follow['tags'] ) $Tags->query()->where([['weiban_extid','=',$extId],['staff_id','=',$staffId]])->whereNotIn('id',array_column($follow['tags'],'id'))->delete();
- // 返回结果
- return true;
- }
- /**
- * 注册处理
- */
- private function customHandle($phone,$extUser,Custom $Custom){
- // 是否已经注册
- $custom = $Custom->getOneByPhone($phone);
- // 如果已经注册
- $uid = $custom ? $Custom->edit($custom['uid'],['username'=>$extUser['name'],'userpic'=>$extUser['avatar'],'sex'=>$extUser['gender'],'weiban_extid'=>$extUser['id']]) : $Custom->add(['phone'=>$phone,'username'=>$extUser['name'],'userpic'=>$extUser['avatar'],'sex'=>$extUser['gender'],'weiban_extid'=>$extUser['id']]);
- // 成功,赋值
- return $uid;
- }
- /**
- * 客服处理
- */
- private function staffHandle($follow,$extUser,WeiBanFollow $Follow){
- // 备注手机号,如果存在,解析成数组,转字符串
- $follow['remark_mobiles'] = $follow['remark_mobiles'] ? implode(',',json_decode($follow['remark_mobiles'],true)): '';
- // 获取必要数据
- $follow = [
- 'staff_id'=>(string)$follow['staff_id'],
- 'staff_name'=>str_ireplace('http://','https://',(string)$follow['staff_name']),
- 'staff_avatar'=>(string)$follow['staff_avatar'],
- 'phone_number'=>(string)$follow['phone_number'],
- 'remark'=>(string)$follow['remark'],
- 'remark_state'=>(string)$follow['remark_state'],
- 'remark_corp_name'=>(string)$follow['remark_corp_name'],
- 'remark_mobiles'=>(string)$follow['remark_mobiles'],
- 'state_name'=>(string)$follow['state_name'],
- 'state_text'=>(string)$follow['state_text'],
- 'state_type'=>(string)$follow['state_type'],
- 'deleted_by'=>(string)$follow['deleted_by'],
- // 员工删除客户1,客户删除的2
- 'status'=>( $follow['deleted'] ? 1 : ( $follow['deleted_each_other'] ? 2 : 0)),
- 'weiban_extid'=>$extUser['id'],
- ];
- // 如果没有企微企业,使用客服备注的企业
- if( !$extUser['corp_name']) $extUser['corp_name'] = $follow['remark_corp_name'];
- // 判断客户是否跟进中
- $followId = $Follow->query()->where([['weiban_extid','=',$extUser['id']],['staff_id','=',$follow['staff_id']]])->value('id');
- // 有则更新,无则增加
- $followId ? $Follow->edit($followId,$follow) : $Follow->add($follow);
- }
- }
|