diff --git a/src/main/java/com/ycwl/basic/pricing/mapper/PriceCouponClaimRecordMapper.java b/src/main/java/com/ycwl/basic/pricing/mapper/PriceCouponClaimRecordMapper.java index 9d17dfd1..d5484183 100644 --- a/src/main/java/com/ycwl/basic/pricing/mapper/PriceCouponClaimRecordMapper.java +++ b/src/main/java/com/ycwl/basic/pricing/mapper/PriceCouponClaimRecordMapper.java @@ -33,7 +33,7 @@ public interface PriceCouponClaimRecordMapper extends BaseMapper selectUserCouponRecords(@Param("userId") Long userId, @Param("couponId") Long couponId); /** diff --git a/src/main/java/com/ycwl/basic/pricing/service/impl/AutoCouponServiceImpl.java b/src/main/java/com/ycwl/basic/pricing/service/impl/AutoCouponServiceImpl.java index 06034381..92a3a269 100644 --- a/src/main/java/com/ycwl/basic/pricing/service/impl/AutoCouponServiceImpl.java +++ b/src/main/java/com/ycwl/basic/pricing/service/impl/AutoCouponServiceImpl.java @@ -54,12 +54,14 @@ public class AutoCouponServiceImpl implements IAutoCouponService { for (Long couponId : couponIds) { try { // 检查用户是否已领取过该券(领券即消耗首次资格) - PriceCouponClaimRecord existingRecord = couponClaimRecordMapper.selectUserCouponRecord( + List existingRecords = couponClaimRecordMapper.selectUserCouponRecords( memberId, couponId ); - if (existingRecord != null) { + if (existingRecords != null && !existingRecords.isEmpty()) { + // 只要有记录(无论状态),都算已领取过 + PriceCouponClaimRecord existingRecord = existingRecords.get(0); log.debug("用户已领取过优惠券,跳过: memberId={}, couponId={}, claimTime={}", memberId, couponId, existingRecord.getClaimTime()); skipCount++; diff --git a/src/main/java/com/ycwl/basic/pricing/service/impl/CouponServiceImpl.java b/src/main/java/com/ycwl/basic/pricing/service/impl/CouponServiceImpl.java index fe35367e..b9c257a1 100644 --- a/src/main/java/com/ycwl/basic/pricing/service/impl/CouponServiceImpl.java +++ b/src/main/java/com/ycwl/basic/pricing/service/impl/CouponServiceImpl.java @@ -223,15 +223,23 @@ public class CouponServiceImpl implements ICouponService { @Override @Transactional public CouponUseResult useCoupon(CouponUseRequest request) { - PriceCouponClaimRecord record = couponClaimRecordMapper.selectUserCouponRecord( + List records = couponClaimRecordMapper.selectUserCouponRecords( request.getUserId(), request.getCouponId()); - if (record == null) { + if (records == null || records.isEmpty()) { throw new CouponInvalidException("用户未拥有该优惠券"); } + + // 查找一张可用的优惠券(状态为CLAIMED) + PriceCouponClaimRecord record = records.stream() + .filter(r -> r.getStatus() == CouponStatus.CLAIMED) + .findFirst() + .orElse(null); - if (record.getStatus() != CouponStatus.CLAIMED) { - throw new CouponInvalidException("优惠券状态无效: " + record.getStatus()); + if (record == null) { + // 如果没有可用的,抛出异常。为了错误信息准确,可以检查最后一张的状态 + CouponStatus lastStatus = records.getFirst().getStatus(); + throw new CouponInvalidException("优惠券状态无效: " + lastStatus); } int updateCount = couponConfigMapper.incrementUsedQuantity(request.getCouponId());