Search in sources :

Example 1 with FlashItemDTO

use of com.actionworks.flashsale.app.model.dto.FlashItemDTO in project flash-sale by ThoughtsBeta.

the class DefaultFlashItemAppService method getFlashItem.

@Override
public AppSimpleResult<FlashItemDTO> getFlashItem(Long itemId) {
    FlashItemCache flashItemCache = flashItemCacheService.getCachedItem(itemId, null);
    if (!flashItemCache.isExist()) {
        throw new BizException(ACTIVITY_NOT_FOUND.getErrDesc());
    }
    if (flashItemCache.isLater()) {
        return AppSimpleResult.tryLater();
    }
    updateLatestItemStock(null, flashItemCache.getFlashItem());
    FlashItemDTO flashItemDTO = FlashItemAppBuilder.toFlashItemDTO(flashItemCache.getFlashItem());
    flashItemDTO.setVersion(flashItemCache.getVersion());
    return AppSimpleResult.ok(flashItemDTO);
}
Also used : FlashItemCache(com.actionworks.flashsale.app.service.item.cache.model.FlashItemCache) BizException(com.actionworks.flashsale.app.exception.BizException) FlashItemDTO(com.actionworks.flashsale.app.model.dto.FlashItemDTO)

Example 2 with FlashItemDTO

use of com.actionworks.flashsale.app.model.dto.FlashItemDTO in project flash-sale by ThoughtsBeta.

the class FlashItemController method getOnlineFlashItems.

@GetMapping(value = "/activities/{activityId}/flash-items/online")
@SentinelResource("GetOnlineFlashItems")
public MultiResponse<FlashItemResponse> getOnlineFlashItems(@RequestAttribute Long userId, @PathVariable Long activityId, @RequestParam Integer pageSize, @RequestParam Integer pageNumber, @RequestParam(required = false) String keyword) {
    FlashItemsQuery flashItemsQuery = new FlashItemsQuery().setKeyword(keyword).setPageSize(pageSize).setPageNumber(pageNumber).setStatus(FlashItemStatus.ONLINE.getCode());
    AppMultiResult<FlashItemDTO> flashItemsResult = flashItemAppService.getFlashItems(userId, activityId, flashItemsQuery);
    if (!flashItemsResult.isSuccess() || flashItemsResult.getData() == null) {
        return ResponseBuilder.withMulti(flashItemsResult);
    }
    return MultiResponse.of(toFlashItemsResponse(flashItemsResult.getData()), flashItemsResult.getTotal());
}
Also used : FlashItemDTO(com.actionworks.flashsale.app.model.dto.FlashItemDTO) FlashItemsQuery(com.actionworks.flashsale.app.model.query.FlashItemsQuery) GetMapping(org.springframework.web.bind.annotation.GetMapping) SentinelResource(com.alibaba.csp.sentinel.annotation.SentinelResource)

Example 3 with FlashItemDTO

use of com.actionworks.flashsale.app.model.dto.FlashItemDTO in project flash-sale by ThoughtsBeta.

the class DefaultFlashItemAppService method getFlashItems.

@Override
public AppMultiResult<FlashItemDTO> getFlashItems(Long userId, Long activityId, FlashItemsQuery flashItemsQuery) {
    if (flashItemsQuery == null) {
        return AppMultiResult.empty();
    }
    flashItemsQuery.setActivityId(activityId);
    List<FlashItem> activities;
    Integer total;
    if (flashItemsQuery.isOnlineFirstPageQuery()) {
        FlashItemsCache flashItemsCache = flashItemsCacheService.getCachedItems(activityId, flashItemsQuery.getVersion());
        if (flashItemsCache.isLater()) {
            return AppMultiResult.tryLater();
        }
        activities = flashItemsCache.getFlashItems();
        total = flashItemsCache.getTotal();
    } else {
        PageResult<FlashItem> flashItemPageResult = flashItemDomainService.getFlashItems(toFlashItemsQuery(flashItemsQuery));
        activities = flashItemPageResult.getData();
        total = flashItemPageResult.getTotal();
    }
    List<FlashItemDTO> flashItemDTOList = activities.stream().map(FlashItemAppBuilder::toFlashItemDTO).collect(Collectors.toList());
    return AppMultiResult.of(flashItemDTOList, total);
}
Also used : FlashItem(com.actionworks.flashsale.domain.model.entity.FlashItem) FlashItemDTO(com.actionworks.flashsale.app.model.dto.FlashItemDTO) FlashItemsCache(com.actionworks.flashsale.app.service.item.cache.model.FlashItemsCache)

