Пример #1
0
    /// <summary>
    /// 加入购物车
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Linkbtn_Shoppingcart_Click(object sender, EventArgs e)
    {
        if (Session["User"] != null)
        {
            Customer currentUser = (Customer)Session["User"];//读取当前用户信息
            BookItem bookItem = new BookItem(book);//创建一个bookItem

            //下面的逻辑有点乱
            //可以只通过读取User信息就好了
            if (Session["ShoppingCart"] != null)
            {
                ShoppingCart shoppingCart = (ShoppingCart)Session["ShoppingCart"];
                shoppingCart.AddBookItemToShoppingCart(bookItem);//将bookItem添加到shoppingCart中
                Session["ShoppingCart"] = shoppingCart;//保存购物车
            }
            else
            {
                currentUser.ShoppingCart.AddBookItemToShoppingCart(bookItem);//将bookItem添加到shoppingCart中
                Session["ShoppingCart"] = currentUser.ShoppingCart;//保存购物车
            }

            //提醒用户已经添加到购物车
            Response.Write("<script>alert('成功添加到购物车中!')</script>");
        }

        else
        {
            Response.Write("<script>alert('登陆失效,请重新登陆!')</script>");
        }
    }
Пример #2
0
        /// <summary>
        /// 查询订单信息(所有订单
        /// orderList->Order->bookItemList->Book ①
        /// 思路:
        /// 1.对比当前订单与之前订单的订单号
        /// 2.如果订单号相同,说明图书属于一个订单,获取订单列表中的最后一个订单,然后删除(删除是为了步骤5插入订单列表时方便操作)
        /// 3.如果订单号不同,说明图书不属于同一订单,则新建一个订单
        /// 4.获取到订单后,根据①创建订单对象
        /// 5.将订单插入到订单列表尾部
        /// </summary>
        /// <returns></returns>
        public List <Order> QueryOrderInformation()
        {
            SqlConnection sql        = DataBaseOperations.CreateConnection();
            string        preOrderId = null;              //上一个订单号
            string        curOrderId;                     //当前订单号
            List <Order>  orderList = new List <Order>(); //订单列表
            Order         order;                          //订单

            //根据当前用户名查询订单
            string cmdText =
                "select a.OrderID,a.UserName,a.Address,a.Remark,a.Price,a.isValid,a.OrderDate,b.BookID,b.Amount,c.BookName,c.BookAuthor,c.BookPrice,c.BookPress,c.BookDetail,c.BookCategory,c.BookImageURL,c.ShelveDate,c.Inventory,c.Rating from [Order] a inner join [OrderDetail] b on a.OrderID=b.OrderID inner join [Book] c on b.BookID=c.BookID where a.UserName='******' order by a.OrderID desc";

            DataSet dataSet = DataBaseOperations.GetDataSet(cmdText, sql);

            if (dataSet != null)
            {
                for (int i = 0; i < dataSet.Tables[0].Rows.Count; i++)
                {
                    curOrderId = dataSet.Tables[0].Rows[i][0].ToString();                                                                                    //获取到当前订单id
                    ShippingAddress shippingAddress = new ShippingAddress(dataSet.Tables[0].Rows[i][2].ToString(), dataSet.Tables[0].Rows[i][3].ToString()); //创建收货对象

                    //创建book对象
                    Book aBook = new Book(dataSet.Tables[0].Rows[i][7].ToString(),
                                          dataSet.Tables[0].Rows[i][9].ToString(), dataSet.Tables[0].Rows[i][10].ToString(),
                                          Convert.ToDecimal(dataSet.Tables[0].Rows[i][11]), dataSet.Tables[0].Rows[i][12].ToString(),
                                          dataSet.Tables[0].Rows[i][13].ToString(), dataSet.Tables[0].Rows[i][14].ToString(),
                                          dataSet.Tables[0].Rows[i][15].ToString(),
                                          Convert.ToInt32(dataSet.Tables[0].Rows[i][17]), Convert.ToInt32(dataSet.Tables[0].Rows[i][18]), dataSet.Tables[0].Rows[i][16].ToString());
                    //创建bookItem对象(book对象,book的数量)
                    BookItem bookItem = new BookItem(aBook, Convert.ToInt32(dataSet.Tables[0].Rows[i][8]));

                    //图书来自不同的订单
                    if (!isFromSameOrder(preOrderId, curOrderId))
                    {
                        order = new Order();//新建订单,初始化bookItemList
                        //不同的订单需要设置 OrerId/userName/orderPrice/isValid/oderDate/shippingAddress
                        //相同订单的信息已经设置过了,不需要重新设置
                        order.SetOrderInformation(dataSet.Tables[0].Rows[i][0].ToString(),
                                                  dataSet.Tables[0].Rows[i][1].ToString(), Convert.ToDecimal(dataSet.Tables[0].Rows[i][4]),
                                                  Convert.ToInt32(dataSet.Tables[0].Rows[i][5]), dataSet.Tables[0].Rows[i][6].ToString(),
                                                  shippingAddress);
                    }
                    else
                    {
                        order = orderList[orderList.Count - 1];  //获取到订单列表的最后一个订单
                        orderList.RemoveAt(orderList.Count - 1); //删除这个订单信息
                    }

                    order.bookItemList.Add(bookItem); //将bookItem添加到bookItemList中
                    orderList.Add(order);             //将order添加到orderList中
                    //设置订单的OrderID/UserName/价格/状态/OrderDate/ShippingAddres

                    preOrderId = curOrderId;//处理完一个订单后,将curOrderId赋值给preOrderId
                }
            }
            sql.Close();//关闭sql连接
            return(orderList);
        }
