Commit ddc2c93d by 林永健

refactor(pe-analysis): 优化数据报告AI文本摘要服务

- 引入ObjectUtil工具类处理对象空值判断
- 添加最大重试次数配置支持,在查询处理中快照时过滤重试次数
- 提取重试逻辑为独立方法updateBrandSnapshotWithMaxTry统一处理
- 优化查询结果轮询逻辑,提前返回成功和处理中状态避免重复处理
- 重构响应数据转义处理逻辑到独立方法convertResponseDataEscape
- 修复任务查询异常时的错误日志输出
- 更新成功判断逻辑同时校验返回码和任务完成状态
1 parent 3abb0836
......@@ -3,6 +3,7 @@ package cn.com.glsx.dj.peanalysis.service;
import cn.com.glsx.dj.peanalysis.config.DateReportAiTextSummaryConfig;
import cn.com.glsx.dj.peanalysis.mapper.DataReportBrandSnapshotMapper;
import cn.com.glsx.dj.peanalysis.model.DataReportBrandSnapshot;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
......@@ -79,11 +80,16 @@ public class DataReportAiTextSummaryService {
public List<DataReportBrandSnapshot> findProcessingSnapshots() {
try {
// 构建查询条件:summary_task_status = 1 (处理中)
Integer maxTryCnt = environment.getProperty("dj.pe.analysis.data.report.task.max.try", Integer.class, 5);
Example example = new Example(DataReportBrandSnapshot.class);
example.createCriteria()
.andEqualTo("summaryTaskStatus", 1)
.andIsNotNull("summaryTaskId")
.andEqualTo("deletedFlag", "N");
Example.Criteria subCriteria = example.createCriteria()
.orIsNull("maxTry")
.orLessThanOrEqualTo("maxTry", maxTryCnt);
example.and(subCriteria);
List<DataReportBrandSnapshot> processingList = brandSnapshotMapper.selectByExample(example);
log.info("查询到处理中的快照数量:{}", processingList != null ? processingList.size() : 0);
......@@ -120,21 +126,13 @@ public class DataReportAiTextSummaryService {
snapshot.setSummaryTaskId(taskId);
snapshot.setSummaryTaskStatus(1);
successCount++;
snapshot.setUpdatedDate(new Date());
brandSnapshotMapper.updateByPrimaryKeySelective(snapshot);
log.info("快照提交成功,id: {}, taskId: {}", snapshot.getId(), taskId);
} else {
Integer maxTryCnt = environment.getProperty("dj.pe.analysis.data.report.task.max.try", Integer.class, 5);
int currentMaxTry = snapshot.getMaxTry() + 1;
if (currentMaxTry > maxTryCnt) {
snapshot.setSummaryTaskStatus(3);
log.error("快照提交失败,达到最大尝试次数,id: {}", snapshot.getId());
} else {
snapshot.setMaxTry(currentMaxTry);
log.error("快照提交失败,重试中,id: {}, 重试次数: {}", snapshot.getId(), snapshot.getMaxTry());
}
log.error("获取 taskId 失败,snapshotId: {}", snapshot.getId());
updateBrandSnapshotWithMaxTry(snapshot);
}
snapshot.setUpdatedDate(new Date());
brandSnapshotMapper.updateByPrimaryKeySelective(snapshot);
} catch (Exception e) {
log.error("提交快照失败,id: {}, error: {}", snapshot.getId(), e.getMessage(), e);
}
......@@ -145,6 +143,23 @@ public class DataReportAiTextSummaryService {
}
/**
* 更新品牌数据快照,并维护最大重试次数
*/
public void updateBrandSnapshotWithMaxTry(DataReportBrandSnapshot snapshot) {
Integer maxTryCnt = environment.getProperty("dj.pe.analysis.data.report.task.max.try", Integer.class, 5);
int currentMaxTry = ObjectUtil.defaultIfNull(snapshot.getMaxTry(), 0) + 1;
if (currentMaxTry > maxTryCnt) {
snapshot.setSummaryTaskStatus(3);
log.error("快照提交失败,达到最大尝试次数,id: {}, config:{}", snapshot.getId(), maxTryCnt);
} else {
snapshot.setMaxTry(currentMaxTry);
log.error("快照提交失败,稍后重试,id: {}, 重试次数: {}", snapshot.getId(), snapshot.getMaxTry());
}
snapshot.setUpdatedDate(new Date());
brandSnapshotMapper.updateByPrimaryKeySelective(snapshot);
}
/**
* 轮询处理中的快照数据,查询 AI 任务结果
*
* @param processingSnapshots 处理中的快照列表
......@@ -171,27 +186,30 @@ public class DataReportAiTextSummaryService {
// 调用二方接口查询结果
QueryResult result = callQueryResult(queryParams);
if (result != null && result.isSuccess()) {
// 更新快照:保存结论和建议,状态改为成功
snapshot.setOverallConclusion(result.getConclusion());
snapshot.setActionSuggestion(result.getAction());
snapshot.setSummaryTaskStatus(2); // 成功
snapshot.setUpdatedDate(new Date());
successCount++;
brandSnapshotMapper.updateByPrimaryKeySelective(snapshot);
log.info("快照结果查询成功,id: {}, taskId: {}", snapshot.getId(), snapshot.getSummaryTaskId());
} else if (result != null && result.isProcessing()) {
// 任务仍在处理中,不更新状态
log.debug("任务仍在处理中,id: {}, taskId: {}", snapshot.getId(), snapshot.getSummaryTaskId());
} else {
// 任务失败,更新状态为失败
snapshot.setSummaryTaskStatus(3);
brandSnapshotMapper.updateByPrimaryKeySelective(snapshot);
log.error("任务查询失败,id: {}, taskId: {}", snapshot.getId(), snapshot.getSummaryTaskId());
if (result != null) {
if (result.isSuccess()) {
// 更新快照:保存结论和建议,状态改为成功
snapshot.setOverallConclusion(result.getConclusion());
snapshot.setActionSuggestion(result.getAction());
snapshot.setSummaryTaskStatus(2); // 成功
snapshot.setUpdatedDate(new Date());
successCount++;
brandSnapshotMapper.updateByPrimaryKeySelective(snapshot);
log.info("快照结果查询成功,id: {}, taskId: {}", snapshot.getId(), snapshot.getSummaryTaskId());
return successCount;
}
if (result.isProcessing()) {
// 任务仍在处理中,不更新状态
log.debug("任务仍在处理中,id: {}, taskId: {}", snapshot.getId(), snapshot.getSummaryTaskId());
return successCount;
}
}
} catch (Exception e) {
log.error("轮询快照结果失败,id: {}, error: {}", snapshot.getId(), e.getMessage(), e);
log.error("轮询快照结果发送异常,id: {}, error: {}", snapshot.getId(), e.getMessage(), e);
}
// 任务失败,更新状态为失败
updateBrandSnapshotWithMaxTry(snapshot);
log.error("任务查询失败,id: {}, taskId: {}", snapshot.getId(), snapshot.getSummaryTaskId());
}
log.info("批量轮询快照完成,总数:{}, 成功:{}", processingSnapshots.size(), successCount);
......@@ -272,16 +290,12 @@ public class DataReportAiTextSummaryService {
result.setCode(outer.getInteger("code"));
result.setTaskFinished(outer.getBoolean("taskFinished"));
result.setSuccess(outer.getBoolean("success"));
// 处理双层转义
String dataStr = outer.getString("data");
if (dataStr.startsWith("\"") && dataStr.endsWith("\"")) {
dataStr = dataStr.substring(1, dataStr.length() - 1);
// 处理转义字符
dataStr = dataStr.replace("\\\"", "\"")
.replace("\\\\", "\\")
.replace("\\n", "\n")
.replace("\\r", "\r");
log.debug("响应结果, result:{}", result);
if (!result.isSuccess()) {
return result;
}
// 处理双层转义
String dataStr = convertResponseDataEscape(outer.getString("data"));
JSONObject innerData = JSON.parseObject(dataStr);
String conclusion = innerData.getString("conclusion");
String action = innerData.getString("action");
......@@ -289,8 +303,7 @@ public class DataReportAiTextSummaryService {
result.setAction(action);
return result;
} else {
log.error("调用查询结果接口失败,status: {}, body: {}",
response.getStatusCode(), response.getBody());
log.error("调用查询结果接口失败,status: {}, body: {}", response.getStatusCode(), response.getBody());
}
} catch (Exception e) {
log.error("调用查询结果接口异常", e);
......@@ -298,6 +311,27 @@ public class DataReportAiTextSummaryService {
return null;
}
private String convertResponseDataEscape(String dataStr) {
try {
if (StringUtils.isBlank(dataStr)) {
log.warn("data数据为空");
return null;
}
if (dataStr.startsWith("\"") && dataStr.endsWith("\"")) {
dataStr = dataStr.substring(1, dataStr.length() - 1);
// 处理转义字符
dataStr = dataStr.replace("\\\"", "\"")
.replace("\\\\", "\\")
.replace("\\n", "\n")
.replace("\\r", "\r");
}
return dataStr;
} catch (Exception e) {
log.error("处理响应data数据异常, data:{}", dataStr, e);
}
return null;
}
/**
* 内部类:查询结果封装
*/
......@@ -310,7 +344,7 @@ public class DataReportAiTextSummaryService {
private Boolean success;
public boolean isSuccess() {
return taskFinished != null && taskFinished;
return Objects.equals(code, 200) && taskFinished != null && taskFinished;
}
public boolean isProcessing() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!