OrderService { @Transactional public Order placeOrder(PlaceOrderDto placeOrderDto) { Order order = new Order(); order.setUserId(placeOrderDto.getUserId()); order.setStatus(OrderStatus.CREATE_PENDING); order.setOrderNo(Long.parseLong(RandomStringUtils.randomNumeric(8))); List<Long> productIds = placeOrderDto.getPlaceOrderItemList().stream() .map(PlaceOrderItemDto::getProductId) .collect(Collectors.toList()); List<ProductDto> productDtoList = productGateway.findProducts(productIds); Map<Long, ProductDto> productDtoMap = productDtoList.stream() .collect(Collectors.toMap(ProductDto::getId, Function.identity())); List<OrderItem> orderItemList = placeOrderDto.getPlaceOrderItemList().stream().map(placeOrderItemDto -> { OrderItem orderItem = new OrderItem(); orderItem.setProductId(placeOrderItemDto.getProductId()); orderItem.setQuantity(placeOrderItemDto.getQuantity()); ProductDto productDto = productDtoMap.get(placeOrderItemDto.getProductId()); orderItem.setPrice(productDto.getPrice()); return orderItem; }).collect(Collectors.toList()); order.setOrderItemList(orderItemList); order.setTotalAmount(order.calcTotalAmount()); List<OrderCoupon> orderCouponList = new ArrayList<>(); Set<Long> couponIdSet = new HashSet<>(placeOrderDto.getCouponIdList()); if (!couponIdSet.isEmpty()) { List<CouponDto> couponDtoList = couponGateway.findCoupons(new ArrayList<>(couponIdSet)); orderCouponList = couponDtoList.stream().map(couponDto -> { OrderCoupon orderCoupon = new OrderCoupon(); orderCoupon.setCouponAmount(couponDto.getAmount()); orderCoupon.setCouponCode(couponDto.getCode()); orderCoupon.setCouponId(couponDto.getId()); return orderCoupon; }).collect(Collectors.toList()); } long couponAmount = orderCouponList.stream().mapToLong(OrderCoupon::getCouponAmount).sum(); order.setPayAmount(order.calcPayAmount(order.getTotalAmount(), couponAmount)); if (order.getPayAmount() > 0L) { boolean balanceEnough = accountGateway.isBalanceEnough(placeOrderDto.getUserId(), order.getPayAmount()); if(!balanceEnough) { throw new AppBusinessException(CommonErrorCode.BAD_REQUEST, "下单失败, 账户余额不足"); } } orderRepository.save(order); orderItemList.forEach(orderItem -> { orderItem.setOrderId(order.getId()); orderItemRepository.save(orderItem); }); orderCouponList.forEach(orderCoupon -> { orderCoupon.setOrderId(order.getId()); orderCouponRepository.save(orderCoupon); }); Optional<AskReduceBalance> askReduceBalance = Optional.empty(); if(order.getPayAmount() > 0L) { askReduceBalance = Optional.of(new AskReduceBalance(placeOrderDto.getUserId(), order.getPayAmount())); } Optional<AskUseCoupon> askUseCoupon = Optional.empty(); if(!orderCouponList.isEmpty()) { List<Long> couponIds = orderCouponList.stream().map(OrderCoupon::getCouponId).collect(Collectors.toList()); askUseCoupon = Optional.of(new AskUseCoupon(couponIds, placeOrderDto.getUserId(), order.getId())); } if(!askReduceBalance.isPresent() && !askUseCoupon.isPresent()) { markCreateSuccess(order.getId()); } else { eventBus.ask( AskParameterBuilder.askOptional(askReduceBalance, askUseCoupon) .callbackClass(OrderCreateCallback.class) .addParam("orderId", String.valueOf(order.getId())) .build() ); } return order; }  @Transactional(readOnly = true) Order get(Long orderId); @Transactional Order placeOrder(PlaceOrderDto placeOrderDto); @Transactional void markCreateSuccess(Long orderId); @Transactional void markCreateFail(Long orderId);  }
@Test @Ignore public void test() { PlaceOrderDto placeOrderDto = new PlaceOrderDto(); placeOrderDto.setUserId(1L); PlaceOrderItemDto placeOrderItemDto1 = new PlaceOrderItemDto(); placeOrderItemDto1.setProductId(1L); placeOrderItemDto1.setQuantity(1); PlaceOrderItemDto placeOrderItemDto2 = new PlaceOrderItemDto(); placeOrderItemDto2.setProductId(2L); placeOrderItemDto2.setQuantity(2); placeOrderDto.setPlaceOrderItemList(Lists.newArrayList(placeOrderItemDto1, placeOrderItemDto2)); Order order = orderService.placeOrder(placeOrderDto); System.out.println(order.getId()); }