123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- namespace App\Service;
- use App\Models\ChatGroup;
- use App\Models\ChatGroupTransfer;
- use App\Models\ChatGroupTransferRecord;
- use App\Models\DjUser;
- use App\Models\System\Users;
- class ChatGroupTransferService
- {
- const CHAT_GROUP_TRANSFER_ERR = [
- '-1' => '未知',
- '60011' => '指定的成员/部门/标签参数无权限',
- '90500' => '群主并未离职',
- '90501' => '该群不是客户群',
- '90502' => '群主已经离职',
- '90503' => '满人 & 99个微信成员,没办法踢,要客户端确认',
- '90504' => '群主没变',
- '90507' => '离职群正在继承处理中',
- '90508' => '离职群已经继承',
- '90509' => '非企业微信客户群',
- '90510' => '企业一年内无活跃用户',
- '90511' => 'opengid不存在或者无效',
- ];
- /**
- * 新增/编辑 客户群分配规则
- * */
- public static function editConfig($configId, $params)
- {
- # 设置分配规则
- return ChatGroupTransfer::editConfig($configId, $params);
- }
- /**
- * 获取客户群分配配置列表
- * */
- public static function configList($sysGroupId, $corpid, $keyword, $newOwner, $status, $page, $pageSize)
- {
- # 查询配置
- list($list, $count) = ChatGroupTransfer::getConfigList($sysGroupId, $corpid, $keyword, $newOwner, $status, $page, $pageSize);
- if($list->isEmpty()) return [[], 0];
- # 获取客服信息
- $userData = DjUser::select('user_id', 'name')->where('corpid', $corpid)->where('enable', 1)->get();
- $adminIds = $list->pluck('admin_id');
- $sysUserData = Users::select('id', 'name')->whereIn('id', $adminIds)->get();
- foreach ($list as $item) {
- # 新群主信息处理
- $userInfo = $userData->where('user_id', $item->new_owner)->first();
- $item->new_owner_name = $userInfo->name ?? '-';
- # 获取操作人信息
- $sysUserInfo = $sysUserData->where('id', $item->admin_id)->first();
- $item->create_user = $sysUserInfo->name ?? '-';
- }
- return [$list, $count];
- }
- /**
- * 获取分配客户群配置详情
- * */
- public static function getConfigDetail($sysGroupId, $corpid, $configId, &$errno)
- {
- $detail = ChatGroupTransfer::select('title', 'type', 'filter_type', 'new_owner', 'transfer_type', 'transfer_at', 'status', 'condition')
- ->where('sys_group_id', $sysGroupId)->where('corpid', $corpid)
- ->where('id', $configId)->where('enable', 1)->first();
- if(empty($detail)) {
- $errno = 2531;
- return [];
- }
- return $detail;
- }
- /**
- * 变更客户群分配规则状态
- * */
- public static function changeConfigStatus($sysGroupId, $corpid, $status, $configId)
- {
- $detail = ChatGroupTransfer::where('sys_group_id', $sysGroupId)
- ->where('corpid', $corpid)->where('id', $configId)
- ->where('enable', 1)->first();
- if(empty($detail)) return 2531;
- if($detail->status > 1) return 2533;
- if($detail->status == $status) return 2534;
- $detail->status = $status;
- $result = $detail->save();
- return $result ? 0 : 2535;
- }
- /**
- * 获取客户群分配记录
- * */
- public static function getTransferRecord($corpid, $configId, $page)
- {
- # 查询分配记录
- $recordData = ChatGroupTransferRecord::where('config_id', $configId)->where('batch_no', $page)->first();
- if(empty($recordData)) return [[], 0];
- $pageTotal = ChatGroupTransferRecord::where('config_id', $configId)->max('batch_no');
- $count = $pageTotal * 100;
- $errno = $recordData['errno'];
- $errMsg = $recordData['err_msg'];
- # 处理群信息
- $chatIdList = json_decode($recordData->chat_id_list, true);
- $chatGroupData = ChatGroup::select('chat_id', 'name', 'owner')->where('corpid', $corpid)->whereIn('chat_id', $chatIdList)->get();
- # 获取群主数据
- $ownerList = $chatGroupData->pluck('owner');
- $ownerData = DjUser::select('user_id', 'name')->where('corpid', $corpid)->whereIn('user_id', $ownerList)->get();
- # 分配失败群组
- $failedChatIdList = json_decode($recordData->failed_chat_id_list, true);
- $failedChatIds = [];
- if(!empty($failedChatIdList)) {
- $failedChatIds = array_column($failedChatIdList, 'chat_id');
- $failedMsgList = array_column($failedChatIdList, 'errcode', 'chat_id');
- }
- foreach($chatGroupData as $datum) {
- # 群主信息处理
- $ownerInfo = $ownerData->where('user_id', $datum->owner)->first();
- $datum->owner_name = $ownerInfo->name ?? '-';
- if($errno) {
- $datum->errno = $errno;
- $datum->msg = ChatGroupTransferService::CHAT_GROUP_TRANSFER_ERR[$datum->errno] ?? $errMsg;
- } else {
- if(in_array($datum->chat_id, $failedChatIds)) {
- $datum->errno = $failedMsgList[$datum->chat_id] ?? -1;
- $datum->msg = ChatGroupTransferService::CHAT_GROUP_TRANSFER_ERR[$datum->errno] ?? '请联系系统管理员确认';
- } else {
- $datum->errno = 0;
- $datum->msg = '成功';
- }
- }
- }
- return [$chatGroupData, $count];
- }
- }
|