WeiBanSync.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace App\Jobs;
  3. use App\Facades\Servers\Logs\Log;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldBeUnique;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. use App\Facades\Servers\WeiBan\OpenApi;
  11. use App\Models\WeiBan\Sync;
  12. use App\Models\Custom;
  13. use App\Models\WeiBan\External as WeiBanExternal;
  14. use App\Models\WeiBan\Follow as WeiBanFollow;
  15. use App\Models\WeiBan\Tags as WeiBanTags;
  16. class WeiBanSync implements ShouldQueue
  17. {
  18. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  19. /**
  20. * 任务可尝试的次数
  21. *
  22. * @var int
  23. */
  24. public $tries = 3;
  25. /**
  26. * 队列参数
  27. * @var array|mixed
  28. */
  29. protected $extUser = [];
  30. /**
  31. * Create a new job instance.
  32. *
  33. * @return void
  34. */
  35. public function __construct($extUser=[])
  36. {
  37. // 获取参数赋值
  38. $this->extUser = $extUser;
  39. }
  40. /**
  41. * Execute the job.
  42. *
  43. * @return void
  44. */
  45. public function handle()
  46. {
  47. // 如果存在需要更新的的客户
  48. if( $this->extUser['id'] ){
  49. // 实例化
  50. $Sync = new Sync();
  51. // 尝试执行
  52. try{
  53. // 记录错误信息
  54. Log::error('weiban_sync','进行更新',$this->extUser);
  55. // 通过id查询详情信息
  56. $extUser = OpenApi::getUserDetail($this->extUser['id']);
  57. // 不存在客户信息,再次获取
  58. if( !$extUser ) $extUser = OpenApi::getUserDetail($this->extUser['id']);
  59. // 存在客户信息,则继续
  60. if( $extUser ) $this->sync_user($extUser);
  61. // 解除锁定
  62. $Sync->unlockSyncExtidMark($this->extUser['id']);
  63. // 记录错误信息
  64. Log::error('weiban_sync','更新成功',$this->extUser);
  65. } catch (\Exception $exception) {
  66. // 解除锁定
  67. $Sync->unlockSyncExtidMark($this->extUser['id']);
  68. // 记录错误信息
  69. Log::error('weiban_sync_error',$exception->getMessage().'尝试执行第'.$this->attempts().'次',$this->extUser);
  70. // 每次尝试执行 时间间隔
  71. $this->release($this->attempts() * 5);
  72. }
  73. }
  74. }
  75. /**
  76. * 同步
  77. *
  78. * */
  79. public function sync_user($extUser){
  80. // 实例
  81. $Custom = New Custom();
  82. $External = New WeiBanExternal();
  83. $Follow = New WeiBanFollow();
  84. $Tags = New WeiBanTags();
  85. // 获取结果
  86. $followList = $extUser['follow_staffs'];
  87. // 获取结构数据
  88. $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];
  89. // 手机号
  90. $phone = '';
  91. // 循环跟进客服
  92. foreach ( $followList as $follow ) {
  93. // 标签处理
  94. $this->tagsHandle($extUser['id'],$follow['staff_id'],$follow['tags'],$Tags);
  95. // 客服处理
  96. $this->staffHandle($follow,$extUser,$Follow);
  97. // 有手机号才获取手机号
  98. if( $follow['phone_number'] ) $phone = $follow['phone_number'];
  99. }
  100. // 如果没有客服,状态流失
  101. if( !$followList ) $extUser['status'] = 4;
  102. // 判断客户是否存在
  103. $oldExtUser = $External->query()->find($extUser['id'],['custom_uid']);
  104. // 如果存在账号的话获取客户UID
  105. if( $oldExtUser ) $extUser['custom_uid'] = ($oldExtUser->custom_uid);
  106. // 存在手机号,才创建账号
  107. if( $phone ) $extUser['custom_uid'] = $this->customHandle($phone,$extUser,$Custom);
  108. // 新增或者修改
  109. $External->query()->upsert($extUser,'id',['name','avatar','gender','type','corp_name','corp_full_name','status','custom_uid','update_time']);
  110. }
  111. /**
  112. * 标签处理
  113. */
  114. private function tagsHandle($extId,$staffId,$tagList,WeiBanTags $Tags){
  115. // 如果标签不存在,删除客服给客户的标签
  116. if( !$tagList ) return $Tags->query()->where([['weiban_extid','=',$extId],['staff_id','=',$staffId]])->delete();
  117. // 查询客户的标签
  118. $oldTags = $Tags->getListByExtStaff($extId,$staffId);
  119. // 循环标签数据
  120. foreach ($tagList as $k=>$tag) {
  121. // 标签数据
  122. $tag['id'] = 0;
  123. // 获取结果
  124. foreach ($oldTags as $oldtag) {
  125. // 如果有相同的话,获取ID
  126. if( $oldtag['name'] == $tag['name'] && $oldtag['group'] == $tag['group'] ) $tag['id'] = $oldtag['id'];
  127. }
  128. // 如果没有ID
  129. if( !$tag['id'] ) {
  130. // 返回结果
  131. $tag['id'] = $Tags->add(['name'=>$tag['name'],'group'=>$tag['group'],'weiban_extid'=>$extId,'staff_id'=>$staffId]);
  132. }
  133. // 重组
  134. $follow['tags'][$k] = $tag;
  135. }
  136. // 如果不在标签内的,删除
  137. if( $follow['tags'] ) $Tags->query()->where([['weiban_extid','=',$extId],['staff_id','=',$staffId]])->whereNotIn('id',array_column($follow['tags'],'id'))->delete();
  138. // 返回结果
  139. return true;
  140. }
  141. /**
  142. * 注册处理
  143. */
  144. private function customHandle($phone,$extUser,Custom $Custom){
  145. // 是否已经注册
  146. $custom = $Custom->getOneByPhone($phone);
  147. // 如果已经注册
  148. $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']]);
  149. // 成功,赋值
  150. return $uid;
  151. }
  152. /**
  153. * 客服处理
  154. */
  155. private function staffHandle($follow,$extUser,WeiBanFollow $Follow){
  156. // 备注手机号,如果存在,解析成数组,转字符串
  157. $follow['remark_mobiles'] = $follow['remark_mobiles'] ? implode(',',json_decode($follow['remark_mobiles'],true)): '';
  158. // 获取必要数据
  159. $follow = [
  160. 'staff_id'=>(string)$follow['staff_id'],
  161. 'staff_name'=>str_ireplace('http://','https://',(string)$follow['staff_name']),
  162. 'staff_avatar'=>(string)$follow['staff_avatar'],
  163. 'phone_number'=>(string)$follow['phone_number'],
  164. 'remark'=>(string)$follow['remark'],
  165. 'remark_state'=>(string)$follow['remark_state'],
  166. 'remark_corp_name'=>(string)$follow['remark_corp_name'],
  167. 'remark_mobiles'=>(string)$follow['remark_mobiles'],
  168. 'state_name'=>(string)$follow['state_name'],
  169. 'state_text'=>(string)$follow['state_text'],
  170. 'state_type'=>(string)$follow['state_type'],
  171. 'deleted_by'=>(string)$follow['deleted_by'],
  172. // 员工删除客户1,客户删除的2
  173. 'status'=>( $follow['deleted'] ? 1 : ( $follow['deleted_each_other'] ? 2 : 0)),
  174. 'weiban_extid'=>$extUser['id'],
  175. ];
  176. // 如果没有企微企业,使用客服备注的企业
  177. if( !$extUser['corp_name']) $extUser['corp_name'] = $follow['remark_corp_name'];
  178. // 判断客户是否跟进中
  179. $followId = $Follow->query()->where([['weiban_extid','=',$extUser['id']],['staff_id','=',$follow['staff_id']]])->value('id');
  180. // 有则更新,无则增加
  181. $followId ? $Follow->edit($followId,$follow) : $Follow->add($follow);
  182. }
  183. }