UserInheritanceJobs.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace App\Jobs\OpenWork\External;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldBeUnique;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use App\Servers\DB\DbService;
  10. use App\Models\OpenWork\job\Records;
  11. use App\Facades\Servers\Logs\Log;
  12. use App\Servers\OpenWork\External\ContactService;
  13. use App\Models\OpenWork\External\AllocationRecords as AllocationRecordsModel;
  14. use Illuminate\Support\Facades\Cache;
  15. /**
  16. * 继承状态同步任务(在职继承&离职继承)
  17. * @author 唐远望
  18. * @version 1.0
  19. * @date 2025-11-14
  20. */
  21. class UserInheritanceJobs implements ShouldQueue
  22. {
  23. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  24. protected $message_data;
  25. protected $Records;
  26. /**
  27. * Create a new job instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct(array $message_data)
  32. {
  33. $this->message_data = $message_data;
  34. }
  35. /**
  36. * Execute the job.
  37. *
  38. * @return void
  39. */
  40. public function handle()
  41. {
  42. try {
  43. $corpId = $this->message_data['corpid'];
  44. (new DbService())->getConnectionNameByCorpId($corpId);
  45. // 创建任务记录
  46. $this->Records = Records::create([
  47. 'job_id' => $corpId . '_UserInheritanceJobs',
  48. 'name' => static::class,
  49. 'payload' => json_encode($this->message_data),
  50. 'status' => 'processing',
  51. 'started_at' => now()
  52. ]);
  53. $handoveruserId = $this->message_data['handover_userid'];
  54. $takeoveruserId = $this->message_data['takeover_userid'];
  55. $inherit_type = $this->message_data['inherit_type'];
  56. $execution_count = isset($this->message_data['execution_count'])? $this->message_data['execution_count'] : 1;
  57. $cursor = isset($this->message_data['cursor'])? $this->message_data['cursor'] : '';
  58. if($inherit_type == '1'){
  59. $this->queryTransferStatus($corpId, $handoveruserId, $takeoveruserId,$cursor,$execution_count);
  60. }
  61. if($inherit_type == '2'){
  62. $this->queryTransferStatus_resigned($corpId, $handoveruserId, $takeoveruserId,$cursor,$execution_count);
  63. }
  64. //删除任务记录
  65. $this->Records->delete();
  66. } catch (\Exception $e) {
  67. // 失败处理...
  68. if ($this->Records) {
  69. $this->Records->delete();
  70. }
  71. Log::info('job_error', '继承查询更新任务失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  72. }
  73. }
  74. /**
  75. * 查询客户接替状态-在职继承
  76. * @author 唐远望
  77. * @version 1.0
  78. * @date 2025-11-14
  79. * @param $authCorpId 授权方企业微信id
  80. * @param $handoveruserId 原添加成员的userid
  81. * @param $takeoveruserId 接替成员的userid
  82. * @return array
  83. *
  84. */
  85. private function queryTransferStatus($authCorpId, $handoveruserId, $takeoveruserId, $cursor = '',$execution_count = 1)
  86. {
  87. $AllocationRecordsModel = new AllocationRecordsModel();
  88. // 数据结果
  89. $result = (new ContactService())->transferResultLine($authCorpId, $handoveruserId, $takeoveruserId, $cursor);
  90. // 如果获取结果失败
  91. if (!$result) return true;
  92. // 错误提示
  93. if ($result['errcode']){
  94. if (strpos($result['errmsg'], '45035') || strpos($result['errmsg'], '45033')) { //并发操作冲突|接口并发调用超过限制
  95. if ($execution_count > 2) {
  96. Log::info('job_error', '在职继承状态任务队列查询失败,重试了3次仍然失败', ['data' => $this->message_data, 'error' => $result]);
  97. return true;
  98. }
  99. $message_data = $this->message_data;
  100. $message_data['execution_count'] = $execution_count + 1;
  101. $seconds = 5 * $message_data['execution_count']; //每次延长5秒
  102. $this->dispatch($message_data)->delay($seconds);
  103. return true;
  104. } else {
  105. Log::info('job_error', '在职继承状态任务队列查询错误', ['data' => $this->message_data, 'error' => $result]);
  106. return true;
  107. }
  108. }
  109. if(!isset($result['customer']) || empty($result['customer'])){
  110. Log::info('job_error', '在职继承状态任务队列查询错误,找不到转接客户信息', ['data' => $this->message_data, 'error' => $result]);
  111. return true;
  112. }
  113. // 列表结果
  114. $customer_list = $result['customer'];
  115. $status_external_userid_datas =[];
  116. foreach ($customer_list as $key => $value) {
  117. $status = $value['status']; //接替状态, 1-接替完毕 2-等待接替 3-客户拒绝 4-接替成员客户达到上限
  118. switch ($value['status']) {
  119. case '1':
  120. $status = '2'; //转移状态1=进行中2=成功3=失败
  121. break;
  122. case '2':
  123. $status = '1'; //转移状态1=进行中2=成功3=失败
  124. # code...
  125. break;
  126. case '3':
  127. $status = '3'; //转移状态1=进行中2=成功3=失败
  128. # code...
  129. break;
  130. case '4':
  131. $status = '4'; //转移状态1=进行中2=成功3=失败
  132. break;
  133. }
  134. //状态分组
  135. $status_external_userid_datas[$status]['external_userid'][] = $value['external_userid'];
  136. $status_external_userid_datas[$status]['wecom_status'] = $value['status'];
  137. }
  138. foreach ($status_external_userid_datas as $status_key => $status_data) {
  139. $AllocationRecordsModel->query()
  140. ->whereIn('external_userid', $status_data['external_userid'])
  141. ->where(['original_followup_person' => $handoveruserId, 'current_followup_person' => $takeoveruserId, 'corpid' => $authCorpId])
  142. ->update(['status' => $status_key, 'wecom_status' => $status_data['wecom_status'],'update_time'=> time()]);
  143. }
  144. //校验是否存在下一页
  145. if(isset($result['next_cursor']) && $result['next_cursor'] != ''){
  146. $next_cursor = $result['next_cursor'];
  147. $message_data = $this->message_data;
  148. $message_data['cursor'] = $next_cursor;
  149. $message_data['execution_count'] = 1;
  150. $this->dispatch($message_data);
  151. }
  152. return true;
  153. }
  154. /**
  155. * 查询客户接替状态-离职继承
  156. * @author 唐远望
  157. * @version 1.0
  158. * @date 2025-11-14
  159. * @param $authCorpId 授权方企业微信id
  160. * @param $handoveruserId 原添加成员的userid
  161. * @param $takeoveruserId 接替成员的userid
  162. * @return array
  163. *
  164. */
  165. private function queryTransferStatus_resigned($authCorpId, $handoveruserId, $takeoveruserId, $cursor = '',$execution_count = 1)
  166. {
  167. $AllocationRecordsModel = new AllocationRecordsModel();
  168. // 数据结果
  169. $result = (new ContactService())->transferResultLine_resigned($authCorpId, $handoveruserId, $takeoveruserId, $cursor);
  170. // 如果获取结果失败
  171. if (!$result) return true;
  172. // 错误提示
  173. if ($result['errcode']){
  174. if (strpos($result['errmsg'], '45035') || strpos($result['errmsg'], '45033')) { //并发操作冲突|接口并发调用超过限制
  175. if ($execution_count > 2) {
  176. Log::info('job_error', '离职继承状态任务队列查询失败,重试了3次仍然失败', ['data' => $this->message_data, 'error' => $result]);
  177. return true;
  178. }
  179. $message_data = $this->message_data;
  180. $message_data['execution_count'] = $execution_count + 1;
  181. $seconds = 5 * $message_data['execution_count']; //每次延长5秒
  182. $this->dispatch($message_data)->delay($seconds);
  183. return true;
  184. } else {
  185. Log::info('job_error', '离职继承状态任务队列查询错误', ['data' => $this->message_data, 'error' => $result]);
  186. return true;
  187. }
  188. }
  189. // 列表结果
  190. $customer_list = $result['customer'];
  191. if (empty($customer_list)) {
  192. return true;
  193. }
  194. $status_external_userid_datas =[];
  195. foreach ($customer_list as $key => $value) {
  196. $status = $value['status']; //接替状态, 1-接替完毕 2-等待接替 3-客户拒绝 4-接替成员客户达到上限
  197. switch ($value['status']) {
  198. case '1':
  199. $status = '2'; //转移状态1=进行中2=成功3=失败
  200. break;
  201. case '2':
  202. $status = '1'; //转移状态1=进行中2=成功3=失败
  203. # code...
  204. break;
  205. case '3':
  206. $status = '3'; //转移状态1=进行中2=成功3=失败
  207. # code...
  208. break;
  209. case '4':
  210. $status = '4'; //转移状态1=进行中2=成功3=失败
  211. break;
  212. }
  213. //状态分组
  214. $status_external_userid_datas[$status]['external_userid'][] = $value['external_userid'];
  215. $status_external_userid_datas[$status]['wecom_status'] = $value['status'];
  216. }
  217. foreach ($status_external_userid_datas as $status_key => $status_data) {
  218. $AllocationRecordsModel->query()
  219. ->whereIn('external_userid', $status_data['external_userid'])
  220. ->where(['original_followup_person' => $handoveruserId, 'current_followup_person' => $takeoveruserId, 'corpid' => $authCorpId])
  221. ->update(['status' => $status_key, 'wecom_status' => $status_data['wecom_status']]);
  222. }
  223. //校验是否存在下一页
  224. if(isset($result['next_cursor']) && $result['next_cursor'] != ''){
  225. $next_cursor = $result['next_cursor'];
  226. $message_data = $this->message_data;
  227. $message_data['cursor'] = $next_cursor;
  228. $message_data['execution_count'] = 1;
  229. $this->dispatch($message_data);
  230. }
  231. return true;
  232. }
  233. public function failed(\Throwable $exception)
  234. {
  235. Log::info('job_error', '继承查询更新任务完全失败', ['data' => $this->message_data, 'error' => $exception->getMessage()]);
  236. $this->Records->delete();
  237. }
  238. }