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; } }