예제 #1
0
        /*
         *购物车存储说明:
         * 游客访问时,点击加入购物车,购物车信息保存至Cookie中,游客点击结算时,Cookie中的购物车信息转移至数据库中并清空Cookie中购物车信息。
         *登录会员点击加入购物车时,购物车信息保存至数据库中。
         * Cookie存储格式: skuId1:count1,skuId2:count2,.....
         */

        /// <summary>
        /// 同步客户端购物车信息至服务器
        /// </summary>
        public void UpdateCartInfoFromCookieToServer(long memberId)
        {
            string cartInfo = WebHelper.GetCookie(CookieKeysCollection.HIMALL_CART);

            if (!string.IsNullOrWhiteSpace(cartInfo))
            {
                string[] cartItems         = cartInfo.Split(',');
                var      shoppingCartItems = new List <Himall.Entities.ShoppingCartItem>();
                foreach (string cartItem in cartItems)
                {
                    var cartItemParts = cartItem.Split(':');
                    shoppingCartItems.Add(new Himall.Entities.ShoppingCartItem()
                    {
                        SkuId = cartItemParts[0], Quantity = int.Parse(cartItemParts[1])
                    });
                }
                CartApplication.AddToCart(shoppingCartItems, memberId);
            }
            WebHelper.DeleteCookie(CookieKeysCollection.HIMALL_CART);
        }
예제 #2
0
        public void AddToCart(string skuId, int count, long memberId)
        {
            CheckSkuIdIsValid(skuId);
            //判断库存
            var sku = ProductManagerApplication.GetSKU(skuId);

            if (sku == null)
            {
                throw new HimallException("错误的SKU");
            }
            if (count > sku.Stock)
            {
                throw new HimallException("库存不足");
            }
            #region 商品限购判断
            var prouctInfo = ProductManagerApplication.GetProduct(sku.ProductId);
            if (prouctInfo != null && prouctInfo.MaxBuyCount > 0 && !prouctInfo.IsOpenLadder)//商品有限购数量
            {
                var carInfo = CartApplication.GetCart(memberId);
                if (carInfo != null)
                {
                    var quantity = carInfo.Items.Where(p => p.ProductId == sku.ProductId).Sum(d => d.Quantity); //当前用户该商品已加入购物车数量
                    if (count + quantity > prouctInfo.MaxBuyCount)                                              //已有数量+新数量
                    {
                        throw new HimallException(string.Format("每个ID限购{0}件", prouctInfo.MaxBuyCount));
                    }
                }
            }
            #endregion

            if (memberId > 0)
            {
                CartApplication.AddToCart(skuId, count, memberId);
            }
            else
            {
                string cartInfo = WebHelper.GetCookie(CookieKeysCollection.HIMALL_CART);
                if (!string.IsNullOrWhiteSpace(cartInfo))
                {
                    string[] cartItems   = cartInfo.Split(',');
                    string   newCartInfo = string.Empty;
                    bool     exist       = false;
                    foreach (string cartItem in cartItems)
                    {
                        var cartItemParts = cartItem.Split(':');
                        if (cartItemParts[0] == skuId)
                        {
                            newCartInfo += "," + skuId + ":" + (int.Parse(cartItemParts[1]) + count);
                            exist        = true;
                        }
                        else
                        {
                            newCartInfo += "," + cartItem;
                        }
                    }
                    if (!exist)
                    {
                        newCartInfo += "," + skuId + ":" + count;
                    }

                    if (!string.IsNullOrWhiteSpace(newCartInfo))
                    {
                        newCartInfo = newCartInfo.Substring(1);
                    }
                    WebHelper.SetCookie(CookieKeysCollection.HIMALL_CART, newCartInfo);
                }
                else
                {
                    WebHelper.SetCookie(CookieKeysCollection.HIMALL_CART, string.Format("{0}:{1}", skuId, count));
                }
            }
        }