Search in sources :

Example 11 with FlashItem

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

the class NormalStockCacheService method alignItemStocks.

@Override
public boolean alignItemStocks(Long itemId) {
    if (itemId == null) {
        logger.info("alignItemStocks|参数为空");
        return false;
    }
    try {
        FlashItem flashItem = flashItemDomainService.getFlashItem(itemId);
        if (flashItem == null) {
            logger.info("alignItemStocks|秒杀品不存在|{}", itemId);
            return false;
        }
        if (flashItem.getInitialStock() == null) {
            logger.info("alignItemStocks|秒杀品未设置库存|{}", itemId);
            return false;
        }
        String key1ItemStocksCacheKey = getItemStocksCacheKey(itemId);
        String key2ItemStocksAlignKey = getItemStocksCacheAlignKey(itemId);
        List<String> keys = Lists.newArrayList(key1ItemStocksCacheKey, key2ItemStocksAlignKey);
        DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>(INIT_OR_ALIGN_ITEM_STOCK_LUA, Long.class);
        Long result = redisCacheService.getRedisTemplate().execute(redisScript, keys, flashItem.getAvailableStock());
        if (result == null) {
            logger.info("alignItemStocks|秒杀品库存校准失败|{},{},{}", itemId, key1ItemStocksCacheKey, flashItem.getInitialStock());
            return false;
        }
        if (result == -997) {
            logger.info("alignItemStocks|已在校准中,本次校准取消|{},{},{},{}", result, itemId, key1ItemStocksCacheKey, flashItem.getInitialStock());
            return true;
        }
        if (result == 1) {
            logger.info("alignItemStocks|秒杀品库存校准完成|{},{},{},{}", result, itemId, key1ItemStocksCacheKey, flashItem.getInitialStock());
            return true;
        }
        return false;
    } catch (Exception e) {
        logger.error("alignItemStocks|秒杀品库存校准错误|{}", itemId, e);
        return false;
    }
}
Also used : DefaultRedisScript(org.springframework.data.redis.core.script.DefaultRedisScript) FlashItem(com.actionworks.flashsale.domain.model.entity.FlashItem)

Example 12 with FlashItem

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

the class StocksAlignScheduler method alignStocksTask.

@Scheduled(cron = "*/2 * * * * ?")
@BetaTrace
public void alignStocksTask() {
    logger.info("alignStocksTask|校准库存缓存开始");
    PagesQueryCondition pagesQueryCondition = new PagesQueryCondition();
    pagesQueryCondition.setStatus(FlashItemStatus.ONLINE.getCode());
    PageResult<FlashItem> pageResult = flashItemDomainService.getFlashItems(pagesQueryCondition);
    pageResult.getData().forEach(flashItem -> {
        boolean result = itemStockCacheService.alignItemStocks(flashItem.getId());
        if (!result) {
            logger.info("alignStocksTask|库存校准失败", flashItem.getId(), flashItem.getAvailableStock());
            return;
        }
        logger.info("alignStocksTask|库存校准完成", flashItem.getId(), flashItem.getAvailableStock());
    });
    logger.info("alignStocksTask|校准库存缓存结束");
}
Also used : FlashItem(com.actionworks.flashsale.domain.model.entity.FlashItem) PagesQueryCondition(com.actionworks.flashsale.domain.model.PagesQueryCondition) Scheduled(org.springframework.scheduling.annotation.Scheduled) BetaTrace(com.actionworks.flashsale.config.annotion.BetaTrace)

Example 13 with FlashItem

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

the class DefaultFlashItemAppService method publishFlashItem.