Пример #3
0
        /// <summary>
        /// 删除购物车中的某本书
        /// </summary>
        /// <param name="toDeleteBookItem"></param>
        /// <returns></returns>
        public List <BookItem> DeleteBookItemFromShoppingCart_(BookItem toDeleteBookItem)
        {
            //为了保证安全,还是检测一下要删除的图书是否在购物车中
            int index = bookitemList.FindIndex(x => x.Book.bookID == toDeleteBookItem.Book.bookID);

            if (index != -1)
            {
                bookitemList.RemoveAt(index);
                bookcategory.Remove(toDeleteBookItem.Book.bookCategory);
            }
            return(bookitemList);
        }
Пример #4
0
        /// <summary>
        /// 将图书添加到购物车中
        /// </summary>
        /// <param name="toAddBookItem"></param>
        public List <BookItem> AddBookItemToShoppingCart(BookItem toAddBookItem)
        {
            //查找图书是否已经加入到购物车中
            int index = bookitemList.FindIndex(x => x.Book.bookID == toAddBookItem.Book.bookID);

            if (index != -1)//已经在购物车中
            {
                bookitemList[index].AddBookToItem();
            }
            //图书未加入购物车
            else
            {
                bookitemList.Add(toAddBookItem);
                bookcategory.Add(toAddBookItem.Book.bookCategory);
            }

            return(bookitemList);
        }
Пример #5
0
        /// <summary>
        /// 将图书从购物车中删除
        /// </summary>
        /// <param name="toDeleteBookItem"></param>
        /// <returns></returns>
        public List <BookItem> DeleteBookItemFromShoppingCart(BookItem toDeleteBookItem)
        {
            //为了保证安全,还是检测一下要删除的图书是否在购物车中
            int index = bookitemList.FindIndex(x => x.Book.bookID == toDeleteBookItem.Book.bookID);

            if (index != -1)
            {
                //如果购物车中图书数量大于1,数量-1
                if (bookitemList[index].bookAmount > 1)
                {
                    bookitemList[index].DeleteBookFromItem();
                }
                //如果购物车中图书数量小于1,那么删除该bookItem
                //else
                //{
                //    bookitemList.RemoveAt(index);
                //    bookcategory.Remove(toDeleteBookItem.Book.bookCategory);
                //}
            }
            return(bookitemList);
        }
Пример #6
0
    /// <summary>
    /// 分类查看书籍模块
    /// 一键购买
    /// 放入购物车
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ListView1_OnItemCommand(object sender, ListViewCommandEventArgs e)
    {
        //待完善
        if (e.CommandName == "BuyNow")
        {

        }


        if (e.CommandName == "AddBookToShoppingCart" || e.CommandName == "NewAddBookToShoppingCart")
        {
            List<Book> bookList = new List<Book>();

            if (e.CommandName == "AddBookToShoppingCart")
            {
                if (Session["RecommendBooks"] != null)
                {
                    bookList = (List<Book>)Session["RecommendBooks"]; //推荐书籍List
                }
            }
            else
            {
                if (Session["NewBooks"] != null)
                {
                    bookList = (List<Book>)Session["NewBooks"]; //推荐书籍List
                }
            }

            Book selectedBook = bookList.Find(x => x.bookID == e.CommandArgument.ToString());//查找用户选择的书籍
            if (Session["User"] != null)
            {
                Customer currentUser = (Customer)Session["User"];//读取当前用户信息
                BookItem bookItem = new BookItem(selectedBook);//创建一个bookItem

                //下面的逻辑有点乱
                //可以只通过读取User信息就好了
                if (Session["ShoppingCart"] != null)
                {
                    ShoppingCart shoppingCart = (ShoppingCart)Session["ShoppingCart"];
                    shoppingCart.AddBookItemToShoppingCart(bookItem);//将bookItem添加到shoppingCart中
                    Session["ShoppingCart"] = shoppingCart;//保存购物车
                }
                else
                {
                    currentUser.ShoppingCart.AddBookItemToShoppingCart(bookItem);//将bookItem添加到shoppingCart中
                    Session["ShoppingCart"] = currentUser.ShoppingCart;//保存购物车
                }

                //提醒用户已经添加到购物车
                Response.Write("<script>alert('成功添加到购物车中!')</script>");
            }

            else
            {
                Response.Write("<script>alert('登陆失效,请重新登陆!')</script>");
            }
        }
    }
