fix(OrderServiceImpl.java): 清除购物车的操作移至支付回调中处理,关单时同时在微信进行关单

This commit is contained in:
Gadfly 2021-08-10 12:04:36 +08:00
parent d9a3d19c51
commit 6095e29622

View File

@ -177,8 +177,8 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
if (sku != null) { if (sku != null) {
return sku.getPrice() * item.getCount(); return sku.getPrice() * item.getCount();
} }
return 0l; return 0L;
}).reduce(0l, Long::sum); }).reduce(0L, Long::sum);
if (currentTotalPrice.compareTo(submitDTO.getTotalPrice()) != 0) { if (currentTotalPrice.compareTo(submitDTO.getTotalPrice()) != 0) {
throw new BizException("页面已过期,请重新刷新页面再提交"); throw new BizException("页面已过期,请重新刷新页面再提交");
@ -192,7 +192,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
.build()) .build())
.collect(Collectors.toList()); .collect(Collectors.toList());
Result lockResult = skuFeignService.lockStock(skuLockList); Result<?> lockResult = skuFeignService.lockStock(skuLockList);
if (!Result.success().getCode().equals(lockResult.getCode())) { if (!Result.success().getCode().equals(lockResult.getCode())) {
throw new BizException(Result.failed().getMsg()); throw new BizException(Result.failed().getMsg());
@ -205,8 +205,8 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
.setMemberId(JwtUtils.getUserId()) .setMemberId(JwtUtils.getUserId())
.setRemark(submitDTO.getRemark()) .setRemark(submitDTO.getRemark())
.setPayAmount(submitDTO.getPayAmount()) .setPayAmount(submitDTO.getPayAmount())
.setTotalQuantity(orderItems.stream().map(item -> item.getCount()).reduce(0, (x, y) -> x + y)) .setTotalQuantity(orderItems.stream().map(OrderItemDTO::getCount).reduce(0, Integer::sum))
.setTotalAmount(orderItems.stream().map(item -> item.getPrice() * item.getCount()).reduce(0l, (x, y) -> x + y)) .setTotalAmount(orderItems.stream().map(item -> item.getPrice() * item.getCount()).reduce(0L, Long::sum))
.setGmtCreate(new Date()); .setGmtCreate(new Date());
this.save(order); this.save(order);
@ -258,8 +258,8 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
if (sku != null) { if (sku != null) {
return sku.getPrice() * item.getCount(); return sku.getPrice() * item.getCount();
} }
return 0l; return 0L;
}).reduce(0l, Long::sum); }).reduce(0L, Long::sum);
if (currentTotalPrice.compareTo(submitDTO.getTotalPrice()) != 0) { if (currentTotalPrice.compareTo(submitDTO.getTotalPrice()) != 0) {
throw new BizException("页面已过期,请重新刷新页面再提交"); throw new BizException("页面已过期,请重新刷新页面再提交");
@ -273,7 +273,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
.build()) .build())
.collect(Collectors.toList()); .collect(Collectors.toList());
Result lockResult = skuFeignService.lockStock(skuLockList); Result<?> lockResult = skuFeignService.lockStock(skuLockList);
if (!Result.success().getCode().equals(lockResult.getCode())) { if (!Result.success().getCode().equals(lockResult.getCode())) {
throw new BizException(Result.failed().getMsg()); throw new BizException(Result.failed().getMsg());
@ -291,7 +291,6 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
return submitVO; return submitVO;
} }
/** /**
* 订单支付 * 订单支付
* *
@ -336,8 +335,6 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
} }
lock.unlock(); lock.unlock();
// 支付成功删除购物车已勾选的商品
cartService.removeCheckedItem();
return result; return result;
} }
@ -355,6 +352,8 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
order.setPayType(PayTypeEnum.BALANCE.getCode()); order.setPayType(PayTypeEnum.BALANCE.getCode());
order.setPayTime(new Date()); order.setPayTime(new Date());
this.updateById(order); this.updateById(order);
// 支付成功删除购物车已勾选的商品
cartService.removeCheckedItem();
return Boolean.TRUE; return Boolean.TRUE;
} }
@ -412,6 +411,16 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
if (order == null || !OrderStatusEnum.PENDING_PAYMENT.getCode().equals(order.getStatus())) { if (order == null || !OrderStatusEnum.PENDING_PAYMENT.getCode().equals(order.getStatus())) {
return false; return false;
} }
// 如果已经有outTradeNo了就先进行关单
if (PayTypeEnum.WEIXIN_JSAPI.getCode().equals(order.getPayType()) && StrUtil.isNotBlank(order.getOutTradeNo())) {
try {
wxPayService.closeOrderV3(order.getOutTradeNo());
order.setOutTradeNo(null);
} catch (WxPayException e) {
log.error(e.getMessage(), e);
throw new BizException("微信关单异常");
}
}
order.setStatus(OrderStatusEnum.AUTO_CANCEL.getCode()); order.setStatus(OrderStatusEnum.AUTO_CANCEL.getCode());
return this.updateById(order); return this.updateById(order);
} }
@ -420,19 +429,31 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
public boolean cancelOrder(Long id) { public boolean cancelOrder(Long id) {
log.info("=======================订单取消订单ID{}=======================", id); log.info("=======================订单取消订单ID{}=======================", id);
OmsOrder order = this.getById(id); OmsOrder order = this.getById(id);
if (order == null) {
throw new BizException("订单不存在");
}
if (order != null && !OrderStatusEnum.PENDING_PAYMENT.getCode().equals(order.getStatus())) { if (!OrderStatusEnum.PENDING_PAYMENT.getCode().equals(order.getStatus())) {
throw new BizException("取消失败,订单状态不支持取消"); // 通过自定义异常将异常信息抛出由异常处理器捕获显示给前端页面 throw new BizException("取消失败,订单状态不支持取消"); // 通过自定义异常将异常信息抛出由异常处理器捕获显示给前端页面
} }
// 如果已经有outTradeNo了就先进行关单
if (PayTypeEnum.WEIXIN_JSAPI.getCode().equals(order.getPayType()) && StrUtil.isNotBlank(order.getOutTradeNo())) {
try {
wxPayService.closeOrderV3(order.getOutTradeNo());
order.setOutTradeNo(null);
} catch (WxPayException e) {
log.error(e.getMessage(), e);
throw new BizException("微信关单异常");
}
}
order.setStatus(OrderStatusEnum.USER_CANCEL.getCode()); order.setStatus(OrderStatusEnum.USER_CANCEL.getCode());
boolean result = this.updateById(order); boolean result = this.updateById(order);
if (result) { if (result) {
// 释放被锁定的库存 // 释放被锁定的库存
Result unlockResult = skuFeignService.unlockStock(order.getOrderSn()); Result<?> unlockResult = skuFeignService.unlockStock(order.getOrderSn());
if (!Result.isSuccess(unlockResult)) { if (!Result.isSuccess(unlockResult)) {
throw new BizException(unlockResult.getMsg()); throw new BizException(unlockResult.getMsg());
} }
result = true;
} }
return result; return result;
} }
@ -479,6 +500,8 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
this.updateById(orderDO); this.updateById(orderDO);
} }
log.info("账单更新成功"); log.info("账单更新成功");
// 支付成功删除购物车已勾选的商品
cartService.removeCheckedItem();
} }
@Override @Override