定时任务处理舆情异常全部流程补偿

This commit is contained in:
lxu75
2025-03-14 17:48:04 +08:00
parent 4cf0751938
commit d5fdc77ced

View File

@@ -101,6 +101,69 @@ public class MqMessageRecordServiceImpl extends ServiceImpl<MqMessageRecordMappe
.anyMatch(contentNode -> NodeTypeEnum.IMAGE.getCode().equals(contentNode.getNodeType()));
//如果存在图片节点,则先处理图片节点
StringBuilder sb = new StringBuilder();
processingImageInfo(hasImageNodeType, communityTargetDTO, sb);
String textContent = Optional.ofNullable(communityTargetDTO.getContent())
.orElse(Collections.emptyList()).stream()
.filter(contentNode -> NodeTypeEnum.TEXT.getCode().equals(contentNode.getNodeType()))
.map(CommunityTargetDTO.ContentNode::getNodeContent)
.collect(Collectors.joining(" "));
//构建舆情分析请求对象
DifyCommunityTargetDTO difyCommunityTargetDTO = new DifyCommunityTargetDTO();
difyCommunityTargetDTO.setTargetId(communityTargetDTO.getTargetId());
difyCommunityTargetDTO.setTargetType(communityTargetDTO.getTargetType());
//脱敏处理
dataMasking(textContent, sb, difyCommunityTargetDTO);
//根据aiAnalysisRequestId更新请求日志表的difyRequest字段
syncUpdateDiFyRequest(difyCommunityTargetDTO, aiAnalysisRequestId);
DiFyReq diFyReq = new DiFyReq();
diFyReq.setUser(user);
diFyReq.setFlowId(flowId);
diFyReq.setInputs(difyCommunityTargetDTO);
//调用舆情文本分析dify工作流
difResult = (JSONObject) diFyService.getDiFyObject(diFyReq);
//处理结果
processingCommunityDifyResponse(difResult);
//异步更新请求日志表的difyResponse字段
syncUpdateDiFyResponse(difResult, aiAnalysisRequestId);
} catch (Exception e) {
log.error("舆情自动化异常:{}", e.getMessage());
//保存错误日志
aiAnalysisErrorsMapper.insert(AiAnalysisErrors.builder()
.aiAnalysisRequestId(aiAnalysisRequestId)
.difyResponse(difResult.toJSONString())
.aiAnalysisErrorMessage(e.getMessage())
.aiAnalysisRequestType(BusinessTypeEnum.COMMUNITYTARGET.getCode())
.build());
}
return true;
}
/**
* 舆情数据脱敏
* @param textContent
* @param sb
* @param difyCommunityTargetDTO
*/
private void dataMasking(String textContent, StringBuilder sb, DifyCommunityTargetDTO difyCommunityTargetDTO) {
List<DataMaskingRule> maskingRuleItems = dataMaskingRuleService.getDataMaskingRuleListByApplicationChannel(Constant.CHANNEL_DCC);
RunMaskingRuleInput runMaskingRuleInput = new RunMaskingRuleInput();
runMaskingRuleInput.setDataMaskingRules(maskingRuleItems);
runMaskingRuleInput.setOldStr(textContent + sb);
String corpusChat = dataMaskingRuleService.runMaskingRule(runMaskingRuleInput);
difyCommunityTargetDTO.setTargetContent(corpusChat);
}
/**
* 处理图片信息
* @param hasImageNodeType
* @param communityTargetDTO
* @param sb
*/
private void processingImageInfo(boolean hasImageNodeType, CommunityTargetDTO communityTargetDTO, StringBuilder sb) {
if (hasImageNodeType) {
communityTargetDTO.getContent().forEach(contentNode -> {
if (contentNode.getNodeType().equals(NodeTypeEnum.IMAGE.getCode())) {
@@ -121,55 +184,13 @@ public class MqMessageRecordServiceImpl extends ServiceImpl<MqMessageRecordMappe
}
});
}
String textContent = Optional.ofNullable(communityTargetDTO.getContent())
.orElse(Collections.emptyList()).stream()
.filter(contentNode -> NodeTypeEnum.TEXT.getCode().equals(contentNode.getNodeType()))
.map(CommunityTargetDTO.ContentNode::getNodeContent)
.collect(Collectors.joining(" "));
//构建舆情分析请求对象
DifyCommunityTargetDTO difyCommunityTargetDTO = new DifyCommunityTargetDTO();
difyCommunityTargetDTO.setTargetId(communityTargetDTO.getTargetId());
difyCommunityTargetDTO.setTargetType(communityTargetDTO.getTargetType());
//脱敏处理
List<DataMaskingRule> maskingRuleItems = dataMaskingRuleService.getDataMaskingRuleListByApplicationChannel(Constant.CHANNEL_DCC);
RunMaskingRuleInput runMaskingRuleInput = new RunMaskingRuleInput();
runMaskingRuleInput.setDataMaskingRules(maskingRuleItems);
runMaskingRuleInput.setOldStr(textContent + sb);
String corpusChat = dataMaskingRuleService.runMaskingRule(runMaskingRuleInput);
difyCommunityTargetDTO.setTargetContent(corpusChat);
//根据aiAnalysisRequestId更新请求日志表的difyRequest字段
syncUpdateDiFyRequest(difyCommunityTargetDTO, aiAnalysisRequestId);
DiFyReq diFyReq = new DiFyReq();
diFyReq.setUser(user);
diFyReq.setFlowId(flowId);
diFyReq.setInputs(difyCommunityTargetDTO);
//调用舆情文本分析dify工作流
difResult = (JSONObject) diFyService.getDiFyObject(diFyReq);
//处理结果
dueCommunityDifyResponse(difResult);
//异步更新请求日志表的difyResponse字段
syncUpdateDiFyResponse(difResult, aiAnalysisRequestId);
} catch (Exception e) {
log.error("舆情自动化异常:{}", e.getMessage());
//保存错误日志
aiAnalysisErrorsMapper.insert(AiAnalysisErrors.builder()
.aiAnalysisRequestId(aiAnalysisRequestId)
.difyResponse(difResult.toJSONString())
.aiAnalysisErrorMessage(e.getMessage())
.aiAnalysisRequestType(BusinessTypeEnum.COMMUNITYTARGET.getCode())
.build());
}
return true;
}
/**
* 处理dify返回结果
* @param difResult
*/
private void dueCommunityDifyResponse(JSONObject difResult) {
private void processingCommunityDifyResponse(JSONObject difResult) {
if (difResult != null) {
DifyCommunityTargetResult difyCommunityTargetResult = new DifyCommunityTargetResult();
String targetId = difResult.getString("targetId");
@@ -549,14 +570,41 @@ public class MqMessageRecordServiceImpl extends ServiceImpl<MqMessageRecordMappe
if (aiAnalysisRequestLogs != null) {
//获取dify_request字段
String difyRequest = aiAnalysisRequestLogs.getDifyRequest();
String message = aiAnalysisRequestLogs.getBusinessRequest();
if (StringUtils.isNotEmpty(difyRequest)) {
//调用dify接口
CommunityTargetDTO communityTargetDTO = JSON.parseObject(message, CommunityTargetDTO.class);
boolean hasImageNodeType = communityTargetDTO.getContent().stream()
.anyMatch(contentNode -> NodeTypeEnum.IMAGE.getCode().equals(contentNode.getNodeType()));
//如果存在图片节点,则先处理图片节点
StringBuilder sb = new StringBuilder();
processingImageInfo(hasImageNodeType, communityTargetDTO, sb);
String textContent = Optional.ofNullable(communityTargetDTO.getContent())
.orElse(Collections.emptyList()).stream()
.filter(contentNode -> NodeTypeEnum.TEXT.getCode().equals(contentNode.getNodeType()))
.map(CommunityTargetDTO.ContentNode::getNodeContent)
.collect(Collectors.joining(" "));
//构建舆情分析请求对象
DifyCommunityTargetDTO difyCommunityTargetDTO = new DifyCommunityTargetDTO();
difyCommunityTargetDTO.setTargetId(communityTargetDTO.getTargetId());
difyCommunityTargetDTO.setTargetType(communityTargetDTO.getTargetType());
//脱敏处理
dataMasking(textContent, sb, difyCommunityTargetDTO);
//根据aiAnalysisRequestId更新请求日志表的difyRequest字段
syncUpdateDiFyRequest(difyCommunityTargetDTO, aiAnalysisError.getAiAnalysisRequestId());
DiFyReq diFyReq = new DiFyReq();
diFyReq.setUser(user);
diFyReq.setFlowId(flowId);
diFyReq.setInputs(JSONObject.parseObject(difyRequest));
diFyReq.setInputs(difyCommunityTargetDTO);
//调用舆情文本分析dify工作流
JSONObject difResult = (JSONObject) diFyService.getDiFyObject(diFyReq);
this.dueCommunityDifyResponse(difResult);
//处理结果
processingCommunityDifyResponse(difResult);
//根据ai_analysis_request_id更新ai_analysis_errors表中的retry_count字段+1,更新status字段为1
aiAnalysisErrorsMapper.update(new AiAnalysisErrors(), new LambdaUpdateWrapper<AiAnalysisErrors>()
.eq(AiAnalysisErrors::getAiAnalysisRequestId, aiAnalysisError.getAiAnalysisRequestId())