123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999 |
- <?php
- /**
- * Created by PhpStorm.
- * User: shensong
- * Date: 2022/6/23
- * Time: 17:39
- */
- namespace App\Service;
- use App\Log;
- use App\Models\ChatMenuGroup;
- use App\Models\ChatMenuMsg;
- use App\Models\ChatMenuRecord;
- use App\Models\ChatMenuRule;
- use App\Models\Material;
- use App\Models\UserSop\UserSopMsg;
- use App\RedisModel;
- use App\Support\EmailQueue;
- class ChatMenuService
- {
- public static function createChatMenuGroup($params)
- {
- try{
- // 验证雷达组名称是否重复
- $info = ChatMenuGroup::getGroupInfoByName($params['corpid'], $params['sys_group_id'], $params['group_name']);
- if(!empty($info)) {
- Log::logError('createChatMenuGroup', [
- 'params' => $params,
- 'message' => '话术组名称已使用',
- ],'interface');
- return ['话术组名称已使用', 3403];
- }
- // 获取雷达组最大排序值
- $maxSortOrder = ChatMenuGroup::getMaxSortOrder($params['corpid'], $params['sys_group_id']);
- $res = ChatMenuGroup::query()
- ->insert([
- 'sys_group_id' => $params['sys_group_id'],
- 'corpid' => $params['corpid'],
- 'group_name' => $params['group_name'],
- 'creator_id' => \Auth::id(),
- 'sort_order' => $maxSortOrder + 1,
- ]);
- if($res) {
- return ['添加成功', 0];
- } else {
- Log::logError('createChatMenuGroup', [
- 'params' => $params,
- 'message' => '添加失败',
- 'res' => $res,
- ],'interface');
- return ['添加失败', 400];
- }
- } catch (\Exception $exception) {
- Log::logError('createChatMenuGroup', [
- 'params' => $params,
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'message' => $exception->getMessage(),
- 'trace' => $exception->getTraceAsString(),
- ], 'interface');
- return ['添加失败', 400];
- }
- }
- public static function chatMenuGroupList($params)
- {
- try{
- $query = ChatMenuGroup::query()
- ->where('corpid', $params['corpid'])
- ->where('enable', 1);
- if(isset($params['sys_group_id']) && $params['sys_group_id'])
- $query->where('sys_group_id', $params['sys_group_id']);
- if(!empty($params['keyword'])) {
- $query->where('group_name', 'like', '%'.$params['keyword'].'%');
- }
- $count = $query->count();
- $list = $query->select(['group_name', 'sort_order'])
- ->selectRaw('id as group_id')
- ->orderBy('sort_order', 'desc')
- ->offset(($params['page']-1) * $params['page_size'])
- ->limit($params['page_size'])
- ->get();
- } catch (\Exception $exception) {
- Log::logError('chatMenuGroupList', [
- 'params' => $params,
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'message' => $exception->getMessage(),
- 'trace' => $exception->getTraceAsString(),
- ], 'interface');
- return [[], 0];
- }
- return [$list, $count];
- }
- // 编辑话术组
- public static function updateChatMenuGroup($params)
- {
- try{
- // 验证雷达组名称是否重复
- $info = ChatMenuGroup::getGroupInfoByName($params['corpid'], $params['sys_group_id'], $params['group_name']);
- if(isset($info->id) && $info->id != $params['group_id']) {
- Log::logError('updateChatMenuGroup', [
- 'params' => $params,
- 'message' => '话术组名称已使用',
- ],'interface');
- return ['话术组名称已使用', 3403];
- }
- $radarGroupInfo = ChatMenuGroup::query()
- ->where('id', $params['group_id'])
- ->first();
- if($params['group_name'] == $radarGroupInfo->group_name) {
- return ['修改成功', 0];
- }
- $res = ChatMenuGroup::query()
- ->where('id', $params['group_id'])
- ->update([
- 'group_name' => $params['group_name']
- ]);
- if($res) {
- return ['修改成功', 0];
- } else {
- Log::logError('updateChatMenuGroup', [
- 'params' => $params,
- 'message' => '修改失败',
- 'res' => $res,
- ],'interface');
- return ['修改失败', 400];
- }
- } catch (\Exception $exception) {
- Log::logError('updateChatMenuGroup', [
- 'params' => $params,
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'message' => $exception->getMessage(),
- 'trace' => $exception->getTraceAsString(),
- ], 'interface');
- return ['修改失败', 400];
- }
- }
- public static function deleteChatMenuGroup($params)
- {
- try{
- \DB::begintransaction();
- $groupRes = ChatMenuGroup::query()
- ->where('id', $params['group_id'])
- ->update([
- 'enable' => 0
- ]);
- $detailCount = ChatMenuRule::query()
- ->where('group_id', $params['group_id'])
- ->where('enable', 1)
- ->count();
- // 防止出现本来雷达组下就没有雷达,更新不到数据导致整体删除失败
- if($detailCount > 0) {
- if(1 == $params['type']) {
- $detailRes = ChatMenuRule::query()
- ->where('group_id', $params['group_id'])
- ->where('enable', 1)
- ->update(['group_id' => 0]);
- } else {
- $detailRes = ChatMenuRule::query()
- ->where('group_id', $params['group_id'])
- ->where('enable', 1)
- ->update(['enable' => 0]);
- }
- } else {
- $detailRes = true;
- }
- if($groupRes && $detailRes) {
- \DB::commit();
- return ['删除成功', 0];
- } else {
- \DB::rollBack();
- Log::logError('deleteChatMenuGroup', [
- 'params' => $params,
- 'message' => '删除失败',
- 'group_res' => $groupRes,
- 'detail_res' => $detailRes,
- ],'interface');
- return ['删除失败', 400];
- }
- } catch (\Exception $exception) {
- Log::logError('deleteChatMenuGroup', [
- 'params' => $params,
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'message' => $exception->getMessage(),
- 'trace' => $exception->getTraceAsString(),
- ], 'interface');
- return ['删除失败', 400];
- }
- }
- public static function updateChatMenuGroupSortOrder($params)
- {
- try{
- // 查询被操作分组原始排序值
- $operatorGroup = ChatMenuGroup::query()
- ->where('id', $params['operate_group_id'])
- ->first();
- $operatorGroupOriginalSortOrder = $operatorGroup->sort_order;
- if(!empty($params['behind_group_id']) && empty($params['front_group_id'])) {
- // 上移到第一位,判断是否有调整后的排在调整分组后面的分组id
- $behindGroup = ChatMenuGroup::query()
- ->where('id', $params['behind_group_id'])
- ->first();
- $behindGroupOriginalSortOrder = $behindGroup->sort_order;
- list($data, $code) = self::radarGroupMoveUp($params, $behindGroupOriginalSortOrder, $operatorGroupOriginalSortOrder);
- return [$data, $code];
- } else if(!empty($params['front_group_id']) && empty($params['behind_group_id'])) {
- // 下移到最后一位,判断是否有调整后的排在调整分组前面的分组id
- $frontGroup = ChatMenuGroup::query()
- ->where('id', $params['front_group_id'])
- ->first();
- $frontGroupOriginalSortOrder = $frontGroup->sort_order;
- list($data, $code) = self::radarGroupMoveDown($params, $frontGroupOriginalSortOrder, $operatorGroupOriginalSortOrder);
- return [$data, $code];
- } else if(!empty($params['front_group_id']) && !empty($params['behind_group_id'])) {
- $frontGroup = ChatMenuGroup::query()
- ->where('id', $params['front_group_id'])
- ->first();
- $frontGroupOriginalSortOrder = $frontGroup->sort_order;
- $behindGroup = ChatMenuGroup::query()
- ->where('id', $params['behind_group_id'])
- ->first();
- $behindGroupOriginalSortOrder = $behindGroup->sort_order;
- if($operatorGroupOriginalSortOrder > $frontGroupOriginalSortOrder) {
- // 下移
- list($data, $code) = self::radarGroupMoveDown($params, $frontGroupOriginalSortOrder, $operatorGroupOriginalSortOrder);
- } else {
- // 上移
- list($data, $code) = self::radarGroupMoveUp($params, $behindGroupOriginalSortOrder, $operatorGroupOriginalSortOrder);
- }
- return [$data, $code];
- } else {
- Log::logError('updateChatMenuGroupSortOrder', [
- 'params' => $params,
- 'message' => '被操作话术组前后分组id不可同时为空',
- ],'interface');
- return ['被操作话术组前后分组id不可同时为空', 3507];
- }
- } catch (\Exception $exception) {
- Log::logError('updateChatMenuGroupSortOrder', [
- 'params' => $params,
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'message' => $exception->getMessage(),
- 'trace' => $exception->getTraceAsString(),
- ], 'interface');
- return ['移动失败', 400];
- }
- }
- // 话术组上移
- public static function radarGroupMoveUp($params, $behindGroupOriginalSortOrder, $operatorGroupOriginalSortOrder)
- {
- \DB::begintransaction();
- $moveRes = ChatMenuGroup::query()
- ->where('corpid', $params['corpid'])
- ->where('sys_group_id', $params['sys_group_id'])
- ->where('enable', 1)
- ->where('sort_order', '<=', $behindGroupOriginalSortOrder)
- ->where('sort_order', '>', $operatorGroupOriginalSortOrder)
- ->decrement('sort_order');// 自减1
- $operatorRes = ChatMenuGroup::query()
- ->where('id', $params['operate_group_id'])
- ->update(['sort_order' => $behindGroupOriginalSortOrder]);
- if($moveRes && $operatorRes) {
- \DB::commit();
- return ['移动成功', 0];
- } else {
- \DB::rollBack();
- Log::logError('updateChatMenuGroupSortOrder', [
- 'params' => $params,
- 'message' => '移动失败',
- 'move_res' => $moveRes,
- 'operator_res' => $operatorRes,
- 'operator_sort_order' => $operatorGroupOriginalSortOrder,
- 'behind_sort_order' => $behindGroupOriginalSortOrder,
- ],'interface');
- return ['移动失败', 400];
- }
- }
- // 话术组下移
- public static function radarGroupMoveDown($params, $frontGroupOriginalSortOrder, $operatorGroupOriginalSortOrder)
- {
- \DB::begintransaction();
- $moveRes = ChatMenuGroup::query()
- ->where('corpid', $params['corpid'])
- ->where('sys_group_id', $params['sys_group_id'])
- ->where('enable', 1)
- ->where('sort_order', '>=', $frontGroupOriginalSortOrder)
- ->where('sort_order', '<', $operatorGroupOriginalSortOrder)
- ->increment('sort_order');// 自增1
- $operatorRes = ChatMenuGroup::query()
- ->where('id', $params['operate_group_id'])
- ->update(['sort_order' => $frontGroupOriginalSortOrder]);
- if($moveRes && $operatorRes) {
- \DB::commit();
- return ['移动成功', 0];
- } else {
- \DB::rollBack();
- Log::logError('updateChatMenuGroupSortOrder', [
- 'params' => $params,
- 'message' => '移动失败',
- 'move_res' => $moveRes,
- 'operator_res' => $operatorRes,
- 'operator_sort_order' => $operatorGroupOriginalSortOrder,
- 'front_sort_order' => $frontGroupOriginalSortOrder,
- ],'interface');
- return ['移动失败', 400];
- }
- }
- public static function createChatMenuRule($params)
- {
- try{
- // 解析话术消息
- $msgData = json_decode($params['msg_data_list'], 1);
- if(count($msgData) > 9) {
- Log::logError('createChatMenuRule', [
- 'params' => $params,
- 'errmsg' => '最多添加9条话术内容',
- ], 'interface');
- return [['error' => '最多添加9条话术内容'], 3505];
- }
- if(empty($msgData)) {
- Log::logError('createChatMenuRule', [
- 'params' => $params,
- 'errmsg' => '最少添加1条话术内容',
- ], 'interface');
- return [['error' => '最少添加1条话术内容'], 3506];
- }
- // 获取最高的排序值
- $maxSortOrder = ChatMenuRule::getMaxSortOrder($params['corpid'], $params['sys_group_id']);
- \DB::begintransaction();
- //保存话术规则
- $chatMenuRule = new ChatMenuRule();
- $chatMenuRule->group_id = empty($params['group_id']) ? 0 : $params['group_id'];
- $chatMenuRule->title = $params['title'];
- $chatMenuRule->sort_order = $maxSortOrder+1;
- $chatMenuRule->corpid = $params['corpid'];
- $chatMenuRule->sys_group_id = $params['sys_group_id'];
- $chatMenuRule->creator_id = \Auth::id();
- if($chatMenuRule->save()){
- $ruleId = $chatMenuRule->id;
- //分类型保存话术消息
- foreach ($msgData as $msgInfo) {
- $res = ChatMenuMsg::saveData(null, $msgInfo['chat_type'], $msgInfo,
- $params['corpid'], $params['sys_group_id'], $ruleId);
- if(!$res) {
- Log::logError('createChatMenuRule', [
- 'params' => $params,
- 'chat_menu_rule' => (array)$chatMenuRule,
- 'chat_menu_msg' => (array)$res,
- 'errmsg' => '添加失败,消息保存失败',
- ], 'interface');
- \DB::rollBack();
- // 添加失败
- return ['添加失败', 400];
- }
- }
- \DB::commit();
- return ['添加成功', 0];
- } else {
- Log::logError('createChatMenuRule', [
- 'params' => $params,
- 'chat_menu_rule' => (array)$chatMenuRule,
- 'chat_menu_msg' => [],
- 'errmsg' => '添加失败,规则保存失败',
- ], 'interface');
- \DB::rollBack();
- // 添加失败
- return ['添加失败', 400];
- }
- } catch (\Exception $exception) {
- Log::logError('createChatMenuRule', [
- 'params' => $params,
- 'errmsg' => $exception->getMessage(),
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'trace' => $exception->getTraceAsString(),
- ],'interface');
- return ['添加失败', 400];
- }
- }
- public static function chatMenuRuleList($params)
- {
- try{
- $offset = ($params['page'] - 1) * $params['page_size'];
- $query = ChatMenuRule::query()
- ->where('corpid', $params['corpid'])
- ->where('sys_group_id', $params['sys_group_id'])
- ->where('enable', 1);
- if(!empty($params['group_id'])) {
- $query->where('group_id', $params['group_id']);
- }
- if(!empty($params['title'])) {
- $query->where('title', 'like', '%'.$params['title'].'%');
- }
- $count = $query->count();
- $data = $query->select(['group_id', 'title', 'sort_order'])
- ->selectRaw('id as rule_id')
- ->orderBy('sort_order', 'desc')
- ->offset($offset)
- ->limit($params['page_size'])
- ->get();
- // 提取分组id,并获取分组名称
- $groupIdList = array_unique(array_column($data->toArray(), 'group_id'));
- $groupDataList = ChatMenuGroup::query()
- ->whereIn('id', $groupIdList)
- ->where('enable', 1)
- ->get();
- // 提取话术规则id
- $ruleIdList = array_column($data->toArray(), 'rule_id');
- // 根据规则id批量查询发送次数
- $ruleSendStatisticsData = ChatMenuRecord::getRuleSendStatistics($ruleIdList);
- // 根据规则id获取下边所有的消息内容
- $newRuleMsgList = ChatMenuMsg::getMsgListByRuleId($ruleIdList);
- // 数据拼接
- foreach($data as &$val) {
- // 分组
- $groupInfo = $groupDataList->where('id', $val->group_id)->first();
- $val->group_name = !empty($groupInfo->group_name) ? $groupInfo->group_name : null;
- // 统计
- $statistcsInfo = $ruleSendStatisticsData->where('rule_id', $val->rule_id)->first();
- $val->send_num = !empty($statistcsInfo->count) ? $statistcsInfo->count : 0;
- // 话术内容以及类型
- $msgList = isset($newRuleMsgList[$val->rule_id]) ? $newRuleMsgList[$val->rule_id] : [];
- $msgList['chat_type'] = array_unique($msgList['chat_type']);
- $val->chat_type_name = isset($msgList['chat_type']) ?
- (count($msgList['chat_type']) > 1 ? '复合类型' : ChatMenuMsg::CHAT_TYPE[$msgList['chat_type'][0]]) : null;
- $val->chat_message = isset($msgList['chat_message']) ? $msgList['chat_message'] : [];
- }
- return [$data, $count];
- } catch (\Exception $exception) {
- Log::logError('chatMenuRuleList', [
- 'params' => $params,
- 'errmsg' => $exception->getMessage(),
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'trace' => $exception->getTraceAsString(),
- ],'interface');
- return [[], 0];
- }
- }
- /**
- * 侧边栏快捷话术信息列表
- * */
- public static function chatMenuRuleListForH5($params)
- {
- try{
- $query = ChatMenuRule::where('corpid', $params['corpid'])
- ->where('group_id', $params['group_id'])
- ->where('enable', 1);
- if(isset($params['title']) && $params['title']) {
- $query->where('title', 'like', '%'.$params['title'].'%');
- }
- $count = $query->count();
- $ruleList = $query->selectRaw('id as rule_id, title, sort_order')
- ->orderBy('sort_order', 'desc')
- ->offset(($params['page'] - 1) * $params['page_size'])
- ->limit($params['page_size'])
- ->get();
- // 提取快捷话术信息
- $ruleIdList = $ruleList->pluck('rule_id');
- $ruleMsgList = ChatMenuMsg::selectRaw('id as msg_id, rule_id, chat_type, chat_message')
- ->whereIn('rule_id', $ruleIdList)->where('enable', 1)->get();
- foreach($ruleList as $rule) {
- $msgList = $ruleMsgList->where('rule_id', $rule->rule_id)->all();
- $rule->msg_list = array_values($msgList);
- }
- } catch (\Exception $exception) {
- Log::logError('chatMenuRuleListForH5', [
- 'params' => $params,
- 'errmsg' => $exception->getMessage(),
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'trace' => $exception->getTraceAsString(),
- ],'interface');
- return [[], 0];
- }
- return [$ruleList, $count];
- }
- public static function chatMenuRuleDetail($params)
- {
- try{
- $chatMenuRule = ChatMenuRule::query()
- ->select(['group_id', 'title'])
- ->selectRaw('id as rule_id')
- ->where('id', $params['rule_id'])
- ->where('enable', 1)
- ->first();
- if(empty($chatMenuRule)) {
- return [[], 3002];
- }
- $chatMenuRule = json_decode(json_encode($chatMenuRule), 1);
- $newRuleMsgList = ChatMenuMsg::getMsgListByRuleId([$params['rule_id']]);
- $chatMenuRule['msg_list'] = $newRuleMsgList[$params['rule_id']]['chat_message'];
- $chatMenuRule['group_id'] = empty($chatMenuRule['group_id']) ? null : $chatMenuRule['group_id'];
- return [$chatMenuRule, 0];
- } catch (\Exception $exception) {
- Log::logError('chatMenuRuleList', [
- 'params' => $params,
- 'errmsg' => $exception->getMessage(),
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'trace' => $exception->getTraceAsString(),
- ],'interface');
- return [[], 400];
- }
- }
- public static function updateChatMenuRuleSortOrder($params)
- {
- try{
- // 查询被操作分组原始排序值
- $operatorRule = ChatMenuRule::query()
- ->where('id', $params['operate_rule_id'])
- ->first();
- $operatorRuleOriginalSortOrder = $operatorRule->sort_order;
- if(!empty($params['behind_rule_id']) && empty($params['front_rule_id'])) {
- // 上移到第一位,判断是否有调整后的排在调整分组后面的分组id
- $behindRule = ChatMenuRule::query()
- ->where('id', $params['behind_rule_id'])
- ->first();
- $behindRuleOriginalSortOrder = $behindRule->sort_order;
- list($data, $code) = self::radarRuleMoveUp($params, $behindRuleOriginalSortOrder, $operatorRuleOriginalSortOrder);
- return [$data, $code];
- } else if(!empty($params['front_rule_id']) && empty($params['behind_rule_id'])) {
- // 下移到最后一位,判断是否有调整后的排在调整分组前面的分组id
- $frontRule = ChatMenuRule::query()
- ->where('id', $params['front_rule_id'])
- ->first();
- $frontRuleOriginalSortOrder = $frontRule->sort_order;
- list($data, $code) = self::radarRuleMoveDown($params, $frontRuleOriginalSortOrder, $operatorRuleOriginalSortOrder);
- return [$data, $code];
- } else if(!empty($params['front_rule_id']) && !empty($params['behind_rule_id'])) {
- $frontRule = ChatMenuRule::query()
- ->where('id', $params['front_rule_id'])
- ->first();
- $frontRuleOriginalSortOrder = $frontRule->sort_order;
- $behindRule = ChatMenuRule::query()
- ->where('id', $params['behind_rule_id'])
- ->first();
- $behindRuleOriginalSortOrder = $behindRule->sort_order;
- if($operatorRuleOriginalSortOrder > $frontRuleOriginalSortOrder) {
- // 下移
- list($data, $code) = self::radarRuleMoveDown($params, $frontRuleOriginalSortOrder, $operatorRuleOriginalSortOrder);
- } else {
- // 上移
- list($data, $code) = self::radarRuleMoveUp($params, $behindRuleOriginalSortOrder, $operatorRuleOriginalSortOrder);
- }
- return [$data, $code];
- } else {
- Log::logError('updateChatMenuRuleSortOrder', [
- 'params' => $params,
- 'message' => '被操作话术规则前后规则id不可同时为空',
- ],'interface');
- return [['error' => '被操作话术规则前后规则id不可同时为空'], 3507];
- }
- } catch (\Exception $exception) {
- Log::logError('updateChatMenuRuleSortOrder', [
- 'params' => $params,
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'message' => $exception->getMessage(),
- 'trace' => $exception->getTraceAsString(),
- ], 'interface');
- return ['移动失败', 400];
- }
- }
- // 话术规则上移
- public static function radarRuleMoveUp($params, $behindRuleOriginalSortOrder, $operatorRuleOriginalSortOrder)
- {
- \DB::begintransaction();
- $moveRes = ChatMenuRule::query()
- ->where('corpid', $params['corpid'])
- ->where('sys_group_id', $params['sys_group_id'])
- ->where('enable', 1)
- ->where('sort_order', '<=', $behindRuleOriginalSortOrder)
- ->where('sort_order', '>', $operatorRuleOriginalSortOrder)
- ->decrement('sort_order');// 自减1
- $operatorRes = ChatMenuRule::query()
- ->where('id', $params['operate_rule_id'])
- ->update(['sort_order' => $behindRuleOriginalSortOrder]);
- if($moveRes && $operatorRes) {
- \DB::commit();
- return ['移动成功', 0];
- } else {
- \DB::rollBack();
- Log::logError('updateChatMenuRuleSortOrder', [
- 'params' => $params,
- 'message' => '移动失败',
- 'move_res' => $moveRes,
- 'operator_res' => $operatorRes,
- 'operator_sort_order' => $operatorRuleOriginalSortOrder,
- 'behind_sort_order' => $behindRuleOriginalSortOrder,
- ],'interface');
- return ['移动失败', 400];
- }
- }
- // 话术规则下移
- public static function radarRuleMoveDown($params, $frontRuleOriginalSortOrder, $operatorRuleOriginalSortOrder)
- {
- \DB::begintransaction();
- $moveRes = ChatMenuRule::query()
- ->where('corpid', $params['corpid'])
- ->where('sys_group_id', $params['sys_group_id'])
- ->where('enable', 1)
- ->where('sort_order', '>=', $frontRuleOriginalSortOrder)
- ->where('sort_order', '<', $operatorRuleOriginalSortOrder)
- ->increment('sort_order');// 自增1
- $operatorRes = ChatMenuRule::query()
- ->where('id', $params['operate_rule_id'])
- ->update(['sort_order' => $frontRuleOriginalSortOrder]);
- if($moveRes && $operatorRes) {
- \DB::commit();
- return ['移动成功', 0];
- } else {
- \DB::rollBack();
- Log::logError('updateChatMenuRuleSortOrder', [
- 'params' => $params,
- 'message' => '移动失败',
- 'move_res' => $moveRes,
- 'operator_res' => $operatorRes,
- 'operator_sort_order' => $operatorRuleOriginalSortOrder,
- 'front_sort_order' => $frontRuleOriginalSortOrder,
- ],'interface');
- return ['移动失败', 400];
- }
- }
- public static function deleteChatMenuRule($params)
- {
- try{
- \DB::begintransaction();
- $ruleRes = ChatMenuRule::query()
- ->where('id', $params['rule_id'])
- ->update([
- 'enable' => 0
- ]);
- $msgCount = ChatMenuMsg::query()
- ->where('rule_id', $params['rule_id'])
- ->where('enable', 1)
- ->count();
- // 防止出现本来雷达组下就没有雷达,更新不到数据导致整体删除失败
- if($msgCount > 0) {
- $msgRes = ChatMenuMsg::query()
- ->where('rule_id', $params['rule_id'])
- ->where('enable', 1)
- ->update(['enable' => 0]);
- } else {
- $msgRes = true;
- }
- if($ruleRes && $msgRes) {
- \DB::commit();
- return ['删除成功', 0];
- } else {
- \DB::rollBack();
- Log::logError('deleteChatMenuRule', [
- 'params' => $params,
- 'message' => '删除失败',
- 'rule_res' => $ruleRes,
- 'msg_res' => $msgRes,
- ],'interface');
- return ['删除失败', 400];
- }
- } catch (\Exception $exception) {
- Log::logError('deleteChatMenuRule', [
- 'params' => $params,
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'message' => $exception->getMessage(),
- 'trace' => $exception->getTraceAsString(),
- ], 'interface');
- return ['删除失败', 400];
- }
- }
- public static function updateChatMenuRule($params)
- {
- try{
- // 解析话术消息
- $msgData = json_decode($params['msg_data_list'], 1);
- if(count($msgData) > 9) {
- Log::logError('updateChatMenuRule', [
- 'params' => $params,
- 'errmsg' => '最多添加9条话术内容',
- ],'interface');
- return [['error' => '最多添加9条话术内容'], 3505];
- }
- if(empty($msgData)) {
- Log::logError('updateChatMenuRule', [
- 'params' => $params,
- 'errmsg' => '最少添加1条话术内容',
- ],'interface');
- return [['error' => '最少添加1条话术内容'], 3506];
- }
- \DB::begintransaction();
- //保存话术规则
- $chatMenuRule = new ChatMenuRule();
- $chatMenuRule = $chatMenuRule->find($params['rule_id']);
- $chatMenuRule->group_id = empty($params['group_id']) ? 0 : $params['group_id'];
- $chatMenuRule->title = $params['title'];
- if($chatMenuRule->save()){
- // 提取消息id
- $msgIdList = array_column($msgData, 'msg_id');
- // 将不属于本次消息中的消息删除
- $count = ChatMenuMsg::query()
- ->where('rule_id', $params['rule_id'])
- ->where('enable', 1)
- ->whereNotIn('id', $msgIdList)
- ->count();
- if($count > 0) {
- $res = ChatMenuMsg::query()
- ->where('rule_id', $params['rule_id'])
- ->where('enable', 1)
- ->whereNotIn('id', $msgIdList)
- ->update(['enable' => 0]);
- if(!$res) {
- Log::logError('updateChatMenuRule', [
- 'params' => $params,
- 'chat_menu_rule' => (array)$chatMenuRule,
- 'chat_menu_msg_del_res' => (array)$res,
- 'errmsg' => '编辑失败,删除消息失败',
- ],'interface');
- \DB::rollBack();
- // 添加失败
- return ['编辑失败', 400];
- }
- }
- //分类型保存话术消息
- foreach ($msgData as $msgInfo) {
- $res = ChatMenuMsg::saveData($msgInfo['msg_id'], $msgInfo['chat_type'], $msgInfo,
- $params['corpid'], $params['sys_group_id'], $params['rule_id']);
- if(!$res) {
- Log::logError('updateChatMenuRule', [
- 'params' => $params,
- 'chat_menu_rule' => (array)$chatMenuRule,
- 'chat_menu_msg_save_res' => (array)$res,
- 'errmsg' => '编辑失败,消息保存失败',
- ],'interface');
- \DB::rollBack();
- // 添加失败
- return ['编辑失败', 400];
- }
- }
- \DB::commit();
- return ['编辑成功', 0];
- } else {
- Log::logError('updateChatMenuRule', [
- 'params' => $params,
- 'chat_menu_rule' => (array)$chatMenuRule,
- 'chat_menu_msg' => [],
- 'errmsg' => '添加失败,规则保存失败',
- ],'interface');
- \DB::rollBack();
- // 添加失败
- return ['编辑失败', 400];
- }
- } catch (\Exception $exception) {
- Log::logError('updateChatMenuRule', [
- 'params' => $params,
- 'errmsg' => $exception->getMessage(),
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'trace' => $exception->getTraceAsString(),
- ],'interface');
- return ['编辑失败', 400];
- }
- }
- /**
- * 快捷回复处理成可发送的数据结构
- * */
- public static function getChatMessage($params, &$errno)
- {
- try {
- if($params['type'] == 1) {
- $msgInfo = ChatMenuMsg::select(['chat_type', 'chat_message', 'corpid'])->where('corpid', $params['corpid'])
- ->where('rule_id', $params['rule_id'])->where('id', $params['msg_id'])
- ->where('enable', 1)->first();
- } else {
- $msgInfo = UserSopMsg::selectRaw("msg_type as chat_type, content as chat_message, corpid")
- ->where('corpid', $params['corpid'])
- ->where('rule_id', $params['rule_id'])->where('id', $params['msg_id'])
- ->where('enable', 1)->first();
- $chatType = $msgInfo->chat_type ?? '';
- $message = json_decode($msgInfo->chat_message, true);
- # 处理数据
- switch($chatType) {
- case 1:
- $chatMessage = [
- "msgtype" => "text",
- "text" => $message
- ];
- break;
- case 2:
- $chatMessage= [
- "msgtype" => "image",
- "image" => $message
- ];
- break;
- case 3:
- $chatMessage = [
- "msgtype" => "video",
- "video" => $message
- ];
- break;
- case 4:
- $chatMessage = [
- "msgtype" => "file",
- "file" => $message
- ];
- break;
- case 5:
- $chatMessage = [
- "msgtype" => "news",
- "news" => $message
- ];
- break;
- case 6:
- $chatMessage = [
- "msgtype" => "miniprogram",
- "miniprogram" => $message
- ];
- break;
- }
- $msgInfo->chat_message = json_encode($chatMessage);
- }
- if(empty($msgInfo)) {
- $errno = 3501;
- return [];
- }
- $corpid = $msgInfo->corpid ?? '';
- $chatType = $msgInfo->chat_type ?? 0;
- $chatMessage = $msgInfo->chat_message ? json_decode($msgInfo->chat_message, true) : [];
- if(in_array($chatType, [2, 3, 4])) { // 图片、视频、文件类获取对应素材ID
- $mediaType = $chatType == 2 ? 1 : $chatType;
- $materialId = $chatMessage[$chatMessage['msgtype']]['mediaid'];
- $mediaId = MaterialService::getMediaId($materialId, $corpid, false, $mediaType);
- if($mediaId===false) {
- Log::logInfo('临时素材ID获取失败', [
- 'chatMessage' => $chatMessage,
- 'corpid' => $corpid
- ], 'ChatMenuMsgDealTrace');
- $errno = 3503;
- return [];
- }
- $chatMessage[$chatMessage['msgtype']]['mediaid'] = $mediaId;
- }
- } catch(\Exception $e) {
- EmailQueue::rPush('快捷回复信息处理成链接类型消息体过程发生异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com'],
- '快捷回复信息处理成链接类型消息体过程发生异常');
- Log::logError('快捷回复信息处理成链接类型消息体过程发生异常', [
- 'line' => $e->getLine(),
- 'msg' => $e->getMessage()
- ], 'ChatMenuMsgDealTrace');
- $errno = 3502;
- return [];
- }
- return $chatMessage;
- }
- /**
- * 快捷回复数据上报
- * */
- public static function behaviorReport($param)
- {
- try {
- if($param['external_userid'])
- $param['con_user_cus'] = $param['user_id'] . $param['external_userid'];
- # 行为数据存储
- $result = ChatMenuRecord::insert($param);
- if(!$result) {
- Log::logError('发送数据上报失败', [
- 'param' => $param,
- ], 'ChatMenuBehaviorReport');
- return 3504;
- }
- } catch (\Exception $e) {
- Log::logError('发送数据上报发生异常', [
- 'param' => $param,
- 'line' => $e->getLine(),
- 'msg' => $e->getMessage()
- ], 'ChatMenuBehaviorReport-Exception');
- return 3504;
- }
- return 0;
- }
- }
|