Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Litemall
Commits
45b0da77
Commit
45b0da77
authored
Nov 22, 2018
by
Junling Bu
Browse files
fix: 商品货品库存增加和减少采用手写MySQL语句。
parent
42e686ab
Changes
7
Hide whitespace changes
Inline
Side-by-side
litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminOrderController.java
View file @
45b0da77
...
...
@@ -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
)
{
...
...
litemall-db/src/main/java/org/linlinjava/litemall/db/dao/GoodsProductMapper.java
0 → 100644
View file @
45b0da77
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
litemall-db/src/main/java/org/linlinjava/litemall/db/service/LitemallGoodsProductService.java
View file @
45b0da77
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
g
oodsProductMapper
.
selectByExample
(
example
);
return
litemallG
oodsProductMapper
.
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
)
{
g
oodsProductMapper
.
logicalDeleteByPrimaryKey
(
id
);
litemallG
oodsProductMapper
.
logicalDeleteByPrimaryKey
(
id
);
}
public
void
add
(
LitemallGoodsProduct
goodsProduct
)
{
goodsProduct
.
setAddTime
(
LocalDateTime
.
now
());
goodsProduct
.
setUpdateTime
(
LocalDateTime
.
now
());
g
oodsProductMapper
.
insertSelective
(
goodsProduct
);
litemallG
oodsProductMapper
.
insertSelective
(
goodsProduct
);
}
public
int
count
()
{
LitemallGoodsProductExample
example
=
new
LitemallGoodsProductExample
();
example
.
or
().
andDeletedEqualTo
(
false
);
return
(
int
)
g
oodsProductMapper
.
countByExample
(
example
);
return
(
int
)
litemallG
oodsProductMapper
.
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
litemall-db/src/main/resources/org/linlinjava/litemall/db/dao/GoodsProductMapper.xml
0 → 100644
View file @
45b0da77
<?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>
\ No newline at end of file
litemall-db/src/main/resources/org/linlinjava/litemall/db/dao/OrderMapper.xml
View file @
45b0da77
...
...
@@ -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"
>
...
...
litemall-db/src/test/java/org/linlinjava/litemall/db/StockTest.java
0 → 100644
View file @
45b0da77
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
);
}
}
litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxOrderController.java
View file @
45b0da77
...
...
@@ -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
)
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment