|
|
@@ -35,7 +35,7 @@ import com.etotem.cfc.util.SortUtil;
|
|
|
* 对应书稿《终章-绘制你家的幸福全景图》15题自检问卷
|
|
|
*
|
|
|
* 计分:A=3分,B=2分,C=1分,D=0分,每维度 3 题满分 9 分
|
|
|
- * 结果解读:≥7 健康 / 4-6 留意 / ≤3 紧绷(紧绷维度触发五行相生寻源建议)
|
|
|
+ * 结果解读:≥7 健康 / 4-6 留意 / ≤3 紧绷(紧绷维度触发 AI 建议)
|
|
|
*/
|
|
|
@Service
|
|
|
public class FiveDimensionSelfCheckService {
|
|
|
@@ -57,6 +57,9 @@ public class FiveDimensionSelfCheckService {
|
|
|
@Resource
|
|
|
private SelfCheckIgnoreMapper ignoreMapper;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private SelfCheckAnalysisService selfCheckAnalysisService;
|
|
|
+
|
|
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
|
/** 维度中文名与五行 */
|
|
|
@@ -467,12 +470,18 @@ public class FiveDimensionSelfCheckService {
|
|
|
}
|
|
|
Collections.sort(submittedQuestionIds);
|
|
|
|
|
|
- // 生成寻源建议(仅 ≤3 紧绷维度)
|
|
|
+ // 生成 AI 建议(AI 优先,失败降级为空建议)
|
|
|
Map<String, Integer> scoreMap = new LinkedHashMap<>();
|
|
|
for (SelfCheckResultVO.DimensionScoreVO dvo : dimensions) {
|
|
|
scoreMap.put(dvo.getDimension(), dvo.getScore());
|
|
|
}
|
|
|
- List<WuxingSourcingAdviceVO> advices = wuxingSourcingService.getAdvicesForLowScores(scoreMap);
|
|
|
+ String adviceJson = null;
|
|
|
+ try {
|
|
|
+ Map<String, Object> adviceResult = selfCheckAnalysisService.generateAdvice(userId, scoreMap, submittedQuestionIds);
|
|
|
+ adviceJson = (String) adviceResult.get("adviceJson");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("AI 建议生成异常,使用空建议: {}", e.getMessage());
|
|
|
+ }
|
|
|
|
|
|
// 保存记录
|
|
|
FiveDimensionSelfCheck record = new FiveDimensionSelfCheck();
|
|
|
@@ -481,7 +490,7 @@ public class FiveDimensionSelfCheckService {
|
|
|
record.setAnswersJson(toJson(dto.getAnswers()));
|
|
|
record.setScoresJson(toJson(scoreMap));
|
|
|
record.setTotalScore(totalScore);
|
|
|
- record.setAdviceJson(toJson(advices));
|
|
|
+ record.setAdviceJson(adviceJson != null ? adviceJson : "[]");
|
|
|
record.setQuestionIdsJson(toJson(submittedQuestionIds));
|
|
|
record.setCreatedAt(new Date());
|
|
|
selfCheckMapper.insert(record);
|
|
|
@@ -499,7 +508,7 @@ public class FiveDimensionSelfCheckService {
|
|
|
vo.setFamilyMemberId(record.getFamilyMemberId());
|
|
|
vo.setDimensions(dimensions);
|
|
|
vo.setTotalScore(totalScore);
|
|
|
- vo.setAdvices(advices);
|
|
|
+ vo.setAdvices(parseAdvices(adviceJson));
|
|
|
vo.setCreatedAt(record.getCreatedAt());
|
|
|
return vo;
|
|
|
}
|
|
|
@@ -602,6 +611,18 @@ public class FiveDimensionSelfCheckService {
|
|
|
return ignore.getCreatedAt();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取自检趋势分析结果(含历史列表)
|
|
|
+ */
|
|
|
+ public Map<String, Object> getTrendAnalysis(Long userId) {
|
|
|
+ Map<String, Object> trend = selfCheckAnalysisService.generateTrend(userId);
|
|
|
+ Map<String, Object> resp = new HashMap<>();
|
|
|
+ resp.put("history", selfCheckAnalysisService.getRecentHistory(userId, 3));
|
|
|
+ resp.put("aiInsight", trend.getOrDefault("aiInsight", ""));
|
|
|
+ resp.put("trendSummary", trend.getOrDefault("trendSummary", ""));
|
|
|
+ return resp;
|
|
|
+ }
|
|
|
+
|
|
|
// === 内部方法 ===
|
|
|
|
|
|
private SelfCheckResultVO.DimensionScoreVO buildDimensionScoreVO(String dimension, int score) {
|
|
|
@@ -666,6 +687,19 @@ public class FiveDimensionSelfCheckService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private List<WuxingSourcingAdviceVO> parseAdvices(String adviceJson) {
|
|
|
+ if (adviceJson == null || adviceJson.isEmpty() || "[]".equals(adviceJson)) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return objectMapper.readValue(adviceJson,
|
|
|
+ objectMapper.getTypeFactory().constructCollectionType(List.class, WuxingSourcingAdviceVO.class));
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("解析 AI 建议 JSON 失败: {}", e.getMessage());
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@SuppressWarnings("unchecked")
|
|
|
private SelfCheckResultVO toVO(FiveDimensionSelfCheck record) {
|
|
|
SelfCheckResultVO vo = new SelfCheckResultVO();
|
|
|
@@ -694,19 +728,7 @@ public class FiveDimensionSelfCheckService {
|
|
|
vo.setDimensions(dimensions);
|
|
|
|
|
|
// 解析寻源建议
|
|
|
- if (record.getAdviceJson() != null) {
|
|
|
- try {
|
|
|
- List<WuxingSourcingAdviceVO> advices = objectMapper.readValue(
|
|
|
- record.getAdviceJson(),
|
|
|
- objectMapper.getTypeFactory().constructCollectionType(List.class, WuxingSourcingAdviceVO.class));
|
|
|
- vo.setAdvices(advices);
|
|
|
- } catch (Exception e) {
|
|
|
- log.warn("解析adviceJson失败: {}", e.getMessage());
|
|
|
- vo.setAdvices(new ArrayList<>());
|
|
|
- }
|
|
|
- } else {
|
|
|
- vo.setAdvices(new ArrayList<>());
|
|
|
- }
|
|
|
+ vo.setAdvices(parseAdvices(record.getAdviceJson()));
|
|
|
return vo;
|
|
|
}
|
|
|
}
|