From 6095e29622ddc20f89bfb6084e937b9802a2acc6 Mon Sep 17 00:00:00 2001 From: Gadfly Date: Tue, 10 Aug 2021 12:04:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(OrderServiceImpl.java):=20=E6=B8=85?= =?UTF-8?q?=E9=99=A4=E8=B4=AD=E7=89=A9=E8=BD=A6=E7=9A=84=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E7=A7=BB=E8=87=B3=E6=94=AF=E4=BB=98=E5=9B=9E=E8=B0=83=E4=B8=AD?= =?UTF-8?q?=E5=A4=84=E7=90=86=EF=BC=8C=E5=85=B3=E5=8D=95=E6=97=B6=E5=90=8C?= =?UTF-8?q?=E6=97=B6=E5=9C=A8=E5=BE=AE=E4=BF=A1=E8=BF=9B=E8=A1=8C=E5=85=B3?= =?UTF-8?q?=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../oms/service/impl/OrderServiceImpl.java | 51 ++++++++++++++----- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/service/impl/OrderServiceImpl.java b/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/service/impl/OrderServiceImpl.java index a6a29b05f..b781144e2 100644 --- a/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/service/impl/OrderServiceImpl.java +++ b/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/service/impl/OrderServiceImpl.java @@ -177,8 +177,8 @@ public class OrderServiceImpl extends ServiceImpl impleme if (sku != null) { return sku.getPrice() * item.getCount(); } - return 0l; - }).reduce(0l, Long::sum); + return 0L; + }).reduce(0L, Long::sum); if (currentTotalPrice.compareTo(submitDTO.getTotalPrice()) != 0) { throw new BizException("页面已过期,请重新刷新页面再提交"); @@ -192,7 +192,7 @@ public class OrderServiceImpl extends ServiceImpl impleme .build()) .collect(Collectors.toList()); - Result lockResult = skuFeignService.lockStock(skuLockList); + Result lockResult = skuFeignService.lockStock(skuLockList); if (!Result.success().getCode().equals(lockResult.getCode())) { throw new BizException(Result.failed().getMsg()); @@ -205,8 +205,8 @@ public class OrderServiceImpl extends ServiceImpl impleme .setMemberId(JwtUtils.getUserId()) .setRemark(submitDTO.getRemark()) .setPayAmount(submitDTO.getPayAmount()) - .setTotalQuantity(orderItems.stream().map(item -> item.getCount()).reduce(0, (x, y) -> x + y)) - .setTotalAmount(orderItems.stream().map(item -> item.getPrice() * item.getCount()).reduce(0l, (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, Long::sum)) .setGmtCreate(new Date()); this.save(order); @@ -258,8 +258,8 @@ public class OrderServiceImpl extends ServiceImpl impleme if (sku != null) { return sku.getPrice() * item.getCount(); } - return 0l; - }).reduce(0l, Long::sum); + return 0L; + }).reduce(0L, Long::sum); if (currentTotalPrice.compareTo(submitDTO.getTotalPrice()) != 0) { throw new BizException("页面已过期,请重新刷新页面再提交"); @@ -273,7 +273,7 @@ public class OrderServiceImpl extends ServiceImpl impleme .build()) .collect(Collectors.toList()); - Result lockResult = skuFeignService.lockStock(skuLockList); + Result lockResult = skuFeignService.lockStock(skuLockList); if (!Result.success().getCode().equals(lockResult.getCode())) { throw new BizException(Result.failed().getMsg()); @@ -291,7 +291,6 @@ public class OrderServiceImpl extends ServiceImpl impleme return submitVO; } - /** * 订单支付 * @@ -336,8 +335,6 @@ public class OrderServiceImpl extends ServiceImpl impleme } lock.unlock(); - // 支付成功删除购物车已勾选的商品 - cartService.removeCheckedItem(); return result; } @@ -355,6 +352,8 @@ public class OrderServiceImpl extends ServiceImpl impleme order.setPayType(PayTypeEnum.BALANCE.getCode()); order.setPayTime(new Date()); this.updateById(order); + // 支付成功删除购物车已勾选的商品 + cartService.removeCheckedItem(); return Boolean.TRUE; } @@ -412,6 +411,16 @@ public class OrderServiceImpl extends ServiceImpl impleme if (order == null || !OrderStatusEnum.PENDING_PAYMENT.getCode().equals(order.getStatus())) { 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()); return this.updateById(order); } @@ -420,19 +429,31 @@ public class OrderServiceImpl extends ServiceImpl impleme public boolean cancelOrder(Long id) { log.info("=======================订单取消,订单ID:{}=======================", 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("取消失败,订单状态不支持取消"); // 通过自定义异常,将异常信息抛出由异常处理器捕获显示给前端页面 } + // 如果已经有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()); boolean result = this.updateById(order); if (result) { // 释放被锁定的库存 - Result unlockResult = skuFeignService.unlockStock(order.getOrderSn()); + Result unlockResult = skuFeignService.unlockStock(order.getOrderSn()); if (!Result.isSuccess(unlockResult)) { throw new BizException(unlockResult.getMsg()); } - result = true; } return result; } @@ -479,6 +500,8 @@ public class OrderServiceImpl extends ServiceImpl impleme this.updateById(orderDO); } log.info("账单更新成功"); + // 支付成功删除购物车已勾选的商品 + cartService.removeCheckedItem(); } @Override