From e268d236f44fbdea1afb7c13e7de019b02cc51e7 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Tue, 20 Jan 2026 15:53:33 +0800 Subject: [PATCH] =?UTF-8?q?fix(coupon):=20=E4=BF=AE=E5=A4=8D=E4=BC=98?= =?UTF-8?q?=E6=83=A0=E5=88=B8=E9=87=8D=E5=A4=8D=E9=A2=86=E5=8F=96=E5=92=8C?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E6=A3=80=E6=9F=A5=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改数据库查询方法返回类型为List以支持多条记录查询 - 更新AutoCouponServiceImpl中的重复领取检查逻辑 - 在CouponServiceImpl中实现可用优惠券筛选功能 - 优化优惠券状态验证逻辑并改进错误信息提示 - 修复使用优惠券时的状态判断条件 --- .../mapper/PriceCouponClaimRecordMapper.java | 2 +- .../service/impl/AutoCouponServiceImpl.java | 6 ++++-- .../pricing/service/impl/CouponServiceImpl.java | 16 ++++++++++++---- 3 files changed, 17 insertions(+), 7 deletions(-) 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());