use of com.actionworks.flashsale.app.exception.BizException 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());
}
}
Aggregations