|
|
@@ -1,7 +1,12 @@
|
|
|
package com.ruoyi.system.service.impl;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
@@ -152,7 +157,17 @@ public class PromotionCouponBatchServiceImpl extends ServiceImpl<BaseMapper<Prom
|
|
|
}
|
|
|
if (existing.getReceivedCount() != null && existing.getReceivedCount() > 0)
|
|
|
{
|
|
|
- // 已有人领取,只更新批次级别字段,不修改规则
|
|
|
+ // 已有人领取:只更新批次级别字段;若试图修改规则则拒绝
|
|
|
+ if (rule != null)
|
|
|
+ {
|
|
|
+ List<PromotionCouponRule> existingRules = ruleMapper.selectRulesByBatchId(batch.getId());
|
|
|
+ List<PromotionCouponRule> newRules = new ArrayList<>();
|
|
|
+ newRules.add(rule);
|
|
|
+ if (couponRulesDifferent(existingRules, newRules))
|
|
|
+ {
|
|
|
+ throw new ServiceException("该优惠券已被领取,无法修改适用商品/规则");
|
|
|
+ }
|
|
|
+ }
|
|
|
promotionCouponBatchMapper.updateById(batch);
|
|
|
}
|
|
|
else
|
|
|
@@ -195,6 +210,15 @@ public class PromotionCouponBatchServiceImpl extends ServiceImpl<BaseMapper<Prom
|
|
|
}
|
|
|
if (existing.getReceivedCount() != null && existing.getReceivedCount() > 0)
|
|
|
{
|
|
|
+ // 已有人领取:只更新批次级别字段;若试图修改规则则拒绝
|
|
|
+ if (rules != null && !rules.isEmpty())
|
|
|
+ {
|
|
|
+ List<PromotionCouponRule> existingRules = ruleMapper.selectRulesByBatchId(batch.getId());
|
|
|
+ if (couponRulesDifferent(existingRules, rules))
|
|
|
+ {
|
|
|
+ throw new ServiceException("该优惠券已被领取,无法修改适用商品/规则");
|
|
|
+ }
|
|
|
+ }
|
|
|
promotionCouponBatchMapper.updateById(batch);
|
|
|
}
|
|
|
else
|
|
|
@@ -240,4 +264,52 @@ public class PromotionCouponBatchServiceImpl extends ServiceImpl<BaseMapper<Prom
|
|
|
promotionCouponBatchMapper.deleteById(id);
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 比较新旧优惠券规则是否一致(用于判断已领取券是否在尝试修改规则)。
|
|
|
+ * 按 productId 对齐逐条比较 discountRate/threshold/amount/isMutex,与顺序无关。
|
|
|
+ */
|
|
|
+ private boolean couponRulesDifferent(List<PromotionCouponRule> oldRules, List<PromotionCouponRule> newRules)
|
|
|
+ {
|
|
|
+ int oldSize = oldRules == null ? 0 : oldRules.size();
|
|
|
+ int newSize = newRules == null ? 0 : newRules.size();
|
|
|
+ if (oldSize != newSize)
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ Map<Long, PromotionCouponRule> oldMap = new HashMap<>();
|
|
|
+ if (oldRules != null)
|
|
|
+ {
|
|
|
+ for (PromotionCouponRule r : oldRules)
|
|
|
+ {
|
|
|
+ oldMap.put(r.getProductId(), r);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (newRules != null)
|
|
|
+ {
|
|
|
+ for (PromotionCouponRule n : newRules)
|
|
|
+ {
|
|
|
+ PromotionCouponRule o = oldMap.get(n.getProductId());
|
|
|
+ if (o == null)
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ if (compareDecimal(o.getDiscountRate(), n.getDiscountRate()) != 0
|
|
|
+ || compareDecimal(o.getThreshold(), n.getThreshold()) != 0
|
|
|
+ || compareDecimal(o.getAmount(), n.getAmount()) != 0
|
|
|
+ || !Objects.equals(o.getIsMutex(), n.getIsMutex()))
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private int compareDecimal(BigDecimal a, BigDecimal b)
|
|
|
+ {
|
|
|
+ BigDecimal x = a == null ? BigDecimal.ZERO : a;
|
|
|
+ BigDecimal y = b == null ? BigDecimal.ZERO : b;
|
|
|
+ return x.compareTo(y);
|
|
|
+ }
|
|
|
}
|