/// <summary> /// 更新用户购物车 /// </summary> /// <param name="cart"></param> /// <param name="removeItems"></param> private void ResetUserCart(UserCartModel cart, IEnumerable<Order_Product> removeItems) { foreach (var orderProduct in removeItems) { Order_Product product = orderProduct; cart.ProductItems.Remove(p => p.ProductID == product.ProductID); cart.BuyList = new List<CartProduct>(); } MongoDBHelper.UpdateModel<UserCartModel>(cart, c => c.VisitorKey == this.UserSession.VisitorKey); }
public ActionResult OrderInfo(int[] proIds, int[] quantity) { try { var orderInfo = new OrderInfoViewModel(); var addresses = new UserReceiveAddressService().QueryReceiveAddressByUserID(this.UserSession.UserID); if (addresses != null) { orderInfo.UserReceiveAddressList = new List<UserReceiveAddressModel>(); foreach (var userRecieveAddress in addresses) { orderInfo.UserReceiveAddressList.Add( DataTransfer.Transfer<UserReceiveAddressModel>(userRecieveAddress, typeof(User_RecieveAddress))); } } var userCart = MongoDBHelper.GetModel<UserCartModel>(m => m.VisitorKey == this.UserSession.VisitorKey); CartController.WriteUserCartOperationLog( new UserCartOperationLog() { OperateTime = DateTime.Now, SessionKey = this.Session.SessionID, VisitorKey = this.UserSession.VisitorKey, OperationType = "Get", Message = "订单确认获取当前User购物车(" + this.UserSession.VisitorKey + "):Selector:m => m.UserId == this.UserSession.UserID || m.VisitorKey == this.UserSession.VisitorKey", UserCart = userCart ?? new UserCartModel() { BuyList = new List<CartProduct>(), ProductItems = new List<CartProduct>() }, UserID = this.UserSession.UserID }); var products = BuildCartProducts(proIds, quantity); if (userCart == null) { userCart = new UserCartModel { ProductItems = products, UserId = this.UserSession.UserID, VisitorKey = this.UserSession.VisitorKey }; } if (userCart.ProductItems == null) { userCart.ProductItems = new List<CartProduct>(); } foreach (var cartProduct in products) { if (userCart.ProductItems.FirstOrDefault(p => p.ProductID == cartProduct.ProductID) == null) { if (cartProduct != null) { userCart.ProductItems.Add(cartProduct); } } } userCart.BuyList = products; MongoDBHelper.UpdateModel<UserCartModel>(userCart, u => u.VisitorKey == this.UserSession.VisitorKey); CartController.WriteUserCartOperationLog( new UserCartOperationLog() { OperateTime = DateTime.Now, SessionKey = this.Session.SessionID, VisitorKey = this.UserSession.VisitorKey, OperationType = "Get", Message = "订单确认已更新User购物车(" + this.UserSession.VisitorKey + "):Selector:m => m.UserId == this.UserSession.UserID || m.VisitorKey == this.UserSession.VisitorKey", UserCart = MongoDBHelper.GetModel<UserCartModel>( m => m.VisitorKey == this.UserSession.VisitorKey) ?? new UserCartModel() { BuyList = new List<CartProduct>(), ProductItems = new List<CartProduct>() }, UserID = this.UserSession.UserID }); orderInfo.Products = userCart.BuyList; //获取促销相关信息 var productDictionary = new Dictionary<int, int>(); foreach (var cartProduct in orderInfo.Products) { productDictionary.Add(cartProduct.ProductID, cartProduct.Quantity); } var orderBill = this.GetOrderBill(productDictionary); foreach (var product in orderBill.Products) { if (product.ProductID == 4153) { orderBill.TotalPrice -= product.GoujiuPrice; product.PromotePrice = 0; product.Quantity = 1; } } orderInfo.BillDetail = orderBill; return this.View(orderInfo); } catch (Exception exception) { LogUtils.Log( string.Format( "订单核对出错,参数信息:proIds:{0},quantity:{1},错误消息:{2},堆栈:{3}", proIds, quantity, exception.Message, exception.StackTrace), "OrderInfo", Category.Error); throw; } }
private void UpdateUserCart(UserCartModel cartModel) { MongoDBHelper.UpdateModel<UserCartModel>( cartModel, c => c.VisitorKey == cartModel.VisitorKey); }
/// <summary> /// 移除购物车 /// </summary> /// <param name="cart"></param> public static void CartRemove(UserCartModel cart) { cart.VisitorKey = Guid.NewGuid().ToString(); MongoDBHelper.UpdateModel<UserCartModel>(cart, uc => uc.VisitorKey == cart.VisitorKey); //此处为了防止删除异常,故现将对象visitorkey更新为新的key,然后再移除这个key MongoDBHelper.RemoveModel<UserCartModel>(uc => uc.VisitorKey == cart.VisitorKey); }
/// <summary> /// 获取用户购物车对象 /// </summary> /// <returns></returns> private UserCartModel GetUserCart() { UserCartModel cartModel = null; cartModel = MongoDBHelper.GetModel<UserCartModel>(c => c.VisitorKey == this.UserSession.VisitorKey); if (cartModel == null) { cartModel = new UserCartModel() { VisitorKey = this.UserSession.VisitorKey, UserId = this.UserSession.UserID, ProductItems = new List<CartProduct>(), BuyList = new List<CartProduct>() }; } WriteUserCartOperationLog( new UserCartOperationLog() { OperateTime = DateTime.Now, SessionKey = this.Session.SessionID, VisitorKey = this.UserSession.VisitorKey, OperationType = "Get", Message = "Seletor:c => c.VisitorKey == this.UserSession.VisitorKey", UserCart = cartModel, UserID = this.UserSession.UserID }); return cartModel; }
private AjaxResponse Edit(int productId, int quantity, bool isAdd) { AjaxResponse ajaxResponse = null; if (productId < 1 || quantity < 1) { LogUtils.Log( string.Format("加入或者修改购物车时商品/数量 参数错误,错误参数为:productId:{0},quantity:{1}", productId, quantity), "Cart/Edit", Category.Error, this.UserSession.VisitorKey, this.GetUserID(), "Cart/Edit"); return new AjaxResponse(-1, "商品错误~~"); } var userId = this.UserSession.UserID; if (userId < 0) { LogUtils.Log( string.Format("加入或者修改购物车时用户编码错误,错误参数为:userId:{0}", userId), "Cart/Edit", Category.Error, this.UserSession.VisitorKey, this.GetUserID(), "Cart/Edit"); return new AjaxResponse(-2, "用户编码错误!"); } var billProduct = new ProductService().QueryByID(productId); if (billProduct == null || billProduct.Status!=2 || billProduct.InventoryNumber < 1) { this.RemoveCartProducts(billProduct.ID); LogUtils.Log( string.Format("加入或者修改购物车时发现 商品不存在、库存小于1或已下架,商品编码:{0}", productId), "Cart/Edit", Category.Info, this.UserSession.VisitorKey, this.GetUserID(), "Cart/Edit"); return new AjaxResponse(-2,"对不起,此商品库存不足或已下架,不能购买。"); } var cart = this.GetUserCart(); if (cart == null) //用户购物车不存在 { cart = new UserCartModel { UserId = this.UserSession.UserID, VisitorKey = this.UserSession.VisitorKey, ProductItems = new List<CartProduct>() }; } if (cart.ProductItems == null) { cart.ProductItems=new List<CartProduct>(); } var cartProduct = cart.ProductItems.Find(p => p.ProductID == productId); if (cartProduct == null) { ajaxResponse = this.AddCartProducts(quantity, billProduct, ref cart); } else //更新购物车中的商品 { int updateQuantity = 0; if (isAdd) { updateQuantity = quantity + cartProduct.Quantity; } else { updateQuantity = quantity; } ajaxResponse = this.SetUpdateQuantity(billProduct, ref updateQuantity); cartProduct.Quantity = updateQuantity; } cart.BuyList=new List<CartProduct>(); //由于MongoDB不能存储为Null的List对象 this.UpdateUserCart(cart); return ajaxResponse ?? new AjaxResponse(1, "执行成功"); }
/// <summary> /// 添加商品到购物车 /// </summary> /// <param name="updateQuantity"></param> /// <param name="product"></param> /// <param name="cart"></param> /// <returns></returns> private AjaxResponse AddCartProducts(int updateQuantity, ProductSearchResult product, ref UserCartModel cart) { if (cart.ProductItems == null) { cart.ProductItems = new List<CartProduct>(); } AjaxResponse ajaxResponse = SetUpdateQuantity(product, ref updateQuantity); if (updateQuantity > 0) { cart.ProductItems.Add(this.ConvertToCartProduct(product, updateQuantity)); //设置购买数量 } return ajaxResponse; }
/// <summary> /// 写入购物车操作日志 /// </summary> /// <param name="UserCart"></param> /// <param name="Message"></param> /// <param name="OperationType"></param> public static void WriteUserCartOperationLog(UserCartModel UserCart, string Message, string OperationType) { CartController.WriteUserCartOperationLog( new UserCartOperationLog() { OperateTime = DateTime.Now, SessionKey = UserSessionManager.SessionID, VisitorKey = UserSessionManager.SessionID, OperationType = OperationType, Message = Message, UserCart = UserCart ?? new UserCartModel() { BuyList = new List<CartProduct>(), ProductItems = new List<CartProduct>() }, UserID = UserSessionManager.UserID }); }
/// <summary> /// 写入购物车操作日志 /// </summary> /// <param name="UserCart"></param> /// <param name="Message"></param> public static void WriteUserCartOperationLog(UserCartModel UserCart, string Message) { WriteUserCartOperationLog(UserCart, Message, "GET"); }