Parcourir la source

fix(cfc-backend): fix FamilyMemberService.addMember DTO compatibility for generationLevel/peerType

Commit 538224b replaced relationshipType with generationLevel+peerType in AddFamilyMemberDTO, but FamilyMemberService.addMember still called dto.getRelationshipType() causing 7 compilation errors.

Fix: map generationLevel/peerType to relationshipTypeKey for RelationshipType lookup, set generation/isSpouse on FamilyMember entity.
Sisyphus il y a 2 mois
Parent
commit
1cbefa53e2

+ 45 - 7
cfc-backend/src/main/java/com/etotem/cfc/service/FamilyMemberService.java

@@ -4,8 +4,17 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.etotem.cfc.dto.AddFamilyMemberDTO;
 import com.etotem.cfc.dto.FamilyMemberVO;
 import com.etotem.cfc.dto.SwitchMemberVO;
-import com.etotem.cfc.entity.*;
-import com.etotem.cfc.mapper.*;
+import com.etotem.cfc.entity.Family;
+import com.etotem.cfc.entity.FamilyMember;
+import com.etotem.cfc.entity.FamilyMemberAttributes;
+import com.etotem.cfc.entity.RelationshipType;
+import com.etotem.cfc.entity.User;
+import com.etotem.cfc.enums.GenerationLevel;
+import com.etotem.cfc.mapper.FamilyMapper;
+import com.etotem.cfc.mapper.FamilyMemberAttributesMapper;
+import com.etotem.cfc.mapper.FamilyMemberMapper;
+import com.etotem.cfc.mapper.RelationshipTypeMapper;
+import com.etotem.cfc.mapper.UserMapper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
@@ -52,8 +61,8 @@ public class FamilyMemberService {
         if (dto.getNickname() == null || dto.getNickname().trim().isEmpty()) {
             throw new IllegalArgumentException("成员昵称不能为空");
         }
-        if (dto.getRelationshipType() == null || dto.getRelationshipType().trim().isEmpty()) {
-            throw new IllegalArgumentException("关系类型不能为空");
+        if (dto.getGenerationLevel() == null || dto.getGenerationLevel().trim().isEmpty()) {
+            throw new IllegalArgumentException("辈分等级不能为空");
         }
 
         User user = userMapper.selectById(userId);
@@ -65,13 +74,40 @@ public class FamilyMemberService {
         }
         Long familyId = user.getFamilyId();
 
+        GenerationLevel genLevel = GenerationLevel.fromValue(dto.getGenerationLevel());
+        if (genLevel == null) {
+            throw new IllegalArgumentException("无效的辈分等级: " + dto.getGenerationLevel());
+        }
+        String relationshipTypeKey;
+        boolean isSpouse = false;
+        if (genLevel == GenerationLevel.PEER) {
+            if (dto.getPeerType() == null || dto.getPeerType().trim().isEmpty()) {
+                throw new IllegalArgumentException("同辈关系必须指定 peerType (sibling/spouse)");
+            }
+            switch (dto.getPeerType()) {
+                case "spouse":
+                    relationshipTypeKey = "spouse";
+                    isSpouse = true;
+                    break;
+                case "sibling":
+                    relationshipTypeKey = "sibling";
+                    break;
+                default:
+                    throw new IllegalArgumentException("无效的同辈类型: " + dto.getPeerType());
+            }
+        } else if (genLevel.getOffset() > 0) {
+            relationshipTypeKey = "parent";
+        } else {
+            relationshipTypeKey = "child";
+        }
+
         // 验证关系类型是否存在
         RelationshipType relType = relationshipTypeMapper.selectOne(
                 new LambdaQueryWrapper<RelationshipType>()
-                        .eq(RelationshipType::getTypeKey, dto.getRelationshipType())
+                        .eq(RelationshipType::getTypeKey, relationshipTypeKey)
                         .eq(RelationshipType::getEnabled, 1));
         if (relType == null) {
-            throw new IllegalArgumentException("无效的关系类型: " + dto.getRelationshipType());
+            throw new IllegalArgumentException("无效的关系类型: " + relationshipTypeKey);
         }
 
         // 创建家庭成员记录
@@ -89,7 +125,9 @@ public class FamilyMemberService {
                 throw new IllegalArgumentException("出生日期格式错误,应为yyyy-MM-dd");
             }
         }
-        member.setRelationshipType(dto.getRelationshipType());
+        member.setRelationshipType(relationshipTypeKey);
+        member.setGeneration(genLevel.getOffset());
+        member.setIsSpouse(isSpouse);
         member.setRoleOverride("auto");
         member.setShowToFamily(1);
         member.setTotalPoints(0);