You've already forked FrameTour-BE
feat(pricing): 优化优惠券领取逻辑与并发控制
- 为 CouponInvalidException 添加错误码支持 - 在 countUserCouponClaims 查询中添加 FOR UPDATE 锁 - 新增 incrementClaimedQuantityIfAvailable 方法用于原子性增加已领取数量 - 移除重复的用户优惠券领取检查逻辑 - 调整领取流程步骤编号并优化事务回滚处理 - 增加对优惠券库存耗尽情况的业务异常处理 - 使用
This commit is contained in:
@@ -18,6 +18,7 @@ import java.util.Date;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.interceptor.TransactionAspectSupport;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
@@ -243,6 +244,7 @@ public class CouponServiceImpl implements ICouponService {
|
||||
if (coupon.getUserClaimLimit() != null && coupon.getUserClaimLimit() > 0) {
|
||||
int userClaimCount = couponClaimRecordMapper.countUserCouponClaims(
|
||||
request.getUserId(), request.getCouponId());
|
||||
// countUserCouponClaims 使用 FOR UPDATE + 复合索引,确保并发下的计数准确
|
||||
if (userClaimCount >= coupon.getUserClaimLimit()) {
|
||||
return CouponClaimResult.failure(
|
||||
CouponClaimResult.ERROR_CLAIM_LIMIT_REACHED,
|
||||
@@ -250,14 +252,7 @@ public class CouponServiceImpl implements ICouponService {
|
||||
}
|
||||
}
|
||||
|
||||
// 7. 检查用户是否已经领取过该优惠券(兼容旧逻辑,双重保护)
|
||||
PriceCouponClaimRecord existingRecord = couponClaimRecordMapper.selectUserCouponRecord(
|
||||
request.getUserId(), request.getCouponId());
|
||||
if (existingRecord != null) {
|
||||
return CouponClaimResult.failure(CouponClaimResult.ERROR_ALREADY_CLAIMED, "您已经领取过该优惠券");
|
||||
}
|
||||
|
||||
// 8. 创建领取记录
|
||||
// 7. 创建领取记录
|
||||
Date claimTime = new Date();
|
||||
PriceCouponClaimRecord claimRecord = new PriceCouponClaimRecord();
|
||||
claimRecord.setCouponId(request.getCouponId());
|
||||
@@ -269,7 +264,7 @@ public class CouponServiceImpl implements ICouponService {
|
||||
claimRecord.setUpdateTime(claimTime);
|
||||
claimRecord.setDeleted(0);
|
||||
|
||||
// 9. 插入领取记录
|
||||
// 8. 插入领取记录
|
||||
int insertResult = couponClaimRecordMapper.insert(claimRecord);
|
||||
if (insertResult <= 0) {
|
||||
log.error("插入优惠券领取记录失败: userId={}, couponId={}",
|
||||
@@ -277,24 +272,38 @@ public class CouponServiceImpl implements ICouponService {
|
||||
return CouponClaimResult.failure(CouponClaimResult.ERROR_SYSTEM_ERROR, "领取失败,请稍后重试");
|
||||
}
|
||||
|
||||
// 10. 更新优惠券已领取数量(区分于已使用数量)
|
||||
// 9. 更新优惠券已领取数量(区分于已使用数量)
|
||||
// 仅在有总量限制时才更新claimedQuantity(totalQuantity为正整数)
|
||||
if (coupon.getTotalQuantity() != null && coupon.getTotalQuantity() > 0) {
|
||||
int affected = couponConfigMapper.incrementClaimedQuantityIfAvailable(coupon.getId());
|
||||
if (affected == 0) {
|
||||
throw new CouponInvalidException(
|
||||
CouponClaimResult.ERROR_COUPON_OUT_OF_STOCK,
|
||||
"优惠券已被领取完,请稍后重试");
|
||||
}
|
||||
int updatedClaimedQuantity = (coupon.getClaimedQuantity() == null ? 0 : coupon.getClaimedQuantity()) + 1;
|
||||
coupon.setClaimedQuantity(updatedClaimedQuantity);
|
||||
couponConfigMapper.updateById(coupon);
|
||||
}
|
||||
|
||||
log.info("优惠券领取成功: userId={}, couponId={}, claimRecordId={}",
|
||||
request.getUserId(), request.getCouponId(), claimRecord.getId());
|
||||
|
||||
// 11. 返回成功结果
|
||||
// 10. 返回成功结果
|
||||
return CouponClaimResult.success(claimRecord, coupon);
|
||||
|
||||
} catch (CouponInvalidException e) {
|
||||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||||
log.warn("领取优惠券失败(业务校验不通过): userId={}, couponId={}, reason={}",
|
||||
request.getUserId(), request.getCouponId(), e.getMessage());
|
||||
String errorCode = e.getErrorCode() == null
|
||||
? CouponClaimResult.ERROR_SYSTEM_ERROR
|
||||
: e.getErrorCode();
|
||||
return CouponClaimResult.failure(errorCode, e.getMessage());
|
||||
} catch (Exception e) {
|
||||
log.error("领取优惠券失败: userId={}, couponId={}",
|
||||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||||
log.error("领取优惠券失败: userId={}, couponId={}",
|
||||
request.getUserId(), request.getCouponId(), e);
|
||||
return CouponClaimResult.failure(CouponClaimResult.ERROR_SYSTEM_ERROR, "系统错误,领取失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user