Преглед изворни кода

提供根据订单数量获取商品价格接口

yzc пре 2 година
родитељ
комит
9d847b34b2

+ 12 - 0
sckw-modules/sckw-product/src/main/java/com/sckw/product/controller/KwpGoodsController.java

@@ -223,4 +223,16 @@ public class KwpGoodsController {
     public HttpResult nameDuplicationJudgment(@RequestParam String name, @RequestParam Long supplyEntId) {
         return HttpResult.ok("商品名称判重成功", kwpGoodsService.nameDuplicationJudgment(name, supplyEntId, null));
     }
+
+    /**
+     * @desc: 获取商品价格
+     * @author: yzc
+     * @date: 2023-09-05 10:54
+     * @Param param:
+     * @return: com.sckw.core.web.response.HttpResult
+     */
+    @PostMapping(value = "/getPrice", produces = MediaType.APPLICATION_JSON_VALUE)
+    public HttpResult getPrice(@RequestBody @Validated GetPriceParam param) {
+        return HttpResult.ok("获取商品价格成功", kwpGoodsService.getPrice(param));
+    }
 }

+ 28 - 0
sckw-modules/sckw-product/src/main/java/com/sckw/product/model/vo/req/GetPriceParam.java

@@ -0,0 +1,28 @@
+package com.sckw.product.model.vo.req;
+
+import jakarta.validation.constraints.DecimalMin;
+import jakarta.validation.constraints.NotNull;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.ToString;
+
+import java.math.BigDecimal;
+
+/**
+ * @desc: 获取价格请求参数
+ * @author: yzc
+ * @date: 2023-09-05 10:03
+ */
+@Getter
+@Setter
+@ToString
+public class GetPriceParam {
+
+    @NotNull(message = "商品id不能为空")
+    private Long goodsId;
+
+    @NotNull(message = "订单数量不能为空")
+    @DecimalMin(value = "0.01",message = "订单数量必须大于零")
+    private BigDecimal num;
+
+}

+ 35 - 2
sckw-modules/sckw-product/src/main/java/com/sckw/product/service/KwpGoodsService.java

@@ -243,7 +243,9 @@ public class KwpGoodsService {
         List<KwpGoodsPriceRange> priceRanges = kwpGoodsPriceRangeService.getByGoodsId(id);
         List<GoodsPriceRangesDetail> ranges = BeanUtils.copyToList(priceRanges, GoodsPriceRangesDetail.class);
         ranges.stream().filter(r -> r.getEndAmount().compareTo(new BigDecimal("-1.00")) == 0)
-                .forEach(r -> {r.setEndAmountLabel("不限");});
+                .forEach(r -> {
+                    r.setEndAmountLabel("不限");
+                });
         //商品属性信息
         List<KwpGoodsAttribute> attributesList = kwpGoodsAttributeService.getByGoodsId(id);
         List<GoodsAttributesDetail> attributes = BeanUtils.copyToList(attributesList, GoodsAttributesDetail.class);
@@ -1018,8 +1020,39 @@ public class KwpGoodsService {
      */
     public Long getCountBySupplyEnt(Long supplyEntId) {
         LambdaQueryWrapper<KwpGoods> wrapper = new LambdaQueryWrapper<>();
-        wrapper.eq(KwpGoods::getSupplyEntId,supplyEntId).eq(KwpGoods::getDelFlag, Global.NO).
+        wrapper.eq(KwpGoods::getSupplyEntId, supplyEntId).eq(KwpGoods::getDelFlag, Global.NO).
                 ne(KwpGoods::getStatus, GoodsStatusEnum.SAVED.getCode());
         return kwpGoodsMapper.selectCount(wrapper);
     }
+
+    /**
+     * @desc: 获取商品价格
+     * @author: yzc
+     * @date: 2023-09-05 10:54
+     * @Param param:
+     * @return: java.math.BigDecimal
+     */
+    public BigDecimal getPrice(GetPriceParam param) {
+        List<KwpGoodsPriceRange> priceRanges = kwpGoodsPriceRangeService.getByGoodsId(param.getGoodsId());
+        if (CollectionUtils.isEmpty(priceRanges)) {
+            throw new BusinessException("商品价格信息异常!");
+        }
+        //价格区间空挡四舍五入取整,保证数量在价格区间段
+        BigDecimal num = param.getNum().setScale(0, RoundingMode.HALF_UP);
+        BigDecimal notLimit = null;
+        BigDecimal price = null;
+        for (KwpGoodsPriceRange e : priceRanges) {
+            if (num.compareTo(e.getStartAmount()) >= 0) {
+                BigDecimal endAmount = e.getEndAmount();
+                if (num.compareTo(endAmount) <= 0) {
+                    price = e.getPrice();
+                    break;
+                }
+                if (endAmount.compareTo(new BigDecimal("-1.00")) == 0) {
+                    notLimit = e.getPrice();
+                }
+            }
+        }
+        return Objects.nonNull(price) ? price : notLimit;
+    }
 }