Search in sources :

Example 1 with OrderException

use of com.amusing.start.order.exception.OrderException in project amusing-project by m-lvqingyu.

the class InwardManager method accountDetail.

/**
 * 获取用户账户信息
 *
 * @param reserveId 预定人ID
 * @return
 * @throws OrderException
 */
public UserAccountOutput accountDetail(String reserveId) {
    UserAccountOutput userAccountOutput = null;
    try {
        userAccountOutput = userClient.account(reserveId);
    } catch (Exception e) {
        log.error("[order]-create getUserAccountDetails err! reserveUserId:{}, msg:{}", reserveId, Throwables.getStackTraceAsString(e));
    }
    log.info("[order]-create reserveUserId:{}, userAccount:{}", reserveId, userAccountOutput);
    return userAccountOutput;
}
Also used : UserAccountOutput(com.amusing.start.client.output.UserAccountOutput) OrderException(com.amusing.start.order.exception.OrderException)

Example 2 with OrderException

use of com.amusing.start.order.exception.OrderException in project amusing-project by m-lvqingyu.

the class CreateServiceImpl method doSaveOrder.

/**
 * 保存订单
 *
 * @param createDto       订单信息
 * @param userAccount     账户详情
 * @param productsDetails 商品详情
 * @return
 * @throws OrderException
 */
public String doSaveOrder(CreateDto createDto, UserAccountOutput userAccount, Map<String, Integer> productMap, List<ProductOutput> productsDetails) throws OrderException {
    Snowflake snowflake = IdUtil.getSnowflake(orderWorker, orderDataCenter);
    String orderNo = snowflake.nextIdStr();
    Iterator<Map.Entry<String, Integer>> iterator = productMap.entrySet().iterator();
    BigDecimal totalAmount = BigDecimal.ZERO;
    List<OrderProductInfo> infoList = new ArrayList<>();
    while (iterator.hasNext()) {
        Map.Entry<String, Integer> next = iterator.next();
        String productId = next.getKey();
        Integer productNum = next.getValue();
        for (ProductOutput output : productsDetails) {
            if (productId.equals(output.getProductId())) {
                BigDecimal price = output.getPrice();
                BigDecimal amount = price.multiply(new BigDecimal(productNum));
                totalAmount = totalAmount.add(amount);
                OrderProductInfo productInfo = OrderProductInfo.builder().orderNo(orderNo).shopId(output.getShopId()).shopName(output.getShopName()).productId(output.getProductId()).productName(output.getProductName()).priceId(output.getPriceId()).price(output.getPrice()).num(productNum).amount(amount).build();
                infoList.add(productInfo);
            }
        }
    }
    Long currentTime = System.currentTimeMillis();
    OrderInfo orderInfo = OrderInfo.builder().orderNo(orderNo).reserveId(createDto.getReserveId()).consigneeId(createDto.getConsigneeId()).freightAmount(BigDecimal.ZERO).totalAmount(totalAmount).couponAmount(BigDecimal.ZERO).activityAmount(BigDecimal.ZERO).status(OrderStatus.SCHEDULED.getKey()).isFreight(YesNo.YES.getKey()).isEvaluate(YesNo.NO.getKey()).createBy(createDto.getReserveId()).createTime(currentTime).updateBy(createDto.getReserveId()).updateTime(currentTime).build();
    BigDecimal userAmount = userAccount.getMainAmount().add(userAccount.getGiveAmount()).subtract(userAccount.getFrozenAmount());
    if (userAmount.compareTo(totalAmount) < OrderConstant.ZERO) {
        throw new OrderException(OrderCode.INSUFFICIENT_BALANCE);
    }
    // 保存订单-口库存-扣余额
    orderInfoMapper.insert(orderInfo);
    for (OrderProductInfo orderProductInfo : infoList) {
        orderProductInfoMapper.insert(orderProductInfo);
    }
    Boolean result = inwardManager.mainSettlement(orderInfo.getReserveId(), totalAmount);
    if (!result) {
        throw new OrderException(OrderCode.UNABLE_PROVIDE_SERVICE);
    }
    result = inwardManager.deductionStock(productMap);
    if (!result) {
        throw new OrderException(OrderCode.UNABLE_PROVIDE_SERVICE);
    }
    return orderNo;
}
Also used : OrderInfo(com.amusing.start.order.pojo.OrderInfo) OrderProductInfo(com.amusing.start.order.pojo.OrderProductInfo) BigDecimal(java.math.BigDecimal) Snowflake(cn.hutool.core.lang.Snowflake) ProductOutput(com.amusing.start.client.output.ProductOutput) OrderException(com.amusing.start.order.exception.OrderException)

