Search in sources :

Example 6 with FlashItem

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

the class FlashItemRepositoryImpl method findById.

@Override
public Optional<FlashItem> findById(Long itemId) {
    FlashItemDO flashItemDO = flashItemMapper.getById(itemId);
    if (flashItemDO == null) {
        return Optional.empty();
    }
    FlashItem flashItem = FlashItemBuilder.toDomainObject(flashItemDO);
    return Optional.of(flashItem);
}
Also used : FlashItem(com.actionworks.flashsale.domain.model.entity.FlashItem) FlashItemDO(com.actionworks.flashsale.persistence.model.FlashItemDO)

Example 7 with FlashItem

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

the class FlashItemCacheService method tryToUpdateItemCacheByLock.

public FlashItemCache tryToUpdateItemCacheByLock(Long itemId) {
    logger.info("itemCache|更新远程缓存|{}", itemId);
    DistributedLock lock = distributedLockFactoryService.getDistributedLock(UPDATE_ITEM_CACHE_LOCK_KEY + itemId);
    try {
        boolean isLockSuccess = lock.tryLock(1, 5, TimeUnit.SECONDS);
        if (!isLockSuccess) {
            return new FlashItemCache().tryLater();
        }
        FlashItem flashItem = flashItemDomainService.getFlashItem(itemId);
        FlashItemCache flashItemCache;
        if (flashItem == null) {
            flashItemCache = new FlashItemCache().notExist();
        } else {
            flashItemCache = new FlashItemCache().with(flashItem).withVersion(System.currentTimeMillis());
        }
        distributedCacheService.put(buildItemCacheKey(itemId), JSON.toJSONString(flashItemCache), FIVE_MINUTES);
        logger.info("itemCache|远程缓存已更新|{}", itemId);
        return flashItemCache;
    } catch (InterruptedException e) {
        logger.error("itemCache|远程缓存更新失败|{}", itemId);
        return new FlashItemCache().tryLater();
    } finally {
        lock.unlock();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) FlashItemCache(com.actionworks.flashsale.app.service.item.cache.model.FlashItemCache) FlashItem(com.actionworks.flashsale.domain.model.entity.FlashItem)

Example 8 with FlashItem

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

the class FlashItemsCacheService method tryToUpdateItemsCacheByLock.

public FlashItemsCache tryToUpdateItemsCacheByLock(Long activityId) {
    logger.info("itemsCache|更新远程缓存|{}", activityId);
    DistributedLock lock = distributedLockFactoryService.getDistributedLock(UPDATE_ITEMS_CACHE_LOCK_KEY + activityId);
    try {
        boolean isLockSuccess = lock.tryLock(1, 5, TimeUnit.SECONDS);
        if (!isLockSuccess) {
            return new FlashItemsCache().tryLater();
        }
        PagesQueryCondition pagesQueryCondition = new PagesQueryCondition();
        pagesQueryCondition.setActivityId(activityId);
        pagesQueryCondition.setStatus(FlashItemStatus.ONLINE.getCode());
        PageResult<FlashItem> flashItemPageResult = flashItemDomainService.getFlashItems(pagesQueryCondition);
        FlashItemsCache flashItemsCache;
        if (flashItemPageResult == null) {
            flashItemsCache = new FlashItemsCache().notExist();
        } else {
            flashItemsCache = new FlashItemsCache().setTotal(flashItemPageResult.getTotal()).setFlashItems(flashItemPageResult.getData()).setVersion(System.currentTimeMillis());
        }
        distributedCacheService.put(buildItemCacheKey(activityId), JSON.toJSONString(flashItemsCache), FIVE_MINUTES);
        logger.info("itemsCache|远程缓存已更新|{}", activityId);
        return flashItemsCache;
    } catch (Exception e) {
        logger.error("itemsCache|远程缓存更新失败|{}", activityId);
        return new FlashItemsCache().tryLater();
    } finally {
        lock.unlock();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) FlashItem(com.actionworks.flashsale.domain.model.entity.FlashItem) FlashItemsCache(com.actionworks.flashsale.app.service.item.cache.model.FlashItemsCache) PagesQueryCondition(com.actionworks.flashsale.domain.model.PagesQueryCondition)

Example 9 with FlashItem

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

the class DefaultBucketsAPPService method arrangeStockBuckets.

@Override
public AppSimpleResult arrangeStockBuckets(Long userId, Long itemId, BucketsArrangementCommand arrangementCommand) {
    logger.info("arrangeBuckets|编排库存分桶|{},{},{}", userId, itemId, JSON.toJSON(arrangementCommand));
    String arrangementKey = getArrangementKey(userId, itemId);
    DistributedLock arrangementLock = lockFactoryService.getDistributedLock(arrangementKey);
    try {
        boolean isLockSuccess = arrangementLock.tryLock(5, 5, TimeUnit.SECONDS);
        if (!isLockSuccess) {
            return AppSimpleResult.failed(FREQUENTLY_ERROR.getErrCode(), FREQUENTLY_ERROR.getErrDesc());
        }
        FlashItem flashItem = flashItemDomainService.getFlashItem(itemId);
        if (flashItem == null) {
            throw new BizException(ITEM_NOT_FOUND.getErrDesc());
        }
        bucketsArrangementService.arrangeStockBuckets(itemId, arrangementCommand.getTotalStocksAmount(), arrangementCommand.getBucketsQuantity(), arrangementCommand.getArrangementMode());
        logger.info("arrangeBuckets|库存编排完成|{}", itemId);
        return AppSimpleResult.ok(true);
    } catch (AppException e) {
        logger.error("arrangeBuckets|库存编排失败|{}", itemId, e);
        return AppSimpleResult.failed(BUSINESS_ERROR.getErrCode(), e.getMessage());
    } catch (Exception e) {
        logger.error("arrangeBuckets|库存编排错误|{}", itemId, e);
        return AppSimpleResult.failed(ARRANGE_STOCK_BUCKETS_FAILED);
    } finally {
        arrangementLock.unlock();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) AppException(com.actionworks.flashsale.app.exception.AppException) BizException(com.actionworks.flashsale.app.exception.BizException) FlashItem(com.actionworks.flashsale.domain.model.entity.FlashItem) BizException(com.actionworks.flashsale.app.exception.BizException) AppException(com.actionworks.flashsale.app.exception.AppException)

Example 10 with FlashItem

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

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