From 90a21c093366eb6741bb5b99834338974ce1c7ef Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Tue, 16 Sep 2025 17:55:24 +0800 Subject: [PATCH] =?UTF-8?q?fix(pricing):=20=E5=AE=8C=E5=96=84=E5=88=B8?= =?UTF-8?q?=E7=A0=81=E9=AA=8C=E8=AF=81=E9=80=BB=E8=BE=91=E5=B9=B6=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E4=BD=BF=E7=94=A8=E6=9D=83=E9=99=90=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增用户在指定批次下的使用次数统计和最后使用时间获取功能 - 重构券码验证逻辑,支持未领取券码的使用权限判断 - 优化已领取券码的使用限制检查,包括使用次数和间隔时间- 改进日志记录,增加剩余使用次数信息 -修复一些潜在的逻辑问题和边界情况处理 --- .../mapper/PriceVoucherUsageRecordMapper.java | 20 +++ .../service/impl/VoucherDiscountProvider.java | 25 +++- .../service/impl/VoucherServiceImpl.java | 115 +++++++++++++----- 3 files changed, 125 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/ycwl/basic/pricing/mapper/PriceVoucherUsageRecordMapper.java b/src/main/java/com/ycwl/basic/pricing/mapper/PriceVoucherUsageRecordMapper.java index 4b9c8744..66dc111e 100644 --- a/src/main/java/com/ycwl/basic/pricing/mapper/PriceVoucherUsageRecordMapper.java +++ b/src/main/java/com/ycwl/basic/pricing/mapper/PriceVoucherUsageRecordMapper.java @@ -89,6 +89,26 @@ public interface PriceVoucherUsageRecordMapper extends BaseMapper= maxUseCount) { - return "券码使用次数已达上限"; + // 1. 检查券码使用次数限制(仅针对已领取的券码) + if (isClaimed) { + Integer maxUseCount = batchConfig.getMaxUseCount(); + Integer currentUseCount = voucherCode.getCurrentUseCount() != null ? voucherCode.getCurrentUseCount() : 0; + + if (maxUseCount != null && currentUseCount >= maxUseCount) { + return "券码使用次数已达上限"; + } } // 2. 检查用户使用次数限制 Integer maxUsePerUser = batchConfig.getMaxUsePerUser(); - if (maxUsePerUser != null && faceId != null) { - Integer userUseCount = usageRecordMapper.countByFaceIdAndVoucherCodeId(faceId, voucherCode.getId()); - if (userUseCount >= maxUsePerUser) { - return "您使用该券码的次数已达上限"; + if (maxUsePerUser != null) { + Integer userUseCount; + if (isClaimed) { + // 已领取:检查用户对该具体券码的使用次数 + userUseCount = usageRecordMapper.countByFaceIdAndVoucherCodeId(faceId, voucherCode.getId()); + if (userUseCount >= maxUsePerUser) { + return "您使用该券码的次数已达上限"; + } + } else { + // 未领取:检查用户在该批次下的总使用次数 + userUseCount = usageRecordMapper.countByFaceIdAndBatchId(faceId, batchConfig.getId()); + if (userUseCount >= maxUsePerUser) { + return "您在该批次下使用券码的次数已达上限"; + } } } // 3. 检查使用间隔时间限制 Integer useIntervalHours = batchConfig.getUseIntervalHours(); - if (useIntervalHours != null && faceId != null) { - Date lastUseTime = usageRecordMapper.getLastUseTimeByFaceIdAndVoucherCodeId(faceId, voucherCode.getId()); - if (lastUseTime != null) { - long diffMillis = System.currentTimeMillis() - lastUseTime.getTime(); - long diffHours = TimeUnit.MILLISECONDS.toHours(diffMillis); - if (diffHours < useIntervalHours) { - return String.format("请等待%d小时后再次使用该券码", useIntervalHours - diffHours); + if (useIntervalHours != null) { + Date lastUseTime; + if (isClaimed) { + // 已领取:检查该券码的最后使用时间 + lastUseTime = usageRecordMapper.getLastUseTimeByFaceIdAndVoucherCodeId(faceId, voucherCode.getId()); + if (lastUseTime != null) { + long diffMillis = System.currentTimeMillis() - lastUseTime.getTime(); + long diffHours = TimeUnit.MILLISECONDS.toHours(diffMillis); + if (diffHours < useIntervalHours) { + return String.format("请等待%d小时后再次使用该券码", useIntervalHours - diffHours); + } + } + } else { + // 未领取:检查该批次下的最后使用时间 + lastUseTime = usageRecordMapper.getLastUseTimeByFaceIdAndBatchId(faceId, batchConfig.getId()); + if (lastUseTime != null) { + long diffMillis = System.currentTimeMillis() - lastUseTime.getTime(); + long diffHours = TimeUnit.MILLISECONDS.toHours(diffMillis); + if (diffHours < useIntervalHours) { + return String.format("请等待%d小时后再次使用该批次券码", useIntervalHours - diffHours); + } } } }