Example 4 with FlashItemDTO

use of com.actionworks.flashsale.app.model.dto.FlashItemDTO 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 5 with FlashItemDTO

use of com.actionworks.flashsale.app.model.dto.FlashItemDTO in project flash-sale by ThoughtsBeta.

the class QueuedPlaceOrderService method doPlaceOrder.

@Override
public PlaceOrderResult doPlaceOrder(Long userId, FlashPlaceOrderCommand placeOrderCommand) {
    logger.info("placeOrder|开始下单|{},{}", userId, JSON.toJSONString(placeOrderCommand));
    if (placeOrderCommand == null || !placeOrderCommand.validateParams()) {
        return PlaceOrderResult.failed(INVALID_PARAMS);
    }
    AppSimpleResult<FlashItemDTO> flashItemResult = flashItemAppService.getFlashItem(placeOrderCommand.getItemId());
    if (!flashItemResult.isSuccess() || flashItemResult.getData() == null) {
        logger.info("placeOrder|获取秒杀品失败|{},{}", userId, placeOrderCommand.getActivityId());
        return PlaceOrderResult.failed(GET_ITEM_FAILED);
    }
    FlashItemDTO flashItemDTO = flashItemResult.getData();
    if (!flashItemDTO.isOnSale()) {
        logger.info("placeOrder|当前非在售时间|{},{}", userId, placeOrderCommand.getActivityId());
        return PlaceOrderResult.failed(ITEM_NOT_ON_SALE);
    }
    String placeOrderTaskId = orderTaskIdGenerateService.generatePlaceOrderTaskId(userId, placeOrderCommand.getItemId());
    PlaceOrderTask placeOrderTask = PlaceOrderTaskBuilder.with(userId, placeOrderCommand);
    placeOrderTask.setPlaceOrderTaskId(placeOrderTaskId);
    OrderTaskSubmitResult submitResult = placeOrderTaskService.submit(placeOrderTask);
    logger.info("placeOrder|任务提交结果|{},{},{}", userId, placeOrderTaskId, JSON.toJSONString(placeOrderTask));
    if (!submitResult.isSuccess()) {
        logger.info("placeOrder|下单任务提交失败|{},{}", userId, placeOrderCommand.getActivityId());
        return PlaceOrderResult.failed(submitResult.getCode(), submitResult.getMessage());
    }
    logger.info("placeOrder|下单任务提交完成|{},{}", userId, placeOrderTaskId);
    return PlaceOrderResult.ok(placeOrderTaskId);
}
Also used : OrderTaskSubmitResult(com.actionworks.flashsale.app.model.result.OrderTaskSubmitResult) PlaceOrderTask(com.actionworks.flashsale.app.model.PlaceOrderTask) FlashItemDTO(com.actionworks.flashsale.app.model.dto.FlashItemDTO)

Aggregations

FlashItemDTO (com.actionworks.flashsale.app.model.dto.FlashItemDTO)7 BizException (com.actionworks.flashsale.app.exception.BizException)2 FlashItemsQuery (com.actionworks.flashsale.app.model.query.FlashItemsQuery)2 SentinelResource (com.alibaba.csp.sentinel.annotation.SentinelResource)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 PlaceOrderTask (com.actionworks.flashsale.app.model.PlaceOrderTask)1 OrderTaskSubmitResult (com.actionworks.flashsale.app.model.result.OrderTaskSubmitResult)1 FlashItemCache (com.actionworks.flashsale.app.service.item.cache.model.FlashItemCache)1 FlashItemsCache (com.actionworks.flashsale.app.service.item.cache.model.FlashItemsCache)1 OrderNoGenerateContext (com.actionworks.flashsale.app.util.OrderNoGenerateContext)1 StockDeduction (com.actionworks.flashsale.domain.model.StockDeduction)1 FlashItem (com.actionworks.flashsale.domain.model.entity.FlashItem)1 FlashOrder (com.actionworks.flashsale.domain.model.entity.FlashOrder)1