浏览代码

fix: 商品货品库存增加和减少采用手写MySQL语句。

Junling Bu 7 年之前
父节点
当前提交
45b0da77eb

+ 6 - 9
litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminOrderController.java

@@ -142,11 +142,9 @@ public class AdminOrderController {
             List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(orderId);
             for (LitemallOrderGoods orderGoods : orderGoodsList) {
                 Integer productId = orderGoods.getProductId();
-                LitemallGoodsProduct product = productService.findById(productId);
-                Integer number = product.getNumber() + orderGoods.getNumber();
-                product.setNumber(number);
-                if (productService.updateById(product) == 0) {
-                    throw new Exception("跟新数据失败");
+                Short number = orderGoods.getNumber();
+                if (productService.addStock(productId, number) == 0) {
+                    throw new Exception("商品货品库存增加失败");
                 }
             }
         } catch (Exception ex) {
@@ -303,10 +301,9 @@ public class AdminOrderController {
                 for (LitemallOrderGoods orderGoods : orderGoodsList) {
                     Integer productId = orderGoods.getProductId();
                     LitemallGoodsProduct product = productService.findById(productId);
-                    Integer number = product.getNumber() + orderGoods.getNumber();
-                    product.setNumber(number);
-                    if (productService.updateById(product) == 0) {
-                        throw new Exception("跟新数据失败");
+                    Short number = orderGoods.getNumber();
+                    if (productService.addStock(productId, number) == 0) {
+                        throw new Exception("商品货品库存增加失败");
                     }
                 }
             } catch (Exception ex) {

+ 8 - 0
litemall-db/src/main/java/org/linlinjava/litemall/db/dao/GoodsProductMapper.java

@@ -0,0 +1,8 @@
+package org.linlinjava.litemall.db.dao;
+
+import org.apache.ibatis.annotations.Param;
+
+public interface GoodsProductMapper {
+    int addStock(@Param("id") Integer id, @Param("num") Short num);
+    int reduceStock(@Param("id") Integer id, @Param("num") Short num);
+}

+ 19 - 12
litemall-db/src/main/java/org/linlinjava/litemall/db/service/LitemallGoodsProductService.java

@@ -1,5 +1,7 @@
 package org.linlinjava.litemall.db.service;
 
+import org.apache.ibatis.annotations.Param;
+import org.linlinjava.litemall.db.dao.GoodsProductMapper;
 import org.linlinjava.litemall.db.dao.LitemallGoodsProductMapper;
 import org.linlinjava.litemall.db.domain.LitemallGoodsProduct;
 import org.linlinjava.litemall.db.domain.LitemallGoodsProductExample;
@@ -12,42 +14,47 @@ import java.util.List;
 @Service
 public class LitemallGoodsProductService {
     @Resource
-    private LitemallGoodsProductMapper goodsProductMapper;
+    private LitemallGoodsProductMapper litemallGoodsProductMapper;
+    @Resource
+    private GoodsProductMapper goodsProductMapper;
 
     public List<LitemallGoodsProduct> queryByGid(Integer gid) {
         LitemallGoodsProductExample example = new LitemallGoodsProductExample();
         example.or().andGoodsIdEqualTo(gid).andDeletedEqualTo(false);
-        return goodsProductMapper.selectByExample(example);
+        return litemallGoodsProductMapper.selectByExample(example);
     }
 
     public LitemallGoodsProduct findById(Integer id) {
-        return goodsProductMapper.selectByPrimaryKey(id);
-    }
-
-    public int updateById(LitemallGoodsProduct goodsProduct) {
-        goodsProduct.setUpdateTime(LocalDateTime.now());
-        return goodsProductMapper.updateByPrimaryKeySelective(goodsProduct);
+        return litemallGoodsProductMapper.selectByPrimaryKey(id);
     }
 
     public void deleteById(Integer id) {
-        goodsProductMapper.logicalDeleteByPrimaryKey(id);
+        litemallGoodsProductMapper.logicalDeleteByPrimaryKey(id);
     }
 
     public void add(LitemallGoodsProduct goodsProduct) {
         goodsProduct.setAddTime(LocalDateTime.now());
         goodsProduct.setUpdateTime(LocalDateTime.now());
-        goodsProductMapper.insertSelective(goodsProduct);
+        litemallGoodsProductMapper.insertSelective(goodsProduct);
     }
 
     public int count() {
         LitemallGoodsProductExample example = new LitemallGoodsProductExample();
         example.or().andDeletedEqualTo(false);
-        return (int) goodsProductMapper.countByExample(example);
+        return (int) litemallGoodsProductMapper.countByExample(example);
     }
 
     public void deleteByGid(Integer gid) {
         LitemallGoodsProductExample example = new LitemallGoodsProductExample();
         example.or().andGoodsIdEqualTo(gid);
-        goodsProductMapper.logicalDeleteByExample(example);
+        litemallGoodsProductMapper.logicalDeleteByExample(example);
+    }
+
+    public int addStock(Integer id, Short num){
+        return goodsProductMapper.addStock(id, num);
+    }
+
+    public int reduceStock(Integer id, Short num){
+        return goodsProductMapper.reduceStock(id, num);
     }
 }

+ 14 - 0
litemall-db/src/main/resources/org/linlinjava/litemall/db/dao/GoodsProductMapper.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="org.linlinjava.litemall.db.dao.GoodsProductMapper">
+    <update id="addStock" parameterType="map">
+        update litemall_goods_product
+        set number = number + #{num,jdbcType=INTEGER}, update_time = now()
+        where id = #{id,jdbcType=INTEGER}
+    </update>
+    <update id="reduceStock" parameterType="map">
+        update litemall_goods_product
+        set number = number - #{num,jdbcType=INTEGER}, update_time = now()
+        where id = #{id,jdbcType=INTEGER} and number >= #{num,jdbcType=INTEGER}
+    </update>
+</mapper>

+ 0 - 4
litemall-db/src/main/resources/org/linlinjava/litemall/db/dao/OrderMapper.xml

@@ -2,10 +2,6 @@
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="org.linlinjava.litemall.db.dao.OrderMapper">
     <update id="updateWithOptimisticLocker" parameterType="map">
-        <!--
-          WARNING - @mbg.generated
-          This element is automatically generated by MyBatis Generator, do not modify.
-        -->
         update litemall_order
         <set>
             <if test="order.id != null">

+ 31 - 0
litemall-db/src/test/java/org/linlinjava/litemall/db/StockTest.java

@@ -0,0 +1,31 @@
+package org.linlinjava.litemall.db;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.linlinjava.litemall.db.dao.GoodsProductMapper;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.context.web.WebAppConfiguration;
+
+@WebAppConfiguration
+@RunWith(SpringJUnit4ClassRunner.class)
+@SpringBootTest
+public class StockTest {
+    @Autowired
+    private GoodsProductMapper goodsProductMapper;
+
+    @Test
+    public void testReduceStock() {
+        Integer id = 1;
+        Short num = 10;
+        goodsProductMapper.reduceStock(id, num);
+    }
+
+    @Test
+    public void testAddStock() {
+        Integer id = 1;
+        Short num = 10;
+        goodsProductMapper.addStock(id, num);
+    }
+}

+ 6 - 9
litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxOrderController.java

@@ -336,7 +336,7 @@ public class WxOrderController {
             }
         }
 
-        // 根据订单商品总价计算运费,满88则免运费,否则8元;
+        // 根据订单商品总价计算运费,满足条件(例如88元)则免运费,否则需要支付运费(例如8元
         BigDecimal freightPrice = new BigDecimal(0.00);
         if (checkedGoodsPrice.compareTo(SystemConfig.getFreightLimit()) < 0) {
             freightPrice = SystemConfig.getFreight();
@@ -414,9 +414,8 @@ public class WxOrderController {
                 if (remainNumber < 0) {
                     throw new RuntimeException("下单的商品货品数量大于库存量");
                 }
-                product.setNumber(remainNumber);
-                if (productService.updateById(product) == 0) {
-                    throw new Exception("更新数据失败");
+                if (productService.reduceStock(productId, checkGoods.getNumber()) == 0) {
+                    throw new Exception("商品货品库存减少失败");
                 }
             }
 
@@ -508,11 +507,9 @@ public class WxOrderController {
             List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(orderId);
             for (LitemallOrderGoods orderGoods : orderGoodsList) {
                 Integer productId = orderGoods.getProductId();
-                LitemallGoodsProduct product = productService.findById(productId);
-                Integer number = product.getNumber() + orderGoods.getNumber();
-                product.setNumber(number);
-                if (productService.updateById(product) == 0) {
-                    throw new Exception("更新数据失败");
+                Short number = orderGoods.getNumber();
+                if (productService.addStock(productId, number) == 0) {
+                    throw new Exception("商品货品库存增加失败");
                 }
             }
         } catch (Exception ex) {