Search in sources :

Example 1 with PlaceOrderResult

use of com.actionworks.flashsale.app.model.result.PlaceOrderResult in project flash-sale by ThoughtsBeta.

the class DefaultFlashOrderAppService method placeOrder.

@Override
@Transactional
public AppSimpleResult<PlaceOrderResult> placeOrder(Long userId, FlashPlaceOrderCommand placeOrderCommand) {
    logger.info("placeOrder|下单|{},{}", userId, JSON.toJSONString(placeOrderCommand));
    if (userId == null || placeOrderCommand == null || !placeOrderCommand.validateParams()) {
        throw new BizException(INVALID_PARAMS);
    }
    String placeOrderLockKey = getPlaceOrderLockKey(userId);
    DistributedLock placeOrderLock = lockFactoryService.getDistributedLock(placeOrderLockKey);
    try {
        boolean isLockSuccess = placeOrderLock.tryLock(5, 5, TimeUnit.SECONDS);
        if (!isLockSuccess) {
            return AppSimpleResult.failed(FREQUENTLY_ERROR.getErrCode(), FREQUENTLY_ERROR.getErrDesc());
        }
        boolean isPassRiskInspect = securityService.inspectRisksByPolicy(userId);
        if (!isPassRiskInspect) {
            logger.info("placeOrder|综合风控检验未通过|{}", userId);
            return AppSimpleResult.failed(PLACE_ORDER_FAILED);
        }
        PlaceOrderResult placeOrderResult = placeOrderService.doPlaceOrder(userId, placeOrderCommand);
        if (!placeOrderResult.isSuccess()) {
            return AppSimpleResult.failed(placeOrderResult.getCode(), placeOrderResult.getMessage());
        }
        logger.info("placeOrder|下单完成|{}", userId);
        return AppSimpleResult.ok(placeOrderResult);
    } catch (Exception e) {
        logger.error("placeOrder|下单失败|{},{}", userId, JSON.toJSONString(placeOrderCommand), e);
        return AppSimpleResult.failed(PLACE_ORDER_FAILED);
    } finally {
        placeOrderLock.unlock();
    }
}
Also used : PlaceOrderResult(com.actionworks.flashsale.app.model.result.PlaceOrderResult) DistributedLock(com.actionworks.flashsale.lock.DistributedLock) BizException(com.actionworks.flashsale.app.exception.BizException) BizException(com.actionworks.flashsale.app.exception.BizException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with PlaceOrderResult

use of com.actionworks.flashsale.app.model.result.PlaceOrderResult in project flash-sale by ThoughtsBeta.

the class FlashOrderController method placeOrder.

@PostMapping(value = "/flash-orders")
@SentinelResource("PlaceOrderResource")
public SingleResponse<PlaceOrderResult> placeOrder(@RequestAttribute Long userId, @RequestBody FlashPlaceOrderRequest flashPlaceOrderRequest) {
    FlashPlaceOrderCommand placeOrderCommand = FlashOrderBuilder.toCommand(flashPlaceOrderRequest);
    AppSimpleResult<PlaceOrderResult> placeOrderResult = flashOrderAppService.placeOrder(userId, placeOrderCommand);
    if (!placeOrderResult.isSuccess() || placeOrderResult.getData() == null) {
        return ResponseBuilder.withSingle(placeOrderResult);
    }
    return SingleResponse.of(placeOrderResult.getData());
}
Also used : PlaceOrderResult(com.actionworks.flashsale.app.model.result.PlaceOrderResult) FlashPlaceOrderCommand(com.actionworks.flashsale.app.model.command.FlashPlaceOrderCommand) PostMapping(org.springframework.web.bind.annotation.PostMapping) SentinelResource(com.alibaba.csp.sentinel.annotation.SentinelResource)

Aggregations

PlaceOrderResult (com.actionworks.flashsale.app.model.result.PlaceOrderResult)2 BizException (com.actionworks.flashsale.app.exception.BizException)1 FlashPlaceOrderCommand (com.actionworks.flashsale.app.model.command.FlashPlaceOrderCommand)1 DistributedLock (com.actionworks.flashsale.lock.DistributedLock)1 SentinelResource (com.alibaba.csp.sentinel.annotation.SentinelResource)1 Transactional (org.springframework.transaction.annotation.Transactional)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1