123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- <?php
- namespace App\Jobs;
- use App\Facades\Servers\Logs\Log;
- use Illuminate\Bus\Queueable;
- 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;
- use App\Models\City;
- use App\Models\CustomAddr;
- 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 = '';
- // 标签处理
- $staffId = '';
- // 循环跟进客服
- 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);
- // 需要有客服,如果存在的旧账号但是 没有关联客户UID 还存在系统客户UID可关联
- if( $staffId && isset($oldExtUser['custom_uid']) && empty($oldExtUser['custom_uid']) && $extUser['custom_uid'] ){
- // 城市标签
- $this->cityTags($extUser,$staffId,$Custom);
- // 终端标签
- $this->shopTypeTag($staffId,$extUser);
- }
- // 新增或者修改
- $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);
- }
- /**
- * 城市标签
- */
- private function cityTags($extUser,$staffId,Custom $Custom){
- // 实例化
- $City = New City();
- // 获取城市ID
- $cityId = $Custom->getValue($extUser['custom_uid'],'city_id');
- // 如果有城市
- if( !$cityId ) return ['info'=>'请先设置城市'];
- // 获取用户城市信息
- $city = $City->getOne($cityId);
- // 如果有城市信息
- if( !$city ) return ['info'=>'未查询到城市'];
- // 获取上级城市/省份
- $parentCity = $City->getOne($city['pid']);
- // 如果上级是省辖,获取省份
- if( isset($parentCity['name']) && $parentCity['name'] == '直辖县级' ) $parentCity = $City->getOne($parentCity['pid']);
- // 如果有获取到省份信息
- if( !$parentCity ) return ['info'=>'未查询到省份'];
- // 省份
- $province = str_ireplace(['自治区','壮族','回族','维吾尔','特别行政区','省'],'',$parentCity['name']);
- // 省份
- $cityName = str_ireplace(['自治州','自治县','蒙古','蒙古族','回族','藏族','维吾尔','苗族','彝族','壮族','布依族','朝鲜族','满族','侗族','瑶族','白族','土家族','哈尼族','哈萨克','傣族','黎族','傈僳族','佤族','畲族','拉祜族','水族','东乡族','纳西族','景颇族','柯尔克孜','土族','达斡尔族','仫佬族','羌族','布朗族','撒拉族','毛南族','仡佬族','锡伯','阿昌族','普米族','塔吉克','怒族','鄂温克族','德昂族','保安族','裕固族','塔塔尔','独龙族'],'',$city['name']);
- // 打省份标签
- if( !$province ) return ['info'=>'暂无省份数据'];
- // 打省份标签
- if( !$cityName ) return ['info'=>'暂无城市数据'];
- // 打省份标签
- $result['province'] = OpenApi::addTag($staffId,$extUser['id'],'省份',$province);
- // 打城市标签
- $result['cityNname'] = OpenApi::addTag($staffId,$extUser['id'],$province,$cityName);
- // 返回结果
- return ['success'=>'返回结果','result'=>$result];
- }
- /**
- * 终端类型标签
- *
- */
- public function shopTypeTag($staffId,$extUser){
- // 地址模型
- $Addr = New CustomAddr();
- // 打终端类型标签
- $shopType = (int) $Addr->query()->where([['custom_uid','=',$extUser['custom_uid']],['shop_type','>',0]])->value('shop_type');
- // 如果有地址信息
- switch ($shopType) {
- case '1':
- $shopType = '单店';
- break;
- case '3':
- $shopType = '连锁';
- break;
- case '4':
- $shopType = '第三终端';
- break;
- default:
- $shopType = '';
- break;
- }
- // 没有数据,直接结束
- if( !$shopType ) return ['info'=>'没有终端类型标签'];
- // 如果有终端类型
- $result = OpenApi::addTag($staffId,$extUser['id'],'企业类型',$shopType);
- // 数据返回
- return ['success'=>'返回结果','result'=>$result];
- }
- }
|