package com.xly.thread; import cn.hutool.core.lang.generator.UUIDGenerator; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import com.xly.config.SpringContextHolder; import com.xly.entity.UserSceneSession; import com.xly.milvus.service.AiGlobalAgentQuestionSqlEmitterService; import com.xly.service.DynamicExeDbService; import com.xly.service.RedisService; import com.xly.util.SqlValidateUtil; import com.xly.util.SqlWhereUtil; import dev.langchain4j.data.message.ChatMessage; import dev.langchain4j.data.message.ChatMessageType; import java.util.HashMap; import java.util.List; import java.util.Map; public class AiUserAgentQuestionThread implements Runnable { private UserSceneSession session; private String sSqlContent; private String sQuestion; private String cachType; private Boolean bSucess; List userMessage; public final String sProName="Sp_Ai_AiUserAgentQuestionThread"; public AiUserAgentQuestionThread(UserSceneSession session,String sQuestion,String sSqlContent,String cachType, List userMessage,Boolean bSucess) { this.session = session; this.sSqlContent = sSqlContent; this.sQuestion = sQuestion; this.userMessage = userMessage; this.cachType = cachType; this.bSucess = bSucess; } @Override public void run() { sQuestion = sQuestion.replace("用户输入:",StrUtil.EMPTY); String sSceneId = session.getCurrentScene().getSId(); String sMethodId = session.getCurrentTool().getSId(); DynamicExeDbService dynamicExeDbService = SpringContextHolder.getBean(DynamicExeDbService.class); RedisService redisService = SpringContextHolder.getBean(RedisService.class); AiGlobalAgentQuestionSqlEmitterService aiGlobalAgentQuestionSqlEmitterService = SpringContextHolder.getBean(AiGlobalAgentQuestionSqlEmitterService.class); String sQuestionGroupNo = session.getSUserQuestionList().get(0); Integer bRedis = (session.getSUserQuestionList().size()==1)?1:0; Map data = getMap(sSceneId, sMethodId,bRedis,sQuestionGroupNo); data.put("sQuestion",SqlValidateUtil.getsQuestion(session.getSUserQuestionList())); data.put("sId",new UUIDGenerator().next()); data.put("cachType",cachType); data.put("userInput",sQuestion); //插入Redis缓存 if(bRedis==1 && ObjectUtil.isNotEmpty(sSqlContent)){ String sReidKey = SqlValidateUtil.getsKey( sSceneId, sMethodId, sQuestionGroupNo); redisService.set(sReidKey,sSqlContent); } String searchText = String.format("场景:%s 方法:%s 客户问题:%s", sSceneId, sMethodId, sQuestion); // sSceneId+"_"+sMethodId +"_"+sQuestion; // SqlValidateUtil.getsKey( sSceneId, sMethodId, SqlValidateUtil.getsQuestion(session.getSUserQuestionList())); //存入向量库 存在SQL语句并且没有where 并且执行成功 if(!SqlWhereUtil.hasValidConditionAfterClean(sSqlContent) && ObjectUtil.isNotEmpty(sSqlContent) && bSucess){ aiGlobalAgentQuestionSqlEmitterService.addAiGlobalAgentQuestionSqlEmitter(searchText,data,sQuestion,sSqlContent,cachType,"ai_global_agent_question_sql",false); } //调用数据库插入数据库 Map searMap = dynamicExeDbService.getDoProMap(sProName, data); dynamicExeDbService.getCallPro(searMap, sProName); } //获取组ID private String getQuestionGroupNo(){ String sQuestionGroupNo = userMessage.stream() .filter(one -> one.type().equals(ChatMessageType.USER) && !"用户输入:initAiService".equals(one.text())) .findFirst() .map(one->one.text()) .orElse(StrUtil.EMPTY); sQuestionGroupNo = sQuestionGroupNo.replace("用户输入:",StrUtil.EMPTY); sQuestionGroupNo = sQuestionGroupNo.replace(" ",StrUtil.EMPTY); sQuestionGroupNo = sQuestionGroupNo.replace("\\t",StrUtil.EMPTY); sQuestionGroupNo = sQuestionGroupNo.replace("\\n",StrUtil.EMPTY); sQuestionGroupNo = sQuestionGroupNo.toLowerCase(); return sQuestionGroupNo; } /*** * @Author 钱豹 * @Date 4:22 2026/3/15 * @Param [sSceneId, sMethodId] * @return java.util.Map * @Description 获取执行的动态Map **/ private Map getMap(String sSceneId, String sMethodId,Integer bRedis,String sQuestionGroupNo) { Map data = new HashMap<>(16); data.put("sSceneId", sSceneId); data.put("sMethodId", sMethodId); data.put("sSqlContent",sSqlContent); data.put("sBrId",session.getSBrandsId()); data.put("sBrandsId",session.getSBrandsId()); data.put("sSuId",session.getSSubsidiaryId()); data.put("sSubsidiaryId",session.getSSubsidiaryId()); data.put("sLoginId",session.getUserName()); data.put("sUserName",session.getUserName()); data.put("sUserId",session.getUserId()); data.put("bRedis",bRedis); data.put("sQuestionGroupNo",sQuestionGroupNo); return data; } }