123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php namespace App\Http\Controllers\Admin;
- use App\Models\Custom;
- use App\Models\Work\Sync as Model;
- use App\Models\Work\Tag as WorkTag;
- use App\Models\Work\User as WorkUser;
- use App\Models\Work\External as WorkExternal;
- use App\Facades\Servers\Redis\RedisLock;
- use App\Facades\Servers\WechatWork\ExternalContact;
- /**
- * 客户同步
- *
- * @author 刘相欣
- *
- */
- class WorkSync extends Auth{
-
- protected function _initialize(){
- parent::_initialize();
- $this->assign('breadcrumb1','客户同步');
- $this->assign('breadcrumb2','同步任务');
- }
- /**
- * 列表页
- *
- * */
- public function index(Model $Model){
- // 接受参数
- // 查询条件
- $map = [];
- // 查询数据
- $list = $Model->query()->where($map)->orderByDesc('id')->paginate(config('page_num',10));
- // 分配数据
- $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
- $this->assign('list',$list);
- // 加载模板
- return $this->fetch();
- }
- /**
- * 添加
- *
- * */
- public function add(Model $Model){
- // 接收数据
- $data['cursor'] = request('cursor','');
- $data['has_total'] = request('has_total',0);
- $data['sync_total'] = request('sync_total',0);
- $data['status'] = request('status',0);
- // 查询是否有进行中的任务
- $runing = $Model->query()->where([['status','=',0]])->value('id');
- // 如果操作失败
- if( $runing ) return json_send(['code'=>'error','msg'=>'请等待任务完成']);
- // 写入数据表
- $id = $Model->add($data);
- // 如果操作失败
- if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']);
- // 记录行为
- $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
- // 告知结果
- return json_send(['code'=>'success','msg'=>'新增成功','path'=>'']);
- }
- /**
- * 仅取消
- *
- * */
- public function set_status(Model $Model){
- // 设置状态
- $id = request('id',0);
- $status = request('status',4);
- // 查询用户
- $oldData = $Model->where(['id'=>$id])->first();
- // 如果用户不存在
- if( !$oldData ) return json_send(['code'=>'error','msg'=>'任务不存在']);
- // 执行修改
- $result = $Model->edit($id,['status'=>$status]);
- // 提示新增失败
- if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
- // 记录行为
- $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
- // 告知结果
- return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
- }
- /**
- * 执行任务
- *
- * */
- public function run_task(){
- // 实例
- $Model = New Model();
- // 获取数据
- $taskList = $Model->getList();
- // 如果没有任务列表,结束
- if( !$taskList ) return ['error'=>'无待执行任务'];
- // 如果没有任务列表,结束
- $task = array_pop($taskList);
- // 返回结果
- if( !$task ) return ['error'=>'无待执行任务'];
- // 异步锁上锁,失败结束
- if( !RedisLock::lock('work:sync:task:id',$task['id'],'180') ) return ['error'=>'任务执行中...'];
- // 获取接待人员
- $userIdList = ExternalContact::getFollowUsers();
- // 如果没有成员ID列表,结束
- if( !$userIdList ) {
- // 解锁
- RedisLock::unlock('work:sync:task:id',$task['id']);
- // 无需同步
- return ['error'=>'未获取到跟进人员'];
- }
- // 获取游标
- $cursor = $task['cursor'];
- // 批量获取客户详情
- $extUserList = ExternalContact::batchGetByUser($userIdList,$cursor,100);
- // 如果没有客户列表,结束
- if( !$extUserList ) {
- // 解锁
- RedisLock::unlock('work:sync:task:id',$task['id']);
- // 结束
- return ['error'=>'未获取到外部联系人'];
- }
- // 实例其他模型
- $CustomModel = New Custom();
- $WorkExternalModel = New WorkExternal();
- $WorkUserModel = New WorkUser();
- $WorkTagModel = New WorkTag();
- // 下一页游标
- $task['cursor'] = $extUserList['next_cursor'];
- // 外部联系人列表
- $extUserList = $extUserList['external_contact_list'];
- // 更新的数据
- $extUsers = [];
- $followUsers = [];
- $followTags = [];
- // 时间
- $time = time();
- // 循环处理
- foreach ( $extUserList as $key=>$value ) {
- // 外部联系人
- $extUser = [];
- $extUser['name'] = $value['external_contact']['name'];
- $extUser['avatar'] = str_ireplace('http://','https://',$value['external_contact']['avatar']);
- $extUser['gender'] = $value['external_contact']['gender'];
- $extUser['work_type'] = $value['external_contact']['type'];
- $extUser['external_userid'] = $value['external_contact']['external_userid'];
- $extUser['insert_time'] = $time;
- $extUser['update_time'] = $time;
- // 处理外部联系人
- $extUsers[] = $extUser;
- // 跟进客服
- $followUser = [];
- $followUser['external_userid'] = $extUser['external_userid'];
- $followUser['work_userid'] = $value['follow_info']['userid'];
- $followUser['remark'] = $value['follow_info']['remark'];
- $followUser['description'] = $value['follow_info']['description'];
- $followUser['insert_time'] = $time;
- $followUser['update_time'] = $time;
- $followUser['remark_mobiles'] = implode(',',$value['follow_info']['remark_mobiles']);
- $followUser['remark_company'] = empty($value['external_contact']['corp_name'])? (empty($value['follow_info']['remark_corp_name']) ? '' : $value['follow_info']['remark_corp_name']) : $value['external_contact']['corp_name'];
- $followUsers[] = $followUser;
- // 跟进人员标签组合
- foreach ( $value['follow_info']['tag_id'] as $tagId ) {
- // 重组
- $followTags[] = ['tag_id'=>$tagId,'external_userid'=>$followUser['external_userid'],'work_userid'=>$followUser['work_userid'],'update_time'=>$time];
- }
- // 跟进人员是否备注手机号
- $extUser['phone'] = $value['follow_info']['remark_mobiles'] ? $value['follow_info']['remark_mobiles'][0] : '';
- // 重组数据
- $extUserList[$key] = $extUser;
- }
- // 标签写入
- if( $followTags ) $WorkTagModel->query()->insert($followTags);
- // 需要更新的数据
- $WorkExternalModel->query()->upsert($extUsers,'external_userid',['name','avatar','gender','work_type','update_time']);
- // 需要更新的数据
- $WorkUserModel->query()->upsert($followUsers,['external_userid','work_userid'],['remark','description','remark_mobiles','remark_company','update_time']);
- // 获取外部联系人ID,并去重
- $customIds = array_values(array_unique(array_filter(array_column($extUserList,'external_userid'))));
- // 获取外部联系人ID,并去重
- $customPhones = array_values(array_unique(array_filter(array_column($extUserList,'phone'))));
- // 查询客户表中是否存在该客户
- $customIds = $customIds ? $WorkExternalModel->getPluckInFiled('external_userid',$customIds,'custom_uid','external_userid') : [];
- // 获取
- $customPhones = $customPhones ? $CustomModel->getPluckInFiled('phone',$customPhones,'external_userid','phone') : [];
- // 循环处理
- foreach ( $extUserList as $value ) {
- // 不存在外部联系人账号的跳过
- if( !isset($customIds[$value['external_userid']]) ) continue;
- // 存在在外部联系人账号但是绑定了账号的跳过
- if( $customIds[$value['external_userid']] ) continue;
- // 不存在手机号,跳过
- if( !$value['phone'] ) continue;
- // 如果绑定了其他外部联系人
- if( !empty($customPhones[$value['phone']]) ) continue;
- // 同步数量 +1
- $task['sync_total'] += 1;
- // 账号UID默认
- $uid = 0;
- // 账号未注册
- if( !isset($customPhones[$value['phone']]) ) {
- // 注册账号
- $uid = $CustomModel->add(['phone'=>$value['phone'],'username'=>$value['name'],'userpic'=>$value['avatar'],'sex'=>$value['gender'],'external_userid'=>$value['external_userid']]);
- // 注册失败,
- if( !$uid ) continue;
- }else{
- // 已经注册的,未绑定外部联系人
- if( !$customPhones[$value['phone']] ) {
- // 查询号码
- $custom = $CustomModel->getOneByPhone($value['phone']);
- // 没有账号,跳过
- if( !$custom ) continue;
- // 账号存在跳过
- if( $custom['external_userid'] ) {
- // 避免重复注册
- $customPhones[$value['phone']] = $custom['external_userid'];
- continue;
- }
- // 账号UID
- $uid = $custom['uid'];
- }
- }
- // 避免重复注册
- $customPhones[$value['phone']] = $value['external_userid'];
- // 修改
- $result = $uid ? $WorkExternalModel->query()->where([['external_userid','=',$value['external_userid']]])->update(['custom_uid'=>$uid,'update_time'=>$time]) : true;
- // 如果修改失败
- if( !$result ) continue;
- }
- // 如果没有下一页游标,标记任务完成
- if( !$task['cursor'] ) $task['status'] = 1;
- // 修改任务情况
- $Model->edit($task['id'],$task);
- // 解锁
- RedisLock::unlock('work:sync:task:id',$task['id']);
- // 结束
- return ['success'=>'批量成功=>'.$task['sync_total']];
- }
- }
|