Example 3 with OrderException

use of com.amusing.start.order.exception.OrderException in project amusing-project by m-lvqingyu.

the class CreateServiceImpl method create.

/**
 * 创建订单
 *
 * @param createDto 订单信息
 * @return
 * @throws OrderException
 */
@GlobalTransactional
@Transactional(rollbackFor = Exception.class)
@Override
public String create(CreateDto createDto) throws OrderException {
    // 1.获取购物车信息
    Map<String, Integer> productMap = shopCarService.get(createDto.getReserveId());
    if (productMap == null || productMap.isEmpty()) {
        throw new OrderException(OrderCode.PRODUCT_NOT_FOUND);
    }
    // 2.判断库存是否足够
    Map<String, Long> productStock = inwardManager.productStock(productMap.keySet());
    if (productStock == null || productMap.size() != productStock.size()) {
        throw new OrderException(OrderCode.PRODUCT_NOT_FOUND);
    }
    Iterator<Map.Entry<String, Long>> iterator = productStock.entrySet().iterator();
    if (iterator.hasNext()) {
        Map.Entry<String, Long> next = iterator.next();
        Long value = next.getValue();
        if (value == null || value <= OrderConstant.ZERO) {
            throw new OrderException(OrderCode.PRODUCT_NOT_FOUND);
        }
    }
    // 3.获取预定人账户信息
    UserAccountOutput userAccount = inwardManager.accountDetail(createDto.getReserveId());
    Optional.ofNullable(userAccount).orElseThrow(() -> new OrderException(OrderCode.USER_NOT_FOUND));
    // 4.获取商品信息及单价
    Set<String> productIdSet = productMap.keySet();
    List<ProductOutput> productsDetails = inwardManager.productDetails(productIdSet);
    // 5.保存订单相关信息
    return doSaveOrder(createDto, userAccount, productMap, productsDetails);
}
Also used : UserAccountOutput(com.amusing.start.client.output.UserAccountOutput) ProductOutput(com.amusing.start.client.output.ProductOutput) OrderException(com.amusing.start.order.exception.OrderException) GlobalTransactional(io.seata.spring.annotation.GlobalTransactional) GlobalTransactional(io.seata.spring.annotation.GlobalTransactional) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with OrderException

use of com.amusing.start.order.exception.OrderException in project amusing-project by m-lvqingyu.

the class OrderInfoController method create.

/**
 * 订单创建
 *
 * @param from  订单信息
 * @return
 */
@PostMapping("create/v1")
public ApiResult<String> create(@Valid @RequestBody CreateFrom from) throws OrderException, UnauthorizedException {
    CreateDto createDto = fromToDto(from);
    String orderId = "";
    try {
        orderId = orderCreateService.create(createDto);
    } catch (Exception e) {
        log.error("[Order]-[create]-msg:{}", Throwables.getStackTraceAsString(e));
    }
    return StringUtils.isEmpty(orderId) ? ApiResult.result(OrderCode.ORDER_SAVE_FAIL) : ApiResult.ok(orderId);
}
Also used : UnauthorizedException(com.amusing.start.exception.UnauthorizedException) OrderException(com.amusing.start.order.exception.OrderException) CreateDto(com.amusing.start.order.dto.create.CreateDto) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

OrderException (com.amusing.start.order.exception.OrderException)4 ProductOutput (com.amusing.start.client.output.ProductOutput)2 UserAccountOutput (com.amusing.start.client.output.UserAccountOutput)2 Snowflake (cn.hutool.core.lang.Snowflake)1 UnauthorizedException (com.amusing.start.exception.UnauthorizedException)1 CreateDto (com.amusing.start.order.dto.create.CreateDto)1 OrderInfo (com.amusing.start.order.pojo.OrderInfo)1 OrderProductInfo (com.amusing.start.order.pojo.OrderProductInfo)1 GlobalTransactional (io.seata.spring.annotation.GlobalTransactional)1 BigDecimal (java.math.BigDecimal)1 Transactional (org.springframework.transaction.annotation.Transactional)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1