Пример #7
0
        /// <summary>
        /// 查询订单信息(所有订单
        /// orderList->Order->bookItemList->Book ①
        /// 思路:
        /// 1.对比当前订单与之前订单的订单号
        /// 2.如果订单号相同,说明图书属于一个订单,获取订单列表中的最后一个订单,然后删除(删除是为了步骤5插入订单列表时方便操作)
        /// 3.如果订单号不同,说明图书不属于同一订单,则新建一个订单
        /// 4.获取到订单后,根据①创建订单对象
        /// 5.将订单插入到订单列表尾部
        /// </summary>
        /// <returns></returns>
        public List<Order> QueryOrderInformation()
        {
            SqlConnection sql = DataBaseOperations.CreateConnection();
            string preOrderId = null;//上一个订单号
            string curOrderId;//当前订单号
            List<Order> orderList = new List<Order>();//订单列表
            Order order;//订单

            //根据当前用户名查询订单
            string cmdText =
                "select a.OrderID,a.UserName,a.Address,a.Remark,a.Price,a.isValid,a.OrderDate,b.BookID,b.Amount,c.BookName,c.BookAuthor,c.BookPrice,c.BookPress,c.BookDetail,c.BookCategory,c.BookImageURL,c.ShelveDate,c.Inventory,c.Rating from [Order] a inner join [OrderDetail] b on a.OrderID=b.OrderID inner join [Book] c on b.BookID=c.BookID where a.UserName='******' order by a.OrderID desc";
            
            DataSet dataSet = DataBaseOperations.GetDataSet(cmdText, sql);
            if (dataSet != null)
            {
                
                for (int i = 0; i < dataSet.Tables[0].Rows.Count; i++)
                {
                    
                    curOrderId = dataSet.Tables[0].Rows[i][0].ToString();//获取到当前订单id
                    ShippingAddress shippingAddress = new ShippingAddress(dataSet.Tables[0].Rows[i][2].ToString(), dataSet.Tables[0].Rows[i][3].ToString());//创建收货对象
                    
                    //创建book对象
                    Book aBook = new Book(dataSet.Tables[0].Rows[i][7].ToString(),
                        dataSet.Tables[0].Rows[i][9].ToString(), dataSet.Tables[0].Rows[i][10].ToString(),
                        Convert.ToDecimal(dataSet.Tables[0].Rows[i][11]), dataSet.Tables[0].Rows[i][12].ToString(),
                        dataSet.Tables[0].Rows[i][13].ToString(), dataSet.Tables[0].Rows[i][14].ToString(),
                        dataSet.Tables[0].Rows[i][15].ToString(), 
                        Convert.ToInt32(dataSet.Tables[0].Rows[i][17]), Convert.ToInt32(dataSet.Tables[0].Rows[i][18]),dataSet.Tables[0].Rows[i][16].ToString());
                    //创建bookItem对象(book对象,book的数量)
                    BookItem bookItem = new BookItem(aBook, Convert.ToInt32(dataSet.Tables[0].Rows[i][8]));

                    //图书来自不同的订单
                    if (!isFromSameOrder(preOrderId, curOrderId))
                    {
                        order = new Order();//新建订单,初始化bookItemList
                        //不同的订单需要设置 OrerId/userName/orderPrice/isValid/oderDate/shippingAddress
                        //相同订单的信息已经设置过了,不需要重新设置
                        order.SetOrderInformation(dataSet.Tables[0].Rows[i][0].ToString(),
                        dataSet.Tables[0].Rows[i][1].ToString(), Convert.ToDecimal(dataSet.Tables[0].Rows[i][4]),
                        Convert.ToInt32(dataSet.Tables[0].Rows[i][5]), dataSet.Tables[0].Rows[i][6].ToString(),
                        shippingAddress);
                    }
                    else
                    {
                        
                        order = orderList[orderList.Count-1];//获取到订单列表的最后一个订单
                        orderList.RemoveAt(orderList.Count-1);//删除这个订单信息
                        
                    }

                    order.bookItemList.Add(bookItem);//将bookItem添加到bookItemList中
                    orderList.Add(order);//将order添加到orderList中  
                    //设置订单的OrderID/UserName/价格/状态/OrderDate/ShippingAddres

                    preOrderId = curOrderId;//处理完一个订单后,将curOrderId赋值给preOrderId

                }
            }
            sql.Close();//关闭sql连接          
            return orderList;

        }