/* *购物车存储说明: * 游客访问时,点击加入购物车,购物车信息保存至Cookie中,游客点击结算时,Cookie中的购物车信息转移至数据库中并清空Cookie中购物车信息。 *登录会员点击加入购物车时,购物车信息保存至数据库中。 * Cookie存储格式: skuId1:count1,skuId2:count2,..... */ /// <summary> /// 同步客户端购物车信息至服务器 /// </summary> public void UpdateCartInfoFromCookieToServer(long memberId) { string cartInfo = WebHelper.GetCookie(CookieKeysCollection.HIMALL_CART_BRANCH); if (!string.IsNullOrWhiteSpace(cartInfo)) { string[] cartItems = cartInfo.Split(','); var shoppingCartItems = new List <Himall.Entities.ShoppingCartItem>(); foreach (string cartItem in cartItems) { var cartItemParts = cartItem.Split(':'); var skuId = cartItemParts[0]; int quantity = int.Parse(cartItemParts[1]); long shopBranchId = long.Parse(cartItemParts[2]); shoppingCartItems.Add(new Himall.Entities.ShoppingCartItem() { ShopBranchId = shopBranchId, SkuId = cartItemParts[0], Quantity = int.Parse(cartItemParts[1]) }); } CartApplication.AddToShopBranchCart(shoppingCartItems, memberId); } WebHelper.DeleteCookie(CookieKeysCollection.HIMALL_CART_BRANCH); }
public void AddToCart(string skuId, int count, long memberId, long shopBranchId) { CheckSkuIdIsValid(skuId, shopBranchId); //判断库存 var sku = ProductManagerApplication.GetSKU(skuId); if (sku == null) { throw new HimallException("错误的SKU"); } if (count > sku.Stock) { throw new HimallException("库存不足"); } var shopBranch = ShopBranchApplication.GetShopBranchById(shopBranchId); if (shopBranch == null) { throw new HimallException("错误的门店id"); } var shopBranchSkuList = ShopBranchApplication.GetSkusByIds(shopBranchId, new List <string> { skuId }); if (shopBranchSkuList == null || shopBranchSkuList.Count == 0) { throw new HimallException("门店没有该商品"); } if (shopBranchSkuList[0].Status == ShopBranchSkuStatus.InStock) { throw new HimallException("此商品已下架"); } if (memberId > 0) { CartApplication.AddToShopBranchCart(skuId, count, memberId, shopBranchId); } else { string cartInfo = WebHelper.GetCookie(CookieKeysCollection.HIMALL_CART_BRANCH); 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 && cartItemParts[2] == shopBranchId.ToString()) { newCartInfo += "," + skuId + ":" + (int.Parse(cartItemParts[1]) + count) + ":" + shopBranchId; exist = true; } else { newCartInfo += "," + cartItem; } } if (!exist) { newCartInfo += "," + skuId + ":" + count + ":" + shopBranchId; } if (!string.IsNullOrWhiteSpace(newCartInfo)) { newCartInfo = newCartInfo.Substring(1); } WebHelper.SetCookie(CookieKeysCollection.HIMALL_CART_BRANCH, newCartInfo); } else { WebHelper.SetCookie(CookieKeysCollection.HIMALL_CART_BRANCH, string.Format("{0}:{1}:{2}", skuId, count, shopBranchId)); } } }