|
|
@@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.fs.common.core.domain.R;
|
|
|
import com.fs.company.domain.AiAddwxSopUserNode;
|
|
|
import com.fs.company.domain.AiAddwxSopUserNodeRule;
|
|
|
@@ -31,6 +33,7 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.ZoneId;
|
|
|
+import java.util.Date;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
@@ -59,6 +62,12 @@ public class AiAddwxSopUserNodeServiceImpl implements IAiAddwxSopUserNodeService
|
|
|
@Autowired
|
|
|
private QwExternalContactMapper qwExternalContactMapper;
|
|
|
|
|
|
+ @Value("${ai.add.wx.sop.key:fastgpt-hPSZNV9pPlX5GlHyjzsGqEPbB2D8tb9mYQcnD1y5lEUIEMgGaO0PFm7BHHjF}")
|
|
|
+ private String AI_ADD_WC_SOP_KEY;
|
|
|
+
|
|
|
+ private static final ObjectMapper mapper = new ObjectMapper();
|
|
|
+
|
|
|
+
|
|
|
|
|
|
@Override
|
|
|
public AiAddwxSopUserNode selectAiAddwxSopUserNodeById(Long userNodeId) {
|
|
|
@@ -108,8 +117,6 @@ public class AiAddwxSopUserNodeServiceImpl implements IAiAddwxSopUserNodeService
|
|
|
return userNodeMapper.updateAiAddwxSopUserNode(node);
|
|
|
}
|
|
|
|
|
|
- @Value("${ai.add.wx.sop.key:fastgpt-hPSZNV9pPlX5GlHyjzsGqEPbB2D8tb9mYQcnD1y5lEUIEMgGaO0PFm7BHHjF}")
|
|
|
- private String AI_ADD_WC_SOP_KEY;
|
|
|
@Override
|
|
|
public void nextNode(Map.Entry<String, List<AiAddwxSopUserWorkflow>> next) {
|
|
|
AiAddWechatSopNodeEnum nodeTypeE = AiAddWechatSopNodeEnum.fromCode(next.getKey());
|
|
|
@@ -153,7 +160,13 @@ public class AiAddwxSopUserNodeServiceImpl implements IAiAddwxSopUserNodeService
|
|
|
targetTime = targetTime.plusDays(1).withHour(9).withMinute(0);
|
|
|
}
|
|
|
firstRule.setRealSendTime(Date.from(targetTime.atZone(ZoneId.systemDefault()).toInstant()));
|
|
|
-
|
|
|
+ try {
|
|
|
+ JsonNode root =mapper.readTree(firstRule.getContentJson());
|
|
|
+ firstRule.setContentJson(root.path("prompt").asText());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.info("解析发送话术失败,用户节点id:{}",firstRule.getUserNodeRuleId());
|
|
|
+ }
|
|
|
aiAddwxSopUserNodeRuleMapper.updateById(firstRule);
|
|
|
continue;
|
|
|
}
|
|
|
@@ -162,62 +175,121 @@ public class AiAddwxSopUserNodeServiceImpl implements IAiAddwxSopUserNodeService
|
|
|
QwExternalContact qwExternalContact = qwExternalContactMapper.selectQwExternalContactById(workflow.getQwExternalId());
|
|
|
initAiChatParam(requestParam, workflow,session,nodeTypeE.getDesc(),qwExternalContact);
|
|
|
|
|
|
- R r = aiAddwxSopTemplateServiceImpl.callAiService(requestParam, session.getSessionId(), AI_ADD_WC_SOP_KEY);
|
|
|
- JSONObject content = extractContentJson(r);
|
|
|
+// R r = aiAddwxSopTemplateServiceImpl.callAiService(requestParam, session.getSessionId(), AI_ADD_WC_SOP_KEY);
|
|
|
+// JSONObject content = extractContentJson(r);
|
|
|
|
|
|
//根据ai生成的结果进行节点转换
|
|
|
- if (content!= null){
|
|
|
- JSONObject userInfo = JSON.parseObject(content.getString("userInfo"));
|
|
|
- if (userInfo != null){
|
|
|
- String currentGeneration = null;
|
|
|
- Boolean suc = false;
|
|
|
- try {
|
|
|
- currentGeneration = userInfo.getString("当前阶段");
|
|
|
- suc =true;
|
|
|
- }catch (Exception e){
|
|
|
- log.error("获取当前阶段失败,无需流转", e);
|
|
|
- }
|
|
|
- if (suc){
|
|
|
- AiAddWechatSopNodeEnum nodeEnum = AiAddWechatSopNodeEnum.fromCode(workflow.getCurrentNodeType());
|
|
|
- if (nodeEnum == null){
|
|
|
- log.error("当前阶段异常,请检查AI返回的当前阶段");
|
|
|
- return;
|
|
|
- }
|
|
|
- if (!currentGeneration.equals(nodeEnum.getDesc() )){
|
|
|
- AiAddwxSopUserWorkflow newWorkflow = new AiAddwxSopUserWorkflow();
|
|
|
- AiAddwxSopUserNode currentNode = userNodeMapper.selectById(workflow.getCurrentNodeId());
|
|
|
- List<AiAddwxSopUserNode> aiAddwxSopUserNodes = userNodeMapper.selectList(new LambdaQueryWrapper<AiAddwxSopUserNode>()
|
|
|
- .eq(AiAddwxSopUserNode::getPrevNodeId, currentNode.getTemplateNodeId())
|
|
|
- .eq(AiAddwxSopUserNode::getUserWorkflowId,currentNode.getUserWorkflowId()));
|
|
|
- AiAddwxSopUserNode nextNode = aiAddwxSopUserNodes.stream().filter(node -> node.getNodeType().equals(nodeEnum.getCode())).findFirst().orElse(null);
|
|
|
- if (nextNode != null){
|
|
|
- newWorkflow.setUserWorkflowId(workflow.getUserWorkflowId())
|
|
|
- .setCurrentNodeId(nextNode.getUserNodeId())
|
|
|
- .setCurrentNodeKey(nextNode.getNodeKey())
|
|
|
- .setCurrentNodeType(nextNode.getNodeType()); //新节点
|
|
|
- aiAddwxSopUserWorkflowMapper.updateById(newWorkflow);
|
|
|
- }
|
|
|
- }
|
|
|
- }else {
|
|
|
- //未变更就继续对话
|
|
|
- String aiContent = content.getString("aiContent");
|
|
|
- List<AiAddwxSopUserNodeRule> aiAddwxSopUserNodeRules = aiAddwxSopUserNodeRuleMapper.selectList(new LambdaQueryWrapper<AiAddwxSopUserNodeRule>()
|
|
|
- .eq(AiAddwxSopUserNodeRule::getUserNodeId, workflow.getCurrentNodeId()).eq(AiAddwxSopUserNodeRule::getIsSent, 1).orderByAsc(AiAddwxSopUserNodeRule::getUpdateTime));
|
|
|
- if (!aiAddwxSopUserNodeRules.isEmpty()){
|
|
|
- AiAddwxSopUserNodeRule finalRule = aiAddwxSopUserNodeRules.get(aiAddwxSopUserNodeRules.size() - 1);
|
|
|
- Date date = new Date();
|
|
|
- Date datePlus5Seconds = new Date(date.getTime() + 5000);
|
|
|
- finalRule.setIsSent(0).setUserNodeRuleId(null).setRealSendTime(datePlus5Seconds).setContentJson(aiContent);
|
|
|
- aiAddwxSopUserNodeRuleMapper.updateById(finalRule);
|
|
|
- }
|
|
|
- }
|
|
|
+// if (content!= null){
|
|
|
+// JSONObject userInfo = JSON.parseObject(content.getString("userInfo"));
|
|
|
+// if (userInfo != null){
|
|
|
+// String currentGeneration = null;
|
|
|
+// Boolean suc = false;
|
|
|
+// try {
|
|
|
+// currentGeneration = userInfo.getString("当前阶段");
|
|
|
+// suc =true;
|
|
|
+// }catch (Exception e){
|
|
|
+// log.error("获取当前阶段失败,无需流转", e);
|
|
|
+// }
|
|
|
+// if (suc){
|
|
|
+// AiAddWechatSopNodeEnum nodeEnum = AiAddWechatSopNodeEnum.fromCode(workflow.getCurrentNodeType());
|
|
|
+// if (nodeEnum == null){
|
|
|
+// log.error("当前阶段异常,请检查AI返回的当前阶段");
|
|
|
+// return;
|
|
|
+// }
|
|
|
+// if (!currentGeneration.equals(nodeEnum.getDesc() )){
|
|
|
+// AiAddwxSopUserWorkflow newWorkflow = new AiAddwxSopUserWorkflow();
|
|
|
+// AiAddwxSopUserNode currentNode = userNodeMapper.selectById(workflow.getCurrentNodeId());
|
|
|
+// List<AiAddwxSopUserNode> aiAddwxSopUserNodes = userNodeMapper.selectList(new LambdaQueryWrapper<AiAddwxSopUserNode>()
|
|
|
+// .eq(AiAddwxSopUserNode::getPrevNodeId, currentNode.getTemplateNodeId())
|
|
|
+// .eq(AiAddwxSopUserNode::getUserWorkflowId,currentNode.getUserWorkflowId()));
|
|
|
+// AiAddwxSopUserNode nextNode = aiAddwxSopUserNodes.stream().filter(node -> node.getNodeType().equals(nodeEnum.getCode())).findFirst().orElse(null);
|
|
|
+// if (nextNode != null){
|
|
|
+// newWorkflow.setUserWorkflowId(workflow.getUserWorkflowId())
|
|
|
+// .setCurrentNodeId(nextNode.getUserNodeId())
|
|
|
+// .setCurrentNodeKey(nextNode.getNodeKey())
|
|
|
+// .setCurrentNodeType(nextNode.getNodeType()); //新节点
|
|
|
+// aiAddwxSopUserWorkflowMapper.updateById(newWorkflow);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }else {
|
|
|
+// //未变更就继续对话
|
|
|
+// String aiContent = content.getString("aiContent");
|
|
|
+// List<AiAddwxSopUserNodeRule> aiAddwxSopUserNodeRules = aiAddwxSopUserNodeRuleMapper.selectList(new LambdaQueryWrapper<AiAddwxSopUserNodeRule>()
|
|
|
+// .eq(AiAddwxSopUserNodeRule::getUserNodeId, workflow.getCurrentNodeId()).eq(AiAddwxSopUserNodeRule::getIsSent, 1).orderByAsc(AiAddwxSopUserNodeRule::getUpdateTime));
|
|
|
+// if (!aiAddwxSopUserNodeRules.isEmpty()){
|
|
|
+// AiAddwxSopUserNodeRule finalRule = aiAddwxSopUserNodeRules.get(aiAddwxSopUserNodeRules.size() - 1);
|
|
|
+// Date date = new Date();
|
|
|
+// Date datePlus5Seconds = new Date(date.getTime() + 5000);
|
|
|
+// finalRule.setIsSent(0).setUserNodeRuleId(null).setRealSendTime(datePlus5Seconds).setContentJson(aiContent);
|
|
|
+// aiAddwxSopUserNodeRuleMapper.updateById(finalRule);
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// }
|
|
|
+// }
|
|
|
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void processNodeTransitionFromAi(R r, AiAddwxSopUserWorkflow workflow) {
|
|
|
+ JSONObject content = extractContentJson(r);
|
|
|
+ if (content == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ JSONObject userInfo = JSON.parseObject(content.getString("userInfo"));
|
|
|
+ if (userInfo == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String currentGeneration = null;
|
|
|
+ boolean suc = false;
|
|
|
+ try {
|
|
|
+ currentGeneration = userInfo.getString("当前阶段");
|
|
|
+ suc = true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取当前阶段失败,无需流转", e);
|
|
|
+ }
|
|
|
+ if (suc) {
|
|
|
+ AiAddWechatSopNodeEnum nodeEnum = AiAddWechatSopNodeEnum.fromCode(workflow.getCurrentNodeType());
|
|
|
+ if (nodeEnum == null) {
|
|
|
+ log.error("当前阶段异常,请检查AI返回的当前阶段");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!currentGeneration.equals(nodeEnum.getDesc())) {
|
|
|
+ AiAddwxSopUserWorkflow newWorkflow = new AiAddwxSopUserWorkflow();
|
|
|
+ AiAddwxSopUserNode currentNode = userNodeMapper.selectById(workflow.getCurrentNodeId());
|
|
|
+ List<AiAddwxSopUserNode> aiAddwxSopUserNodes = userNodeMapper.selectList(new LambdaQueryWrapper<AiAddwxSopUserNode>()
|
|
|
+ .eq(AiAddwxSopUserNode::getPrevNodeId, currentNode.getTemplateNodeId())
|
|
|
+ .eq(AiAddwxSopUserNode::getUserWorkflowId, currentNode.getUserWorkflowId()));
|
|
|
+ AiAddwxSopUserNode nextNode = aiAddwxSopUserNodes.stream()
|
|
|
+ .filter(node -> node.getNodeType().equals(nodeEnum.getCode()))
|
|
|
+ .findFirst().orElse(null);
|
|
|
+ if (nextNode != null) {
|
|
|
+ newWorkflow.setUserWorkflowId(workflow.getUserWorkflowId())
|
|
|
+ .setCurrentNodeId(nextNode.getUserNodeId())
|
|
|
+ .setCurrentNodeKey(nextNode.getNodeKey())
|
|
|
+ .setCurrentNodeType(nextNode.getNodeType());
|
|
|
+ aiAddwxSopUserWorkflowMapper.updateById(newWorkflow);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+ } else {
|
|
|
+ // 未变更就继续对话
|
|
|
+ String aiContent = content.getString("aiContent");
|
|
|
+ List<AiAddwxSopUserNodeRule> aiAddwxSopUserNodeRules = aiAddwxSopUserNodeRuleMapper.selectList(new LambdaQueryWrapper<AiAddwxSopUserNodeRule>()
|
|
|
+ .eq(AiAddwxSopUserNodeRule::getUserNodeId, workflow.getCurrentNodeId())
|
|
|
+ .eq(AiAddwxSopUserNodeRule::getIsSent, 1)
|
|
|
+ .orderByAsc(AiAddwxSopUserNodeRule::getUpdateTime));
|
|
|
+ if (!aiAddwxSopUserNodeRules.isEmpty()) {
|
|
|
+ AiAddwxSopUserNodeRule finalRule = aiAddwxSopUserNodeRules.get(aiAddwxSopUserNodeRules.size() - 1);
|
|
|
+ Date date = new Date();
|
|
|
+ Date datePlus5Seconds = new Date(date.getTime() + 5000);
|
|
|
+ finalRule.setIsSent(0).setUserNodeRuleId(null).setRealSendTime(datePlus5Seconds).setContentJson(aiContent);
|
|
|
+ aiAddwxSopUserNodeRuleMapper.updateById(finalRule);
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
+
|
|
|
private static JSONObject extractContentJson(R result) {
|
|
|
try {
|
|
|
Object data = result.get("data");
|
|
|
@@ -252,7 +324,7 @@ public class AiAddwxSopUserNodeServiceImpl implements IAiAddwxSopUserNodeService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void initAiChatParam(Map<String, Object> requestParam, AiAddwxSopUserWorkflow workflow, FastGptChatSession session, String nodeTypeE, QwExternalContact externalUser) {
|
|
|
+ public void initAiChatParam(Map<String, Object> requestParam, AiAddwxSopUserWorkflow workflow, FastGptChatSession session, String nodeTypeE, QwExternalContact externalUser) {
|
|
|
// 1. 构建 userInfo
|
|
|
Map<String, Object> userInfo = new HashMap<>();
|
|
|
userInfo.put("姓名", externalUser.getName()!=null?externalUser.getName():"");
|