Bläddra i källkod

feat(activity): S-cfc-490 activity.enroll.join 契约宿主实现使绿

新增纯契约宿主 ActivityEnrollService:报名→占名额→确认、名额满拒(-1)、重复报名按 memberId 幂等、超卖守卫(max<=0 不限)、取消释放不为负。实例级状态 + synchronized 原子占位,与 ActivityCheckinBoostService 同风格。ActivityEnrollJoinContractTest 9/9 + BoundaryContractRedTest 6/6 全绿(原 14 红→0 红)。
Agnes Agent 1 dag sedan
förälder
incheckning
dcf872b5d2

+ 144 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/ActivityEnrollService.java

@@ -0,0 +1,144 @@
+package com.etotem.cfc.service;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * 活动报名服务(能力键 {@code activity.enroll.join},单元 activity)。
+ *
+ * <p><b>契约溯源</b>:{@code tests/user-scenarios/应用场景总览.md} SC-P-76「活动报名(名额/并发)⭐」
+ * ——主流程「报名 → 占名额 → 确认」;边界「<b>并发超卖拦截(CFC 真实 bug 已修);名额满拒;
+ * 重复报名幂等</b>」;SC-X-16「活动名额并发 E2E」同名额多端并发不超发。</p>
+ *
+ * <p><b>设计溯源</b>:{@code docs/superpowers/api/API_REFERENCE.md} §4.55.1
+ * ({@code POST /api/activity/register} 免审即 approved 并原子占名额)、§4.55.5
+ * (报名名册 approvedCount 直接读 activities.approved_count)、§4.65.6
+ * ({@code ActivityRegistrationService.occupyQuota} 条件自增
+ * {@code approved_count < max_participants},多端并发不超发;max 为空或 ≤0 视为不限;
+ * {@code releaseQuota} 原子回退且不小于 0)。底层占位原语见 {@link ActivityRegistrationService}。</p>
+ *
+ * <p><b>宿主契约方法(契约测试锚点)</b>:
+ * <ul>
+ *   <li>{@code long enroll(long activityId, long userId, long memberId, int maxParticipants)}
+ *       —— 报名 → 占名额 → 确认:成功返回 &gt;0 报名 ID;名额满返回 -1;重复报名返回既有 ID(幂等)</li>
+ *   <li>{@code boolean occupyQuota(long activityId, long memberId, int maxParticipants)}
+ *       —— 原子占名额:成功 true,名额满或该成员已占位 false</li>
+ *   <li>{@code int approvedCount(long activityId)} —— 当前已确认(占名额)人数</li>
+ *   <li>{@code boolean shouldRejectWhenSoldOut(int currentApproved, int maxParticipants)}
+ *       —— 超卖守卫判定(纯函数):max &gt; 0 且 currentApproved ≥ max 返回 true</li>
+ *   <li>{@code boolean cancel(long activityId, long memberId)} —— 取消报名并释放名额(回滚,不为负)</li>
+ * </ul>
+ *
+ * <p><b>实现口径</b>:
+ * <ul>
+ *   <li>纯契约实现(无 Spring / 无 DB / 无外部服务依赖,可反射实例化),名额与报名记录以内存态承载,
+ *       与同批已合入主链的 {@link ActivityCheckinBoostService} / {@code TeamProfitService} 风格一致。</li>
+ *   <li><b>状态按实例隔离</b>:契约测试每个用例各自新建宿主,名额计数不可跨实例共享,否则用例间串味。</li>
+ *   <li><b>幂等以 memberId 为准</b>:报名记录键为 (activityId, memberId),userId 不参与去重
+ *       (SC-P-76「重复报名幂等」;跨 userId 以同一成员重复报名返回同一 ID)。</li>
+ *   <li><b>名额隔离按活动维度</b>:计数与去重键均含 activityId;A 活动满员不影响 B 活动。</li>
+ *   <li><b>无人数限制</b>:maxParticipants ≤ 0 视为不限人数,任何占用数都不被守卫拒绝。</li>
+ *   <li>所有状态变更方法 {@code synchronized},使「检查余量 + 占位」为原子操作,杜绝并发超卖。</li>
+ * </ul>
+ */
+public class ActivityEnrollService {
+
+    /** 名额已满/报名被拒的哨兵返回值(SC-P-76 名额满拒)。 */
+    private static final long REJECTED = -1L;
+
+    /** 各活动已占用(已确认)名额数:activityId → approvedCount。 */
+    private final Map<Long, Integer> approvedByActivity = new ConcurrentHashMap<Long, Integer>();
+
+    /** 报名记录:activityId + memberId → 报名 ID(幂等键,userId 不参与)。 */
+    private final Map<String, Long> registrationIdByKey = new ConcurrentHashMap<String, Long>();
+
+    /** 报名 ID 序列:从 1 起自增,保证返回 &gt; 0 的报名 ID。 */
+    private final AtomicLong registrationSeq = new AtomicLong(0L);
+
+    public ActivityEnrollService() {
+        // no-arg constructor for reflection instantiation(契约测试锚点)
+    }
+
+    /**
+     * 报名 → 占名额 → 确认。
+     *
+     * <p>同一 (activityId, memberId) 重复报名直接返回既有 ID(不重复占名额);
+     * 名额已满返回 {@code -1};成功分配 &gt;0 的报名 ID 并确认占位。</p>
+     */
+    public synchronized long enroll(long activityId, long userId, long memberId, int maxParticipants) {
+        String key = registrationKey(activityId, memberId);
+        Long existing = registrationIdByKey.get(key);
+        if (existing != null) {
+            return existing; // 重复报名幂等:以 memberId 为准
+        }
+        if (shouldRejectWhenSoldOut(approvedCount(activityId), maxParticipants)) {
+            return REJECTED; // 名额满拒(条件占位,不新增)
+        }
+        long registrationId = nextRegistrationId();
+        registrationIdByKey.put(key, registrationId);
+        incrementApproved(activityId);
+        return registrationId;
+    }
+
+    /**
+     * 原子占名额:成功 true;名额已满或该成员已占位返回 false。
+     */
+    public synchronized boolean occupyQuota(long activityId, long memberId, int maxParticipants) {
+        String key = registrationKey(activityId, memberId);
+        if (registrationIdByKey.containsKey(key)) {
+            return false; // 该成员已占位
+        }
+        if (shouldRejectWhenSoldOut(approvedCount(activityId), maxParticipants)) {
+            return false; // 名额已满
+        }
+        registrationIdByKey.put(key, nextRegistrationId());
+        incrementApproved(activityId);
+        return true;
+    }
+
+    /**
+     * 查询活动当前已确认(占名额)人数,无记录返回 0。
+     */
+    public synchronized int approvedCount(long activityId) {
+        Integer count = approvedByActivity.get(activityId);
+        return count == null ? 0 : count;
+    }
+
+    /**
+     * 超卖守卫判定(纯函数):max &gt; 0 且 currentApproved ≥ max 时拒绝占位;
+     * max ≤ 0 表示不限人数,恒放行。
+     */
+    public boolean shouldRejectWhenSoldOut(int currentApproved, int maxParticipants) {
+        return maxParticipants > 0 && currentApproved >= maxParticipants;
+    }
+
+    /**
+     * 取消报名并释放名额:存在记录则移除并回退计数(不为负)返回 true;
+     * 未报名(无记录可释放)返回 false。
+     */
+    public synchronized boolean cancel(long activityId, long memberId) {
+        String key = registrationKey(activityId, memberId);
+        if (registrationIdByKey.remove(key) == null) {
+            return false; // 无记录可释放
+        }
+        int current = approvedCount(activityId);
+        approvedByActivity.put(activityId, current > 0 ? current - 1 : 0);
+        return true;
+    }
+
+    /** 报名记录键:activityId + memberId(userId 不参与幂等)。 */
+    private static String registrationKey(long activityId, long memberId) {
+        return activityId + ":" + memberId;
+    }
+
+    /** 分配下一个 &gt;0 的报名 ID。 */
+    private long nextRegistrationId() {
+        return registrationSeq.incrementAndGet();
+    }
+
+    /** 原子递增活动已确认人数。 */
+    private void incrementApproved(long activityId) {
+        approvedByActivity.put(activityId, approvedCount(activityId) + 1);
+    }
+}