where('is_system_admin', 1)->where('enable', 1)->count(); // $sysGroupId = Users::where('enable', 1) // ->where(function ($query) use($isSystemAdmin, $adminId, $sysGroupId) { // if($isSystemAdmin) { // $query->where('group_admin_id', $sysGroupId); // } else { // $query->where('id', $adminId); // } // }) // ->value('group_admin_id'); if(!$sysGroupId) { return 1004; } DB::begintransaction(); foreach ($configList as $config) { $configId = $config['config_id'] ?? ''; $config['admin_id'] = $adminId; $config['corpid'] = $corpid; $config['sys_group_id'] = $sysGroupId; $type = $config['type'] ?? 0; if(!in_array($type, [1, 2, 3])) { Log::logError('type不合法', [ 'config' => $config ], 'EditConfig'); continue; } $errno = BatchAddCustomerConfig::editConfig($configId, $config); if($errno) { Log::logError('配置自动提醒及自动回收失败', [ 'config_id' => $configId, 'params' => $config ], 'EditConfig'); DB::rollBack(); return $errno; } } DB::commit(); } catch (\Exception $e) { Log::logError('配置自动提醒及自动回收过程发生异常', [ 'corpid' => $corpid, 'admin_id' => $adminId, 'sys_group_id' => $sysGroupId, 'params' =>$configList, 'line' => $e->getLine(), 'msg' => $e->getMessage() ], 'EditConfig-Exception'); DB::rollBack(); return 2603; } return 0; } /** * 获取配置信息 * */ public static function configInfo($corpid, $sysGroupId, $adminId, &$configInfo) { try { // # 获取登录用户所属sys_group_id // $isSystemAdmin = Users::where('id', $adminId)->where('is_system_admin', 1)->where('enable', 1)->count(); // $sysGroupId = Users::where('enable', 1) // ->where(function ($query) use($isSystemAdmin, $sysGroupId, $adminId) { // if($isSystemAdmin) { // $query->where('group_admin_id', $sysGroupId); // } else { // $query->where('id', $adminId); // } // // }) // ->value('group_admin_id'); if(!$sysGroupId) { return 1004; } $configInfo = array( 'assigned' => array('enable' => 0), 'charged' => array('enable' => 0), 'auto_recycle' => array('enable' => 0) ); $data = BatchAddCustomerConfig::configInfo($corpid, $sysGroupId); foreach ($data as $datum) { switch($datum->type) { case 1: $configInfo['assigned'] = $datum; break; case 2: $configInfo['charged'] = $datum; break; case 3: $configInfo['auto_recycle'] = $datum; break; } } } catch (\Exception $e) { Log::logError('获取配置信息发生异常', [ 'corpid' => $corpid, 'admin_id' => $adminId, 'group_admin_id' => $sysGroupId, 'line' => $e->getLine(), 'msg' => $e->getMessage() ], 'configInfo-Exception'); return 2604; } return 0; } /** * 未完成客户添加工作的成员发送消息提醒 * @param $corpid string 企业id * @param $userIds string 客服id集合 * */ public static function noticeUser($corpid, $userIds) { try { $userIds = array_unique(explode(',', $userIds)); foreach($userIds as $userId) { $customerCount = BatchAddCustomerDetails::where('corpid', $corpid) ->where('user_id', $userId)->where('enable', 1) ->whereIn('add_status', [1,2])->count(); if(!$customerCount) continue; $timestamps = time(); $url = env('DOMAIN', 'http://dj.wenxingshuju.com/') . 'playlet/qwh5/dist/index.html#/?corpid='.$corpid.'&user_id='.$userId.'&token='.get_token($corpid, $userId, $timestamps) . '×tamp='.$timestamps; $content = "【管理员提醒】您有客户未添加哦!\n\n提醒事项:添加客户\n\n客户数量:".$customerCount."名\n\n记得及时添加哦\n\n点击查看详情\n"; # 发送消息提醒 $responseData = ApplicationMsgService::sendTextMsg($corpid, $content, [$userId]); if(isset($responseData['errcode']) && $responseData['errcode'] != 0) { $logData = [ 'corpid' => $corpid, 'user_id' => $userIds, 'responseData' => $responseData ]; EmailQueue::rPush('未完成客户添加工作的成员发送提醒失败', json_encode($logData, JSON_UNESCAPED_UNICODE), ['xiaohua.hou@kuxuan-inc.com'], '未完成客户添加工作的成员发送提醒失败'); Log::logError('未完成客户添加工作的成员发送提醒失败', $logData, 'noticeUserFailed'); return 2512; } # 记录发送日志 Log::logInfo('未完成客户添加工作的成员消息提醒日志', [ 'corpid' => $corpid, 'user_id' => $userIds, 'responseData' => $responseData ], 'noticeUserLog'); } } catch (\Exception $e) { EmailQueue::rPush('未完成客户添加工作的成员消息提醒发生异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com'], '未完成客户添加工作的成员消息提醒发生异常'); Log::logError('未完成客户添加工作的成员消息提醒发生异常', [ 'corpid' => $corpid, 'user_id' => $userIds, 'line' => $e->getLine(), 'msg' => $e->getMessage() ], 'noticeUserLog-Exception'); return 2514; } return 0; } /** * 获取客服已分配的客户列表 * @param $corpid string 企业ID * @param $userId string 客服人员ID * @param $token string 令牌 * @param $timestamp string 时间戳 * @param $addStatus int 添加状态 0已重新分配 1未添加 2待通过 3已添加 4待分配 * */ public static function getCustomerList($corpid, $userId, $token, $timestamp, $addStatus, $page, $pageSize, &$errno) { # 校验请求合法性 $authToken = get_token($corpid, $userId, $timestamp); if($authToken != $token) { $errno = 1004; return [[], 0]; } # 获取已分配的客户列表 list($list, $count) = BatchAddCustomerDetails::getCustomerListOfUser($corpid, $userId, $addStatus, $page, $pageSize); # 获取用户未完成添加的客户数 $customerTotal = BatchAddCustomerDetails::where('corpid', $corpid)->where('user_id', $userId)->where('enable', 1) ->whereIn('add_status', [1,2])->count(); return [$list, $count, $customerTotal]; } /** * 客服添加客户行为数据上报 * @param $corpid string 企业id * @param $phone string 目标客户手机号 * @param $userId string 客服id * @param $recordId int 导入记录ID * */ public static function dataReporting($corpid, $phone, $userId, $recordId) { try { # 校验数据是否存在 $isExist = BatchAddCustomerDetails::where('corpid', $corpid) ->where('phone', $phone)->where('record_id', $recordId) ->where('user_id', $userId)->where('enable', 1)->exists(); if(!$isExist) return 2701; BatchAddCustomerDetails::where('corpid', $corpid) ->where('phone', $phone)->where('record_id', $recordId) ->where('user_id', $userId)->where('enable', 1)->update(['add_status' => 2]); } catch (\Exception $e) { EmailQueue::rPush('客户添加客户行为数据上报流程发生异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com'], '客户添加客户行为数据上报流程发生异常'); Log::logError('客户添加客户行为数据上报流程发生异常', [ 'line' => $e->getLine(), 'msg' => $e->getMessage(), 'corpid' => $corpid, 'phone' => $phone, 'user_id' => $userId, 'record_id' => $recordId ], 'DataReporting-Exception'); return 2702; } return 0; } /** * 数据概览 * */ public static function dataSummary($corpid, $startDate, $endDate, $sysGroupId) { $data = [ 'import_count' => 0, 'assigned_count' => 0, // 待分配 'waiting_add_count' => 0, // 待添加 'passed_count' => 0, // 待通过 'added_count' => 0, // 已添加 'added_rate' => 0 . '%' // 添加完成率 ]; try { $condition = BatchAddCustomerDetails::selectRaw("count(1) as import_count, count(CASE WHEN add_status = 1 THEN 1 END) AS waiting_add_count, count(CASE WHEN add_status = 2 THEN 1 END) AS passed_count, count(CASE WHEN add_status = 3 THEN 1 END) AS added_count,count(CASE WHEN add_status = 4 THEN 1 END) AS assigned_count") ->where('corpid', $corpid)->where('enable', 1) ->where('group_admin_id', $sysGroupId) ->where('import_time', '>=', $startDate . ' 00:00:00') ->where('import_time', '<=', $endDate . ' 23:59:59') ->where('add_status', '>', 0) ->first(); if(empty($condition)) return $data; $importCount = $condition->import_count ?? 0; $addedCount = $condition->added_count ?? 0; $data = [ 'import_count' => $importCount, 'assigned_count' => $condition->assigned_count ?? 0, 'waiting_add_count' => $condition->waiting_add_count ?? 0, 'passed_count' => $condition->passed_count ?? 0, 'added_count' => $addedCount, 'added_rate' => $importCount ? (round($addedCount / $importCount, 4) * 100) . '%' : 0 . '%' ]; } catch (\Exception $e) { Log::logError('数据概览统计过程发生异常', [ 'line' => $e->getLine(), 'msg' => $e->getMessage(), 'param' => [ 'corpid' => $corpid, 'start_date' => $startDate, 'end_date' => $endDate ] ], 'AddCustomerDataSummary-Exception'); } return $data; } /** * 批量加好友统计列表 * */ public static function statistic($corpid, $userId, $startDate, $endDate, $sysGroupId, $page, $pageSize) { try { list($list, $count) = BatchAddCustomerDetails::getStatistic($corpid, $userId, $startDate, $endDate, $sysGroupId, $page, $pageSize); if(empty($list)) return [[], 0]; $userIds = $list->pluck('user_id'); # 获取客服信息 $userList = DjUser::select(['name', 'user_id'])->whereIn('user_id', $userIds)->where('corpid', $corpid)->get(); foreach ($list as $item) { # 计算添加完成率 $assignCount = $item->assign_count ?? 0; $addedCount = $item->added_count ?? 0; $item->add_rate = $assignCount ? (round($addedCount / $assignCount, 4) * 100) . '%' : 0 . '%'; # 客服名称 $userInfo = $userList->where('user_id', $item->user_id)->first(); $item->name = $userInfo->name ?? '-'; } } catch (\Exception $e) { Log::logError('批量加好友列表统计过程发生异常', [ 'line' => $e->getLine(), 'msg' => $e->getMessage(), 'param' => [ 'corpid' => $corpid, 'user_id' => $userId, 'page' => $page, 'page_size' => $pageSize ] ], 'AddCustomerStatistic-Exception'); return [[], 0]; } return [$list, $count]; } /** * 通过手机号添加的客户信息入打标签队列 * */ public static function addTagForCustomer($changeType, $corpid, $followInfo, $externalContact) { $changeTypeAllowList = ['add_external_contact', 'add_half_external_contact']; $addWay = isset($followInfo['add_way']) ? $followInfo['add_way'] : null; $remarkMobiles = isset($followInfo['remark_mobiles']) ? implode(',', $followInfo['remark_mobiles']) : null; if($addWay != 2 || empty($remarkMobiles)) { Log::logInfo('通过手机号添加的客户信息入打标签队列成功', [ 'change_type' => $changeType, 'corpid' => $corpid, 'followInfo' => $followInfo, 'externalContact' => $externalContact, 'addWay' => $addWay, 'remark_mobiles' => $remarkMobiles ], 'addTagForCustomerQueue'); return ''; } try{ if (!is_null($changeType) && in_array($changeType, $changeTypeAllowList)) { $data = [ 'corpid' => $corpid, 'user_id' => $followInfo['userid'], 'external_userid' => $externalContact['external_userid'], 'remark_mobiles' => $remarkMobiles, 'createtime' => isset($followInfo['createtime']) ? $followInfo['createtime'] : null, ]; RedisModel::lPush(BatchAddCustomerDetails::BATCH_ADD_CUSTOMER_TAG_CREATE_RDS, json_encode($data)); Log::logInfo('通过手机号添加的客户信息入打标签队列成功', $data, 'addTagForCustomerQueue'); } } catch(\Exception $e) { Log::logError('通过手机号添加的客户信息入打标签队列时发生异常', [ 'msg' => $e->getMessage(), 'line' => $e->getLine(), 'changeType' => $changeType, 'corpid' => $corpid, 'followInfo' => $followInfo, 'externalContact' => $externalContact, ], 'addTagForCustomerQueue-Exception'); } } }