示例#1
0
        public async Task <BaseApiResponse> Add([FromBody] AddOrderRequest request)
        {
            request.CheckNotNull(nameof(request));
            request.ExpressAddress.CheckNotNull(nameof(request.ExpressAddress));

            var currentAccount = _contextService.GetCurrentAccount(HttpContext);

            var command = new PlaceOrderCommand(
                GuidUtil.NewSequentialId(),
                currentAccount.UserId.ToGuid(),
                new Common.ExpressAddressInfo(
                    request.ExpressAddress.Region,
                    request.ExpressAddress.Address,
                    request.ExpressAddress.Name,
                    request.ExpressAddress.Mobile,
                    request.ExpressAddress.Zip
                    ),
                request.CartGoodses.Select(x => new SpecificationInfo(
                                               x.SpecificationId,
                                               x.GoodsId,
                                               x.StoreId,
                                               x.GoodsName,
                                               x.GoodsPic,
                                               x.SpecificationName,
                                               x.Price,
                                               x.OriginalPrice,
                                               x.Quantity,
                                               x.Benevolence
                                               )).ToList());

            if (!command.Specifications.Any())
            {
                return(new BaseApiResponse {
                    Code = 400, Message = "订单至少包含一个商品"
                });
            }
            var result = await ExecuteCommandAsync(command);

            if (!result.IsSuccess())
            {
                return(new BaseApiResponse {
                    Code = 400, Message = "命令没有执行成功:{0}".FormatWith(result.GetErrorMessage())
                });
            }
            //等待订单预定成功
            var order = WaitUntilReservationCompleted(command.AggregateRootId).Result;

            if (order == null)
            {
                return(new BaseApiResponse {
                    Code = 400, Message = "商品预定失败,请稍后再试"
                });
            }

            //预定成功,处理付款信息,要求用户付款
            if (order.Status == (int)OrderStatus.ReservationSuccess)
            {
                return(await StartPayment(order.OrderId));
            }
            else
            {
                return(new BaseApiResponse {
                    Code = 400, Message = "预定失败,请稍后再试"
                });
            }
        }