Commit 303ada8c authored by Junling Bu's avatar Junling Bu
Browse files

update[litemall-wx-api]:小商场支持事务管理

1. 经过分析,目前只有订单服务里面的下单、取消订单和退款取消订单是需要事务管理,其他业务都不需要。
2. 这里事务管理采用编程式事务管理方法。
parent b8395d51
......@@ -3,9 +3,11 @@ package org.linlinjava.litemall.wx;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication(scanBasePackages={"org.linlinjava.litemall.wx","org.linlinjava.litemall.db"})
@MapperScan("org.linlinjava.litemall.db.dao")
@EnableTransactionManagement
public class Application {
public static void main(String[] args) {
......
......@@ -10,6 +10,10 @@ import org.linlinjava.litemall.db.util.OrderUtil;
import org.linlinjava.litemall.db.util.ResponseUtil;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
......@@ -44,6 +48,9 @@ import java.util.Map;
public class WxOrderController {
private final Log logger = LogFactory.getLog(WxOrderController.class);
@Autowired
private PlatformTransactionManager txManager;
@Autowired
private LitemallOrderService orderService;
@Autowired
......@@ -73,6 +80,7 @@ public class WxOrderController {
/**
* 订单列表
*
* @param userId 用户ID
* @param showType 订单信息
* 0, 全部订单
......@@ -99,10 +107,10 @@ public class WxOrderController {
public Object list(@LoginUser Integer userId, Integer showType,
@RequestParam(value = "page", defaultValue = "1") Integer page,
@RequestParam(value = "size", defaultValue = "10") Integer size) {
if(userId == null){
if (userId == null) {
return ResponseUtil.fail401();
}
if(showType == null){
if (showType == null) {
showType = 0;
}
......@@ -111,7 +119,7 @@ public class WxOrderController {
int count = orderService.countByOrderStatus(userId, orderStatus);
List<Map<String, Object>> orderVoList = new ArrayList<>(orderList.size());
for(LitemallOrder order : orderList){
for (LitemallOrder order : orderList) {
Map<String, Object> orderVo = new HashMap<>();
orderVo.put("id", order.getId());
orderVo.put("orderSn", order.getOrderSn());
......@@ -121,7 +129,7 @@ public class WxOrderController {
List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(order.getId());
List<Map<String, Object>> orderGoodsVoList = new ArrayList<>(orderGoodsList.size());
for(LitemallOrderGoods orderGoods : orderGoodsList){
for (LitemallOrderGoods orderGoods : orderGoodsList) {
Map<String, Object> orderGoodsVo = new HashMap<>();
orderGoodsVo.put("id", orderGoods.getId());
orderGoodsVo.put("goodsName", orderGoods.getGoodsName());
......@@ -160,10 +168,10 @@ public class WxOrderController {
*/
@GetMapping("detail")
public Object detail(@LoginUser Integer userId, Integer orderId) {
if(userId == null){
if (userId == null) {
return ResponseUtil.fail401();
}
if(orderId == null){
if (orderId == null) {
return ResponseUtil.fail402();
}
......@@ -172,7 +180,7 @@ public class WxOrderController {
if (null == order) {
return ResponseUtil.fail(403, "订单不存在");
}
if(!order.getUserId().equals(userId)){
if (!order.getUserId().equals(userId)) {
return ResponseUtil.fail(403, "不是当前用户的订单");
}
Map<String, Object> orderVo = new HashMap<String, Object>();
......@@ -190,7 +198,7 @@ public class WxOrderController {
List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(order.getId());
List<Map<String, Object>> orderGoodsVoList = new ArrayList<>(orderGoodsList.size());
for(LitemallOrderGoods orderGoods : orderGoodsList){
for (LitemallOrderGoods orderGoods : orderGoodsList) {
Map<String, Object> orderGoodsVo = new HashMap<>();
orderGoodsVo.put("id", orderGoods.getId());
orderGoodsVo.put("goodsName", orderGoods.getGoodsName());
......@@ -222,16 +230,16 @@ public class WxOrderController {
*/
@PostMapping("submit")
public Object submit(@LoginUser Integer userId, @RequestBody String body) {
if(userId == null){
if (userId == null) {
return ResponseUtil.unlogin();
}
if(body == null){
if (body == null) {
return ResponseUtil.badArgument();
}
Integer cartId = JacksonUtil.parseInteger(body, "cartId");
Integer addressId = JacksonUtil.parseInteger(body, "addressId");
Integer couponId = JacksonUtil.parseInteger(body, "couponId");
if(cartId == null || addressId == null || couponId == null){
if (cartId == null || addressId == null || couponId == null) {
return ResponseUtil.badArgument();
}
......@@ -246,13 +254,12 @@ public class WxOrderController {
List<LitemallCart> checkedGoodsList = null;
if (cartId.equals(0)) {
checkedGoodsList = cartService.queryByUidAndChecked(userId);
}
else {
} else {
LitemallCart cart = cartService.findById(cartId);
checkedGoodsList = new ArrayList<>(1);
checkedGoodsList.add(cart);
}
if(checkedGoodsList.size() == 0){
if (checkedGoodsList.size() == 0) {
return ResponseUtil.badArgumentValue();
}
BigDecimal checkedGoodsPrice = new BigDecimal(0.00);
......@@ -262,7 +269,7 @@ public class WxOrderController {
// 根据订单商品总价计算运费,满88则免运费,否则8元;
BigDecimal freightPrice = new BigDecimal(0.00);
if(checkedGoodsPrice.compareTo(new BigDecimal(88.00)) < 0){
if (checkedGoodsPrice.compareTo(new BigDecimal(88.00)) < 0) {
freightPrice = new BigDecimal(8.00);
}
......@@ -274,7 +281,7 @@ public class WxOrderController {
BigDecimal orderTotalPrice = checkedGoodsPrice.add(freightPrice).subtract(couponPrice);
BigDecimal actualPrice = orderTotalPrice.subtract(integralPrice);
// 添加订单
// 订单
LitemallOrder order = new LitemallOrder();
order.setUserId(userId);
order.setOrderSn(orderService.generateOrderSn(userId));
......@@ -290,10 +297,10 @@ public class WxOrderController {
order.setIntegralPrice(integralPrice);
order.setOrderPrice(orderTotalPrice);
order.setActualPrice(actualPrice);
orderService.add(order);
// 添加订单商品表
for(LitemallCart cartGoods : checkedGoodsList){
// 订单商品
List<LitemallOrderGoods> orderGoodsList = new ArrayList<>(checkedGoodsList.size());
for (LitemallCart cartGoods : checkedGoodsList) {
LitemallOrderGoods orderGoods = new LitemallOrderGoods();
orderGoods.setOrderId(order.getId());
orderGoods.setGoodsId(cartGoods.getGoodsId());
......@@ -305,6 +312,19 @@ public class WxOrderController {
orderGoods.setNumber(cartGoods.getNumber());
orderGoods.setGoodsSpecificationIds(cartGoods.getGoodsSpecificationIds());
orderGoods.setGoodsSpecificationValues(cartGoods.getGoodsSpecificationValues());
orderGoodsList.add(orderGoods);
}
// 开启事务管理
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = txManager.getTransaction(def);
try {
// 添加订单表项
orderService.add(order);
// 添加订单商品表项
for (LitemallOrderGoods orderGoods : orderGoodsList) {
orderGoodsService.add(orderGoods);
}
......@@ -313,19 +333,22 @@ public class WxOrderController {
// 商品货品数量减少
for (LitemallCart checkGoods : checkedGoodsList) {
Integer productId= checkGoods.getProductId();
Integer productId = checkGoods.getProductId();
LitemallProduct product = productService.findById(productId);
if(product == null){
return ResponseUtil.badArgumentValue();
}
Integer remainNumber = product.getGoodsNumber() - checkGoods.getNumber();
if(remainNumber < 0){
return ResponseUtil.badArgumentValue();
if (remainNumber < 0) {
throw new RuntimeException("下单的商品货品数量大于库存量");
}
product.setGoodsNumber(remainNumber);
productService.updateById(product);
}
} catch (Exception ex) {
txManager.rollback(status);
logger.error("系统内部错误", ex);
return ResponseUtil.fail(403, "下单失败");
}
txManager.commit(status);
Map<String, Object> data = new HashMap<>();
data.put("orderId", order.getId());
......@@ -337,6 +360,7 @@ public class WxOrderController {
* 1. 检测当前订单是否能够取消
* 2. 设置订单取消状态
* 3. 商品货品数量增加
*
* @param userId 用户ID
* @param body 订单信息,{ orderId:xxx }
* @return 订单操作结果
......@@ -345,44 +369,53 @@ public class WxOrderController {
*/
@PostMapping("cancel")
public Object cancel(@LoginUser Integer userId, @RequestBody String body) {
if(userId == null){
if (userId == null) {
return ResponseUtil.unlogin();
}
Integer orderId = JacksonUtil.parseInteger(body, "orderId");
if(orderId == null){
if (orderId == null) {
return ResponseUtil.badArgument();
}
LitemallOrder order = orderService.findById(orderId);
if(order == null){
if (order == null) {
return ResponseUtil.badArgumentValue();
}
if(!order.getUserId().equals(userId)){
if (!order.getUserId().equals(userId)) {
return ResponseUtil.badArgumentValue();
}
// 检测是否能够取消
OrderHandleOption handleOption = OrderUtil.build(order);
if(!handleOption.isCancel()){
if (!handleOption.isCancel()) {
return ResponseUtil.fail(403, "订单不能取消");
}
// 开启事务管理
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = txManager.getTransaction(def);
try {
// 设置订单已取消状态
order.setOrderStatus(OrderUtil.STATUS_CANCEL);
orderService.update(order);
// 商品货品数量增加
List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(orderId);
for(LitemallOrderGoods orderGoods : orderGoodsList){
for (LitemallOrderGoods orderGoods : orderGoodsList) {
Integer productId = orderGoods.getProductId();
LitemallProduct product = productService.findById(productId);
if(product == null){
return ResponseUtil.badArgumentValue();
}
Integer number = product.getGoodsNumber() + orderGoods.getNumber();
product.setGoodsNumber(number);
productService.updateById(product);
}
} catch (Exception ex) {
txManager.rollback(status);
logger.error("系统内部错误", ex);
return ResponseUtil.fail(403, "订单取消失败");
}
txManager.commit(status);
return ResponseUtil.ok();
}
......@@ -391,6 +424,7 @@ public class WxOrderController {
* 1. 检测当前订单是否能够付款
* 2. 设置订单付款状态
* 3. TODO 微信后台申请支付,同时设置付款状态
*
* @param userId 用户ID
* @param body 订单信息,{ orderId:xxx }
* @return 订单操作结果
......@@ -399,25 +433,25 @@ public class WxOrderController {
*/
@PostMapping("pay")
public Object pay(@LoginUser Integer userId, @RequestBody String body) {
if(userId == null){
if (userId == null) {
return ResponseUtil.unlogin();
}
Integer orderId = JacksonUtil.parseInteger(body, "orderId");
if(orderId == null){
if (orderId == null) {
return ResponseUtil.badArgument();
}
LitemallOrder order = orderService.findById(orderId);
if(order == null){
if (order == null) {
return ResponseUtil.badArgument();
}
if(!order.getUserId().equals(userId)){
if (!order.getUserId().equals(userId)) {
return ResponseUtil.badArgumentValue();
}
// 检测是否能够付款
OrderHandleOption handleOption = OrderUtil.build(order);
if(!handleOption.isPay()){
if (!handleOption.isPay()) {
return ResponseUtil.fail(403, "订单不能付款");
}
......@@ -438,43 +472,44 @@ public class WxOrderController {
* 付款成功回调接口
* 1. 检测当前订单是否是付款状态
* 2. 设置订单付款成功状态相关信息
*
* @param userId 用户ID
* @param body 订单信息,{ orderId:xxx, payId: xxx }
* @return 订单操作结果
* 成功则 { errno: 0, errmsg: '成功' }
* 失败则 { errno: XXX, errmsg: XXX }
*
* <p>
* 注意,这里pay_notify是示例地址,开发者应该设立一个隐蔽的回调地址
* TODO 这里需要根据微信支付文档设计
*/
@PostMapping("pay_notify")
public Object pay_notify(@LoginUser Integer userId, @RequestBody String body) {
if(userId == null){
if (userId == null) {
return ResponseUtil.unlogin();
}
Integer orderId = JacksonUtil.parseInteger(body, "orderId");
Integer payId = JacksonUtil.parseInteger(body, "payId");
if(orderId == null || payId == null){
if (orderId == null || payId == null) {
return ResponseUtil.badArgument();
}
LitemallOrder order = orderService.findById(orderId);
if(order == null){
if (order == null) {
return ResponseUtil.badArgument();
}
if(!order.getUserId().equals(userId)){
if (!order.getUserId().equals(userId)) {
return ResponseUtil.badArgumentValue();
}
// 检测是否是付款状态
if(!order.getOrderStatus().equals(OrderUtil.STATUS_PAY)){
if (!order.getOrderStatus().equals(OrderUtil.STATUS_PAY)) {
logger.error("系统内部错误");
}
if(!order.getPayId().equals(payId)){
if (!order.getPayId().equals(payId)) {
logger.error("系统内部错误");
}
Short payStatus = (short)2;
Short payStatus = (short) 2;
order.setPayStatus(payStatus);
order.setPayTime(LocalDateTime.now());
orderService.update(order);
......@@ -489,6 +524,7 @@ public class WxOrderController {
* 2. 设置订单退款取消状态
* 3. TODO 退款
* 4. 商品货品数量增加
*
* @param userId 用户ID
* @param body 订单信息,{ orderId:xxx }
* @return 订单操作结果
......@@ -497,27 +533,32 @@ public class WxOrderController {
*/
@PostMapping("refund")
public Object refund(@LoginUser Integer userId, @RequestBody String body) {
if(userId == null){
if (userId == null) {
return ResponseUtil.unlogin();
}
Integer orderId = JacksonUtil.parseInteger(body, "orderId");
if(orderId == null){
if (orderId == null) {
return ResponseUtil.badArgument();
}
LitemallOrder order = orderService.findById(orderId);
if(order == null){
if (order == null) {
return ResponseUtil.badArgument();
}
if(!order.getUserId().equals(userId)){
if (!order.getUserId().equals(userId)) {
return ResponseUtil.badArgumentValue();
}
OrderHandleOption handleOption = OrderUtil.build(order);
if(!handleOption.isRefund()){
if (!handleOption.isRefund()) {
return ResponseUtil.fail(403, "订单不能取消");
}
// 开启事务管理
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = txManager.getTransaction(def);
try {
// 设置订单取消状态
order.setOrderStatus(OrderUtil.STATUS_REFUND);
orderService.update(order);
......@@ -526,16 +567,19 @@ public class WxOrderController {
// 商品货品数量增加
List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(orderId);
for(LitemallOrderGoods orderGoods : orderGoodsList){
for (LitemallOrderGoods orderGoods : orderGoodsList) {
Integer productId = orderGoods.getProductId();
LitemallProduct product = productService.findById(productId);
if(product == null){
return ResponseUtil.badArgumentValue();
}
Integer number = product.getGoodsNumber() + orderGoods.getNumber();
product.setGoodsNumber(number);
productService.updateById(product);
}
} catch (Exception ex) {
txManager.rollback(status);
logger.error("系统内部错误", ex);
return ResponseUtil.fail(403, "订单退款失败");
}
txManager.commit(status);
return ResponseUtil.ok();
}
......@@ -544,6 +588,7 @@ public class WxOrderController {
* 发货
* 1. 检测当前订单是否能够发货
* 2. 设置订单发货状态
*
* @param userId 用户ID
* @param body 订单信息,{ orderId:xxx, shipSn: xxx, shipChannel: xxx }
* @return 订单操作结果
......@@ -552,26 +597,26 @@ public class WxOrderController {
*/
@PostMapping("ship")
public Object ship(@LoginUser Integer userId, @RequestBody String body) {
if(userId == null){
if (userId == null) {
return ResponseUtil.unlogin();
}
Integer orderId = JacksonUtil.parseInteger(body, "orderId");
String shipSn = JacksonUtil.parseString(body, "shipSn");
String shipChannel = JacksonUtil.parseString(body, "shipChannel");
if(orderId == null || shipSn == null || shipChannel == null){
if (orderId == null || shipSn == null || shipChannel == null) {
return ResponseUtil.badArgument();
}
LitemallOrder order = orderService.findById(orderId);
if(order == null){
if (order == null) {
return ResponseUtil.badArgument();
}
if(!order.getUserId().equals(userId)){
if (!order.getUserId().equals(userId)) {
return ResponseUtil.badArgumentValue();
}
// 如果订单不是已付款状态,则不能发货
if(!order.getOrderStatus().equals(OrderUtil.STATUS_PAY)){
if (!order.getOrderStatus().equals(OrderUtil.STATUS_PAY)) {
return ResponseUtil.fail(403, "订单不能确认收货");
}
......@@ -588,6 +633,7 @@ public class WxOrderController {
* 确认收货
* 1. 检测当前订单是否能够确认订单
* 2. 设置订单确认状态
*
* @param userId 用户ID
* @param body 订单信息,{ orderId:xxx }
* @return 订单操作结果
......@@ -596,24 +642,24 @@ public class WxOrderController {
*/
@PostMapping("confirm")
public Object confirm(@LoginUser Integer userId, @RequestBody String body) {
if(userId == null){
if (userId == null) {
return ResponseUtil.unlogin();
}
Integer orderId = JacksonUtil.parseInteger(body, "orderId");
if(orderId == null){
if (orderId == null) {
return ResponseUtil.badArgument();
}
LitemallOrder order = orderService.findById(orderId);
if(order == null){
if (order == null) {
return ResponseUtil.badArgument();
}
if(!order.getUserId().equals(userId)){
if (!order.getUserId().equals(userId)) {
return ResponseUtil.badArgumentValue();
}
OrderHandleOption handleOption = OrderUtil.build(order);
if(!handleOption.isConfirm()){
if (!handleOption.isConfirm()) {
return ResponseUtil.fail(403, "订单不能确认收货");
}
......@@ -629,6 +675,7 @@ public class WxOrderController {
* 自动确认收货
* 1. 检测当前订单是否能够自动确认订单
* 2. 设置订单自动确认状态
*
* @param userId 用户ID
* @param body 订单信息,{ orderId:xxx }
* @return 订单操作结果
......@@ -637,24 +684,24 @@ public class WxOrderController {
*/
@PostMapping("autoconfirm")
public Object autoconfirm(@LoginUser Integer userId, @RequestBody String body) {
if(userId == null){
if (userId == null) {
return ResponseUtil.unlogin();
}
Integer orderId = JacksonUtil.parseInteger(body, "orderId");
if(orderId == null){
if (orderId == null) {
return ResponseUtil.badArgument();
}
LitemallOrder order = orderService.findById(orderId);
if(order == null){
if (order == null) {
return ResponseUtil.badArgument();
}
if(!order.getUserId().equals(userId)){
if (!order.getUserId().equals(userId)) {
return ResponseUtil.badArgumentValue();
}
OrderHandleOption handleOption = OrderUtil.build(order);
if(!handleOption.isConfirm()){
if (!handleOption.isConfirm()) {
return ResponseUtil.fail(403, "订单不能确认收货");
}
......@@ -669,6 +716,7 @@ public class WxOrderController {
* 删除订单
* 1. 检测当前订单是否删除
* 2. 设置订单删除状态
*
* @param userId 用户ID
* @param body 订单信息,{ orderId:xxx }
* @return 订单操作结果
......@@ -677,24 +725,24 @@ public class WxOrderController {
*/
@PostMapping("delete")
public Object delete(@LoginUser Integer userId, @RequestBody String body) {
if(userId == null){
if (userId == null) {
return ResponseUtil.unlogin();
}
Integer orderId = JacksonUtil.parseInteger(body, "orderId");
if(orderId == null){
if (orderId == null) {
return ResponseUtil.badArgument();
}
LitemallOrder order = orderService.findById(orderId);
if(order == null){
if (order == null) {
return ResponseUtil.badArgument();
}
if(!order.getUserId().equals(userId)){
if (!order.getUserId().equals(userId)) {
return ResponseUtil.badArgumentValue();
}
OrderHandleOption handleOption = OrderUtil.build(order);
if(!handleOption.isDelete()){
if (!handleOption.isDelete()) {
return ResponseUtil.fail(403, "订单不能删除");
}
......@@ -717,10 +765,10 @@ public class WxOrderController {
*/
@GetMapping("comment")
public Object comment(@LoginUser Integer userId, Integer orderId, Integer goodsId) {
if(userId == null){
if (userId == null) {
return ResponseUtil.unlogin();
}
if(orderId == null){
if (orderId == null) {
return ResponseUtil.badArgument();
}
......@@ -729,7 +777,7 @@ public class WxOrderController {
Assert.state(size < 2, "存在多个符合条件的订单商品");
if(size == 0){
if (size == 0) {
return ResponseUtil.badArgumentValue();
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment