Kaynağa Gözat

新增更新商品库存接口、商品列表回显创建时间字段

yzc 2 yıl önce
ebeveyn
işleme
844a1bff97

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

@@ -211,4 +211,18 @@ public class KwpGoodsController {
         return HttpResult.ok(kwpGoodsService.maxPurchaseNum(param));
     }
 
+
+    /**
+     * @desc: 更新商品库存
+     * @author: yzc
+     * @date: 2023-08-03 17:05
+     * @Param param:
+     * @return: com.sckw.core.web.response.HttpResult
+     */
+    @Log(description = "更新商品库存")
+    @PostMapping(value = "/updateAmount", produces = MediaType.APPLICATION_JSON_VALUE)
+    public HttpResult updateAmount(@RequestBody @Validated UpdateAmountParam param) {
+        kwpGoodsService.updateAmount(param);
+        return HttpResult.ok("更新商品库存成功");
+    }
 }

+ 9 - 6
sckw-modules/sckw-product/src/main/java/com/sckw/product/model/GoodsListExport.java

@@ -36,7 +36,7 @@ public class GoodsListExport implements Serializable {
     @ExcelProperty(value = "规格尺寸", index = 4)
     private String spec;
 
-    @ExcelProperty(value = "增值税", index = 5)
+    @ExcelProperty(value = "发票税率", index = 5)
     private String taxRateLabel;
 
     @ExcelProperty(value = "库存数量", index = 6)
@@ -45,13 +45,13 @@ public class GoodsListExport implements Serializable {
     @ExcelProperty(value = "交易方式", index = 7)
     private String tradingLabel;
 
-    @ExcelProperty(value = "最单价", index = 8)
+    @ExcelProperty(value = "最单价", index = 8)
     private String highestPrice;
 
-    @ExcelProperty(value = "最单价", index = 9)
+    @ExcelProperty(value = "最单价", index = 9)
     private String lowestPrice;
 
-    @ExcelProperty(value = "地址信息", index = 10)
+    @ExcelProperty(value = "仓储地址", index = 10)
     private String address;
 
     @ExcelProperty(value = "客户经理", index = 11)
@@ -69,10 +69,13 @@ public class GoodsListExport implements Serializable {
     @ExcelProperty(value = "供应企业", index = 15)
     private String supplyEnt;
 
-    @ExcelProperty(value = "创人", index = 16)
+    @ExcelProperty(value = "创人", index = 16)
     private String createByName;
 
-    @ExcelProperty(value = "备注", index = 17)
+    @ExcelProperty(value = "创建时间", index = 17)
+    private String createTime;
+
+    @ExcelProperty(value = "备注", index = 18)
     private String remark;
 
 }

+ 32 - 0
sckw-modules/sckw-product/src/main/java/com/sckw/product/model/vo/req/UpdateAmountParam.java

@@ -0,0 +1,32 @@
+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 org.hibernate.validator.constraints.Range;
+
+import java.math.BigDecimal;
+
+/**
+ * @desc: 更新商品库存请求参数
+ * @author: yzc
+ * @date: 2023-08-03 17:00
+ */
+@Getter
+@Setter
+@ToString
+public class UpdateAmountParam {
+
+    @NotNull(message = "商品id不能为空")
+    private Long id;
+
+    @NotNull(message = "变更类型不能为空")
+    @Range(min = 0, max = 1,message = "非法变更类型")
+    private Integer updateType;
+
+    @NotNull(message = "变更数量不能为空")
+    @DecimalMin(value = "0.00", message = "变更数量应该大于零")
+    private BigDecimal updateAmount;
+}

+ 6 - 0
sckw-modules/sckw-product/src/main/java/com/sckw/product/model/vo/res/GoodsList.java

@@ -142,6 +142,12 @@ public class GoodsList {
      */
     private String createByName;
 
+    /**
+     * 创建时间
+     */
+    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
+    private Date createTime;
+
     /**
      * 备注
      */

+ 42 - 1
sckw-modules/sckw-product/src/main/java/com/sckw/product/service/KwpGoodsService.java

@@ -27,6 +27,8 @@ import com.sckw.product.enums.GoodsStatusEnum;
 import com.sckw.product.model.*;
 import com.sckw.product.model.vo.req.*;
 import com.sckw.product.model.vo.res.*;
+import com.sckw.redis.constant.RedisConstant;
+import com.sckw.redis.utils.RedissonUtils;
 import com.sckw.system.api.RemoteSystemService;
 import com.sckw.system.api.model.dto.res.EntCacheResDto;
 import com.sckw.system.api.model.dto.res.SysDictResDto;
@@ -229,6 +231,10 @@ public class KwpGoodsService {
         if (Objects.equals(goods.getStatus(), GoodsStatusEnum.PUT_ON_SHELVES.getCode())
                 || Objects.equals(goods.getStatus(), GoodsStatusEnum.TAKE_OFF_SHELVES.getCode())) {
             judgeParameters(param);
+            if (Objects.equals(goods.getStatus(), GoodsStatusEnum.PUT_ON_SHELVES.getCode()) &&
+            goods.getAmount().compareTo(param.getAmount()) != 0) {
+                throw new BusinessException("上架商品不可修改库存数量!");
+            }
         }
         BeanUtils.copyProperties(param, goods);
         if (Objects.nonNull(param.getAddressInfo())) {
@@ -564,7 +570,8 @@ public class KwpGoodsService {
             export.setHighestPrice(Objects.isNull(e.getHighestPrice()) ? null : String.valueOf(e.getHighestPrice()))
                     .setLowestPrice(Objects.isNull(e.getLowestPrice()) ? null : String.valueOf(e.getLowestPrice()))
                     .setAddedTime(Objects.isNull(e.getAddedTime()) ? null : DateUtil.getDateTime(e.getAddedTime()))
-                    .setShelfTime(Objects.isNull(e.getShelfTime()) ? null : DateUtil.getDateTime(e.getShelfTime()));
+                    .setShelfTime(Objects.isNull(e.getShelfTime()) ? null : DateUtil.getDateTime(e.getShelfTime()))
+                    .setCreateTime(Objects.isNull(e.getCreateTime()) ? null : DateUtil.getDateTime(e.getCreateTime()));
             list.add(export);
         });
         return list;
@@ -871,4 +878,38 @@ public class KwpGoodsService {
         BigDecimal divide = new BigDecimal(money).divide(param.getUtilPrice().multiply(new BigDecimal("100")), 2, RoundingMode.HALF_UP);
         return amount.compareTo(divide) < 0 ? amount : divide;
     }
+
+    /**
+     * @desc: 更新商品库存
+     * @author: yzc
+     * @date: 2023-08-03 17:06
+     * @Param param:
+     * @return: void
+     */
+    public void updateAmount(UpdateAmountParam param) {
+        Long id = param.getId();
+        BigDecimal updateAmount = param.getUpdateAmount();
+        LambdaQueryWrapper<KwpGoods> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(KwpGoods::getId, id).eq(KwpGoods::getStatus, GoodsStatusEnum.PUT_ON_SHELVES.getCode())
+                .eq(KwpGoods::getDelFlag, Global.NO).last("LIMIT 1");
+        KwpGoods goods = kwpGoodsMapper.selectOne(wrapper);
+        if (Objects.isNull(goods)) {
+            throw new BusinessException("商品不存在或已下架!");
+        }
+        BigDecimal amount = goods.getAmount();
+        BigDecimal finalAmount = Objects.equals(param.getUpdateType(), 0) ? amount.add(updateAmount) : amount.subtract(updateAmount);
+        if (finalAmount.compareTo(BigDecimal.ZERO) <= 0) {
+            throw new BusinessException("库存数量更新后不能小于等于零!");
+        }
+        String lockKey = String.format(RedisConstant.GOODS_UPDATE_AMOUNT_KEY, id);
+        try {
+            if (Boolean.FALSE.equals(RedissonUtils.tryLock(lockKey, 10L, 30L))) {
+                throw new BusinessException("业务繁忙,请稍后再试!");
+            }
+            goods.setAmount(finalAmount);
+            kwpGoodsMapper.updateById(goods);
+        } finally {
+            RedissonUtils.unlock(lockKey);
+        }
+    }
 }