/// <summary> /// 向购物车中添加商品信息 /// </summary> /// <param name="ProId"></param> /// <param name="Color"></param> /// <param name="Quantity"></param> /// <param name="yardage"></param> public void Add(string ProId, string specification, int Quantity, string fittingsProductId, string fittingsProductCount,int sparepartId) { string CartKey = ChangeHope.Common.DEncryptHelper.GetRandomNumber(); CartItemInfo cartItem=new CartItemInfo() ; foreach (CartItemInfo item in cartItems.Values) { if (item.ProductId == ProId && item.Specification == specification) { bool isFittingsProductId = false; if (!string.IsNullOrEmpty(fittingsProductId.Trim())) { string[] fitProArr = fittingsProductId.Split(','); if (!string.IsNullOrEmpty(item.FittingsProductId.Trim())) { string[] shopFitProArr = item.FittingsProductId.Split(','); for (int f = 0; f < fitProArr.Length; f++) { for (int s = 0; s < shopFitProArr.Length; s++) { if (fitProArr[f] != shopFitProArr[s]) { isFittingsProductId = true; break; } } if (isFittingsProductId) { break; } } } } if (!isFittingsProductId) { CartKey = item.CartKey; } break; } } if (!cartItems.TryGetValue(CartKey, out cartItem)) { CartItemInfo newItem = new CartItemInfo(CartKey, ProId, Quantity, specification, fittingsProductId, fittingsProductCount,sparepartId.ToString()); cartItems.Add(CartKey, newItem); } else { cartItem.Quantity += Quantity; } }
/// <summary> /// Add an item to the cart. /// When ItemId to be added has already existed, this method will update the quantity instead. /// </summary> /// <param name="item">Item to add</param> public void Add(CartItemInfo item) { CartItemInfo cartItem; if (!cartItems.TryGetValue(item.CartKey.ToString(), out cartItem)) cartItems.Add(item.CartKey.ToString(), item); else cartItem.Quantity += item.Quantity; }
/// <summary> /// Retrieve collection of shopping cart items /// </summary> /// <param name="userName">User Name</param> /// <param name="appName">Application Name</param> /// <param name="isShoppingCart">Shopping cart flag</param> /// <returns>Collection of shopping cart items</returns> public IList<CartItemInfo> GetCartItems(string userName, string appName, bool isShoppingCart) { string sqlSelect = "SELECT yxs_cart.cartkey,yxs_cart.productid, yxs_cart.quantity, yxs_cart.specification, yxs_cart.fittingsProductId,yxs_cart.fittingsProductCount,yxs_cart.sparepartId FROM yxs_profiles INNER JOIN yxs_cart ON yxs_profiles.uniqueid = yxs_cart.uniqueid WHERE yxs_profiles.username = @Username AND yxs_profiles.applicationname = @ApplicationName AND IsShoppingCart = @IsShoppingCart;"; SqlParameter[] parms = { new SqlParameter("@Username", SqlDbType.VarChar, 256), new SqlParameter("@ApplicationName", SqlDbType.VarChar, 256), new SqlParameter("@IsShoppingCart", SqlDbType.Bit)}; parms[0].Value = userName; parms[1].Value = appName; parms[2].Value = isShoppingCart; SqlDataReader dr =ChangeHope.DataBase.SQLServerHelper.ExecuteReader(sqlSelect, parms); IList<CartItemInfo> cartItems = new List<CartItemInfo>(); while (dr.Read()) { CartItemInfo cartItem = new CartItemInfo(Convert.ToString(dr["cartkey"]), Convert.ToString(dr["productid"]), Convert.ToInt32(dr["quantity"]), dr["specification"].ToString(), dr["fittingsProductId"].ToString(), dr["fittingsProductCount"].ToString(), dr["sparepartId"].ToString()); cartItems.Add(cartItem); } dr.Close(); return cartItems; }