Search in sources :

Example 6 with FlashOrder

use of com.actionworks.flashsale.domain.model.entity.FlashOrder in project flash-sale by ThoughtsBeta.

the class NormalPlaceOrderService method doPlaceOrder.

@Override
public PlaceOrderResult doPlaceOrder(Long userId, FlashPlaceOrderCommand placeOrderCommand) {
    logger.info("placeOrder|开始下单|{},{}", userId, JSON.toJSONString(placeOrderCommand));
    if (userId == null || placeOrderCommand == null || !placeOrderCommand.validateParams()) {
        throw new BizException(INVALID_PARAMS);
    }
    boolean isActivityAllowPlaceOrder = flashActivityAppService.isAllowPlaceOrderOrNot(placeOrderCommand.getActivityId());
    if (!isActivityAllowPlaceOrder) {
        logger.info("placeOrder|秒杀活动下单规则校验未通过|{},{}", userId, placeOrderCommand.getActivityId());
        return PlaceOrderResult.failed(PLACE_ORDER_FAILED);
    }
    boolean isItemAllowPlaceOrder = flashItemAppService.isAllowPlaceOrderOrNot(placeOrderCommand.getItemId());
    if (!isItemAllowPlaceOrder) {
        logger.info("placeOrder|秒杀品下单规则校验未通过|{},{}", userId, placeOrderCommand.getActivityId());
        return PlaceOrderResult.failed(PLACE_ORDER_FAILED);
    }
    AppSimpleResult<FlashItemDTO> flashItemResult = flashItemAppService.getFlashItem(placeOrderCommand.getItemId());
    if (!flashItemResult.isSuccess() || flashItemResult.getData() == null) {
        return PlaceOrderResult.failed(ITEM_NOT_FOUND);
    }
    FlashItemDTO flashItem = flashItemResult.getData();
    Long orderId = orderNoGenerateService.generateOrderNo(new OrderNoGenerateContext());
    FlashOrder flashOrderToPlace = toDomain(placeOrderCommand);
    flashOrderToPlace.setItemTitle(flashItem.getItemTitle());
    flashOrderToPlace.setFlashPrice(flashItem.getFlashPrice());
    flashOrderToPlace.setUserId(userId);
    flashOrderToPlace.setId(orderId);
    StockDeduction stockDeduction = new StockDeduction().setItemId(placeOrderCommand.getItemId()).setQuantity(placeOrderCommand.getQuantity()).setUserId(userId);
    boolean preDecreaseStockSuccess = false;
    try {
        preDecreaseStockSuccess = itemStockCacheService.decreaseItemStock(stockDeduction);
        if (!preDecreaseStockSuccess) {
            logger.info("placeOrder|库存预扣减失败|{},{}", userId, JSON.toJSONString(placeOrderCommand));
            return PlaceOrderResult.failed(PLACE_ORDER_FAILED.getErrCode(), PLACE_ORDER_FAILED.getErrDesc());
        }
        boolean decreaseStockSuccess = stockDeductionDomainService.decreaseItemStock(stockDeduction);
        if (!decreaseStockSuccess) {
            logger.info("placeOrder|库存扣减失败|{},{}", userId, JSON.toJSONString(placeOrderCommand));
            return PlaceOrderResult.failed(PLACE_ORDER_FAILED.getErrCode(), PLACE_ORDER_FAILED.getErrDesc());
        }
        boolean placeOrderSuccess = flashOrderDomainService.placeOrder(userId, flashOrderToPlace);
        if (!placeOrderSuccess) {
            throw new BizException(PLACE_ORDER_FAILED.getErrDesc());
        }
    } catch (Exception e) {
        if (preDecreaseStockSuccess) {
            boolean recoverStockSuccess = itemStockCacheService.increaseItemStock(stockDeduction);
            if (!recoverStockSuccess) {
                logger.error("placeOrder|预扣库存恢复失败|{},{}", userId, JSON.toJSONString(placeOrderCommand), e);
            }
        }
        logger.error("placeOrder|下单失败|{},{}", userId, JSON.toJSONString(placeOrderCommand), e);
        throw new BizException(PLACE_ORDER_FAILED.getErrDesc());
    }
    logger.info("placeOrder|下单成功|{},{}", userId, orderId);
    return PlaceOrderResult.ok(orderId);
}
Also used : FlashOrder(com.actionworks.flashsale.domain.model.entity.FlashOrder) BizException(com.actionworks.flashsale.app.exception.BizException) OrderNoGenerateContext(com.actionworks.flashsale.app.util.OrderNoGenerateContext) FlashItemDTO(com.actionworks.flashsale.app.model.dto.FlashItemDTO) StockDeduction(com.actionworks.flashsale.domain.model.StockDeduction) BizException(com.actionworks.flashsale.app.exception.BizException)

