zoujiajian 1 maand geleden
bovenliggende
commit
75ff781cb8

+ 0 - 14
netflix-dao/src/main/java/com/cyksj/model/dto/MultiQuantitySkuConfig.java

@@ -1,14 +0,0 @@
-package com.cyksj.model.dto;
-
-import lombok.Getter;
-import lombok.Setter;
-
-/** Purchase limit for one SKU in the multi-quantity configuration. */
-@Getter
-@Setter
-public class MultiQuantitySkuConfig {
-
-    private Long skuId;
-
-    private Integer maxNum;
-}

+ 4 - 0
netflix-dao/src/main/java/com/cyksj/model/entity/GoodsDonSku.java

@@ -102,6 +102,10 @@ public class GoodsDonSku extends BaseEntity {
      */
     private Integer num;
 
+    private Boolean isMultiQuantity;
+
+    private Integer multiQuantityMaxNum;
+
     /**
      * 价格
      */

+ 4 - 0
netflix-dao/src/main/java/com/cyksj/model/manage/request/ReqGoodsSkuInsert.java

@@ -78,6 +78,10 @@ public class ReqGoodsSkuInsert {
          */
         private Integer num;
 
+        private Boolean isMultiQuantity;
+
+        private Integer multiQuantityMaxNum;
+
         /**
          * 有效天数
          */

+ 4 - 0
netflix-dao/src/main/java/com/cyksj/model/views/GoodsDonSkuFrontView.java

@@ -87,6 +87,10 @@ public class GoodsDonSkuFrontView {
 	 */
 	private Boolean status;
 
+	private Boolean isMultiQuantity;
+
+	private Integer multiQuantityMaxNum;
+
 	/**
 	 * 价格
 	 */

+ 13 - 0
netflix-service/src/main/java/com/cyksj/service/mange/goods/CmsGoodsDonSpecServiceImpl.java

@@ -185,6 +185,7 @@ public class CmsGoodsDonSpecServiceImpl extends ServiceImpl<GoodsDonSpecMapper,
 		Set<String> set = null;
 
 		for (ReqGoodsSkuInsert.Sku sku : insert.getSkus()) {
+			normalizeMultiQuantityConfig(sku);
 			// 跳过空值的sku
 			// 解析sku的规格属性
 			final String specIds = sku.getSpecIds();
@@ -295,6 +296,18 @@ public class CmsGoodsDonSpecServiceImpl extends ServiceImpl<GoodsDonSpecMapper,
 		}
 	}
 
+	private void normalizeMultiQuantityConfig(ReqGoodsSkuInsert.Sku sku) {
+		if (!Boolean.TRUE.equals(sku.getIsMultiQuantity())) {
+			sku.setIsMultiQuantity(false);
+			sku.setMultiQuantityMaxNum(1);
+			return;
+		}
+		Integer maxNum = sku.getMultiQuantityMaxNum();
+		if (maxNum == null || maxNum < 2 || maxNum > 100) {
+			throw BusinessRuntimeException.getInstance("多数量单次购买上限必须在2到100之间");
+		}
+	}
+
 	@Override
 	public void combinationSku(Long goodsId, List<CombinationSkuReq> combinations) throws Exception {
 		GoodsDon goodsDon = goodsDonMapper.selectById(goodsId);

+ 0 - 16
netflix-service/src/main/java/com/cyksj/service/order/MultiQuantityOrderConfigService.java

@@ -1,16 +0,0 @@
-package com.cyksj.service.order;
-
-import com.cyksj.model.dto.MultiQuantitySkuConfig;
-
-import java.util.Optional;
-
-public interface MultiQuantityOrderConfigService {
-
-    String CONFIG_KEY = "multi_quantity_order_goods";
-
-    Optional<MultiQuantitySkuConfig> findConfig(Long skuId);
-
-    boolean isMultiQuantity(Long skuId, Integer num);
-
-    MultiQuantitySkuConfig validateAndGet(Long skuId, Integer num);
-}

+ 0 - 137
netflix-service/src/main/java/com/cyksj/service/order/impl/MultiQuantityOrderConfigServiceImpl.java

@@ -1,137 +0,0 @@
-package com.cyksj.service.order.impl;
-
-import cn.hutool.core.collection.CollUtil;
-import cn.hutool.core.util.StrUtil;
-import com.baomidou.mybatisplus.core.toolkit.Wrappers;
-import com.cyksj.common.exception.BusinessRuntimeException;
-import com.cyksj.common.util.Jsons;
-import com.cyksj.model.dto.MultiQuantitySkuConfig;
-import com.cyksj.model.entity.SysConfig;
-import com.cyksj.redis.RedisService;
-import com.cyksj.service.order.MultiQuantityOrderConfigService;
-import com.cyksj.service.sys.SysConfigService;
-import lombok.RequiredArgsConstructor;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.stereotype.Service;
-
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-
-@Slf4j
-@Service
-@RequiredArgsConstructor
-public class MultiQuantityOrderConfigServiceImpl implements MultiQuantityOrderConfigService {
-
-    private static final int ABSOLUTE_MAX_NUM = 100;
-
-    private static final long CONFIG_CACHE_SECONDS = 300L;
-
-    private volatile String cachedConfigValue;
-
-    private volatile Map<Long, MultiQuantitySkuConfig> cachedConfigMap = Collections.emptyMap();
-
-    private final SysConfigService sysConfigService;
-
-    private final RedisService redisService;
-
-    @Override
-    public Optional<MultiQuantitySkuConfig> findConfig(Long skuId) {
-        if (skuId == null || skuId <= 0) {
-            return Optional.empty();
-        }
-        return Optional.ofNullable(loadConfigMap().get(skuId));
-    }
-
-    @Override
-    public boolean isMultiQuantity(Long skuId, Integer num) {
-        return num != null && num > 1 && findConfig(skuId).isPresent();
-    }
-
-    @Override
-    public MultiQuantitySkuConfig validateAndGet(Long skuId, Integer num) {
-        if (num == null || num <= 1) {
-            throw BusinessRuntimeException.getInstance("多数量下单数量必须大于1");
-        }
-        MultiQuantitySkuConfig config = findConfig(skuId)
-                .orElseThrow(() -> BusinessRuntimeException.getInstance("该规格不支持一次购买多个"));
-        if (num > config.getMaxNum()) {
-            throw BusinessRuntimeException.getInstance("该规格单次最多购买{0}个", config.getMaxNum());
-        }
-        return config;
-    }
-
-    private Map<Long, MultiQuantitySkuConfig> loadConfigMap() {
-        SysConfig sysConfig = getSysConfig();
-        if (sysConfig == null || StrUtil.isBlank(sysConfig.getSysValue())) {
-            return Collections.emptyMap();
-        }
-        String configValue = sysConfig.getSysValue();
-        if (configValue.equals(cachedConfigValue)) {
-            return cachedConfigMap;
-        }
-        synchronized (this) {
-            if (configValue.equals(cachedConfigValue)) {
-                return cachedConfigMap;
-            }
-            Map<Long, MultiQuantitySkuConfig> parsedConfig = parseConfig(configValue);
-            cachedConfigMap = parsedConfig;
-            cachedConfigValue = configValue;
-            return parsedConfig;
-        }
-    }
-
-    private Map<Long, MultiQuantitySkuConfig> parseConfig(String configValue) {
-        try {
-            List<MultiQuantitySkuConfig> configs = Jsons.parseList(configValue, MultiQuantitySkuConfig.class);
-            if (CollUtil.isEmpty(configs)) {
-                return Collections.emptyMap();
-            }
-            Map<Long, MultiQuantitySkuConfig> configMap = new HashMap<>(configs.size());
-            for (MultiQuantitySkuConfig config : configs) {
-                validateConfigEntry(config);
-                if (configMap.put(config.getSkuId(), config) != null) {
-                    throw new IllegalArgumentException("duplicate skuId: " + config.getSkuId());
-                }
-            }
-            return Collections.unmodifiableMap(configMap);
-        } catch (Exception e) {
-            log.error("Invalid multi-quantity SKU config", e);
-            throw BusinessRuntimeException.getInstance("多数量规格配置异常,请联系客服");
-        }
-    }
-
-    private SysConfig getSysConfig() {
-        String cacheKey = RedisService.key.SYS_CONFIG_CACHE_KEY.getName() + CONFIG_KEY;
-        try {
-            Object cached = redisService.get(cacheKey);
-            if (cached instanceof SysConfig) {
-                return (SysConfig) cached;
-            }
-        } catch (Exception e) {
-            log.warn("Read multi-quantity config cache failed: {}", e.getMessage());
-        }
-
-        SysConfig sysConfig = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class)
-                .eq(SysConfig::getSysKey, CONFIG_KEY)
-                .last("limit 1"));
-        if (sysConfig != null) {
-            try {
-                redisService.set(cacheKey, sysConfig, CONFIG_CACHE_SECONDS);
-            } catch (Exception e) {
-                log.warn("Write multi-quantity config cache failed: {}", e.getMessage());
-            }
-        }
-        return sysConfig;
-    }
-
-    private void validateConfigEntry(MultiQuantitySkuConfig config) {
-        if (config == null || config.getSkuId() == null || config.getSkuId() <= 0
-                || config.getMaxNum() == null || config.getMaxNum() < 2
-                || config.getMaxNum() > ABSOLUTE_MAX_NUM) {
-            throw new IllegalArgumentException("skuId and maxNum must be valid");
-        }
-    }
-}

+ 12 - 5
netflix-service/src/main/java/com/cyksj/service/order/impl/OrderDonServiceImpl.java

@@ -188,8 +188,6 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 
 	private final OrderDonTicketQueryMapper orderDonTicketQueryMapper;
 
-	private final MultiQuantityOrderConfigService multiQuantityOrderConfigService;
-
 	private final MultiQuantityOrderService multiQuantityOrderService;
 
 	private final EnvCommonService envCommonService;
@@ -477,14 +475,13 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 			throw BusinessRuntimeException.getInstance("购买数量必须大于0");
 		}
 		payRequest.setNum(quantity);
