企微短剧业务系统

ChatGroupTransferService.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Service;
  3. use App\Models\ChatGroup;
  4. use App\Models\ChatGroupTransfer;
  5. use App\Models\ChatGroupTransferRecord;
  6. use App\Models\DjUser;
  7. use App\Models\System\Users;
  8. class ChatGroupTransferService
  9. {
  10. const CHAT_GROUP_TRANSFER_ERR = [
  11. '-1' => '未知',
  12. '60011' => '指定的成员/部门/标签参数无权限',
  13. '90500' => '群主并未离职',
  14. '90501' => '该群不是客户群',
  15. '90502' => '群主已经离职',
  16. '90503' => '满人 & 99个微信成员,没办法踢,要客户端确认',
  17. '90504' => '群主没变',
  18. '90507' => '离职群正在继承处理中',
  19. '90508' => '离职群已经继承',
  20. '90509' => '非企业微信客户群',
  21. '90510' => '企业一年内无活跃用户',
  22. '90511' => 'opengid不存在或者无效',
  23. ];
  24. /**
  25. * 新增/编辑 客户群分配规则
  26. * */
  27. public static function editConfig($configId, $params)
  28. {
  29. # 设置分配规则
  30. return ChatGroupTransfer::editConfig($configId, $params);
  31. }
  32. /**
  33. * 获取客户群分配配置列表
  34. * */
  35. public static function configList($sysGroupId, $corpid, $keyword, $newOwner, $status, $page, $pageSize)
  36. {
  37. # 查询配置
  38. list($list, $count) = ChatGroupTransfer::getConfigList($sysGroupId, $corpid, $keyword, $newOwner, $status, $page, $pageSize);
  39. if($list->isEmpty()) return [[], 0];
  40. # 获取客服信息
  41. $userData = DjUser::select('user_id', 'name')->where('corpid', $corpid)->where('enable', 1)->get();
  42. $adminIds = $list->pluck('admin_id');
  43. $sysUserData = Users::select('id', 'name')->whereIn('id', $adminIds)->get();
  44. foreach ($list as $item) {
  45. # 新群主信息处理
  46. $userInfo = $userData->where('user_id', $item->new_owner)->first();
  47. $item->new_owner_name = $userInfo->name ?? '-';
  48. # 获取操作人信息
  49. $sysUserInfo = $sysUserData->where('id', $item->admin_id)->first();
  50. $item->create_user = $sysUserInfo->name ?? '-';
  51. }
  52. return [$list, $count];
  53. }
  54. /**
  55. * 获取分配客户群配置详情
  56. * */
  57. public static function getConfigDetail($sysGroupId, $corpid, $configId, &$errno)
  58. {
  59. $detail = ChatGroupTransfer::select('title', 'type', 'filter_type', 'new_owner', 'transfer_type', 'transfer_at', 'status', 'condition')
  60. ->where('sys_group_id', $sysGroupId)->where('corpid', $corpid)
  61. ->where('id', $configId)->where('enable', 1)->first();
  62. if(empty($detail)) {
  63. $errno = 2531;
  64. return [];
  65. }
  66. return $detail;
  67. }
  68. /**
  69. * 变更客户群分配规则状态
  70. * */
  71. public static function changeConfigStatus($sysGroupId, $corpid, $status, $configId)
  72. {
  73. $detail = ChatGroupTransfer::where('sys_group_id', $sysGroupId)
  74. ->where('corpid', $corpid)->where('id', $configId)
  75. ->where('enable', 1)->first();
  76. if(empty($detail)) return 2531;
  77. if($detail->status > 1) return 2533;
  78. if($detail->status == $status) return 2534;
  79. $detail->status = $status;
  80. $result = $detail->save();
  81. return $result ? 0 : 2535;
  82. }
  83. /**
  84. * 获取客户群分配记录
  85. * */
  86. public static function getTransferRecord($corpid, $configId, $page)
  87. {
  88. # 查询分配记录
  89. $recordData = ChatGroupTransferRecord::where('config_id', $configId)->where('batch_no', $page)->first();
  90. if(empty($recordData)) return [[], 0];
  91. $pageTotal = ChatGroupTransferRecord::where('config_id', $configId)->max('batch_no');
  92. $count = $pageTotal * 100;
  93. $errno = $recordData['errno'];
  94. $errMsg = $recordData['err_msg'];
  95. # 处理群信息
  96. $chatIdList = json_decode($recordData->chat_id_list, true);
  97. $chatGroupData = ChatGroup::select('chat_id', 'name', 'owner')->where('corpid', $corpid)->whereIn('chat_id', $chatIdList)->get();
  98. # 获取群主数据
  99. $ownerList = $chatGroupData->pluck('owner');
  100. $ownerData = DjUser::select('user_id', 'name')->where('corpid', $corpid)->whereIn('user_id', $ownerList)->get();
  101. # 分配失败群组
  102. $failedChatIdList = json_decode($recordData->failed_chat_id_list, true);
  103. $failedChatIds = [];
  104. if(!empty($failedChatIdList)) {
  105. $failedChatIds = array_column($failedChatIdList, 'chat_id');
  106. $failedMsgList = array_column($failedChatIdList, 'errcode', 'chat_id');
  107. }
  108. foreach($chatGroupData as $datum) {
  109. # 群主信息处理
  110. $ownerInfo = $ownerData->where('user_id', $datum->owner)->first();
  111. $datum->owner_name = $ownerInfo->name ?? '-';
  112. if($errno) {
  113. $datum->errno = $errno;
  114. $datum->msg = ChatGroupTransferService::CHAT_GROUP_TRANSFER_ERR[$datum->errno] ?? $errMsg;
  115. } else {
  116. if(in_array($datum->chat_id, $failedChatIds)) {
  117. $datum->errno = $failedMsgList[$datum->chat_id] ?? -1;
  118. $datum->msg = ChatGroupTransferService::CHAT_GROUP_TRANSFER_ERR[$datum->errno] ?? '请联系系统管理员确认';
  119. } else {
  120. $datum->errno = 0;
  121. $datum->msg = '成功';
  122. }
  123. }
  124. }
  125. return [$chatGroupData, $count];
  126. }
  127. }