123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- <?php
- namespace App\Service;
- use App\Log;
- use App\Models\Customer;
- use App\Models\CustomerDetails;
- use App\Models\DjUser;
- use App\Models\System\Users;
- use App\Models\UserSop\UserSopMsg;
- use App\Models\UserSop\UserSopRecord;
- use App\Models\UserSop\UserSopRule;
- use App\Support\EmailQueue;
- use Illuminate\Support\Facades\DB;
- class UserSopService
- {
- /**
- * 配置个人SOP规则
- * */
- public static function setRule($params, $ruleId)
- {
- try {
- DB::beginTransaction();
- # 记录基本信息到dj_user_sop_rule表
- $errno = UserSopRule::editRule($params, $ruleId);
- if($errno || !$ruleId) {
- DB::rollBack();
- return $errno;
- }
- // 处理消息内容
- $msgData = json_decode($params['msg_data'], true);
- # 提取消息id
- $msgIdList = array_column($msgData, 'msg_id');
- # 删除用户去除的消息
- $count = UserSopMsg::where('rule_id', $ruleId)->where('enable', 1)
- ->whereNotIn('id', $msgIdList)->count();
- if($count > 0) {
- $result = UserSopMsg::where('rule_id', $ruleId)->where('enable', 1)
- ->whereNotIn('id', $msgIdList)->update(['enable' => 0]);
- if(!$result) {
- Log::logError('删除用户去除的消息失败', [
- 'params' => $params,
- 'rule_id' => $ruleId,
- 'result' => $result
- ], 'UserSopRuleCreate');
- DB::rollBack();
- return 5203;
- }
- }
- # 消息内容入库
- foreach ($msgData as $index=>$msgInfo) {
- $msgId = $msgInfo['msg_id'] ?? null;
- $params['sort'] = $index;
- $errno = UserSopMsg::editMsg($ruleId, $msgId, $msgInfo, $params);
- if($errno) {
- Log::logError('消息内容入库失败', [
- 'params' => $params,
- 'msg_info' => $msgInfo,
- 'rule_id' => $ruleId,
- 'result' => $errno
- ], 'UserSopRuleCreate');
- DB::rollBack();
- return $errno;
- }
- }
- DB::commit();
- } catch (\Exception $e) {
- EmailQueue::rPush('配置个人SOP规则发生异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com'], '配置个人SOP规则发生异常');
- Log::logError('配置个人SOP规则发生异常', [
- 'line' => $e->getLine(),
- 'msg' => $e->getMessage(),
- 'param' => $params
- ], 'UserSopRuleCreate');
- DB::rollBack();
- return 5205;
- }
- return 0;
- }
- /**
- * 获取SOP规则列表
- * */
- public static function ruleList($corpid, $sysGroupId, $page, $pageSize)
- {
- # 查询SOP规则列表
- list($list, $count) = UserSopRule::getRuleList($corpid, $sysGroupId, $page, $pageSize);
- if(!$count) return [[], 0];
- # 获取创建成员信息
- $adminIds = $list->pluck('admin_id');
- $adminData = Users::query()
- ->select(['id','name'])
- ->whereIn('id', $adminIds)
- ->get();
- # 获取客服信息
- $userData = DjUser::select('user_id', 'name')->where('corpid', $corpid)->where('enable', 1)->get();
- foreach ($list as $item) {
- # 创建成员
- $adminInfo = $adminData->where('id', $item->admin_id)->first();
- $item->creator_name = $adminInfo->name ?? null;
- # 客服信息
- if($item->is_all == 1) {
- $item->user_list = '所有';
- } else {
- $userIds = explode(',', $item->user_ids);
- $userList = $userData->whereIn('user_id', $userIds)->pluck('name')->toArray();
- $item->user_list = implode(',', $userList);
- }
- # 规则描述
- if($item->type == 1) {
- $hour = floor($item->interval_time / 60);
- $minute = $item->interval_time % 60;
- $item->desc = "添加客服{$hour}小时{$minute}分后提醒发送消息";
- } elseif ($item->type == 2) {
- $days = floor($item->interval_time / 60 / 24);
- $item->desc = "添加客服{$days}天后,当天{$item->notice_time}提醒发送消息";
- }
- unset($item->corpid, $item->admin_id, $item->is_all, $item->user_ids, $item->type, $item->interval_time, $item->notice_time);
- }
- return [$list, $count];
- }
- /**
- * 获取配置详情
- * */
- public static function ruleDetail($corpid, $sysGroupId, $ruleId)
- {
- # 配置基础信息
- $ruleDetail = UserSopRule::select('id', 'title', 'is_all', 'user_ids', 'type', 'interval_time', 'notice_time', 'updated_at', 'status', 'admin_id', 'customer_filter', 'gender', 'tag_screen_type', 'tag_list', 'exclude_tag_list', 'pay_status', 'pay_num_min', 'pay_num_max')
- ->where('enable', 1)->where('corpid', $corpid)->where('sys_group_id', $sysGroupId)
- ->where('id', $ruleId)->first();
- if(empty($ruleDetail)) return [];
- # 获取创建者信息
- $ruleDetail->creator_name = Users::where('id', $ruleDetail->admin_id)->value('name');
- # 规则描述
- if($ruleDetail->type == 1) {
- $hour = floor($ruleDetail->interval_time / 60);
- $minute = $ruleDetail->interval_time % 60;
- $ruleDetail->desc = "添加客服{$hour}小时{$minute}分后提醒发送消息";
- } elseif ($ruleDetail->type == 2) {
- $days = floor($ruleDetail->interval_time / 60 / 24);
- $ruleDetail->desc = "添加客服{$days}天后,当天{$ruleDetail->notice_time}提醒发送消息";
- }
- if($ruleDetail->is_all == 1) {
- $ruleDetail->user_list = '所有';
- } else {
- $userIds = explode(',', $ruleDetail->user_ids);
- if(empty($userIds)) {
- $ruleDetail->user_list = '未配置';
- } else {
- $userList = DjUser::select('name')->whereIn('user_id', $userIds)->pluck('name')->toArray();
- $ruleDetail->user_list = implode(',', $userList);
- }
- }
- # SOP规则对应的消息内容
- $msgList = UserSopMsg::select('id', 'msg_type', 'content')
- ->where('rule_id', $ruleId)->where('enable', 1)->where('sys_group_id', $sysGroupId)
- ->where('corpid', $corpid)->orderBy('sort')
- ->get();
- $msgData = [];
- foreach ($msgList as $msg) {
- $msgData[] = [
- 'msg_id' => $msg->id,
- 'msg_type' => $msg->msg_type,
- 'content' => json_decode($msg->content, true)
- ];
- }
- $ruleDetail->msg_data = $msgData;
- return $ruleDetail;
- }
- /**
- * 配置状态变更
- * */
- public static function ruleStatusChange($corpid, $sysGroupId, $ruleId, $status)
- {
- $result = UserSopRule::where('enable', 1)->where('corpid', $corpid)->where('sys_group_id', $sysGroupId)
- ->where('id', $ruleId)->update(['status' => $status]);
- return $result ? 0 : 5206;
- }
- /**
- * 个人SOP规则详情
- * */
- public static function getDetail($corpid, $ruleId, &$errno)
- {
- # 规则基本信息
- $ruleInfo = UserSopRule::select('admin_id', 'type', 'interval_time', 'notice_time')
- ->where('corpid', $corpid)->where('id', $ruleId)
- ->where('enable', 1)->where('status', 1)
- ->first();
- if(empty($ruleInfo)) {
- $errno = 5207;
- return [];
- }
- # 获取创建者信息
- $creator = Users::where('id', $ruleInfo->admin_id)->value('name');
- if($ruleInfo->type == 1) {
- $hour = floor($ruleInfo->interval_time / 60);
- $minute = $ruleInfo->interval_time % 60;
- $desc = $creator . '创建了一条SOP任务,提醒你给已添加' . $hour . '小时'. $minute . '分的客户发送消息';
- } else {
- $days = floor($ruleInfo->interval_time / 60 / 24);
- $desc = $creator . '创建了一条SOP任务,提醒你给已添加' . $days . '天的客户发送消息';
- }
- # 推送内容
- $msgList = UserSopMsg::select('id', 'msg_type', 'content')
- ->where('rule_id', $ruleId)->where('corpid', $corpid)
- ->where('enable', 1)->orderBy('sort')
- ->get();
- $msgData = [];
- foreach ($msgList as $msg) {
- $content = json_decode($msg->content, true);
- $content['msg_id'] = $msg->id;
- $msgData[] = [
- 'msg_id' => $msg->id,
- 'msg_type' => $msg->msg_type,
- 'content' => $content
- ];
- }
- return ['desc' => $desc, 'msg_list' => $msgData];
- }
- /**
- * 待跟进客户信息
- * */
- public static function getCustomerList($corpid, $ruleId, $userId, $page, $pageSize)
- {
- # 判断SOP规则是否可用
- $isEnable = UserSopRule::where('id', $ruleId)->where('status', 1)->where('enable', 1)->count();
- if(!$isEnable) return [[], 0];
-
- # 待跟进客户
- $userSopModel = UserSopRecord::where('rule_id', $ruleId)->where('user_id', $userId)
- ->where('status', 2)->orderBy('id', 'desc')->where('enable', 1);
- $count = $userSopModel->count();
- $externalUserIds = $userSopModel->select('external_userid')
- ->limit($pageSize)->offset(($page-1) * $pageSize)
- ->pluck('external_userid');
- $customerData = CustomerDetails::suffix($corpid)->select('external_userid', 'name', 'createtime')
- ->where('corpid', $corpid)->whereIn('external_userid', $externalUserIds)
- ->where('user_id', $userId)
- ->get();
- if(empty($customerData)) return [[], 0];
- $customerSourceData = Customer::suffix($corpid)->select('external_userid', 'type', 'avatar')
- ->whereIn('external_userid', $externalUserIds)->where('corpid', $corpid)
- ->get();
- $customerType = Customer::CUSTOMER_TYPE;
- foreach ($customerData as $customer) {
- $customerInfo = $customerSourceData->where('external_userid', $customer->external_userid)->first();
- $customer->createtime = date('Y-m-d H:i:s', $customer->createtime);
- $customer->type = $customerInfo->type ?? 0;
- $customer->avatar = $customerInfo->avatar ?? '';
- $customer->source = isset($customerType[$customer->type]) ? $customerType[$customer->type] : '';
- }
- return [$customerData, $count];
- }
- /**
- * 跟进数据上报
- * */
- public static function report($corpid, $ruleId, $userId, $externalUserid)
- {
- $isExist = UserSopRecord::where('user_id', $userId)->where('corpid', $corpid)
- ->where('external_userid', $externalUserid)
- ->where('rule_id', $ruleId)->where('status', 2)->count();
- if($isExist) {
- $result = UserSopRecord::where('user_id', $userId)->where('corpid', $corpid)
- ->where('external_userid', $externalUserid)
- ->where('rule_id', $ruleId)->update(['status' => 3]);
- if(!$result) {
- Log::logError('SOP跟进数据上报,状态变更失败', [
- 'corpid' => $corpid,
- 'rule_id' => $ruleId,
- 'user_id' => $userId,
- 'external_userid' => $externalUserid
- ], 'SopUserReport');
- }
- }
-
- return 0;
- }
- }
|