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);
}
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();
}
}
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();
}
}
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();
}
}
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);
}
Aggregations