فهرست منبع

新增sku组合数量校验

codingliang 3 ماه پیش
والد
کامیت
2293efe99d
1فایلهای تغییر یافته به همراه15 افزوده شده و 0 حذف شده
  1. 15 0
      src/main/java/com/sqx/modules/goods/service/impl/GoodsServiceImpl.java

+ 15 - 0
src/main/java/com/sqx/modules/goods/service/impl/GoodsServiceImpl.java

@@ -249,6 +249,21 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsDao, Goods> implements Go
         //1.获取商品规格值
         List<GoodsAttrValue> attr = goodsAttr.getAttrValue();
         int attrSize = attr.size();
+
+        // 临时校验规则总组合数
+        final long MAX_ALLOW_SKU = 5000;
+        long totalCombination = 1L;
+        for (GoodsAttrValue goodsAttrValue : attr) {
+            String detail = goodsAttrValue.getDetail();
+            int optionCount = detail.split(",").length;
+            // 每个规格的选项数相乘,得到总组合数
+            totalCombination = totalCombination * optionCount;
+            // 超过阈值直接返回错误,避免继续生成撑爆内存
+            if (totalCombination > MAX_ALLOW_SKU) {
+                throw new SqxException("SKU规格选项过多,总组合数超过上限" + MAX_ALLOW_SKU + ",请减少规格/规格选项数量");
+            }
+        }
+
         //2.准备返回值
         Map<String, Object> map = new HashMap<>();
         List<String> header = new ArrayList<>(); //表头