Beispiel #1
0
        /// <summary>
        /// 判断购物车提交时,阶梯商品是否达最小批量
        /// </summary>
        /// <param name="cartItemIds"></param>
        /// <returns></returns>
        public static bool IsExistLadderMinMath(string cartItemIds, ref string msg)
        {
            msg = "结算的商品必须满足最小批量才能购买!";
            var result         = true;
            var cartItemIdsArr = cartItemIds.Split(',').Where(d => !string.IsNullOrWhiteSpace(d)).Select(t => long.Parse(t));
            var cartItems      = CartApplication.GetCartItems(cartItemIdsArr);

            if (cartItems.Any())
            {
                var groupCart = cartItems.Where(item => item.ShopBranchId == 0).ToList().Select(c =>
                {
                    var cItem   = new Himall.Entities.ShoppingCartItem();
                    var skuInfo = Service.GetSku(c.SkuId);
                    if (skuInfo != null)
                    {
                        cItem = c;
                    }
                    return(cItem);
                }).GroupBy(i => i.ProductId);
                foreach (var cart in cartItems.ToList())
                {
                    var product = GetProduct(cart.ProductId);
                    if (product.IsOpenLadder)
                    {
                        var quantity =
                            groupCart.Where(i => i.Key == cart.ProductId)
                            .ToList()
                            .Sum(cartitem => cartitem.Sum(i => i.Quantity));
                        var minMath = GetProductLadderMinMath(cart.ProductId);
                        if (minMath > 0 && quantity < minMath)
                        {
                            result = false;
                        }
                    }
                    else
                    {
                        var sku = Service.GetSku(cart.SkuId);
                        if (cart.Quantity > sku.Stock)
                        {
                            msg = string.Format("商品“{0}”库存不足,仅剩{1}件", CutString(product.ProductName, 10, "..."), sku.Stock);
                            return(false);
                        }
                    }
                }
            }
            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// 取用户在门店的购物车数量
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="branchIds"></param>
        /// <returns></returns>
        public static Dictionary <long, int> GetShopBranchCartItemCount(long userId, List <long> branchIds)
        {
            Dictionary <long, int> result = new Dictionary <long, int>();

            if (userId == 0)
            {
                return(result);
            }
            Himall.Entities.ShoppingCartInfo memberCartInfo = new Himall.Entities.ShoppingCartInfo();
            List <DTO.Product.Product>       onSaleProducts = new List <DTO.Product.Product>();

            if (userId > 0)
            {//如果已登陆取购物车数据
                memberCartInfo = CartApplication.GetShopBranchCart(userId);
                if (memberCartInfo != null)
                {
                    onSaleProducts = ProductManagerApplication.GetAllStatusProductByIds(memberCartInfo.Items.Select(e => e.ProductId)).ToList();
                }
            }

            Dictionary <long, long> buyedCounts = null;

            if (userId > 0)
            {
                buyedCounts = new Dictionary <long, long>();
                buyedCounts = OrderApplication.GetProductBuyCount(userId, memberCartInfo.Items.Select(x => x.ProductId));
            }
            var shopBranchSkuList = Service.GetSkusByBranchIds(branchIds, skuids: memberCartInfo.Items.Select(s => s.SkuId).ToList());

            foreach (var id in branchIds)
            {
                //var cartQuantity = memberCartInfo.Items.Where(c => c.ShopBranchId.HasValue && c.ShopBranchId.Value == query.shopBranchId).Sum(c => c.Quantity);
                //过滤购物车 无效商品
                var cartQuantity = memberCartInfo.Items.Where(c => c.ShopBranchId == id).Select(item =>
                {
                    var product       = onSaleProducts.FirstOrDefault(p => p.Id == item.ProductId);
                    var shopbranchsku = shopBranchSkuList.FirstOrDefault(x => x.ShopBranchId == id && x.SkuId == item.SkuId);
                    long stock        = shopbranchsku == null ? 0 : shopbranchsku.Stock;

                    if (stock > product.MaxBuyCount && product.MaxBuyCount != 0)
                    {
                        stock = product.MaxBuyCount;
                    }
                    if (product.MaxBuyCount > 0 && buyedCounts != null && buyedCounts.ContainsKey(item.ProductId))
                    {
                        long buynum = buyedCounts[item.ProductId];
                        stock       = stock - buynum;
                    }
                    var status = product.IsOpenLadder ? 1 : (shopbranchsku == null ? 1 : (shopbranchsku.Status == ShopBranchSkuStatus.Normal) ? (item.Quantity > stock ? 2 : 0) : 1);//0:正常;1:冻结;2:库存不足
                    if (status == 0)
                    {
                        return(item.Quantity);
                    }
                    else
                    {
                        return(0);
                    }
                }).Sum(count => count);
                if (cartQuantity > 0)
                {
                    result.Add(id, cartQuantity);
                }
            }
            return(result);
        }