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; ...@@ -3,9 +3,11 @@ package org.linlinjava.litemall.wx;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication(scanBasePackages={"org.linlinjava.litemall.wx","org.linlinjava.litemall.db"}) @SpringBootApplication(scanBasePackages={"org.linlinjava.litemall.wx","org.linlinjava.litemall.db"})
@MapperScan("org.linlinjava.litemall.db.dao") @MapperScan("org.linlinjava.litemall.db.dao")
@EnableTransactionManagement
public class Application { public class Application {
public static void main(String[] args) { public static void main(String[] args) {
......
...@@ -10,6 +10,10 @@ import org.linlinjava.litemall.db.util.OrderUtil; ...@@ -10,6 +10,10 @@ import org.linlinjava.litemall.db.util.OrderUtil;
import org.linlinjava.litemall.db.util.ResponseUtil; import org.linlinjava.litemall.db.util.ResponseUtil;
import org.linlinjava.litemall.wx.annotation.LoginUser; import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.springframework.beans.factory.annotation.Autowired; 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.util.Assert;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -44,6 +48,9 @@ import java.util.Map; ...@@ -44,6 +48,9 @@ import java.util.Map;
public class WxOrderController { public class WxOrderController {
private final Log logger = LogFactory.getLog(WxOrderController.class); private final Log logger = LogFactory.getLog(WxOrderController.class);
@Autowired
private PlatformTransactionManager txManager;
@Autowired @Autowired
private LitemallOrderService orderService; private LitemallOrderService orderService;
@Autowired @Autowired
...@@ -73,36 +80,37 @@ public class WxOrderController { ...@@ -73,36 +80,37 @@ public class WxOrderController {
/** /**
* 订单列表 * 订单列表
* @param userId 用户ID *
* @param userId 用户ID
* @param showType 订单信息 * @param showType 订单信息
* 0, 全部订单 * 0, 全部订单
* 1,待付款 * 1,待付款
* 2,待发货 * 2,待发货
* 3,待收货 * 3,待收货
* 4,待评价 * 4,待评价
* @param page 分页页数 * @param page 分页页数
* @param size 分页大小 * @param size 分页大小
* @return 订单操作结果 * @return 订单操作结果
* 成功则 * 成功则
* { * {
* errno: 0, * errno: 0,
* errmsg: '成功', * errmsg: '成功',
* data: * data:
* { * {
* data: xxx , * data: xxx ,
* count: xxx * count: xxx
* } * }
* } * }
* 失败则 { errno: XXX, errmsg: XXX } * 失败则 { errno: XXX, errmsg: XXX }
*/ */
@RequestMapping("list") @RequestMapping("list")
public Object list(@LoginUser Integer userId, Integer showType, public Object list(@LoginUser Integer userId, Integer showType,
@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "page", defaultValue = "1") Integer page,
@RequestParam(value = "size", defaultValue = "10") Integer size) { @RequestParam(value = "size", defaultValue = "10") Integer size) {
if(userId == null){ if (userId == null) {
return ResponseUtil.fail401(); return ResponseUtil.fail401();
} }
if(showType == null){ if (showType == null) {
showType = 0; showType = 0;
} }
...@@ -111,7 +119,7 @@ public class WxOrderController { ...@@ -111,7 +119,7 @@ public class WxOrderController {
int count = orderService.countByOrderStatus(userId, orderStatus); int count = orderService.countByOrderStatus(userId, orderStatus);
List<Map<String, Object>> orderVoList = new ArrayList<>(orderList.size()); List<Map<String, Object>> orderVoList = new ArrayList<>(orderList.size());
for(LitemallOrder order : orderList){ for (LitemallOrder order : orderList) {
Map<String, Object> orderVo = new HashMap<>(); Map<String, Object> orderVo = new HashMap<>();
orderVo.put("id", order.getId()); orderVo.put("id", order.getId());
orderVo.put("orderSn", order.getOrderSn()); orderVo.put("orderSn", order.getOrderSn());
...@@ -121,7 +129,7 @@ public class WxOrderController { ...@@ -121,7 +129,7 @@ public class WxOrderController {
List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(order.getId()); List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(order.getId());
List<Map<String, Object>> orderGoodsVoList = new ArrayList<>(orderGoodsList.size()); List<Map<String, Object>> orderGoodsVoList = new ArrayList<>(orderGoodsList.size());
for(LitemallOrderGoods orderGoods : orderGoodsList){ for (LitemallOrderGoods orderGoods : orderGoodsList) {
Map<String, Object> orderGoodsVo = new HashMap<>(); Map<String, Object> orderGoodsVo = new HashMap<>();
orderGoodsVo.put("id", orderGoods.getId()); orderGoodsVo.put("id", orderGoods.getId());
orderGoodsVo.put("goodsName", orderGoods.getGoodsName()); orderGoodsVo.put("goodsName", orderGoods.getGoodsName());
...@@ -143,27 +151,27 @@ public class WxOrderController { ...@@ -143,27 +151,27 @@ public class WxOrderController {
/** /**
* 订单详情 * 订单详情
* *
* @param userId 用户ID * @param userId 用户ID
* @param orderId 订单信息 * @param orderId 订单信息
* @return 订单操作结果 * @return 订单操作结果
* 成功则 * 成功则
* { * {
* errno: 0, * errno: 0,
* errmsg: '成功', * errmsg: '成功',
* data: * data:
* { * {
* orderInfo: xxx , * orderInfo: xxx ,
* orderGoods: xxx * orderGoods: xxx
* } * }
* } * }
* 失败则 { errno: XXX, errmsg: XXX } * 失败则 { errno: XXX, errmsg: XXX }
*/ */
@GetMapping("detail") @GetMapping("detail")
public Object detail(@LoginUser Integer userId, Integer orderId) { public Object detail(@LoginUser Integer userId, Integer orderId) {
if(userId == null){ if (userId == null) {
return ResponseUtil.fail401(); return ResponseUtil.fail401();
} }
if(orderId == null){ if (orderId == null) {
return ResponseUtil.fail402(); return ResponseUtil.fail402();
} }
...@@ -172,7 +180,7 @@ public class WxOrderController { ...@@ -172,7 +180,7 @@ public class WxOrderController {
if (null == order) { if (null == order) {
return ResponseUtil.fail(403, "订单不存在"); return ResponseUtil.fail(403, "订单不存在");
} }
if(!order.getUserId().equals(userId)){ if (!order.getUserId().equals(userId)) {
return ResponseUtil.fail(403, "不是当前用户的订单"); return ResponseUtil.fail(403, "不是当前用户的订单");
} }
Map<String, Object> orderVo = new HashMap<String, Object>(); Map<String, Object> orderVo = new HashMap<String, Object>();
...@@ -190,7 +198,7 @@ public class WxOrderController { ...@@ -190,7 +198,7 @@ public class WxOrderController {
List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(order.getId()); List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(order.getId());
List<Map<String, Object>> orderGoodsVoList = new ArrayList<>(orderGoodsList.size()); List<Map<String, Object>> orderGoodsVoList = new ArrayList<>(orderGoodsList.size());
for(LitemallOrderGoods orderGoods : orderGoodsList){ for (LitemallOrderGoods orderGoods : orderGoodsList) {
Map<String, Object> orderGoodsVo = new HashMap<>(); Map<String, Object> orderGoodsVo = new HashMap<>();
orderGoodsVo.put("id", orderGoods.getId()); orderGoodsVo.put("id", orderGoods.getId());
orderGoodsVo.put("goodsName", orderGoods.getGoodsName()); orderGoodsVo.put("goodsName", orderGoods.getGoodsName());
...@@ -215,23 +223,23 @@ public class WxOrderController { ...@@ -215,23 +223,23 @@ public class WxOrderController {
* 4. 商品货品数量减少 * 4. 商品货品数量减少
* *
* @param userId 用户ID * @param userId 用户ID
* @param body 订单信息,{ cartId:xxx, addressId: xxx, couponId: xxx } * @param body 订单信息,{ cartId:xxx, addressId: xxx, couponId: xxx }
* @return 订单操作结果 * @return 订单操作结果
* 成功则 { errno: 0, errmsg: '成功', data: { orderId: xxx } } * 成功则 { errno: 0, errmsg: '成功', data: { orderId: xxx } }
* 失败则 { errno: XXX, errmsg: XXX } * 失败则 { errno: XXX, errmsg: XXX }
*/ */
@PostMapping("submit") @PostMapping("submit")
public Object submit(@LoginUser Integer userId, @RequestBody String body) { public Object submit(@LoginUser Integer userId, @RequestBody String body) {
if(userId == null){ if (userId == null) {
return ResponseUtil.unlogin(); return ResponseUtil.unlogin();
} }
if(body == null){ if (body == null) {
return ResponseUtil.badArgument(); return ResponseUtil.badArgument();
} }
Integer cartId = JacksonUtil.parseInteger(body, "cartId"); Integer cartId = JacksonUtil.parseInteger(body, "cartId");
Integer addressId = JacksonUtil.parseInteger(body, "addressId"); Integer addressId = JacksonUtil.parseInteger(body, "addressId");
Integer couponId = JacksonUtil.parseInteger(body, "couponId"); Integer couponId = JacksonUtil.parseInteger(body, "couponId");
if(cartId == null || addressId == null || couponId == null){ if (cartId == null || addressId == null || couponId == null) {
return ResponseUtil.badArgument(); return ResponseUtil.badArgument();
} }
...@@ -246,13 +254,12 @@ public class WxOrderController { ...@@ -246,13 +254,12 @@ public class WxOrderController {
List<LitemallCart> checkedGoodsList = null; List<LitemallCart> checkedGoodsList = null;
if (cartId.equals(0)) { if (cartId.equals(0)) {
checkedGoodsList = cartService.queryByUidAndChecked(userId); checkedGoodsList = cartService.queryByUidAndChecked(userId);
} } else {
else {
LitemallCart cart = cartService.findById(cartId); LitemallCart cart = cartService.findById(cartId);
checkedGoodsList = new ArrayList<>(1); checkedGoodsList = new ArrayList<>(1);
checkedGoodsList.add(cart); checkedGoodsList.add(cart);
} }
if(checkedGoodsList.size() == 0){ if (checkedGoodsList.size() == 0) {
return ResponseUtil.badArgumentValue(); return ResponseUtil.badArgumentValue();
} }
BigDecimal checkedGoodsPrice = new BigDecimal(0.00); BigDecimal checkedGoodsPrice = new BigDecimal(0.00);
...@@ -262,7 +269,7 @@ public class WxOrderController { ...@@ -262,7 +269,7 @@ public class WxOrderController {
// 根据订单商品总价计算运费,满88则免运费,否则8元; // 根据订单商品总价计算运费,满88则免运费,否则8元;
BigDecimal freightPrice = new BigDecimal(0.00); 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); freightPrice = new BigDecimal(8.00);
} }
...@@ -274,7 +281,7 @@ public class WxOrderController { ...@@ -274,7 +281,7 @@ public class WxOrderController {
BigDecimal orderTotalPrice = checkedGoodsPrice.add(freightPrice).subtract(couponPrice); BigDecimal orderTotalPrice = checkedGoodsPrice.add(freightPrice).subtract(couponPrice);
BigDecimal actualPrice = orderTotalPrice.subtract(integralPrice); BigDecimal actualPrice = orderTotalPrice.subtract(integralPrice);
// 添加订单 // 订单
LitemallOrder order = new LitemallOrder(); LitemallOrder order = new LitemallOrder();
order.setUserId(userId); order.setUserId(userId);
order.setOrderSn(orderService.generateOrderSn(userId)); order.setOrderSn(orderService.generateOrderSn(userId));
...@@ -290,10 +297,10 @@ public class WxOrderController { ...@@ -290,10 +297,10 @@ public class WxOrderController {
order.setIntegralPrice(integralPrice); order.setIntegralPrice(integralPrice);
order.setOrderPrice(orderTotalPrice); order.setOrderPrice(orderTotalPrice);
order.setActualPrice(actualPrice); 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(); LitemallOrderGoods orderGoods = new LitemallOrderGoods();
orderGoods.setOrderId(order.getId()); orderGoods.setOrderId(order.getId());
orderGoods.setGoodsId(cartGoods.getGoodsId()); orderGoods.setGoodsId(cartGoods.getGoodsId());
...@@ -305,27 +312,43 @@ public class WxOrderController { ...@@ -305,27 +312,43 @@ public class WxOrderController {
orderGoods.setNumber(cartGoods.getNumber()); orderGoods.setNumber(cartGoods.getNumber());
orderGoods.setGoodsSpecificationIds(cartGoods.getGoodsSpecificationIds()); orderGoods.setGoodsSpecificationIds(cartGoods.getGoodsSpecificationIds());
orderGoods.setGoodsSpecificationValues(cartGoods.getGoodsSpecificationValues()); orderGoods.setGoodsSpecificationValues(cartGoods.getGoodsSpecificationValues());
orderGoodsService.add(orderGoods); orderGoodsList.add(orderGoods);
} }
// 删除购物车里面的商品信息 // 开启事务管理
cartService.clearGoods(userId); DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = txManager.getTransaction(def);
try {
// 添加订单表项
orderService.add(order);
// 商品货品数量减少 // 添加订单商品表项
for (LitemallCart checkGoods : checkedGoodsList) { for (LitemallOrderGoods orderGoods : orderGoodsList) {
Integer productId= checkGoods.getProductId(); orderGoodsService.add(orderGoods);
LitemallProduct product = productService.findById(productId);
if(product == null){
return ResponseUtil.badArgumentValue();
} }
Integer remainNumber = product.getGoodsNumber() - checkGoods.getNumber(); // 删除购物车里面的商品信息
if(remainNumber < 0){ cartService.clearGoods(userId);
return ResponseUtil.badArgumentValue();
// 商品货品数量减少
for (LitemallCart checkGoods : checkedGoodsList) {
Integer productId = checkGoods.getProductId();
LitemallProduct product = productService.findById(productId);
Integer remainNumber = product.getGoodsNumber() - checkGoods.getNumber();
if (remainNumber < 0) {
throw new RuntimeException("下单的商品货品数量大于库存量");
}
product.setGoodsNumber(remainNumber);
productService.updateById(product);
} }
product.setGoodsNumber(remainNumber); } catch (Exception ex) {
productService.updateById(product); txManager.rollback(status);
logger.error("系统内部错误", ex);
return ResponseUtil.fail(403, "下单失败");
} }
txManager.commit(status);
Map<String, Object> data = new HashMap<>(); Map<String, Object> data = new HashMap<>();
data.put("orderId", order.getId()); data.put("orderId", order.getId());
...@@ -337,52 +360,62 @@ public class WxOrderController { ...@@ -337,52 +360,62 @@ public class WxOrderController {
* 1. 检测当前订单是否能够取消 * 1. 检测当前订单是否能够取消
* 2. 设置订单取消状态 * 2. 设置订单取消状态
* 3. 商品货品数量增加 * 3. 商品货品数量增加
*
* @param userId 用户ID * @param userId 用户ID
* @param body 订单信息,{ orderId:xxx } * @param body 订单信息,{ orderId:xxx }
* @return 订单操作结果 * @return 订单操作结果
* 成功则 { errno: 0, errmsg: '成功' } * 成功则 { errno: 0, errmsg: '成功' }
* 失败则 { errno: XXX, errmsg: XXX } * 失败则 { errno: XXX, errmsg: XXX }
*/ */
@PostMapping("cancel") @PostMapping("cancel")
public Object cancel(@LoginUser Integer userId, @RequestBody String body) { public Object cancel(@LoginUser Integer userId, @RequestBody String body) {
if(userId == null){ if (userId == null) {
return ResponseUtil.unlogin(); return ResponseUtil.unlogin();
} }
Integer orderId = JacksonUtil.parseInteger(body, "orderId"); Integer orderId = JacksonUtil.parseInteger(body, "orderId");
if(orderId == null){ if (orderId == null) {
return ResponseUtil.badArgument(); return ResponseUtil.badArgument();
} }
LitemallOrder order = orderService.findById(orderId); LitemallOrder order = orderService.findById(orderId);
if(order == null){ if (order == null) {
return ResponseUtil.badArgumentValue(); return ResponseUtil.badArgumentValue();
} }
if(!order.getUserId().equals(userId)){ if (!order.getUserId().equals(userId)) {
return ResponseUtil.badArgumentValue(); return ResponseUtil.badArgumentValue();
} }
// 检测是否能够取消 // 检测是否能够取消
OrderHandleOption handleOption = OrderUtil.build(order); OrderHandleOption handleOption = OrderUtil.build(order);
if(!handleOption.isCancel()){ if (!handleOption.isCancel()) {
return ResponseUtil.fail(403, "订单不能取消"); return ResponseUtil.fail(403, "订单不能取消");
} }
// 设置订单已取消状态 // 开启事务管理
order.setOrderStatus(OrderUtil.STATUS_CANCEL); DefaultTransactionDefinition def = new DefaultTransactionDefinition();
orderService.update(order); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = txManager.getTransaction(def);
// 商品货品数量增加 try {
List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(orderId); // 设置订单已取消状态
for(LitemallOrderGoods orderGoods : orderGoodsList){ order.setOrderStatus(OrderUtil.STATUS_CANCEL);
Integer productId = orderGoods.getProductId(); orderService.update(order);
LitemallProduct product = productService.findById(productId);
if(product == null){ // 商品货品数量增加
return ResponseUtil.badArgumentValue(); List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(orderId);
for (LitemallOrderGoods orderGoods : orderGoodsList) {
Integer productId = orderGoods.getProductId();
LitemallProduct product = productService.findById(productId);
Integer number = product.getGoodsNumber() + orderGoods.getNumber();
product.setGoodsNumber(number);
productService.updateById(product);
} }
Integer number = product.getGoodsNumber() + orderGoods.getNumber(); } catch (Exception ex) {
product.setGoodsNumber(number); txManager.rollback(status);
productService.updateById(product); logger.error("系统内部错误", ex);
return ResponseUtil.fail(403, "订单取消失败");
} }
txManager.commit(status);
return ResponseUtil.ok(); return ResponseUtil.ok();
} }
...@@ -391,33 +424,34 @@ public class WxOrderController { ...@@ -391,33 +424,34 @@ public class WxOrderController {
* 1. 检测当前订单是否能够付款 * 1. 检测当前订单是否能够付款
* 2. 设置订单付款状态 * 2. 设置订单付款状态
* 3. TODO 微信后台申请支付,同时设置付款状态 * 3. TODO 微信后台申请支付,同时设置付款状态
*
* @param userId 用户ID * @param userId 用户ID
* @param body 订单信息,{ orderId:xxx } * @param body 订单信息,{ orderId:xxx }
* @return 订单操作结果 * @return 订单操作结果
* 成功则 { errno: 0, errmsg: '成功' } * 成功则 { errno: 0, errmsg: '成功' }
* 失败则 { errno: XXX, errmsg: XXX } * 失败则 { errno: XXX, errmsg: XXX }
*/ */
@PostMapping("pay") @PostMapping("pay")
public Object pay(@LoginUser Integer userId, @RequestBody String body) { public Object pay(@LoginUser Integer userId, @RequestBody String body) {
if(userId == null){ if (userId == null) {
return ResponseUtil.unlogin(); return ResponseUtil.unlogin();
} }
Integer orderId = JacksonUtil.parseInteger(body, "orderId"); Integer orderId = JacksonUtil.parseInteger(body, "orderId");
if(orderId == null){ if (orderId == null) {
return ResponseUtil.badArgument(); return ResponseUtil.badArgument();
} }
LitemallOrder order = orderService.findById(orderId); LitemallOrder order = orderService.findById(orderId);
if(order == null){ if (order == null) {
return ResponseUtil.badArgument(); return ResponseUtil.badArgument();
} }
if(!order.getUserId().equals(userId)){ if (!order.getUserId().equals(userId)) {
return ResponseUtil.badArgumentValue(); return ResponseUtil.badArgumentValue();
} }
// 检测是否能够付款 // 检测是否能够付款
OrderHandleOption handleOption = OrderUtil.build(order); OrderHandleOption handleOption = OrderUtil.build(order);
if(!handleOption.isPay()){ if (!handleOption.isPay()) {
return ResponseUtil.fail(403, "订单不能付款"); return ResponseUtil.fail(403, "订单不能付款");
} }
...@@ -438,43 +472,44 @@ public class WxOrderController { ...@@ -438,43 +472,44 @@ public class WxOrderController {
* 付款成功回调接口 * 付款成功回调接口
* 1. 检测当前订单是否是付款状态 * 1. 检测当前订单是否是付款状态
* 2. 设置订单付款成功状态相关信息 * 2. 设置订单付款成功状态相关信息
*
* @param userId 用户ID * @param userId 用户ID
* @param body 订单信息,{ orderId:xxx, payId: xxx } * @param body 订单信息,{ orderId:xxx, payId: xxx }
* @return 订单操作结果 * @return 订单操作结果
* 成功则 { errno: 0, errmsg: '成功' } * 成功则 { errno: 0, errmsg: '成功' }
* 失败则 { errno: XXX, errmsg: XXX } * 失败则 { errno: XXX, errmsg: XXX }
* * <p>
* 注意,这里pay_notify是示例地址,开发者应该设立一个隐蔽的回调地址 * 注意,这里pay_notify是示例地址,开发者应该设立一个隐蔽的回调地址
* TODO 这里需要根据微信支付文档设计 * TODO 这里需要根据微信支付文档设计
*/ */
@PostMapping("pay_notify") @PostMapping("pay_notify")
public Object pay_notify(@LoginUser Integer userId, @RequestBody String body) { public Object pay_notify(@LoginUser Integer userId, @RequestBody String body) {
if(userId == null){ if (userId == null) {
return ResponseUtil.unlogin(); return ResponseUtil.unlogin();
} }
Integer orderId = JacksonUtil.parseInteger(body, "orderId"); Integer orderId = JacksonUtil.parseInteger(body, "orderId");
Integer payId = JacksonUtil.parseInteger(body, "payId"); Integer payId = JacksonUtil.parseInteger(body, "payId");
if(orderId == null || payId == null){ if (orderId == null || payId == null) {
return ResponseUtil.badArgument(); return ResponseUtil.badArgument();
} }
LitemallOrder order = orderService.findById(orderId); LitemallOrder order = orderService.findById(orderId);
if(order == null){ if (order == null) {
return ResponseUtil.badArgument(); return ResponseUtil.badArgument();
} }
if(!order.getUserId().equals(userId)){ if (!order.getUserId().equals(userId)) {
return ResponseUtil.badArgumentValue(); return ResponseUtil.badArgumentValue();
} }
// 检测是否是付款状态 // 检测是否是付款状态
if(!order.getOrderStatus().equals(OrderUtil.STATUS_PAY)){ if (!order.getOrderStatus().equals(OrderUtil.STATUS_PAY)) {
logger.error("系统内部错误"); logger.error("系统内部错误");
} }
if(!order.getPayId().equals(payId)){ if (!order.getPayId().equals(payId)) {
logger.error("系统内部错误"); logger.error("系统内部错误");
} }
Short payStatus = (short)2; Short payStatus = (short) 2;
order.setPayStatus(payStatus); order.setPayStatus(payStatus);
order.setPayTime(LocalDateTime.now()); order.setPayTime(LocalDateTime.now());
orderService.update(order); orderService.update(order);
...@@ -489,53 +524,62 @@ public class WxOrderController { ...@@ -489,53 +524,62 @@ public class WxOrderController {
* 2. 设置订单退款取消状态 * 2. 设置订单退款取消状态
* 3. TODO 退款 * 3. TODO 退款
* 4. 商品货品数量增加 * 4. 商品货品数量增加
*
* @param userId 用户ID * @param userId 用户ID
* @param body 订单信息,{ orderId:xxx } * @param body 订单信息,{ orderId:xxx }
* @return 订单操作结果 * @return 订单操作结果
* 成功则 { errno: 0, errmsg: '成功' } * 成功则 { errno: 0, errmsg: '成功' }
* 失败则 { errno: XXX, errmsg: XXX } * 失败则 { errno: XXX, errmsg: XXX }
*/ */
@PostMapping("refund") @PostMapping("refund")
public Object refund(@LoginUser Integer userId, @RequestBody String body) { public Object refund(@LoginUser Integer userId, @RequestBody String body) {
if(userId == null){ if (userId == null) {
return ResponseUtil.unlogin(); return ResponseUtil.unlogin();
} }
Integer orderId = JacksonUtil.parseInteger(body, "orderId"); Integer orderId = JacksonUtil.parseInteger(body, "orderId");
if(orderId == null){ if (orderId == null) {
return ResponseUtil.badArgument(); return ResponseUtil.badArgument();
} }
LitemallOrder order = orderService.findById(orderId); LitemallOrder order = orderService.findById(orderId);
if(order == null){ if (order == null) {
return ResponseUtil.badArgument(); return ResponseUtil.badArgument();
} }
if(!order.getUserId().equals(userId)){ if (!order.getUserId().equals(userId)) {
return ResponseUtil.badArgumentValue(); return ResponseUtil.badArgumentValue();
} }
OrderHandleOption handleOption = OrderUtil.build(order); OrderHandleOption handleOption = OrderUtil.build(order);
if(!handleOption.isRefund()){ if (!handleOption.isRefund()) {
return ResponseUtil.fail(403, "订单不能取消"); return ResponseUtil.fail(403, "订单不能取消");
} }
// 设置订单取消状态 // 开启事务管理
order.setOrderStatus(OrderUtil.STATUS_REFUND); DefaultTransactionDefinition def = new DefaultTransactionDefinition();
orderService.update(order); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = txManager.getTransaction(def);
// 退款操作 try {
// 设置订单取消状态
// 商品货品数量增加 order.setOrderStatus(OrderUtil.STATUS_REFUND);
List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(orderId); orderService.update(order);
for(LitemallOrderGoods orderGoods : orderGoodsList){
Integer productId = orderGoods.getProductId(); // 退款操作
LitemallProduct product = productService.findById(productId);
if(product == null){ // 商品货品数量增加
return ResponseUtil.badArgumentValue(); List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(orderId);
for (LitemallOrderGoods orderGoods : orderGoodsList) {
Integer productId = orderGoods.getProductId();
LitemallProduct product = productService.findById(productId);
Integer number = product.getGoodsNumber() + orderGoods.getNumber();
product.setGoodsNumber(number);
productService.updateById(product);
} }
Integer number = product.getGoodsNumber() + orderGoods.getNumber(); } catch (Exception ex) {
product.setGoodsNumber(number); txManager.rollback(status);
productService.updateById(product); logger.error("系统内部错误", ex);
return ResponseUtil.fail(403, "订单退款失败");
} }
txManager.commit(status);
return ResponseUtil.ok(); return ResponseUtil.ok();
} }
...@@ -544,34 +588,35 @@ public class WxOrderController { ...@@ -544,34 +588,35 @@ public class WxOrderController {
* 发货 * 发货
* 1. 检测当前订单是否能够发货 * 1. 检测当前订单是否能够发货
* 2. 设置订单发货状态 * 2. 设置订单发货状态
*
* @param userId 用户ID * @param userId 用户ID
* @param body 订单信息,{ orderId:xxx, shipSn: xxx, shipChannel: xxx } * @param body 订单信息,{ orderId:xxx, shipSn: xxx, shipChannel: xxx }
* @return 订单操作结果 * @return 订单操作结果
* 成功则 { errno: 0, errmsg: '成功' } * 成功则 { errno: 0, errmsg: '成功' }
* 失败则 { errno: XXX, errmsg: XXX } * 失败则 { errno: XXX, errmsg: XXX }
*/ */
@PostMapping("ship") @PostMapping("ship")
public Object ship(@LoginUser Integer userId, @RequestBody String body) { public Object ship(@LoginUser Integer userId, @RequestBody String body) {
if(userId == null){ if (userId == null) {
return ResponseUtil.unlogin(); return ResponseUtil.unlogin();
} }
Integer orderId = JacksonUtil.parseInteger(body, "orderId"); Integer orderId = JacksonUtil.parseInteger(body, "orderId");
String shipSn = JacksonUtil.parseString(body, "shipSn"); String shipSn = JacksonUtil.parseString(body, "shipSn");
String shipChannel = JacksonUtil.parseString(body, "shipChannel"); String shipChannel = JacksonUtil.parseString(body, "shipChannel");
if(orderId == null || shipSn == null || shipChannel == null){ if (orderId == null || shipSn == null || shipChannel == null) {
return ResponseUtil.badArgument(); return ResponseUtil.badArgument();
} }
LitemallOrder order = orderService.findById(orderId); LitemallOrder order = orderService.findById(orderId);
if(order == null){ if (order == null) {
return ResponseUtil.badArgument(); return ResponseUtil.badArgument();
} }
if(!order.getUserId().equals(userId)){ if (!order.getUserId().equals(userId)) {
return ResponseUtil.badArgumentValue(); return ResponseUtil.badArgumentValue();
} }
// 如果订单不是已付款状态,则不能发货 // 如果订单不是已付款状态,则不能发货
if(!order.getOrderStatus().equals(OrderUtil.STATUS_PAY)){ if (!order.getOrderStatus().equals(OrderUtil.STATUS_PAY)) {
return ResponseUtil.fail(403, "订单不能确认收货"); return ResponseUtil.fail(403, "订单不能确认收货");
} }
...@@ -588,32 +633,33 @@ public class WxOrderController { ...@@ -588,32 +633,33 @@ public class WxOrderController {
* 确认收货 * 确认收货
* 1. 检测当前订单是否能够确认订单 * 1. 检测当前订单是否能够确认订单
* 2. 设置订单确认状态 * 2. 设置订单确认状态
*
* @param userId 用户ID * @param userId 用户ID
* @param body 订单信息,{ orderId:xxx } * @param body 订单信息,{ orderId:xxx }
* @return 订单操作结果 * @return 订单操作结果
* 成功则 { errno: 0, errmsg: '成功' } * 成功则 { errno: 0, errmsg: '成功' }
* 失败则 { errno: XXX, errmsg: XXX } * 失败则 { errno: XXX, errmsg: XXX }
*/ */
@PostMapping("confirm") @PostMapping("confirm")
public Object confirm(@LoginUser Integer userId, @RequestBody String body) { public Object confirm(@LoginUser Integer userId, @RequestBody String body) {
if(userId == null){ if (userId == null) {
return ResponseUtil.unlogin(); return ResponseUtil.unlogin();
} }
Integer orderId = JacksonUtil.parseInteger(body, "orderId"); Integer orderId = JacksonUtil.parseInteger(body, "orderId");
if(orderId == null){ if (orderId == null) {
return ResponseUtil.badArgument(); return ResponseUtil.badArgument();
} }
LitemallOrder order = orderService.findById(orderId); LitemallOrder order = orderService.findById(orderId);
if(order == null){ if (order == null) {
return ResponseUtil.badArgument(); return ResponseUtil.badArgument();
} }
if(!order.getUserId().equals(userId)){ if (!order.getUserId().equals(userId)) {
return ResponseUtil.badArgumentValue(); return ResponseUtil.badArgumentValue();
} }
OrderHandleOption handleOption = OrderUtil.build(order); OrderHandleOption handleOption = OrderUtil.build(order);
if(!handleOption.isConfirm()){ if (!handleOption.isConfirm()) {
return ResponseUtil.fail(403, "订单不能确认收货"); return ResponseUtil.fail(403, "订单不能确认收货");
} }
...@@ -629,32 +675,33 @@ public class WxOrderController { ...@@ -629,32 +675,33 @@ public class WxOrderController {
* 自动确认收货 * 自动确认收货
* 1. 检测当前订单是否能够自动确认订单 * 1. 检测当前订单是否能够自动确认订单
* 2. 设置订单自动确认状态 * 2. 设置订单自动确认状态
*
* @param userId 用户ID * @param userId 用户ID
* @param body 订单信息,{ orderId:xxx } * @param body 订单信息,{ orderId:xxx }
* @return 订单操作结果 * @return 订单操作结果
* 成功则 { errno: 0, errmsg: '成功' } * 成功则 { errno: 0, errmsg: '成功' }
* 失败则 { errno: XXX, errmsg: XXX } * 失败则 { errno: XXX, errmsg: XXX }
*/ */
@PostMapping("autoconfirm") @PostMapping("autoconfirm")
public Object autoconfirm(@LoginUser Integer userId, @RequestBody String body) { public Object autoconfirm(@LoginUser Integer userId, @RequestBody String body) {
if(userId == null){ if (userId == null) {
return ResponseUtil.unlogin(); return ResponseUtil.unlogin();
} }
Integer orderId = JacksonUtil.parseInteger(body, "orderId"); Integer orderId = JacksonUtil.parseInteger(body, "orderId");
if(orderId == null){ if (orderId == null) {
return ResponseUtil.badArgument(); return ResponseUtil.badArgument();
} }
LitemallOrder order = orderService.findById(orderId); LitemallOrder order = orderService.findById(orderId);
if(order == null){ if (order == null) {
return ResponseUtil.badArgument(); return ResponseUtil.badArgument();
} }
if(!order.getUserId().equals(userId)){ if (!order.getUserId().equals(userId)) {
return ResponseUtil.badArgumentValue(); return ResponseUtil.badArgumentValue();
} }
OrderHandleOption handleOption = OrderUtil.build(order); OrderHandleOption handleOption = OrderUtil.build(order);
if(!handleOption.isConfirm()){ if (!handleOption.isConfirm()) {
return ResponseUtil.fail(403, "订单不能确认收货"); return ResponseUtil.fail(403, "订单不能确认收货");
} }
...@@ -669,32 +716,33 @@ public class WxOrderController { ...@@ -669,32 +716,33 @@ public class WxOrderController {
* 删除订单 * 删除订单
* 1. 检测当前订单是否删除 * 1. 检测当前订单是否删除
* 2. 设置订单删除状态 * 2. 设置订单删除状态
*
* @param userId 用户ID * @param userId 用户ID
* @param body 订单信息,{ orderId:xxx } * @param body 订单信息,{ orderId:xxx }
* @return 订单操作结果 * @return 订单操作结果
* 成功则 { errno: 0, errmsg: '成功' } * 成功则 { errno: 0, errmsg: '成功' }
* 失败则 { errno: XXX, errmsg: XXX } * 失败则 { errno: XXX, errmsg: XXX }
*/ */
@PostMapping("delete") @PostMapping("delete")
public Object delete(@LoginUser Integer userId, @RequestBody String body) { public Object delete(@LoginUser Integer userId, @RequestBody String body) {
if(userId == null){ if (userId == null) {
return ResponseUtil.unlogin(); return ResponseUtil.unlogin();
} }
Integer orderId = JacksonUtil.parseInteger(body, "orderId"); Integer orderId = JacksonUtil.parseInteger(body, "orderId");
if(orderId == null){ if (orderId == null) {
return ResponseUtil.badArgument(); return ResponseUtil.badArgument();
} }
LitemallOrder order = orderService.findById(orderId); LitemallOrder order = orderService.findById(orderId);
if(order == null){ if (order == null) {
return ResponseUtil.badArgument(); return ResponseUtil.badArgument();
} }
if(!order.getUserId().equals(userId)){ if (!order.getUserId().equals(userId)) {
return ResponseUtil.badArgumentValue(); return ResponseUtil.badArgumentValue();
} }
OrderHandleOption handleOption = OrderUtil.build(order); OrderHandleOption handleOption = OrderUtil.build(order);
if(!handleOption.isDelete()){ if (!handleOption.isDelete()) {
return ResponseUtil.fail(403, "订单不能删除"); return ResponseUtil.fail(403, "订单不能删除");
} }
...@@ -708,19 +756,19 @@ public class WxOrderController { ...@@ -708,19 +756,19 @@ public class WxOrderController {
/** /**
* 可以评价的订单商品信息 * 可以评价的订单商品信息
* *
* @param userId 用户ID * @param userId 用户ID
* @param orderId 订单ID * @param orderId 订单ID
* @param goodsId 商品ID * @param goodsId 商品ID
* @return 订单操作结果 * @return 订单操作结果
* 成功则 { errno: 0, errmsg: '成功', data: xxx } * 成功则 { errno: 0, errmsg: '成功', data: xxx }
* 失败则 { errno: XXX, errmsg: XXX } * 失败则 { errno: XXX, errmsg: XXX }
*/ */
@GetMapping("comment") @GetMapping("comment")
public Object comment(@LoginUser Integer userId, Integer orderId, Integer goodsId) { public Object comment(@LoginUser Integer userId, Integer orderId, Integer goodsId) {
if(userId == null){ if (userId == null) {
return ResponseUtil.unlogin(); return ResponseUtil.unlogin();
} }
if(orderId == null){ if (orderId == null) {
return ResponseUtil.badArgument(); return ResponseUtil.badArgument();
} }
...@@ -729,7 +777,7 @@ public class WxOrderController { ...@@ -729,7 +777,7 @@ public class WxOrderController {
Assert.state(size < 2, "存在多个符合条件的订单商品"); Assert.state(size < 2, "存在多个符合条件的订单商品");
if(size == 0){ if (size == 0) {
return ResponseUtil.badArgumentValue(); 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