智能客服-四合一开发

This commit is contained in:
zren25
2025-04-02 18:31:54 +08:00
parent b7a7a1000b
commit 1cef8c6b5d
14 changed files with 524 additions and 3 deletions

View File

@@ -0,0 +1,15 @@
package com.volvo.ai.analytic.center.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.volvo.ai.analytic.center.entity.AiAnalysisErrors;
import com.volvo.ai.analytic.center.entity.TcIntelligentCustomer;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface TcIntelligentCustomerMapper extends BaseMapper<TcIntelligentCustomer> {
}

View File

@@ -0,0 +1,86 @@
package com.volvo.ai.analytic.center.mq;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.volvo.ai.analytic.center.dto.req.FourInOneRequestDTO;
import com.volvo.ai.analytic.center.service.AiAnalysisRequestLogsService;
import com.volvo.ai.analytic.center.service.IntelligentCustomerService;
import com.volvo.ai.analytic.center.service.TmTelephoneCorpusService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName CorpusProcessKafkaConsumer
* @Description 智能客服 语料解析 四合一
* @Author renzhen
* @Date 2025-03-04 10:18
* @Version 1.0
**/
@Slf4j
@Component
@RefreshScope
@RestController
@RocketMQMessageListener(consumerGroup = "${rocketmq.consumer.intelligentCustomer.group}",
topic = "${rocketmq.consumer.intelligentCustomer.topic}",
instanceName = "intelligentCustomerMq",
consumeThreadNumber = 5,
enableMsgTrace = true)
public class IntelligentCustomerMqConsumer implements RocketMQListener<MessageExt> {
@Autowired
private IntelligentCustomerService intelligentCustomerService;
@Autowired
private AiAnalysisRequestLogsService aiAnalysisRequestLogsService;
@Value("${dify.corpus.checkDccRepeat}")
private String checkDccRepeat;
private final ObjectMapper objectMapper = new ObjectMapper();
@Override
public void onMessage(MessageExt messageExt) {
long startTime = System.currentTimeMillis();
try {
log.info("intelligentCustomerMqConsumer 当前线程: {}, 线程ID: {}", Thread.currentThread().getName(), Thread.currentThread().getId());
String message = new String(messageExt.getBody());
log.info("IntelligentCustomerMqConsumer message: " + message);
FourInOneRequestDTO fourRequestDTO = objectMapper.readValue(message, FourInOneRequestDTO.class);
intelligentCustomerService.fourInOneMqService(fourRequestDTO);
log.info("FourInOneRequestDTO处理完成耗时{}", System.currentTimeMillis() - startTime);
} catch (JsonProcessingException e) {
log.info(" dcc mq 处理失败:{}", e.getMessage());
}
}
@PostMapping("ntelligentCustomerMqConsumer")
public void onMessage(@RequestBody String message) {
long startTime = System.currentTimeMillis();
try {
log.info("intelligentCustomerMqConsumer 当前线程: {}, 线程ID: {}", Thread.currentThread().getName(), Thread.currentThread().getId());
// String message = new String(messageExt.getBody());
log.info("IntelligentCustomerMqConsumer message: " + message);
FourInOneRequestDTO fourRequestDTO = objectMapper.readValue(message, FourInOneRequestDTO.class);
intelligentCustomerService.fourInOneMqService(fourRequestDTO);
log.info("FourInOneRequestDTO处理完成耗时{}", System.currentTimeMillis() - startTime);
} catch (JsonProcessingException e) {
log.info(" dcc mq 处理失败:{}", e.getMessage());
}
}
}

View File

@@ -0,0 +1,20 @@
package com.volvo.ai.analytic.center.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.volvo.ai.analytic.center.dto.corpus.AicorpusTelephoneDTO;
import com.volvo.ai.analytic.center.dto.req.FourInOneRequestDTO;
import com.volvo.ai.analytic.center.entity.TmTelephoneCorpus;
/**
* @description 电话语料表-同步表
* @author BEJSON
* @date 2025-03-04
*/
public interface IntelligentCustomerService{
void fourInOneMqService(FourInOneRequestDTO fourInOneRequestDTO);
void sendMq( String message);
}

View File

@@ -119,7 +119,7 @@ public class TmOdsVdqwMessagearchivingServiceImpl extends ServiceImpl<TmOdsVdqwM
// 获取消息列表
int optimalThreadPoolSize = Runtime.getRuntime().availableProcessors() + 1;
log.info("获取的线程数:{}",optimalThreadPoolSize);
log.info("企微处理总数据量:{},总页数:{},获取的线程数:{}",totalPages,totalPages,optimalThreadPoolSize);
// 创建线程池
ExecutorService executor = Executors.newFixedThreadPool(optimalThreadPoolSize); // 根据需求调整线程池大小

View File

@@ -0,0 +1,203 @@
package com.volvo.ai.analytic.center.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.volvo.ai.analytic.center.constant.Constant;
import com.volvo.ai.analytic.center.dto.req.DiFyReq;
import com.volvo.ai.analytic.center.dto.req.FourInOneRequestDTO;
import com.volvo.ai.analytic.center.dto.req.RunMaskingRuleInput;
import com.volvo.ai.analytic.center.dto.resp.ComplainDTO;
import com.volvo.ai.analytic.center.dto.resp.ConsultDTO;
import com.volvo.ai.analytic.center.dto.resp.FourInOneResponseDTO;
import com.volvo.ai.analytic.center.entity.AiAnalysisErrors;
import com.volvo.ai.analytic.center.entity.AiAnalysisRequestLogs;
import com.volvo.ai.analytic.center.entity.DataMaskingRule;
import com.volvo.ai.analytic.center.entity.TcIntelligentCustomer;
import com.volvo.ai.analytic.center.enums.BusinessTypeEnum;
import com.volvo.ai.analytic.center.enums.IntelligentCustomerTypeEnum;
import com.volvo.ai.analytic.center.feign.RemoteCarModelClient;
import com.volvo.ai.analytic.center.mapper.AiAnalysisErrorsMapper;
import com.volvo.ai.analytic.center.mapper.TcIntelligentCustomerMapper;
import com.volvo.ai.analytic.center.service.*;
import com.volvo.ai.analytic.center.utils.ConstantStr;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @description 智能助手
* @author rz
* @date 2025-03-04
*/
@RefreshScope
@Slf4j
@Service
public class intelligentCustomerServiceImpl implements IntelligentCustomerService {
@Autowired
private DiFyService diFyService;
@Resource
private RocketMQTemplate rocketMqTemplate;
@Value("${rocketmq.producer.corpus.topic}")
private String topic;
@Value("${dify.intelligentCustomer.fourInOneToken}")
private String fourInOneToken;
@Autowired
private RemoteCarModelClient remoteCarModelClient;
@Autowired
private DataMaskingRuleService dataMaskingRuleService;
@Autowired
private AiAnalysisRequestLogsService aiAnalysisRequestLogsService;
@Autowired
private AiAnalysisErrorsService aiAnalysisErrorsService;
@Autowired
private AiAnalysisErrorsMapper aiAnalysisErrorsMapper;
@Autowired
private TcIntelligentCustomerMapper tcIntelligentCustomerMapper;
private static Map<String, List<String>> tcIntelligentCustomer = Collections.EMPTY_MAP;
@Override
public void fourInOneMqService(FourInOneRequestDTO fourInOneRequestDTO) {
if(null != fourInOneRequestDTO){
List<DataMaskingRule> maskingRuleItems = dataMaskingRuleService.getDataMaskingRuleListByApplicationChannel(Constant.INTELLIGENT_CUSTOMER_4IN1);
RunMaskingRuleInput runMaskingRuleInput = new RunMaskingRuleInput();
runMaskingRuleInput.setDataMaskingRules(maskingRuleItems);
Map<String, Object> inputMap = new HashMap();
DiFyReq diFyImageReq = new DiFyReq();
diFyImageReq.setUser(ConstantStr.INTELLIGENT_CUSTOMER_4IN1);
diFyImageReq.setFlowId(fourInOneToken);
inputMap.put("yuliao", fourInOneRequestDTO.getWorkOrderContent());
inputMap.put("laiyuan",fourInOneRequestDTO.getWorkOrderSource());
diFyImageReq.setInputs(inputMap);
// 获取配置
JSONObject execDifyFlow = diFyService.executeDifyFlow(diFyImageReq, BusinessTypeEnum.INTELLIGENT_CUSTOMER.getCode(), JSONObject.toJSONString(fourInOneRequestDTO), fourInOneRequestDTO.getAiAnalysisRequestId());
log.info("runDify execDifyFlow {}",execDifyFlow);
if(null != execDifyFlow && execDifyFlow.get("status").equals("succeeded")){
JSONObject textJsonObject = execDifyFlow.getJSONObject("outputs").getJSONObject("text");
JSONObject consult = textJsonObject.getJSONObject("answer"); // 咨询
ConsultDTO consultDTO = new ConsultDTO();
consultDTO.setOrderType(fourInOneMqServiceFilter(IntelligentCustomerTypeEnum.CONSULT.getCode(),consult.getString("工单类别")));
consultDTO.setProblemDescription(consult.getString("问题描述"));
consultDTO.setContactSex(consult.getString("工单联系人性别"));
consultDTO.setContactName(consult.getString("工单联系人姓名"));
consultDTO.setContactTelephone(consult.getString("工单联系电话"));
JSONObject complainObject = execDifyFlow.getJSONObject("outputs").getJSONObject("text1");
JSONObject complain = complainObject.getJSONObject("complain"); // 投诉
ComplainDTO complainDTO = new ComplainDTO();
complainDTO.setComplainType(fourInOneMqServiceFilter(IntelligentCustomerTypeEnum.CONSULT_TYPE.getCode(),complain.getString("投诉类型")));
complainDTO.setOrderType(fourInOneMqServiceFilter(IntelligentCustomerTypeEnum.COMPLAIN.getCode(),complain.getString("工单类别")));
complainDTO.setComplainTitle(complain.getString("投诉主题"));
complainDTO.setProblemDescription(complain.getString("问题描述"));
complainDTO.setContactSex(consultDTO.getContactSex());
complainDTO.setContactName(consultDTO.getContactName());
complainDTO.setContactTelephone(consultDTO.getContactTelephone());
FourInOneResponseDTO fourInOneResponseDTO = new FourInOneResponseDTO();
String aiAnalysisRequestId = execDifyFlow.getString("aiAnalysisRequestId");
fourInOneResponseDTO.setAiAnalysisRequestId(aiAnalysisRequestId);
fourInOneResponseDTO.setComplain(complainDTO);
fourInOneResponseDTO.setConsult(consultDTO);
fourInOneResponseDTO.setWorkOrderId(fourInOneRequestDTO.getWorkOrderId());
// 发送MQ
String message = JSONObject.toJSONString(fourInOneResponseDTO);
log.info("send mq {}",message);
sendMq(message);
try {
if(StringUtils.isNotEmpty(fourInOneRequestDTO.getAiAnalysisRequestId())){
AiAnalysisErrors aiAnalysisErrors = new AiAnalysisErrors();
aiAnalysisErrors.setAiAnalysisRequestId(aiAnalysisRequestId);
aiAnalysisErrors.setAiAnalysisErrorHandlingStatus("1");
aiAnalysisErrorsService.updateAiAnalysisErrors(aiAnalysisErrors);
}
aiAnalysisRequestLogsService.saveAiAnalysisRequestLogs(AiAnalysisRequestLogs.builder().aiAnalysisRequestId(execDifyFlow.getString("aiAnalysisRequestId")).businessResponse(message).build());
} catch (Exception e) {
log.info(" 电话语料处理保存报告异常processItem{} ", e);
}
}
}
}
@Override
public void sendMq(String message) {
rocketMqTemplate.asyncSend(topic, MessageBuilder.withPayload(message).build(),
new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
log.info("发送MQ成功 消息体:{}", message);
}
@Override
public void onException(Throwable e) {
log.error("送MQ异常 消息体:{}, 异常:", message, e);
}
}, 10000);
}
// 咨询类【201】/投诉类【202】/投诉类型【203】 过滤
private String fourInOneMqServiceFilter(String customerType,String customerValue){
if(tcIntelligentCustomer.isEmpty()){
queryTcIntelligentCustomer();
}
List<String> typeValues = tcIntelligentCustomer.get(customerType);
if(!typeValues.contains(customerValue)){
return "";
}
return customerValue;
}
public Map<String, List<String>> queryTcIntelligentCustomer() {
LambdaQueryWrapper<TcIntelligentCustomer> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(TcIntelligentCustomer::getIsDeleted, "0");
List<TcIntelligentCustomer> tcIntelligentCustomerList =tcIntelligentCustomerMapper.selectList(queryWrapper);
tcIntelligentCustomer = tcIntelligentCustomerList.stream()
.collect(Collectors.groupingBy(
TcIntelligentCustomer::getIntelligentCustomerType, // 按照 IntelligentCustomerType 分组
Collectors.mapping(
customer -> {
if (customer.getIntelligentCustomerType().equals(IntelligentCustomerTypeEnum.CONSULT_TYPE.getCode())) {
return customer.getTypeOne(); // 如果是咨询类型,返回 TypeOne
} else {
return customer.getTypeOne().concat("#").concat(customer.getTypeTwo()).concat("#").concat(customer.getTypeThree()); // 其他类型,拼接 TypeOne、TypeTwo、TypeThree
}
},
Collectors.toList()
)
));
return tcIntelligentCustomer;
}
}

View File

@@ -13,4 +13,8 @@ public class ConstantStr {
public static final String corpus_user = "corpush_user";
public static final String CARMODELLIST_CACHEKEY = "ai_carmodellist";
public static final String INTELLIGENT_CUSTOMER_4IN1 = "INTELLIGENT_CUSTOMER_4IN1";
}