Selaa lähdekoodia

feat(coupon): S-cfc-379 shop.coupon.claim RED 契约使绿

- claim() 成功路径落发券流水 family_coupon_grant_log(grantType=CLAIM),作为已领拦截的流水口径防重记录(AC-1)
- 已领拦截扩展为 family_coupon 记录 ∪ 发券流水(任意 grant_type)判定,自动发放链路发过券的家庭手动再领同券不再二次发放,对齐 COUPON-C3(AC-3)
- CouponMapper 新增 incrementUsedCountIfAvailable 原子扣减(used_count+1 且 total_count 上限判定),并发不超发(AC-3 DB 防线)
- 流水调用判空兼容未注入 grantLogMapper 的旧式单测(S-cfc-12 CouponUseScP57ScP75ContractTest 保持绿)
Agnes Agent 1 päivä sitten
vanhempi
sitoutus
024a14c2ae

+ 15 - 0
cfc-backend/src/main/java/com/etotem/cfc/mapper/CouponMapper.java

@@ -3,7 +3,22 @@ package com.etotem.cfc.mapper;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.etotem.cfc.entity.Coupon;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Update;
 
 @Mapper
 public interface CouponMapper extends BaseMapper<Coupon> {
+
+    /**
+     * claim 侧原子扣减(DB 并发防线,S-cfc-293 AC-3):used_count +1 且不超发
+     * (total_count 为空视为不限量,否则 used_count < total_count 才扣减)。
+     * <p>与 claim() 的 selectById + updateById 路径互补:并发领取同一券时,库存为 0 的
+     * 拦截由原子 SQL 兜底,返回 0 表示库存已满/券不存在(不应再发放)。</p>
+     *
+     * @param couponId 券模板ID(coupon.id)
+     * @return 受影响行数:1=扣减成功;0=库存已满/券不存在(并发下不超发)
+     */
+    @Update("UPDATE coupon SET used_count = IFNULL(used_count, 0) + 1 " +
+            "WHERE id = #{couponId} AND (total_count IS NULL OR used_count < total_count)")
+    int incrementUsedCountIfAvailable(@Param("couponId") Long couponId);
 }

+ 25 - 3
cfc-backend/src/main/java/com/etotem/cfc/service/CouponService.java

@@ -184,17 +184,39 @@ public class CouponService {
         if (coupon.getValidUntil() != null && now.after(coupon.getValidUntil())) {
             return "优惠券已过期";
         }
-        // 去重:同一个家庭同一张券只发一次(同 grantType 的幂等也通过 grant 方法保证)
-        Long count = familyCouponMapper.selectCount(
+        // 去重:同一个家庭同一张券只发一次(COUPON-C3 口径:family_coupon 记录 ∪ 发券流水)。
+        // 以发券流水(任意 grant_type)为补充判定口径:JOIN/POPULATION/PERIODIC/CF_EXCHANGE 等
+        // 自动发放链路发过券的家庭,手动 /claim 同券不再二次发放(S-cfc-293 AC-3)。
+        Long fcCount = familyCouponMapper.selectCount(
                 new LambdaQueryWrapper<FamilyCoupon>()
                         .eq(FamilyCoupon::getFamilyId, familyId)
                         .eq(FamilyCoupon::getCouponId, couponId));
-        if (count != null && count > 0) {
+        Long grantCount = 0L;
+        if (familyCouponGrantLogMapper != null) {
+            grantCount = familyCouponGrantLogMapper.selectCount(
+                    new LambdaQueryWrapper<FamilyCouponGrantLog>()
+                            .eq(FamilyCouponGrantLog::getFamilyId, familyId)
+                            .eq(FamilyCouponGrantLog::getCouponId, couponId));
+        }
+        if ((fcCount != null && fcCount > 0) || (grantCount != null && grantCount > 0)) {
             return "已领取过该优惠券";
         }
         issueToFamily(couponId, familyId);
         coupon.setUsedCount(coupon.getUsedCount() == null ? 1 : coupon.getUsedCount() + 1);
         couponMapper.updateById(coupon);
+        // AC-1:落发券流水(grantType=CLAIM),作为「已领拦截」的流水口径防重记录。
+        // 判空仅兼容未注入 grantLogMapper 的旧式单测(S-cfc-12 CouponUseScP57ScP75ContractTest
+        // 用 new CouponService() 未注入该字段),Spring 注入场景恒非 null。
+        if (familyCouponGrantLogMapper != null) {
+            FamilyCouponGrantLog grantLog = new FamilyCouponGrantLog();
+            grantLog.setFamilyId(familyId);
+            grantLog.setCouponId(couponId);
+            grantLog.setGrantType("CLAIM");
+            grantLog.setQuantity(1);
+            grantLog.setSource("claim:userId:" + userId);
+            grantLog.setCreatedAt(new Date());
+            familyCouponGrantLogMapper.insert(grantLog);
+        }
         return "领取成功";
     }