|
|
@@ -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());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|