Example #1
0
        /// <summary>
        /// 从传入的商品参数创建一个购物车对象
        /// </summary>
        /// <param name="productSysNo">商品编号,多个商品编号用逗号(,)隔开</param>
        /// <param name="quantity">商品数量,多个购买数量用逗号(,)隔开</param>
        /// <returns></returns>
        public static ShoppingCart GetShoppingCartFromParam(string productSysNo, string quantity)
        {
            if (String.IsNullOrWhiteSpace(productSysNo) || String.IsNullOrWhiteSpace(quantity))
            {
                return(null);
            }

            String[] productSysnoArray = productSysNo.Split(new char[] { ',' });
            String[] quantityArray     = quantity.Split(new char[] { ',' });

            if (productSysnoArray.Length != quantityArray.Length)
            {
                return(null);
            }

            int temp;

            if (productSysnoArray.Any(x => !int.TryParse(x, out temp) || temp <= 0) ||
                quantityArray.Any(x => !int.TryParse(x, out temp) || temp <= 0))
            {
                return(null);
            }

            ShoppingCart shoppingCart = ShoppingStorageManager.GetShoppingCartFromCreateNew();

            shoppingCart.ShoppingItemGroupList = new List <ShoppingItemGroup>();

            for (int i = 0; i < productSysnoArray.Length; i++)
            {
                var psysno = int.Parse(productSysnoArray[i]);
                var qty    = int.Parse(quantityArray[i]);

                if (!shoppingCart.ShoppingItemGroupList.Exists(group =>
                {
                    if (group.ShoppingItemList == null)
                    {
                        return(false);
                    }
                    return(group.ShoppingItemList.Exists(x =>
                    {
                        if (x.ProductSysNo == psysno)
                        {
                            group.Quantity += qty;
                            return true;
                        }
                        return false;
                    }));
                }))
                {
                    shoppingCart.ShoppingItemGroupList.Add(new ShoppingItemGroup()
                    {
                        Quantity         = qty,
                        ShoppingItemList = new List <ShoppingItem>()
                        {
                            new ShoppingItem()
                            {
                                ProductSysNo = psysno,
                                UnitQuantity = 1
                            }
                        }
                    });
                }
            }

            return(shoppingCart);
        }