diff --git a/litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminOrderController.java b/litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminOrderController.java index 613ac15b..ac2ad8b0 100644 --- a/litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminOrderController.java +++ b/litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminOrderController.java @@ -142,11 +142,9 @@ public class AdminOrderController { List 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) { diff --git a/litemall-db/src/main/java/org/linlinjava/litemall/db/dao/GoodsProductMapper.java b/litemall-db/src/main/java/org/linlinjava/litemall/db/dao/GoodsProductMapper.java new file mode 100644 index 00000000..3742a630 --- /dev/null +++ b/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); +} \ No newline at end of file diff --git a/litemall-db/src/main/java/org/linlinjava/litemall/db/service/LitemallGoodsProductService.java b/litemall-db/src/main/java/org/linlinjava/litemall/db/service/LitemallGoodsProductService.java index afcd76a2..6d061b8a 100644 --- a/litemall-db/src/main/java/org/linlinjava/litemall/db/service/LitemallGoodsProductService.java +++ b/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 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); } } \ No newline at end of file diff --git a/litemall-db/src/main/resources/org/linlinjava/litemall/db/dao/GoodsProductMapper.xml b/litemall-db/src/main/resources/org/linlinjava/litemall/db/dao/GoodsProductMapper.xml new file mode 100644 index 00000000..1fb23c27 --- /dev/null +++ b/litemall-db/src/main/resources/org/linlinjava/litemall/db/dao/GoodsProductMapper.xml @@ -0,0 +1,14 @@ + + + + + update litemall_goods_product + set number = number + #{num,jdbcType=INTEGER}, update_time = now() + where id = #{id,jdbcType=INTEGER} + + + update litemall_goods_product + set number = number - #{num,jdbcType=INTEGER}, update_time = now() + where id = #{id,jdbcType=INTEGER} and number >= #{num,jdbcType=INTEGER} + + \ No newline at end of file diff --git a/litemall-db/src/main/resources/org/linlinjava/litemall/db/dao/OrderMapper.xml b/litemall-db/src/main/resources/org/linlinjava/litemall/db/dao/OrderMapper.xml index 80757b7b..6d1dc21e 100644 --- a/litemall-db/src/main/resources/org/linlinjava/litemall/db/dao/OrderMapper.xml +++ b/litemall-db/src/main/resources/org/linlinjava/litemall/db/dao/OrderMapper.xml @@ -2,10 +2,6 @@ - update litemall_order diff --git a/litemall-db/src/test/java/org/linlinjava/litemall/db/StockTest.java b/litemall-db/src/test/java/org/linlinjava/litemall/db/StockTest.java new file mode 100644 index 00000000..760bbeeb --- /dev/null +++ b/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); + } +} diff --git a/litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxOrderController.java b/litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxOrderController.java index 50fe1203..6a081545 100644 --- a/litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxOrderController.java +++ b/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 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) {