@Override
public AppResult publishFlashItem(Long userId, Long activityId, FlashItemPublishCommand itemPublishCommand) {
    logger.info("itemPublish|发布秒杀品|{},{},{}", userId, activityId, JSON.toJSON(itemPublishCommand));
    if (userId == null || activityId == null || itemPublishCommand == null || !itemPublishCommand.validate()) {
        throw new BizException(INVALID_PARAMS);
    }
    AuthResult authResult = authorizationService.auth(userId, FLASH_ITEM_CREATE);
    if (!authResult.isSuccess()) {
        throw new AuthException(UNAUTHORIZED_ACCESS);
    }
    DistributedLock itemCreateLock = lockFactoryService.getDistributedLock(getItemCreateLockKey(userId));
    try {
        boolean isLockSuccess = itemCreateLock.tryLock(500, 1000, TimeUnit.MILLISECONDS);
        if (!isLockSuccess) {
            throw new BizException(FREQUENTLY_ERROR);
        }
        FlashActivity flashActivity = flashActivityDomainService.getFlashActivity(activityId);
        if (flashActivity == null) {
            throw new BizException(ACTIVITY_NOT_FOUND);
        }
        FlashItem flashItem = toDomain(itemPublishCommand);
        flashItem.setActivityId(activityId);
        flashItem.setStockWarmUp(0);
        flashItemDomainService.publishFlashItem(flashItem);
        logger.info("itemPublish|秒杀品已发布");
        return AppResult.buildSuccess();
    } catch (Exception e) {
        logger.error("itemPublish|秒杀品发布失败|{}", userId, e);
        throw new BizException("秒杀品发布失败");
    } finally {
        itemCreateLock.unlock();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) FlashActivity(com.actionworks.flashsale.domain.model.entity.FlashActivity) BizException(com.actionworks.flashsale.app.exception.BizException) FlashItem(com.actionworks.flashsale.domain.model.entity.FlashItem) AuthException(com.actionworks.flashsale.controller.exception.AuthException) AuthResult(com.actionworks.flashsale.app.auth.model.AuthResult) AuthException(com.actionworks.flashsale.controller.exception.AuthException) BizException(com.actionworks.flashsale.app.exception.BizException)

Example 14 with FlashItem

use of com.actionworks.flashsale.domain.model.entity.FlashItem 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 15 with FlashItem

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

the class FlashItemBuilder method toDomainObject.

public static FlashItem toDomainObject(FlashItemDO flashItemDO) {
    FlashItem flashItem = new FlashItem();
    BeanUtils.copyProperties(flashItemDO, flashItem);
    return flashItem;
}
Also used : FlashItem(com.actionworks.flashsale.domain.model.entity.FlashItem)

Aggregations

FlashItem (com.actionworks.flashsale.domain.model.entity.FlashItem)15 PagesQueryCondition (com.actionworks.flashsale.domain.model.PagesQueryCondition)4 DistributedLock (com.actionworks.flashsale.lock.DistributedLock)4 BizException (com.actionworks.flashsale.app.exception.BizException)3 FlashItemsCache (com.actionworks.flashsale.app.service.item.cache.model.FlashItemsCache)2 BetaTrace (com.actionworks.flashsale.config.annotion.BetaTrace)2 FlashItemEvent (com.actionworks.flashsale.domain.event.FlashItemEvent)2 DomainException (com.actionworks.flashsale.domain.exception.DomainException)2 Scheduled (org.springframework.scheduling.annotation.Scheduled)2 AuthResult (com.actionworks.flashsale.app.auth.model.AuthResult)1 AppException (com.actionworks.flashsale.app.exception.AppException)1 FlashItemDTO (com.actionworks.flashsale.app.model.dto.FlashItemDTO)1 FlashItemCache (com.actionworks.flashsale.app.service.item.cache.model.FlashItemCache)1 OrderNoGenerateContext (com.actionworks.flashsale.app.util.OrderNoGenerateContext)1 AuthException (com.actionworks.flashsale.controller.exception.AuthException)1 StockDeduction (com.actionworks.flashsale.domain.model.StockDeduction)1 FlashActivity (com.actionworks.flashsale.domain.model.entity.FlashActivity)1 FlashOrder (com.actionworks.flashsale.domain.model.entity.FlashOrder)1 FlashItemDO (com.actionworks.flashsale.persistence.model.FlashItemDO)1 DefaultRedisScript (org.springframework.data.redis.core.script.DefaultRedisScript)1