示例#1
0
        public ActionResult Add(string goodsCode)
        {
            //先获得 goods 和 member 对象
            Goods  goods  = new Goods();
            Member member = new Member();

            try
            {
                goods  = _goodsBLL.GetGoodsByCode(goodsCode);
                member = _memberBLL.GetMemberByOpenId(System.Web.HttpContext.Current.Session["member"] as string);
            }
            catch (Exception ex)
            {
                LogHelper.Log.Write(ex.Message);
                LogHelper.Log.Write(ex.StackTrace);
                throw;
            }

            // 判断是否已经存在购物车上
            if (!_cartBLL.IsInCart(member.Id, goods.Id))
            {
                // 不在购物车上时 直接添加到购物车上
                Cart cart = new Cart();
                cart.Id          = Guid.NewGuid();
                cart.Count       = 1;
                cart.CreatedTime = DateTime.Now.Date;
                cart.DeletedTime = DateTime.MinValue.AddHours(8);
                cart.GoodsId     = goods.Id;
                cart.MemberId    = member.Id;
                cart.IsDeleted   = false;

                _cartBLL.Add(cart);

                return(Json("True", JsonRequestBehavior.AllowGet));
            }
            else
            {
                // 判断存在购物车上时  就取出 Cart 对象
                // 修改 cart 的数量
                Cart cart = _cartBLL.GetCartByMemberIdAndGoodsId(member.Id, goods.Id);
                cart.Count += 1;

                _cartBLL.Update(cart);
                return(Json("True", JsonRequestBehavior.AllowGet));
            }
        }
示例#2
0
        public ActionResult Add(int payway)
        {
            // 先判断 各个session是否为空
            if (System.Web.HttpContext.Current.Session["CartModelList"] == null ||
                System.Web.HttpContext.Current.Session["Address"] == null ||
                System.Web.HttpContext.Current.Session["TotalPrice"] == null)
            {
                // 如果为空 则返回超时提示
                return(Json("Expired", JsonRequestBehavior.AllowGet));
            }

            List <CartModel> cartList   = System.Web.HttpContext.Current.Session["CartModelList"] as List <CartModel>;
            double           TotalPrice = double.Parse(System.Web.HttpContext.Current.Session["TotalPrice"].ToString());
            Address          address    = System.Web.HttpContext.Current.Session["Address"] as Address;
            Member           member     = _memberBLL.GetMemberByOpenId(System.Web.HttpContext.Current.Session["member"].ToString());

            Order order = new Order();

            order.Id          = Guid.NewGuid();
            order.IsDeleted   = false;
            order.CreatedTime = DateTime.Now;
            order.DeletedTime = DateTime.MinValue.AddHours(8);
            order.AddressId   = address.Id;
            order.MemberId    = member.Id;
            string gid = Guid.NewGuid().ToString().Replace("-", "").ToUpper().Substring(0, 6);

            order.OrderNo    = gid + TimeManager.GetCurrentTimestamp().ToString();
            order.Status     = 0;
            order.TotalPrice = TotalPrice;
            order.PayWay     = payway;

            List <OrderDetail> odList = new List <OrderDetail>();

            if (_orderBLL.Add(order))
            {
                foreach (var item in cartList)
                {
                    OrderDetail od = new OrderDetail();
                    od.Id              = Guid.NewGuid();
                    od.Count           = item.Count;
                    od.CreatedTime     = DateTime.Now;
                    od.CurrentDiscount = item.Discount;
                    od.CurrentPrice    = item.Price;
                    od.DeletedTime     = DateTime.MinValue.AddHours(8);
                    od.GoodsId         = item.Id;
                    od.IsDeleted       = false;
                    od.OrderId         = order.Id;
                    od.OrderNo         = order.OrderNo;
                    od.TotalPrice      = od.Count * od.CurrentDiscount * od.CurrentPrice;

                    // 添加订单详情

                    // 如果添加失败 则回滚

                    if (!_orderDetailBLL.Add(od))
                    {
                        // 删除已添加的 OrderDetail
                        _orderDetailBLL.Remove(odList);
                        return(Json("Error", JsonRequestBehavior.AllowGet));
                    }
                    else
                    {
                        odList.Add(od);
                    }
                }

                // 添加订单成功后 要把购物车上的物品标记成 已删除

                foreach (CartModel item in cartList)
                {
                    Cart cart = new Cart();
                    cart           = _cartBLL.GetCartByMemberIdAndGoodsId(member.Id, item.Id);
                    cart.IsDeleted = true;

                    _cartBLL.Update(cart);
                }

                //订单添加后 添加OrderStatus对象 标记订单的配送状态
                OrderStatus os = new OrderStatus();
                os.Id          = Guid.NewGuid();
                os.IsDeleted   = false;
                os.OrderId     = order.Id;
                os.Status      = 0;
                os.CreatedTime = DateTime.Now;
                os.DeletedTime = DateTime.MinValue.AddHours(8);

                _orderStausBLL.Add(os);

                var jsonResult = new
                {
                    payway     = payway,
                    totalprice = order.TotalPrice,
                    orderNo    = order.OrderNo
                };
                return(Json(jsonResult, JsonRequestBehavior.AllowGet));
            }
            else
            {
                return(Json("Error", JsonRequestBehavior.AllowGet));
            }
        }