feat(pricing): 优化优惠券领取逻辑与并发控制

- 为 CouponInvalidException 添加错误码支持
- 在 countUserCouponClaims 查询中添加 FOR UPDATE 锁
- 新增 incrementClaimedQuantityIfAvailable 方法用于原子性增加已领取数量
- 移除重复的用户优惠券领取检查逻辑
- 调整领取流程步骤编号并优化事务回滚处理
- 增加对优惠券库存耗尽情况的业务异常处理
- 使用
This commit is contained in:
2025-11-17 00:53:48 +08:00
parent 88ad6d6b6f
commit 7c906d5529
4 changed files with 54 additions and 22 deletions

View File

@@ -38,6 +38,14 @@ public interface PriceCouponConfigMapper extends BaseMapper<PriceCouponConfig> {
@Update("UPDATE price_coupon_config SET used_quantity = used_quantity + 1, " +
"update_time = NOW() WHERE id = #{couponId} AND used_quantity < total_quantity")
int incrementUsedQuantity(Long couponId);
/**
* 原子性增加已领取数量(仅对有限库存的优惠券生效)
*/
@Update("UPDATE price_coupon_config SET claimed_quantity = COALESCE(claimed_quantity, 0) + 1, " +
"update_time = NOW() WHERE id = #{couponId} AND total_quantity IS NOT NULL AND total_quantity > 0 " +
"AND COALESCE(claimed_quantity, 0) < total_quantity")
int incrementClaimedQuantityIfAvailable(@Param("couponId") Long couponId);
/**
* 插入优惠券配置
@@ -133,4 +141,4 @@ public interface PriceCouponConfigMapper extends BaseMapper<PriceCouponConfig> {
"SUM(used_quantity) as used_quantity " +
"FROM price_coupon_config WHERE scenic_id = #{scenicId}")
java.util.Map<String, Object> selectScenicCouponConfigStats(@Param("scenicId") String scenicId);
}
}