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); + } } } }