解决 sonar问题

This commit is contained in:
ZLI263
2025-09-17 22:02:57 +08:00
parent ade2d6a50a
commit 1d0c86cc2f
3 changed files with 76 additions and 58 deletions

1
.gitignore vendored
View File

@@ -173,3 +173,4 @@ spring-boot-*.jar
# Kubernetes
*.yaml.bak
*.yml.bak

View File

@@ -1,5 +1,4 @@
package com.volvo.ai.analytic.center.service.impl;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
@@ -27,15 +26,12 @@ 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.Service;
import javax.annotation.Resource;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
/**
* @description 铭牌语料表-同步表
* @author rz

View File

@@ -134,8 +134,6 @@ public class TmOdsVdqwMessagearchivingServiceImpl extends ServiceImpl<TmOdsVdqwM
int offset = (i - 1) * pageSize;
List<OdsVdqwMessageOTD> messageList = tmOdsVdqwMessagearchivingMapper.queryOdsVdqwMessageByData(statTime, endTime, offset, pageSize, retry);
// 处理查询到的数据
// 使用多线程并行执行两个业务场景
ExecutorService executorService = Executors.newFixedThreadPool(1);
log.info("待处理企微聊天列表messageList",messageList.toString());
// 使用 CompletableFuture 并行处理
messageList.forEach(item -> {
@@ -151,7 +149,7 @@ public class TmOdsVdqwMessagearchivingServiceImpl extends ServiceImpl<TmOdsVdqwM
log.error("处理企微语料失败: FromUserId={}, AcceptUserId={}, 异常: {}",
item.getFromUserId(), item.getAcceptUserId(), e.getMessage(), e);
}
}, executorService);
}, executor);
log.info("处理企微语料 调用完成: 结束时间:{}, 总用时: {}", System.currentTimeMillis(), System.currentTimeMillis() - startTime2);
@@ -235,61 +233,22 @@ public class TmOdsVdqwMessagearchivingServiceImpl extends ServiceImpl<TmOdsVdqwM
// 使用多线程并行执行两个业务场景
ExecutorService executorService = Executors.newFixedThreadPool(2);
log.info("第1个业务场景 优先执行。 ");
long startTime = System.currentTimeMillis();
CompletableFuture.runAsync(() -> {
// 获取配置
JSONObject execDifyFlow = diFyService.executeDifyFlow(diFyImageReq, BusinessTypeEnum.SMART_ASSISTANT_QIWEI.getCode(), JSONObject.toJSONString(corpusReportDTO),null);
log.info("runDify execDifyFlow , 企微场景总结需求的dify返回 {}", execDifyFlow);
if (null != execDifyFlow && execDifyFlow.get("status").equals("succeeded")) {
JSONObject text = execDifyFlow.getJSONObject("outputs");
// 发送MQ
log.info("send mq企微总结 {}", text);
tmTelephoneCorpusService.sendMq( CategoryEnum.ENTERPRISE_WECHAT.getCode(), text.toJSONString());
try {
ttVdqwRecordMapper.insert(TtVdqwRecord.builder().acceptUserId(item.getFromUserId()).fromUserId(item.getAcceptUserId()).msgTime(maxMsgTimeItem.getMsgTime()).from_accept_user_id(item.getFromAcceptUserId()).build());
aiAnalysisRequestLogsService.saveOrUpdateAiAnalysisRequestLogs(AiAnalysisRequestLogs.builder().aiAnalysisRequestId(execDifyFlow.getString("aiAnalysisRequestId")).businessResponse(text.toJSONString()).build());
} catch (Exception e) {
log.info(" 企业语料处理保存报告异常processItem{} ", e);
}
}
log.info("runDify execDifyFlow ,企微语料 ,总结和分类业务,返回: {}", execDifyFlow);
}, executorService);
long endTime1 = System.currentTimeMillis();
log.info("第一个业务场景(总结和分类)执行时间: {} ms", (endTime1 - startTime));
//第一个业务场景, 结束
CompletableFuture<Void> summaryTask = CompletableFuture.runAsync(() -> {
executeSummaryTask(diFyImageReq, corpusReportDTO, item, maxMsgTimeItem);
}, executor);
long startTime2 = System.currentTimeMillis();
CompletableFuture.runAsync(() -> {
//"analysisScene": "分析类型", // 1、企微会话 2、AI通话录音 3、AI铭牌(客流) 4、AI铭牌(试驾)
DiFyReq diFyImageReq2 = new DiFyReq();
diFyImageReq2.setUser(ConstantStr.corpus_user);
Map<String, Object> inputMap2 = new HashMap<>();
inputMap2.put("businessId", unionId);
inputMap2.put("communicateDate", DateUtil.format(maxMsgTimeItem.getMsgTime(), DatePattern.NORM_DATETIME_PATTERN));
inputMap2.put("analysisScene", "1");
inputMap2.put("version", 2);
inputMap2.put("chat", chatList.toString());
diFyImageReq2.setInputs(inputMap2);
// 创建新的DiFyReq对象以避免线程安全问题
diFyImageReq2.setFlowId(oneTokenPortrait);
log.info("runDify execDifyFlow ,客户画像场景 token-{} , 对象: {}", oneTokenPortrait, diFyImageReq2);
JSONObject execDifyFlowForPortrait = diFyService.executeDifyFlow(diFyImageReq2, BusinessTypeEnum.CORPUS_PORTRAIT_NAMEPLATE.getCode(),
JSONObject.toJSONString(corpusReportDTO), null);
log.info("runDify execDifyFlow ,客户画像场景 ,返回-{} ", execDifyFlowForPortrait);
CompletableFuture<Void> portraitTask = CompletableFuture.runAsync(() -> {
executePortraitTask(unionId, maxMsgTimeItem, chatList, corpusReportDTO);
}, executor);
JSONObject text = execDifyFlowForPortrait.getJSONObject("outputs");
// 发送MQ
log.info("send mq企微客户画像 {}", text);
tmTelephoneCorpusService.sendMq( CategoryEnum.ENTERPRISE_WECHAT.getCode(), text.toJSONString());
// 等待两个任务完成
CompletableFuture.allOf(summaryTask, portraitTask).join();
aiAnalysisRequestLogsService.saveOrUpdateAiAnalysisRequestLogs(AiAnalysisRequestLogs.builder().aiAnalysisRequestId(execDifyFlowForPortrait.getString("aiAnalysisRequestId")).businessResponse(text.toJSONString()).build());
log.info("runDify execDifyFlow ,企微语料 ,客户画像场景,返回: {}", execDifyFlowForPortrait);
}, executorService);
long endTime1 = System.currentTimeMillis();
log.info("第一个业务场景(总结和分类)执行时间: {} ms", (endTime1 - startTime));
long endTime2 = System.currentTimeMillis();
log.info("第二个业务场景(用户画像)执行时间: {} ms", (endTime2 - startTime2));
@@ -318,5 +277,67 @@ public class TmOdsVdqwMessagearchivingServiceImpl extends ServiceImpl<TmOdsVdqwM
return null;
}
/**
* 执行总结任务
*/
private void executeSummaryTask(DiFyReq diFyImageReq, CorpusReportDTO corpusReportDTO, OdsVdqwMessageOTD item, OdsVdqwMessageOTD maxMsgTimeItem) {
try {
// 获取配置
JSONObject execDifyFlow = diFyService.executeDifyFlow(diFyImageReq, BusinessTypeEnum.SMART_ASSISTANT_QIWEI.getCode(), JSONObject.toJSONString(corpusReportDTO),null);
log.info("runDify execDifyFlow , 企微场景总结需求的dify返回 {}", execDifyFlow);
if (null != execDifyFlow && execDifyFlow.get("status").equals("succeeded")) {
JSONObject text = execDifyFlow.getJSONObject("outputs");
// 发送MQ
log.info("send mq企微总结 {}", text);
tmTelephoneCorpusService.sendMq( CategoryEnum.ENTERPRISE_WECHAT.getCode(), text.toJSONString());
try {
ttVdqwRecordMapper.insert(TtVdqwRecord.builder().acceptUserId(item.getFromUserId()).fromUserId(item.getAcceptUserId()).msgTime(maxMsgTimeItem.getMsgTime()).from_accept_user_id(item.getFromAcceptUserId()).build());
aiAnalysisRequestLogsService.saveOrUpdateAiAnalysisRequestLogs(AiAnalysisRequestLogs.builder().aiAnalysisRequestId(execDifyFlow.getString("aiAnalysisRequestId")).businessResponse(text.toJSONString()).build());
} catch (Exception e) {
log.info(" 企业语料处理保存报告异常processItem{} ", e);
}
}
log.info("runDify execDifyFlow ,企微语料 ,总结和分类业务,返回: {}", execDifyFlow);
} catch (Exception e) {
log.error("执行总结任务异常", e);
}
}
/**
* 执行客户画像任务
*/
private void executePortraitTask(String unionId, OdsVdqwMessageOTD maxMsgTimeItem, StringBuffer chatList, CorpusReportDTO corpusReportDTO) {
try {
//"analysisScene": "分析类型", // 1、企微会话 2、AI通话录音 3、AI铭牌(客流) 4、AI铭牌(试驾)
DiFyReq diFyImageReq2 = new DiFyReq();
diFyImageReq2.setUser(ConstantStr.corpus_user);
Map<String, Object> inputMap2 = new HashMap<>();
inputMap2.put("businessId", unionId);
inputMap2.put("communicateDate", DateUtil.format(maxMsgTimeItem.getMsgTime(), DatePattern.NORM_DATETIME_PATTERN));
inputMap2.put("analysisScene", "1");
inputMap2.put("version", 2);
inputMap2.put("chat", chatList.toString());
diFyImageReq2.setInputs(inputMap2);
// 创建新的DiFyReq对象以避免线程安全问题
diFyImageReq2.setFlowId(oneTokenPortrait);
log.info("runDify execDifyFlow ,客户画像场景 token-{} , 对象: {}", oneTokenPortrait, diFyImageReq2);
JSONObject execDifyFlowForPortrait = diFyService.executeDifyFlow(diFyImageReq2, BusinessTypeEnum.CORPUS_PORTRAIT_NAMEPLATE.getCode(),
JSONObject.toJSONString(corpusReportDTO), null);
log.info("runDify execDifyFlow ,客户画像场景 ,返回-{} ", execDifyFlowForPortrait);
JSONObject text = execDifyFlowForPortrait.getJSONObject("outputs");
// 发送MQ
log.info("send mq企微客户画像 {}", text);
tmTelephoneCorpusService.sendMq( CategoryEnum.ENTERPRISE_WECHAT.getCode(), text.toJSONString());
aiAnalysisRequestLogsService.saveOrUpdateAiAnalysisRequestLogs(AiAnalysisRequestLogs.builder().aiAnalysisRequestId(execDifyFlowForPortrait.getString("aiAnalysisRequestId")).businessResponse(text.toJSONString()).build());
log.info("runDify execDifyFlow ,企微语料 ,客户画像场景,返回: {}", execDifyFlowForPortrait);
} catch (Exception e) {
log.error("执行客户画像任务异常", e);
}
}
}