-		Long multiQuantitySkuId = sku == null ? null : sku.getId();
-		boolean multiQuantity = multiQuantityOrderConfigService.isMultiQuantity(multiQuantitySkuId, quantity);
+		boolean multiQuantity = quantity > 1 && sku != null && Boolean.TRUE.equals(sku.getIsMultiQuantity());
 		if (quantity > 1 && !multiQuantity) {
 			throw BusinessRuntimeException.getInstance("该规格不支持一次购买多个");
 		}
 		if (multiQuantity) {
 			validateMultiQuantitySubmit(payRequest, goodsDon, sku, isGiftCardPay.get(), yhsId);
-			multiQuantityOrderConfigService.validateAndGet(multiQuantitySkuId, quantity);
+			validateMultiQuantitySkuLimit(sku, quantity);
 			if (!isSkuEnabled(sku)) {
 				throw BusinessRuntimeException.getInstance("SKU is disabled");
 			}
@@ -1009,6 +1006,16 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 		return false;
 	}
 
+	static void validateMultiQuantitySkuLimit(GoodsDonSku sku, int quantity) {
+		Integer maxNum = sku.getMultiQuantityMaxNum();
+		if (maxNum == null || maxNum < 2 || maxNum > 100) {
+			throw BusinessRuntimeException.getInstance("该规格多数量配置异常");
+		}
+		if (quantity > maxNum) {
+			throw BusinessRuntimeException.getInstance("该规格单次最多购买{0}个", maxNum);
+		}
+	}
+
 	private void validateMultiQuantitySubmit(OrderPayRequest request, GoodsDon goods,
 			GoodsDonSku sku, boolean giftCardOrder, Long shopId) {
 		if (goods.getType() == null || goods.getType() != 1 || sku == null) {

+ 14 - 1
netflix-web/src/main/java/com/cyksj/web/controller/manage/goods/CmsGoodsDonSkuController.java

@@ -119,8 +119,9 @@ public class CmsGoodsDonSkuController {
     public Result<String> skuUpdate(@RequestBody GoodsDonSku sku) throws Exception {
         Long skuId = sku.getId();
         GoodsDonSku dbSku = goodsDonSkuService.getById(skuId);
-        Boolean dbIsSubscribeAlert = dbSku.getIsSubscribeAlert();
         Assert.notNull(dbSku, "规格不存在");
+        Boolean dbIsSubscribeAlert = dbSku.getIsSubscribeAlert();
+        normalizeMultiQuantityConfig(sku);
         Long goodsId = sku.getGoodsId();
         GoodsDon goods = goodsDonService.getById(goodsId);
         if (goods.getType() == 1) {
@@ -201,6 +202,18 @@ public class CmsGoodsDonSkuController {
         return GatewayResponse.SUCCESS.newBuilder().toResult();
     }
 
+    private void normalizeMultiQuantityConfig(GoodsDonSku sku) {
+        if (!Boolean.TRUE.equals(sku.getIsMultiQuantity())) {
+            sku.setIsMultiQuantity(false);
+            sku.setMultiQuantityMaxNum(1);
+            return;
+        }
+        Integer maxNum = sku.getMultiQuantityMaxNum();
+        if (maxNum == null || maxNum < 2 || maxNum > 100) {
+            throw BusinessRuntimeException.getInstance("多数量单次购买上限必须在2到100之间");
+        }
+    }
+
 
     /**
      * 删除 sku