Example 7 with FlashOrder

use of com.actionworks.flashsale.domain.model.entity.FlashOrder in project flash-sale by ThoughtsBeta.

the class QueuedPlaceOrderService method handlePlaceOrderTask.

@Transactional
public void handlePlaceOrderTask(PlaceOrderTask placeOrderTask) {
    try {
        Long userId = placeOrderTask.getUserId();
        boolean isActivityAllowPlaceOrder = flashActivityAppService.isAllowPlaceOrderOrNot(placeOrderTask.getActivityId());
        if (!isActivityAllowPlaceOrder) {
            logger.info("handleOrderTask|秒杀活动下单规则校验未通过|{},{}", placeOrderTask.getPlaceOrderTaskId(), placeOrderTask.getActivityId());
            placeOrderTaskService.updateTaskHandleResult(placeOrderTask.getPlaceOrderTaskId(), false);
            return;
        }
        boolean isItemAllowPlaceOrder = flashItemAppService.isAllowPlaceOrderOrNot(placeOrderTask.getItemId());
        if (!isItemAllowPlaceOrder) {
            logger.info("handleOrderTask|秒杀品下单规则校验未通过|{},{}", placeOrderTask.getPlaceOrderTaskId(), placeOrderTask.getActivityId());
            placeOrderTaskService.updateTaskHandleResult(placeOrderTask.getPlaceOrderTaskId(), false);
            return;
        }
        FlashItem flashItem = flashItemDomainService.getFlashItem(placeOrderTask.getItemId());
        Long orderId = orderNoGenerateService.generateOrderNo(new OrderNoGenerateContext());
        FlashOrder flashOrderToPlace = toDomain(placeOrderTask);
        flashOrderToPlace.setItemTitle(flashItem.getItemTitle());
        flashOrderToPlace.setFlashPrice(flashItem.getFlashPrice());
        flashOrderToPlace.setUserId(userId);
        flashOrderToPlace.setId(orderId);
        StockDeduction stockDeduction = new StockDeduction().setItemId(placeOrderTask.getItemId()).setQuantity(placeOrderTask.getQuantity());
        boolean decreaseStockSuccess = stockDeductionDomainService.decreaseItemStock(stockDeduction);
        if (!decreaseStockSuccess) {
            logger.info("handleOrderTask|库存扣减失败|{},{}", placeOrderTask.getPlaceOrderTaskId(), JSON.toJSONString(placeOrderTask));
            return;
        }
        boolean placeOrderSuccess = flashOrderDomainService.placeOrder(userId, flashOrderToPlace);
        if (!placeOrderSuccess) {
            throw new BizException(PLACE_ORDER_FAILED.getErrDesc());
        }
        placeOrderTaskService.updateTaskHandleResult(placeOrderTask.getPlaceOrderTaskId(), true);
        redisCacheService.put(PLACE_ORDER_TASK_ORDER_ID_KEY + placeOrderTask.getPlaceOrderTaskId(), orderId, HOURS_24);
        logger.info("handleOrderTask|下单任务处理完成|{},{}", placeOrderTask.getPlaceOrderTaskId(), JSON.toJSONString(placeOrderTask));
    } catch (Exception e) {
        placeOrderTaskService.updateTaskHandleResult(placeOrderTask.getPlaceOrderTaskId(), false);
        logger.error("handleOrderTask|下单任务处理错误|{},{}", placeOrderTask.getPlaceOrderTaskId(), JSON.toJSONString(placeOrderTask), e);
        throw new BizException(e.getMessage());
    }
}
Also used : FlashOrder(com.actionworks.flashsale.domain.model.entity.FlashOrder) OrderNoGenerateContext(com.actionworks.flashsale.app.util.OrderNoGenerateContext) BizException(com.actionworks.flashsale.app.exception.BizException) FlashItem(com.actionworks.flashsale.domain.model.entity.FlashItem) StockDeduction(com.actionworks.flashsale.domain.model.StockDeduction) BizException(com.actionworks.flashsale.app.exception.BizException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with FlashOrder

use of com.actionworks.flashsale.domain.model.entity.FlashOrder in project flash-sale by ThoughtsBeta.

the class FlashOrderAppBuilder method toDomain.

public static FlashOrder toDomain(FlashPlaceOrderCommand flashPlaceOrderCommand) {
    if (flashPlaceOrderCommand == null) {
        return null;
    }
    FlashOrder flashOrder = new FlashOrder();
    BeanUtils.copyProperties(flashPlaceOrderCommand, flashOrder);
    return flashOrder;
}
Also used : FlashOrder(com.actionworks.flashsale.domain.model.entity.FlashOrder)

Example 9 with FlashOrder

use of com.actionworks.flashsale.domain.model.entity.FlashOrder in project flash-sale by ThoughtsBeta.

the class FlashOrderDomainServiceImpl method getOrders.

@Override
public List<FlashOrder> getOrders(PagesQueryCondition pagesQueryCondition) {
    if (pagesQueryCondition == null) {
        pagesQueryCondition = new PagesQueryCondition();
    }
    List<FlashOrder> flashOrders = flashOrderRepository.findFlashOrdersByCondition(pagesQueryCondition.buildParams());
    logger.info("Get flash orders:{},{}", flashOrders.size());
    return flashOrders;
}
Also used : FlashOrder(com.actionworks.flashsale.domain.model.entity.FlashOrder) PagesQueryCondition(com.actionworks.flashsale.domain.model.PagesQueryCondition)

Example 10 with FlashOrder

use of com.actionworks.flashsale.domain.model.entity.FlashOrder in project flash-sale by ThoughtsBeta.

the class FlashOrderDomainServiceImpl method cancelOrder.

@Override
public boolean cancelOrder(Long userId, Long orderId) {
    logger.info("placeOrder|取消订单|{},{}", userId, orderId);
    if (StringUtils.isEmpty(userId) || orderId == null) {
        throw new DomainException(PARAMS_INVALID);
    }
    Optional<FlashOrder> flashOrderOptional = flashOrderRepository.findById(orderId);
    if (!flashOrderOptional.isPresent()) {
        throw new DomainException(FLASH_ITEM_DOES_NOT_EXIST);
    }
    FlashOrder flashOrder = flashOrderOptional.get();
    if (!flashOrder.getUserId().equals(userId)) {
        throw new DomainException(FLASH_ITEM_DOES_NOT_EXIST);
    }
    if (FlashOrderStatus.isCancled(flashOrder.getStatus())) {
        return false;
    }
    flashOrder.setStatus(FlashOrderStatus.CANCELED.getCode());
    boolean saveSuccess = flashOrderRepository.updateStatus(flashOrder);
    if (saveSuccess) {
        FlashOrderEvent flashOrderEvent = new FlashOrderEvent();
        flashOrderEvent.setEventType(FlashOrderEventType.CANCEL);
        domainEventPublisher.publish(flashOrderEvent);
    }
    logger.info("placeOrder|订单已取消|{},{}", userId, orderId);
    return saveSuccess;
}
Also used : DomainException(com.actionworks.flashsale.domain.exception.DomainException) FlashOrder(com.actionworks.flashsale.domain.model.entity.FlashOrder) FlashOrderEvent(com.actionworks.flashsale.domain.event.FlashOrderEvent)

Aggregations

FlashOrder (com.actionworks.flashsale.domain.model.entity.FlashOrder)10 BizException (com.actionworks.flashsale.app.exception.BizException)3 StockDeduction (com.actionworks.flashsale.domain.model.StockDeduction)3 OrderNoGenerateContext (com.actionworks.flashsale.app.util.OrderNoGenerateContext)2 PagesQueryCondition (com.actionworks.flashsale.domain.model.PagesQueryCondition)2 Transactional (org.springframework.transaction.annotation.Transactional)2 FlashItemDTO (com.actionworks.flashsale.app.model.dto.FlashItemDTO)1 FlashOrderEvent (com.actionworks.flashsale.domain.event.FlashOrderEvent)1 DomainException (com.actionworks.flashsale.domain.exception.DomainException)1 FlashItem (com.actionworks.flashsale.domain.model.entity.FlashItem)1 FlashOrderDO (com.actionworks.flashsale.persistence.model.FlashOrderDO)1