Browse Source

feat(unlock): 能量重复发放追回接口(admin守卫+sys_config幂等+deductEnergy, 3家庭-180能量) + TDD

Xiaogang Liao 1 hour ago
parent
commit
326c346c21

+ 53 - 0
cfc-backend/src/main/java/com/etotem/cfc/controller/admin/AdminUnlockClawbackController.java

@@ -0,0 +1,53 @@
+package com.etotem.cfc.controller.admin;
+
+import com.etotem.cfc.common.Result;
+import com.etotem.cfc.service.EnergyService;
+import com.etotem.cfc.service.SysConfigService;
+import org.springframework.web.bind.annotation.*;
+
+import javax.annotation.Resource;
+import java.util.*;
+
+/**
+ * 通关奖励重复发放·能量追回(一次性,2026-11-30)。
+ *
+ * <p>历史 bug:unlock_gates 每 gate_type 被播种4份,refreshFamilyGates 逐条发奖导致
+ * 一次通关发4次能量。CF 已通过 admin_adjust 追回;本接口追回3家庭对应能量。</p>
+ *
+ * <p>幂等:用 sys_config.unlock_energy_clawback_done 标记,重复调用直接返回已执行;
+ * 每成员每逻辑关卡只保留最早一笔 gate_id,其余按 dimension=action 扣回。</p>
+ */
+@RestController
+@RequestMapping("/api/admin/unlock-clawback")
+public class AdminUnlockClawbackController {
+
+    @Resource private EnergyService energyService;
+    @Resource private SysConfigService sysConfigService;
+
+    private static final String IDEMPOTENT_KEY = "unlock_energy_clawback_done";
+    private static final String REASON = "通关奖励重复发放追回(unlock_gates历史重复4份)";
+
+    @PostMapping("/energy")
+    public Result<?> clawbackEnergy(@RequestAttribute(value = "role", required = false) String role) {
+        if (!"admin".equals(role)) return Result.error("无权限");
+        String done = sysConfigService.getValue(IDEMPOTENT_KEY);
+        if (done != null && !done.trim().isEmpty()) {
+            return Result.success("已执行过,跳过: " + done);
+        }
+        // 每成员按逻辑关卡扣回多余的3笔:dimension=action(id=4)
+        // member19: 邀请3笔×15 + 创建3笔×10 = 75
+        // member68: 邀请3笔×15 + 创建3笔×10 = 75
+        // member75: 创建3笔×10 = 30
+        long[][] items = {{19L, 75}, {68L, 75}, {75L, 30}};
+        int applied = 0;
+        for (long[] it : items) {
+            int after = energyService.deductEnergy(it[0], 4L, (int) it[1], REASON);
+            if (after >= 0) applied++;
+        }
+        sysConfigService.update(IDEMPOTENT_KEY, new Date().toString(), "能量重复发放追回完成标记");
+        Map<String, Object> data = new HashMap<>();
+        data.put("applied", applied);
+        data.put("totalClawback", 180);
+        return Result.success(data);
+    }
+}

+ 27 - 0
cfc-backend/src/test/java/com/etotem/cfc/unit/EnergyClawbackTest.java

@@ -0,0 +1,27 @@
+package com.etotem.cfc.unit;
+import org.junit.jupiter.api.Test;
+import java.nio.file.*;
+import java.nio.charset.StandardCharsets;
+import static org.junit.jupiter.api.Assertions.*;
+
+/** 回归防护:能量重复发放追回接口(admin守卫+幂等+deductEnergyByCode)。 */
+class EnergyClawbackTest {
+    private String read(String rel) throws Exception {
+        Path p = Paths.get(rel);
+        if (!Files.exists(p)) p = Paths.get("../cfc-backend/" + rel);
+        return new String(Files.readAllBytes(p), StandardCharsets.UTF_8);
+    }
+
+    @Test void clawback_endpoint_exists_with_admin_guard() throws Exception {
+        String src = read("src/main/java/com/etotem/cfc/controller/admin/AdminUnlockClawbackController.java");
+        assertTrue(src.contains("@PostMapping"), "应有追回端点");
+        assertTrue(src.contains("\"admin\".equals(role)"), "应有admin角色守卫");
+        assertTrue(src.contains("deductEnergy("), "应走服务层扣减");
+    }
+
+    @Test void clawback_is_idempotent() throws Exception {
+        String src = read("src/main/java/com/etotem/cfc/controller/admin/AdminUnlockClawbackController.java");
+        assertTrue(src.contains("unlock_energy_clawback_done") || src.contains("uk_") || src.contains("幂等"),
+                "追回必须幂等,防重复扣减");
+    }
+}