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;
}
}
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|校准库存缓存结束");
}
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();
}
}
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());
}
}
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;
}
Aggregations