From 9c5b51efa84c099f1894aa50f236850299226bad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BC=9F=E4=B8=9A?= <270397121@qq.com> Date: Wed, 9 Mar 2022 10:01:21 +0000 Subject: [PATCH] =?UTF-8?q?update=20mall-oms/oms-boot/src/main/java/com/yo?= =?UTF-8?q?ulai/mall/oms/service/impl/OrderServiceImpl.java.=20fix(OrderSe?= =?UTF-8?q?rviceImpl.java):=20=E8=A7=A3=E5=86=B3=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E5=A4=B1=E8=B4=A5,=20=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E9=87=8D=E6=96=B0=E6=8F=90=E4=BA=A4=E8=AE=A2=E5=8D=95=E6=97=B6?= =?UTF-8?q?,=20=E4=B8=80=E7=9B=B4=E6=8A=A5=20"=E8=AE=A2=E5=8D=95=E4=B8=8D?= =?UTF-8?q?=E5=8F=AF=E9=87=8D=E5=A4=8D=E6=8F=90=E4=BA=A4"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../oms/service/impl/OrderServiceImpl.java | 65 ++++++++++--------- 1 file changed, 35 insertions(+), 30 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 bfd8a225b..39d61540a 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 @@ -158,42 +158,47 @@ public class OrderServiceImpl extends ServiceImpl impleme Long execute = this.redisTemplate.execute(redisScript, Collections.singletonList(ORDER_TOKEN_PREFIX + orderToken), orderToken); Assert.isTrue(execute.equals(1l), "订单不可重复提交"); - // 订单验价 - Long orderTotalAmount = orderSubmitForm.getTotalAmount(); - boolean checkResult = this.checkOrderPrice(orderTotalAmount, orderItems); - Assert.isTrue(checkResult, "当前页面已过期,请重新刷新页面再提交"); + OmsOrder order; + try { + // 订单验价 + Long orderTotalAmount = orderSubmitForm.getTotalAmount(); + boolean checkResult = this.checkOrderPrice(orderTotalAmount, orderItems); + Assert.isTrue(checkResult, "当前页面已过期,请重新刷新页面再提交"); - // 锁定商品库存 - this.lockStock(orderToken, orderItems); + // 锁定商品库存 + this.lockStock(orderToken, orderItems); - // 创建订单 - OmsOrder order = new OmsOrder().setOrderSn(orderToken) // 把orderToken赋值给订单编号 - .setStatus(OrderStatusEnum.PENDING_PAYMENT.getCode()) - .setSourceType(OrderTypeEnum.APP.getCode()) - .setMemberId(MemberUtils.getMemberId()) - .setRemark(orderSubmitForm.getRemark()) - .setPayAmount(orderSubmitForm.getPayAmount()) - .setTotalQuantity(orderItems.stream().map(OrderItemDTO::getCount).reduce(0, Integer::sum)) - .setTotalAmount(orderItems.stream().map(item -> item.getPrice() * item.getCount()).reduce(0L, Long::sum)); - boolean result = this.save(order); + // 创建订单 + order = new OmsOrder().setOrderSn(orderToken) // 把orderToken赋值给订单编号 + .setStatus(OrderStatusEnum.PENDING_PAYMENT.getCode()) + .setSourceType(OrderTypeEnum.APP.getCode()) + .setMemberId(MemberUtils.getMemberId()) + .setRemark(orderSubmitForm.getRemark()) + .setPayAmount(orderSubmitForm.getPayAmount()) + .setTotalQuantity(orderItems.stream().map(OrderItemDTO::getCount).reduce(0, Integer::sum)) + .setTotalAmount(orderItems.stream().map(item -> item.getPrice() * item.getCount()).reduce(0L, Long::sum)); + boolean result = this.save(order); - // 添加订单明细 - if (result) { - List saveOrderItems = orderItems.stream().map(orderFormItem -> { - OmsOrderItem omsOrderItem = new OmsOrderItem(); - BeanUtil.copyProperties(orderFormItem, omsOrderItem); - omsOrderItem.setOrderId(order.getId()); - omsOrderItem.setTotalAmount(orderFormItem.getPrice() * orderFormItem.getCount()); - return omsOrderItem; - }).collect(Collectors.toList()); - result = orderItemService.saveBatch(saveOrderItems); + // 添加订单明细 if (result) { - // 订单超时取消 - rabbitTemplate.convertAndSend("order.exchange", "order.create", orderToken); + List saveOrderItems = orderItems.stream().map(orderFormItem -> { + OmsOrderItem omsOrderItem = new OmsOrderItem(); + BeanUtil.copyProperties(orderFormItem, omsOrderItem); + omsOrderItem.setOrderId(order.getId()); + omsOrderItem.setTotalAmount(orderFormItem.getPrice() * orderFormItem.getCount()); + return omsOrderItem; + }).collect(Collectors.toList()); + result = orderItemService.saveBatch(saveOrderItems); + if (result) { + // 订单超时取消 + rabbitTemplate.convertAndSend("order.exchange", "order.create", orderToken); + } } + Assert.isTrue(result, "订单提交失败"); + } catch (Exception e) { + redisTemplate.opsForValue().set(ORDER_TOKEN_PREFIX + orderToken, orderToken); + throw new BizException(e); } - Assert.isTrue(result, "订单提交失败"); - // 成功响应返回值构建 OrderSubmitVO submitVO = new OrderSubmitVO(); submitVO.setOrderId(order.getId());