فهرست منبع

feat: FamilyContextService 新增 buildContextFromDraft 支持草稿上下文

iwt 6 روز پیش
والد
کامیت
c601cf9216
1فایلهای تغییر یافته به همراه41 افزوده شده و 1 حذف شده
  1. 41 1
      cfc-backend/src/main/java/com/etotem/cfc/service/FamilyContextService.java

+ 41 - 1
cfc-backend/src/main/java/com/etotem/cfc/service/FamilyContextService.java

@@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.etotem.cfc.dto.ChildInfoDTO;
 import com.etotem.cfc.entity.*;
 import com.etotem.cfc.mapper.*;
+import com.etotem.cfc.service.HealthReportDraftService;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
@@ -36,6 +38,12 @@ public class FamilyContextService {
     @Resource
     private ReportSurveyService reportSurveyService;
 
+    @Resource
+    private HealthReportDraftService healthReportDraftService;
+
+    @Resource
+    private ObjectMapper objectMapper;
+
     @Resource
     private FiveDimensionScoreService fiveDimensionScoreService;
 
@@ -222,8 +230,40 @@ public class FamilyContextService {
     }
 
     /**
-     * 获取家庭成员的最新五维能量分数
+     * 基于草稿 ID 构建上下文(用于快速分析)
+     *
+     * @param userId  当前用户
+     * @param draftId 草稿 ID
+     * @return 上下文 Map,包含 reportDetail(从 payloadJson 还原)
      */
+    public Map<String, Object> buildContextFromDraft(Long userId, Long draftId) {
+        Map<String, Object> ctx = buildContext(userId);
+
+        if (draftId == null) {
+            return ctx;
+        }
+
+        HealthReportDraft draft = healthReportDraftService.getDraftById(draftId);
+        if (draft == null || !draft.getUserId().equals(userId)) {
+            // 权限不通过静默返回基础上下文
+            return ctx;
+        }
+
+        // 解析 payloadJson
+        Map<String, Object> reportDetail = null;
+        try {
+            if (draft.getPayloadJson() != null && !draft.getPayloadJson().isEmpty()) {
+                reportDetail = objectMapper.readValue(draft.getPayloadJson(), Map.class);
+            }
+        } catch (Exception e) {
+            log.warn("解析草稿 payload 失败: {}", e.getMessage());
+        }
+        if (reportDetail != null) {
+            ctx.put("reportDetail", reportDetail);
+        }
+
+        return ctx;
+    }
     private Map<String, Integer> getDimensionScores(Long familyId) {
         try {
             return fiveDimensionScoreService.getLatestByFamilyMap(familyId);