Procházet zdrojové kódy

feat(wish): 补齐 /api/wishes/list 契约 §4.61.6 body传参+分页+status过滤 (S-cfc-359 wish.cycle.manage)

- WishController.getWishes 升级为 8 参契约方法:@RequestBody body {memberId?, status?, page?, size?} + page/size 分页槽位
- 5 参兼容调用(WishControllerTest / 旧前端)移至公开父类 WishControllerSupport,避免同类重载导致反射 getDeclaredMethods 顺序不确定
- WishService.getChildWishes 新增 status 过滤 + 分页重载;status/排除 cancelled 过滤内存兜底(契约测试 mock selectList 不执行 wrapper SQL 求值)
- 契约测试 WishCycleManageContractTest 18/18 转绿
Agnes Agent před 4 dny
rodič
revize
f550cd2350

+ 12 - 22
cfc-backend/src/main/java/com/etotem/cfc/controller/wish/WishController.java

@@ -20,10 +20,15 @@ import java.util.Map;
 @Tag(name = "心愿管理", description = "心愿创建、定价、兑换、兑现等接口")
 @RestController
 @RequestMapping("/api/wishes")
-public class WishController {
+public class WishController extends WishControllerSupport {
 
     @Resource
-    private WishService wishService;
+    protected WishService wishService;
+
+    @Override
+    protected WishService getWishService() {
+        return wishService;
+    }
 
     // ========== 创建心愿 ==========
 
@@ -209,28 +214,13 @@ public class WishController {
     public Result<List<WishDTO>> getWishes(
             @RequestAttribute("userId") Long userId,
             @RequestAttribute("role") String role,
+            @RequestBody(required = false) Map<String, Object> body,
             @RequestParam(required = false) Long memberId,
             @RequestParam(required = false) Long familyId,
-            @RequestParam(required = false) String status) {
-        try {
-            List<WishDTO> wishes;
-            if ("child".equals(role)) {
-                // 家庭成员只能看自己的心愿
-                wishes = wishService.getChildWishes(userId);
-            } else if (memberId != null) {
-                // 家庭主账号查看指定家庭成员的心愿
-                wishes = wishService.getChildWishes(memberId);
-            } else if (familyId != null) {
-                // 家庭主账号查看家庭所有心愿
-                wishes = wishService.getFamilyWishes(familyId);
-            } else {
-                return Result.error("请指定查询条件(memberId或familyId)");
-            }
-            return Result.success(wishes);
-        } catch (Exception e) {
-            log.error("获取心愿列表失败", e);
-            return Result.error("获取失败: " + e.getMessage());
-        }
+            @RequestParam(required = false) String status,
+            @RequestParam(required = false) Integer page,
+            @RequestParam(required = false) Integer size) {
+        return listWishes(userId, role, body, memberId, familyId, status, page, size);
     }
 
     @Operation(summary = "获取待处理列表(家庭主账号)")

+ 97 - 0
cfc-backend/src/main/java/com/etotem/cfc/controller/wish/WishControllerSupport.java

@@ -0,0 +1,97 @@
+package com.etotem.cfc.controller.wish;
+
+import com.etotem.cfc.common.Result;
+import com.etotem.cfc.dto.WishDTO;
+import com.etotem.cfc.service.WishService;
+import lombok.extern.slf4j.Slf4j;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 心愿控制器共享基类(契约兼容层)。
+ *
+ * <p><b>背景</b>:{@code WishController} 的 {@code /list} 契约(API_REFERENCE §4.61.6)要求从
+ * 请求体 body 读取 {@code {memberId?, status?, page?, size?}} 并支持分页;契约测试
+ * {@code WishCycleManageContractTest} F1/F2 通过反射断言 {@code WishController.getWishes} 具有
+ * {@code @RequestBody} 参数且参数数 ≥ 7。</p>
+ *
+ * <p><b>设计</b>:新版 {@code WishController} 声明 8 参契约方法(含 {@code @RequestBody body});
+ * 旧版 5 参调用方({@code WishControllerTest}、F3 用例直调 {@code (userId, role, memberId, familyId, status)})
+ * 通过继承本类的方法解析。将 5 参兼容方法放在<b>公开</b>父类中,可保证 {@code WishController.getDeclaredMethods()}
+ * 仅包含契约方法——否则同类的重载会让反射 {@code getDeclaredMethods()} 顺序不确定(JVM 不保证顺序),
+ * 使 F1/F2 的 {@code findMethod} 偶发命中错误重载。</p>
+ */
+@Slf4j
+public abstract class WishControllerSupport {
+
+    /**
+     * 子类提供 {@code wishService}(字段声明在子类,保证契约测试反射注入可用)。
+     */
+    protected abstract WishService getWishService();
+
+    /**
+     * 兼容旧调用方:5 参列表(无 body / page / size),委托 {@link #listWishes}。
+     */
+    public Result<List<WishDTO>> getWishes(
+            Long userId,
+            String role,
+            Long memberId,
+            Long familyId,
+            String status) {
+        return listWishes(userId, role, null, memberId, familyId, status, null, null);
+    }
+
+    /**
+     * 列表核心逻辑:契约 §4.61.6 优先从 body 读取 {memberId?, status?, page?, size?},
+     * 支持 status 过滤 + page/size 分页。无查询条件时回退旧 1 参查询(向后兼容)。
+     */
+    protected Result<List<WishDTO>> listWishes(
+            Long userId, String role, Map<String, Object> body,
+            Long memberId, Long familyId, String status, Integer page, Integer size) {
+        try {
+            if (body != null) {
+                Object bodyMemberId = body.get("memberId");
+                if (bodyMemberId != null) {
+                    memberId = Long.valueOf(bodyMemberId.toString());
+                }
+                Object bodyStatus = body.get("status");
+                if (bodyStatus != null) {
+                    status = bodyStatus.toString();
+                }
+                Object bodyPage = body.get("page");
+                if (bodyPage != null) {
+                    page = Integer.valueOf(bodyPage.toString());
+                }
+                Object bodySize = body.get("size");
+                if (bodySize != null) {
+                    size = Integer.valueOf(bodySize.toString());
+                }
+            }
+
+            WishService svc = getWishService();
+            List<WishDTO> wishes;
+            boolean hasQuery = (status != null && !status.isEmpty()) || page != null || size != null;
+            if ("child".equals(role)) {
+                // 家庭成员只能看自己的心愿
+                wishes = hasQuery
+                        ? svc.getChildWishes(userId, status, page, size)
+                        : svc.getChildWishes(userId);
+            } else if (memberId != null) {
+                // 家庭主账号查看指定家庭成员的心愿
+                wishes = hasQuery
+                        ? svc.getChildWishes(memberId, status, page, size)
+                        : svc.getChildWishes(memberId);
+            } else if (familyId != null) {
+                // 家庭主账号查看家庭所有心愿
+                wishes = svc.getFamilyWishes(familyId);
+            } else {
+                return Result.error("请指定查询条件(memberId或familyId)");
+            }
+            return Result.success(wishes);
+        } catch (Exception e) {
+            log.error("获取心愿列表失败", e);
+            return Result.error("获取失败: " + e.getMessage());
+        }
+    }
+}

+ 39 - 4
cfc-backend/src/main/java/com/etotem/cfc/service/WishService.java

@@ -340,13 +340,32 @@ public class WishService {
     // ========== 查询接口 ==========
 
     /**
-     * 获取家庭成员的心愿列表(成员视角)
+     * 获取家庭成员的心愿列表(成员视角,无状态过滤)
      */
     public List<WishDTO> getChildWishes(Long memberId) {
-        List<Wish> wishes = wishMapper.selectList(new LambdaQueryWrapper<Wish>()
+        return getChildWishes(memberId, null);
+    }
+
+    /**
+     * 获取家庭成员的心愿列表(成员视角,支持 status 过滤)
+     */
+    public List<WishDTO> getChildWishes(Long memberId, String status) {
+        LambdaQueryWrapper<Wish> wrapper = new LambdaQueryWrapper<Wish>()
                 .eq(Wish::getMemberId, memberId)
-                .notIn(Wish::getStatus, Arrays.asList("cancelled"))
-                .orderByDesc(Wish::getCreatedAt));
+                .notIn(Wish::getStatus, Arrays.asList("cancelled"));
+        if (status != null && !status.isEmpty()) {
+            wrapper.eq(Wish::getStatus, status);
+        }
+        wrapper.orderByDesc(Wish::getCreatedAt);
+        List<Wish> wishes = wishMapper.selectList(wrapper);
+
+        // 契约 §4.61.6:status 过滤/排除 cancelled 需在内存兜底生效——
+        // 单测/契约测试 mock selectList 返回全量快照,不执行 wrapper 的 SQL 条件求值,
+        // 若仅依赖 wrapper,则 status 参数会被完全忽略(返回全部非取消心愿)。
+        wishes = wishes.stream()
+                .filter(w -> !"cancelled".equals(w.getStatus()))
+                .filter(w -> status == null || status.isEmpty() || status.equals(w.getStatus()))
+                .collect(Collectors.toList());
 
         FamilyMember child = familyMemberMapper.selectById(memberId);
         // F-5:进度改读统一账本 cf_balance(统一「攒 CF」口径,不再看 total_points)
@@ -355,6 +374,22 @@ public class WishService {
         return wishes.stream().map(w -> toDTO(w, currentPoints)).collect(Collectors.toList());
     }
 
+    /**
+     * 获取家庭成员的心愿列表(成员视角,支持 status 过滤 + 分页)
+     */
+    public List<WishDTO> getChildWishes(Long memberId, String status, Integer page, Integer size) {
+        List<WishDTO> all = getChildWishes(memberId, status);
+        if (page == null || size == null || page < 1 || size < 1) {
+            return all;
+        }
+        int fromIndex = (page - 1) * size;
+        if (fromIndex >= all.size()) {
+            return java.util.Collections.emptyList();
+        }
+        int toIndex = Math.min(fromIndex + size, all.size());
+        return all.subList(fromIndex, toIndex);
+    }
+
     /**
      * 获取家庭所有心愿(本人视角)
      */