Search in sources :

Example 16 with BizException

use of com.actionworks.flashsale.app.exception.BizException 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)

Aggregations

BizException (com.actionworks.flashsale.app.exception.BizException)16 DistributedLock (com.actionworks.flashsale.lock.DistributedLock)9 AuthResult (com.actionworks.flashsale.app.auth.model.AuthResult)7 AuthException (com.actionworks.flashsale.controller.exception.AuthException)7 StockDeduction (com.actionworks.flashsale.domain.model.StockDeduction)3 FlashItem (com.actionworks.flashsale.domain.model.entity.FlashItem)3 FlashOrder (com.actionworks.flashsale.domain.model.entity.FlashOrder)3 Transactional (org.springframework.transaction.annotation.Transactional)3 FlashItemDTO (com.actionworks.flashsale.app.model.dto.FlashItemDTO)2 OrderNoGenerateContext (com.actionworks.flashsale.app.util.OrderNoGenerateContext)2 FlashActivity (com.actionworks.flashsale.domain.model.entity.FlashActivity)2 AppException (com.actionworks.flashsale.app.exception.AppException)1 FlashActivityDTO (com.actionworks.flashsale.app.model.dto.FlashActivityDTO)1 OrderTaskHandleResult (com.actionworks.flashsale.app.model.result.OrderTaskHandleResult)1 PlaceOrderResult (com.actionworks.flashsale.app.model.result.PlaceOrderResult)1 FlashActivityCache (com.actionworks.flashsale.app.service.activity.cache.model.FlashActivityCache)1 FlashItemCache (com.actionworks.flashsale.app.service.item.cache.model.FlashItemCache)1 QueuedPlaceOrderService (com.actionworks.flashsale.app.service.placeorder.queued.QueuedPlaceOrderService)1 DomainException (com.actionworks.flashsale.domain.exception.DomainException)1 FlowException (com.alibaba.csp.sentinel.slots.block.flow.FlowException)1