Search in sources :

Example 1 with MQSender

use of com.whoiszxl.mq.MQSender in project shopzz by whoiszxl.

the class OrderServiceImpl method submitOrder.

@Override
@Transactional
public String submitOrder(OrderSubmitVO orderSubmitVo) {
    // 0. 获取当前登录用户的信息
    MemberDetailDTO memberDetailDTO = memberFeignClient.getMemberInfo();
    // 1. 创建订单和订单详细条目
    OrderCreateInfo orderCreateInfo = createOrderInfo(memberDetailDTO, orderSubmitVo);
    // 2. 验证提交的价格和计算的价格是否一致
    if (orderSubmitVo.getTotalAmount().compareTo(orderCreateInfo.getPayPrice()) != 0) {
        ExceptionCatcher.catchValidateEx(ResponseResult.buildError("价格计算异常"));
    }
    // 3. 订单与订单详细条目入库
    Order order = orderCreateInfo.getOrder();
    this.save(order);
    orderItemService.saveBatch(orderCreateInfo.getOrderItemList());
    // 4. 创建一个操作日志订单状态管理器,在订单状态流转到待付款状态时记录操作记录
    orderStateManager.create(order);
    // 5. 通知库存中心订单提交了,锁定库存中心的SKU库存
    OrderCreateInfoDTO orderRequestParam = orderCreateInfo.clone(OrderCreateInfoDTO.class);
    ResponseResult updateInventoryResult = inventoryFeignClient.notifySubmitOrderEvent(orderRequestParam);
    if (!updateInventoryResult.isOk()) {
        ExceptionCatcher.catchValidateEx(ResponseResult.buildError("库存不足"));
    }
    // 6. 使用优惠券
    ResponseResult<Boolean> useCouponResult = promotionFeignClient.useCoupon(orderSubmitVo.getCouponId());
    if (!useCouponResult.isOk()) {
        log.error("优惠券使用失败:{}", useCouponResult.getMessage());
    }
    // 7. 发送Kafka消息到WMS中心进行处理
    // 7.1 通过商品的SKU ID查询到货位库存的明细条目,并进行遍历,一个SKU可能在多个货位上
    // 7.2 创建出需要拣货的条目和发货的条目并进行批量入库
    // 7.4 更新wms中心的库存
    OrderCreateInfoDTO orderCreateInfoDTO = new OrderCreateInfoDTO();
    BeanCopierUtils.copyProperties(orderCreateInfo, orderCreateInfoDTO);
    MQSender kafkaSender = mqSenderFactory.get(MQEnum.KAFKA);
    kafkaSender.send(MQConstants.SUBMIT_ORDER_QUEUE, JsonUtil.toJson(orderCreateInfoDTO));
    return order.getOrderSn();
}
Also used : Order(com.whoiszxl.entity.Order) MQSender(com.whoiszxl.mq.MQSender) OrderCreateInfoDTO(com.whoiszxl.dto.OrderCreateInfoDTO) OrderCreateInfo(com.whoiszxl.entity.vo.OrderCreateInfo) ResponseResult(com.whoiszxl.bean.ResponseResult) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with MQSender

use of com.whoiszxl.mq.MQSender in project shopzz by whoiszxl.

the class OrderServiceImpl method orderSubmit.

@Override
@Transactional
public String orderSubmit(OrderSubmitRequest orderSubmitRequest) {
    // TODO 防重校验
    // 0. 获取当前登录用户的信息
    ResponseResult<MemberDetailDTO> memberInfoResult = memberFeignClient.getMemberInfo();
    if (!memberInfoResult.isOk()) {
        ExceptionCatcher.catchValidateEx(ResponseResult.buildError("用户异常"));
    }
    MemberDetailDTO memberDetailDTO = memberInfoResult.getData();
    // 1. 创建订单和订单详细条目
    OrderCreateInfo orderCreateInfo = createOrderInfo(memberDetailDTO, orderSubmitRequest);
    // 2. 订单与订单详细条目入库
    Order order = orderCreateInfo.getOrder();
    this.save(order);
    orderItemService.saveBatch(orderCreateInfo.getOrderItemList());
    // 4. 创建一个操作日志订单状态管理器,在订单状态流转到待付款状态时记录操作记录
    orderStateManager.create(order);
    // 5. TODO 校验库存,锁定库存
    List<Long> skuIds = orderCreateInfo.getOrderItemList().stream().map(OrderItem::getSkuId).collect(Collectors.toList());
    ResponseResult<Boolean> responseResult = productFeignClient.checkStock(skuIds);
    if (!responseResult.getData()) {
        ExceptionCatcher.catchValidateEx(ResponseResult.buildError("库存不足"));
    }
    // 6. 使用优惠券
    if (StringUtils.isNotBlank(orderSubmitRequest.getCouponId())) {
        ResponseResult<Boolean> useCouponResult = promotionFeignClient.useCoupon(orderSubmitRequest.getCouponId());
        if (!useCouponResult.isOk()) {
            String message = useCouponResult.getMessage();
            log.error("优惠券使用失败:{}", message);
            ExceptionCatcher.catchValidateEx(ResponseResult.buildError("优惠券使用失败"));
        }
    }
    // 7. 清空购物车中选中的item
    cartService.clearCheckedCart();
    // 7. 发送Kafka消息到WMS中心进行处理
    // 7.1 通过商品的SKU ID查询到货位库存的明细条目,并进行遍历,一个SKU可能在多个货位上
    // 7.2 创建出需要拣货的条目和发货的条目并进行批量入库
    // 7.4 更新wms中心的库存
    OrderCreateInfoDTO orderCreateInfoDTO = dozerUtils.map(orderCreateInfo, OrderCreateInfoDTO.class);
    MQSender kafkaSender = mqSenderFactory.get(MQEnum.ROCKETMQ);
    kafkaSender.send(MQConstants.SUBMIT_ORDER_QUEUE, JsonUtil.toJson(orderCreateInfoDTO));
    return order.getOrderSn();
}
Also used : Order(com.whoiszxl.entity.Order) MQSender(com.whoiszxl.mq.MQSender) OrderCreateInfo(com.whoiszxl.entity.vo.OrderCreateInfo) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Order (com.whoiszxl.entity.Order)2 OrderCreateInfo (com.whoiszxl.entity.vo.OrderCreateInfo)2 MQSender (com.whoiszxl.mq.MQSender)2 Transactional (org.springframework.transaction.annotation.Transactional)2 ResponseResult (com.whoiszxl.bean.ResponseResult)1 OrderCreateInfoDTO (com.whoiszxl.dto.OrderCreateInfoDTO)1