GitHub:https://github.com/nimblepeanut/wx-mall.git
项目简介项目使用前后台分离的设计方式、RESTful的设计风格开发。
后台采用Layui框架搭建页面,Ajax 异步请求、Thymeleaf 模板引擎搭配使用。
前台为微信小程序
@Override
@Transactional(
isolation = Isolation.SERIALIZABLE,
rollbackFor = Exception.class
)
public Integer payment(Integer userId) {
// 购物车
List cartList = cartMapper.all(userId);
// 总金额,初始值为0
BigDecimal totalPrice = new BigDecimal("0");
// 遍历修改每个商品的数量
for (Cart cart : cartList) {
// 减少对应商品的库存
goodsMapper.updateCount(cart.getGoods().getId(), cart.getCount());
// 注意:BigDecimal不能用 "*"、"+"等运算,必须用方法
// multiply:乘,
// 需要用 new BigDecimal() 方法把数量从Integer转为BigDecimal
BigDecimal newNum = cart.getGoods().getPrice().multiply(new BigDecimal(cart.getCount()));
// add:加
totalPrice = totalPrice.add(newNum);
}
// 修改用户余额
Integer userMoney = userMapper.updateMoney(userId, totalPrice);
// 清空购物车,
// 修改状态 state = 1
Integer emptyCart = cartMapper.updateState(userId);
// 判断是否成功
if (userMoney > 0 && emptyCart > 0) {
return 1;
} else {
return 0;
}
}



