Esempio n. 1
0
        public async Task <Result> CheckoutByOrder(int?userAddressId, int orderId)
        {
            var user = await _workContext.GetCurrentUserAsync();

            var order = await _orderRepository.Query()
                        .Include(c => c.OrderItems)
                        .FirstOrDefaultAsync(c => c.Id == orderId && c.CustomerId == user.Id);

            if (order == null)
            {
                throw new Exception("订单不存在");
            }
            var param = new CheckoutParam()
            {
                CustomerId    = user.Id,
                UserAddressId = userAddressId
            };

            param.Items = order.OrderItems.Select(c => new CheckoutItemParam()
            {
                ProductId = c.ProductId,
                Quantity  = c.Quantity
            }).ToList();
            var data = await _orderService.OrderCheckout(param);

            return(Result.Ok(data));
        }
Esempio n. 2
0
        public async Task <Result> CheckoutByCart(int?userAddressId)
        {
            var user = await _workContext.GetCurrentUserAsync();

            var customerId = user.Id;
            var cart       = await _cartRepository.Query()
                             .Where(x => x.CustomerId == customerId && x.IsActive)
                             .Include(c => c.Items)
                             .FirstOrDefaultAsync();

            if (cart == null || cart.Items == null || cart.Items.Where(c => c.IsChecked).Count() <= 0)
            {
                throw new Exception("请选择商品");
            }
            if (cart.Items.Where(c => c.IsChecked).Any(c => c.Quantity <= 0))
            {
                throw new Exception("商品数量必须大于0");
            }
            var param = new CheckoutParam()
            {
                CustomerId    = customerId,
                UserAddressId = userAddressId,
            };

            param.Items = cart.Items.Where(c => c.IsChecked).Select(c => new CheckoutItemParam()
            {
                ProductId = c.ProductId,
                Quantity  = c.Quantity
            }).ToList();

            var data = await _orderService.OrderCheckout(param);

            return(Result.Ok(data));
        }
Esempio n. 3
0
        public async Task <Result> CheckoutByProduct(int?userAddressId, int productId, int quantity)
        {
            var user = await _workContext.GetCurrentUserAsync();

            var param = new CheckoutParam()
            {
                CustomerId    = user.Id,
                UserAddressId = userAddressId
            };

            param.Items.Add(new CheckoutItemParam()
            {
                ProductId = productId, Quantity = quantity
            });
            var data = await _orderService.OrderCheckout(param);

            return(Result.Ok(data));
        }
Esempio n. 4
0
        /// <summary>
        /// 验证并获取预下单信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task <CheckoutResult> OrderCheckout(CheckoutParam param)
        {
            if (param == null)
            {
                throw new Exception("参数异常");
            }
            if (param.Items == null || param.Items.Count <= 0)
            {
                throw new Exception("购买商品不存在或已下架");
            }
            if (param.Items.Any(c => c.Quantity <= 0))
            {
                throw new Exception("购买商品数量必须>0");
            }

            var user = await _workContext.GetCurrentUserOrNullAsync();

            var userId     = user?.Id ?? (int)UserWithId.System;
            var customerId = param.CustomerId;
            var customer   = await _userRepository.FirstOrDefaultAsync(customerId);

            if (customer == null)
            {
                throw new Exception("客户不存在");
            }

            var ids      = param.Items.Select(c => c.ProductId).Distinct();
            var products = await _productRepository.Query()
                           .Where(c => ids.Contains(c.Id))
                           .Include(p => p.ThumbnailImage)
                           .Include(p => p.OptionCombinations).ThenInclude(o => o.Option)
                           .Select(x => new CheckoutItemResult
            {
                Id        = x.Id,
                ProductId = x.Id,
                //Quantity = x.Quantity,
                ProductName              = x.Name,
                ProductPrice             = x.Price,
                IsAllowToOrder           = x.IsAllowToOrder,
                DisplayStockAvailability = x.DisplayStockAvailability,
                DisplayStockQuantity     = x.DisplayStockQuantity,
                StockTrackingIsEnabled   = x.StockTrackingIsEnabled,
                ProductImage             = x.ThumbnailImage.Url,
                IsPublished              = x.IsPublished,
                VariationOptions         = x.OptionCombinations.Select(c => new ProductVariationOption
                {
                    OptionName = c.Option.Name,
                    Value      = c.Value
                })
            }).ToListAsync();

            if (products == null || products.Count <= 0)
            {
                throw new Exception("购买商品不存在或已下架");
            }

            var productIds = products.Select(c => c.ProductId).Distinct();
            var stocks     = await _stockRepository.Query().Where(c => productIds.Contains(c.ProductId)).ToListAsync();

            products.ForEach(c =>
            {
                c.Quantity      = param.Items.First(x => x.ProductId == c.ProductId).Quantity;
                c.StockQuantity = stocks.Where(x => x.ProductId == c.ProductId && x.IsEnabled).Sum(x => x.StockQuantity);
                if (!c.IsPublished)
                {
                    c.IsAllowToOrder = false;
                    c.StockQuantity  = 0;
                }
                if (c.IsAllowToOrder && c.StockTrackingIsEnabled)
                {
                    if (c.StockQuantity <= 0)
                    {
                        c.IsAllowToOrder = false;
                    }
                    else
                    {
                        if (!c.DisplayStockQuantity)
                        {
                            c.StockQuantity = 0; // 不显示库存量
                        }
                    }
                }
            });

            var userAddressId = param.UserAddressId ?? customer.DefaultBillingAddressId;
            UserAddressShippingResult address = null;

            if (userAddressId.HasValue)
            {
                var list = await _userAddressService.GetList(userAddressId.Value);

                address = list.FirstOrDefault();
            }

            return(new CheckoutResult()
            {
                CouponCode = "",
                ShippingAmount = 0,
                Discount = 0,
                Address = address,
                Items = products
            });
        }