GroupInheritanceJobs.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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\Facades\Servers\Logs\Log;
  10. use App\Servers\DB\DbService;
  11. use App\Models\OpenWork\Group\Chat as ChatModel;
  12. use App\Models\OpenWork\Group\AllocationRecords as GroupAllocationRecordsModel;
  13. use App\Servers\OpenWork\External\ContactService;
  14. /**
  15. * 客户群在职继承队列
  16. * @author 唐远望
  17. * @version 1.0
  18. * @date 2025-11-18
  19. */
  20. class GroupInheritanceJobs implements ShouldQueue
  21. {
  22. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  23. protected $message_data;
  24. protected $Records;
  25. /**
  26. * Create a new job instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct(array $message_data)
  31. {
  32. $this->message_data = $message_data;
  33. }
  34. /**
  35. * Execute the job.
  36. *
  37. * @return void
  38. */
  39. public function handle()
  40. {
  41. try {
  42. $corpId = $this->message_data['corpid'];
  43. (new DbService())->getConnectionNameByCorpId($corpId);
  44. $chat_id_list_array = $this->message_data['chat_id_list'];
  45. $new_owner = $this->message_data['new_owner'];
  46. $operator_userid = $this->message_data['operator_userid'];
  47. $inherit_type = $this->message_data['inherit_type'];
  48. if ($inherit_type == 1) {
  49. //群在职继承
  50. $this->on_the_job($corpId, $chat_id_list_array, $new_owner, $operator_userid);
  51. }
  52. if ($inherit_type == 2) {
  53. //群离职继承
  54. $this->off_the_job($corpId, $chat_id_list_array, $new_owner, $operator_userid);
  55. }
  56. return true;
  57. // 成功处理...
  58. } catch (\Exception $e) {
  59. Log::info('job_error', 'GroupInheritanceJobs任务队列处理失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  60. }
  61. }
  62. /**
  63. * 在职继承队列任务处理
  64. * @author 唐远望
  65. * @version 1.0
  66. * @date 2025-11-18
  67. */
  68. public function on_the_job($corpId, $chat_id_list_array, $new_owner, $operator_userid)
  69. {
  70. $ChatModel = new ChatModel();
  71. $GroupAllocationRecordsModel = new GroupAllocationRecordsModel();
  72. $ContactService = new ContactService();
  73. $insert_data = [];
  74. //筛选正常的群
  75. $chat_data_list = $ChatModel->where(['corpid' => $corpId, 'status' => '0'])->whereIn('chat_id', $chat_id_list_array)->select(['chat_id', 'owner'])->get()->toArray();
  76. if (empty($chat_data_list)) {
  77. Log::info('job_error', 'GroupInheritanceJobs任务队列处理失败,群在职继承列表为空', ['data' => $this->message_data]);
  78. return true;
  79. }
  80. $chat_id_list = array_column($chat_data_list, 'chat_id');
  81. foreach ($chat_data_list as $key => $value) {
  82. $insert_data[] = [
  83. 'corpid' => $corpId,
  84. 'chat_id' => $value['chat_id'],
  85. 'original_group_leader' => $value['owner'],
  86. 'current_group_leader' => $new_owner,
  87. 'operator_userid' => $operator_userid,
  88. 'inherit_type' => '1', //1=在职继承2=离职继承
  89. 'insert_time' => time(),
  90. ];
  91. }
  92. $result_status = $GroupAllocationRecordsModel->insert($insert_data);
  93. if (!$result_status) {
  94. Log::info('job_error', 'GroupInheritanceJobs任务队列处理失败,群在职继承记录插入失败', ['data' => $this->message_data]);
  95. return true;
  96. }
  97. $result = $ContactService->assignResignedCustomerGroups($corpId, $chat_id_list, $new_owner);
  98. // 如果获取结果失败
  99. if (!$result) {
  100. Log::info('job_error', 'GroupInheritanceJobs任务队列处理失败,群在职继承失败', ['data' => $this->message_data]);
  101. return true;
  102. }
  103. // 错误提示
  104. if ($result['errcode']) {
  105. Log::info('job_error', 'GroupInheritanceJobs任务队列处理失败,群在职继承失败', ['data' => $this->message_data, 'error' => $result['errmsg']]);
  106. return true;
  107. }
  108. //处理失败或成功的数据
  109. if (!empty($result['failed_chat_list'])) {
  110. $failed_chat_list = array_column($result['failed_chat_list'], 'chat_id');
  111. $GroupAllocationRecordsModel->whereIn('chat_id', $failed_chat_list)->update(['status' => 3, 'update_time' => time()]);
  112. //筛选不在失败列表的群
  113. $chat_id_list = array_diff($chat_id_list, $failed_chat_list);
  114. $GroupAllocationRecordsModel->whereIn('chat_id', $chat_id_list)->update(['status' => 2, 'update_time' => time()]);
  115. } else {
  116. $GroupAllocationRecordsModel->whereIn('chat_id', $chat_id_list)->update(['status' => 2, 'update_time' => time()]);
  117. }
  118. return true;
  119. }
  120. /**
  121. * 离职继承队列任务处理
  122. * @author 唐远望
  123. * @version 1.0
  124. * @date 2025-11-18
  125. */
  126. public function off_the_job($corpId, $chat_id_list_array, $new_owner, $operator_userid)
  127. {
  128. $ChatModel = new ChatModel();
  129. $GroupAllocationRecordsModel = new GroupAllocationRecordsModel();
  130. $ContactService = new ContactService();
  131. $insert_data = [];
  132. //筛选正常的群
  133. $chat_data_list = $ChatModel->where(['corpid' => $corpId, 'status' => '0'])->whereIn('chat_id', $chat_id_list_array)->select(['chat_id', 'owner'])->get()->toArray();
  134. if (empty($chat_data_list)) {
  135. Log::info('job_error', 'GroupInheritanceJobs任务队列处理失败,群离职继承列表为空', ['data' => $this->message_data]);
  136. return true;
  137. }
  138. $chat_id_list = array_column($chat_data_list, 'chat_id');
  139. foreach ($chat_data_list as $key => $value) {
  140. $insert_data[] = [
  141. 'corpid' => $corpId,
  142. 'chat_id' => $value['chat_id'],
  143. 'original_group_leader' => $value['owner'],
  144. 'current_group_leader' => $new_owner,
  145. 'operator_userid' => $operator_userid,
  146. 'inherit_type' => '2', //1=在职继承2=离职继承
  147. 'insert_time' => time(),
  148. ];
  149. }
  150. $result_status = $GroupAllocationRecordsModel->insert($insert_data);
  151. if (!$result_status) {
  152. Log::info('job_error', 'GroupInheritanceJobs任务队列处理失败,群离职继承记录插入失败', ['data' => $this->message_data]);
  153. return true;
  154. }
  155. $result = $ContactService->assignResignedCustomerGroups($corpId, $chat_id_list, $new_owner);
  156. // 如果获取结果失败
  157. if (!$result) {
  158. Log::info('job_error', 'GroupInheritanceJobs任务队列处理失败,群离职继承失败', ['data' => $this->message_data]);
  159. return true;
  160. }
  161. // 错误提示
  162. if ($result['errcode']) {
  163. Log::info('job_error', 'GroupInheritanceJobs任务队列处理失败,群离职继承失败', ['data' => $this->message_data, 'error' => $result['errmsg']]);
  164. return true;
  165. }
  166. //处理失败或成功的数据
  167. if (!empty($result['failed_chat_list'])) {
  168. $failed_chat_list = array_column($result['failed_chat_list'], 'chat_id');
  169. $GroupAllocationRecordsModel->whereIn('chat_id', $failed_chat_list)->update(['status' => 3, 'update_time' => time()]);
  170. //筛选不在失败列表的群
  171. $chat_id_list = array_diff($chat_id_list, $failed_chat_list);
  172. $GroupAllocationRecordsModel->whereIn('chat_id', $chat_id_list)->update(['status' => 2, 'update_time' => time()]);
  173. } else {
  174. $GroupAllocationRecordsModel->whereIn('chat_id', $chat_id_list)->update(['status' => 2, 'update_time' => time()]);
  175. }
  176. return true;
  177. }
  178. public function failed(\Throwable $exception)
  179. {
  180. Log::info('job_error', 'GroupInheritanceJobs任务队列彻底失败', ['data' => $this->message_data, 'error' => $exception->getMessage()]);
  181. }
  182. }