Selaa lähdekoodia

fix: 关系分析后端 — 安全类型转换 + 出生日期空值保护

Ultraworked with Sisyphus

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
liaoxg 3 kuukautta sitten
vanhempi
sitoutus
f348b1fa09

+ 10 - 4
num-server/src/main/java/com/etotem/num/controller/RelationAnalysisController.java

@@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.servlet.http.HttpServletRequest;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -51,10 +52,15 @@ public class RelationAnalysisController {
                 return Result.error(1001, "请先登录");
             }
 
-            @SuppressWarnings("unchecked")
-            List<Long> relationIds = body.get("relationIds") != null
-                    ? (List<Long>) body.get("relationIds")
-                    : java.util.Collections.emptyList();
+            List<Long> relationIds = new ArrayList<>();
+            Object rawIds = body.get("relationIds");
+            if (rawIds instanceof List) {
+                for (Object item : (List<?>) rawIds) {
+                    if (item instanceof Number) {
+                        relationIds.add(((Number) item).longValue());
+                    }
+                }
+            }
             String question = (String) body.get("question");
 
             RelationAnalysisRecord record = analysisService.startAnalysis(userId, relationIds, question);

+ 19 - 9
num-server/src/main/java/com/etotem/num/service/RelationAnalysisService.java

@@ -80,10 +80,16 @@ public class RelationAnalysisService {
         self.addProperty("birthDay", user.getBirthDay());
         self.addProperty("gender", user.getGender() != null ? user.getGender() : 0);
 
-        // Calculate user's chart positions
-        Map<String, Object> userChart = calculatorService.calculateFullTriangle(
-                user.getBirthYear(), user.getBirthMonth(), user.getBirthDay());
-        self.addProperty("chartPositions", gson.toJson(userChart));
+        // Calculate user's chart positions (birth fields may be null)
+        if (user.getBirthYear() != null && user.getBirthMonth() != null && user.getBirthDay() != null) {
+            try {
+                Map<String, Object> userChart = calculatorService.calculateFullTriangle(
+                        user.getBirthYear(), user.getBirthMonth(), user.getBirthDay());
+                self.addProperty("chartPositions", gson.toJson(userChart));
+            } catch (Exception e) {
+                log.warn("Failed to calculate user chart positions: {}", e.getMessage());
+            }
+        }
         members.add(self);
 
         // Add selected relations
@@ -105,11 +111,15 @@ public class RelationAnalysisService {
 
             // Calculate relation's chart positions
             if (rel.getBirthDate() != null) {
-                Map<String, Object> relChart = calculatorService.calculateFullTriangle(
-                        rel.getBirthDate().getYear(),
-                        rel.getBirthDate().getMonthValue(),
-                        rel.getBirthDate().getDayOfMonth());
-                member.addProperty("chartPositions", gson.toJson(relChart));
+                try {
+                    Map<String, Object> relChart = calculatorService.calculateFullTriangle(
+                            rel.getBirthDate().getYear(),
+                            rel.getBirthDate().getMonthValue(),
+                            rel.getBirthDate().getDayOfMonth());
+                    member.addProperty("chartPositions", gson.toJson(relChart));
+                } catch (Exception e) {
+                    log.warn("Failed to calculate chart for relation {}: {}", rel.getId(), e.getMessage());
+                }
             }
             members.add(member);
         }