using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

using BookShopOnline.Model;
using BookShopOnline.IDal;
using BookShopOnline.DBHelper;
using BookShopOnline.WebConfig;

namespace BookShopOnline.SQLServerDal
{
    public class BookTypeDal:DBAccess,IBookType
    {

        #region 获取和设置缓存中的节点
        private IList<BookType> BookTypeListCached = new List<BookType>();

        private IList<BookType> GetBookTypeListCached()
        {
            if (BookTypeListCached == null || BookTypeListCached.Count == 0)
                BookTypeListCached=SelectAllBookType();

             return BookTypeListCached;
        }

        public void ResetBookTypeListCached()
        {
            if (BookTypeListCached != null)
                BookTypeListCached.Clear();

            BookTypeListCached=SelectAllBookType();
        }
        #endregion

        #region 插入节点
        public void InsertBookType(BookType bookType)
        {
            SqlParameter[] parms =
                {
                    new SqlParameter("@BookTypeName",bookType.BookTypeName),
                    new SqlParameter("@ParentID",bookType.ParentID),
                    new SqlParameter("@OrderBy",bookType.OrderBy)
                };

            SQLHelper.ExecuteNonQuery(Config.SQLServerConnStr, CommandType.StoredProcedure, StoredProcedureName.sp_InsertBookType.ToString(), parms);
        }
        #endregion

        #region 删除节点
        public int DeleteBookType(int bookTypeID)
        {
            int result = 0;

            //获取BookTypeID=bookTypeID的所有子节点的BookTypeID
            ArrayList bookTypeIDList = new ArrayList();
            bookTypeIDList.Add(bookTypeID);
            GetBookTypeIDList(bookTypeIDList, bookTypeID);

            //批量删除节点,从子节点到父节点
            SqlConnection conn = new SqlConnection(Config.SQLServerConnStr);
            conn.Open();

            for (int i = bookTypeIDList.Count - 1; i >= 0; i--)
            {
                SqlParameter parm = new SqlParameter("@bookTypeID", bookTypeIDList[i]);

                result += SQLHelper.ExecuteNonQuery(Config.SQLServerConnStr, CommandType.StoredProcedure, StoredProcedureName.sp_DeleteBookType.ToString(), parm);
            }

            conn.Close();

            return result;
        }
        #endregion

        #region 修改节点
        public int UpdateBookType(BookType bookType)
        {
            int result = 0;

            SqlParameter[] parms =
                {
                    new SqlParameter("@BookTypeID",bookType.BookTypeID),
                    new SqlParameter("@BookTypeName",bookType.BookTypeName),
                    new SqlParameter("@OrderBy",bookType.OrderBy)
                };

            result = SQLHelper.ExecuteNonQuery(Config.SQLServerConnStr, CommandType.StoredProcedure, StoredProcedureName.sp_UpdateBookType.ToString(), parms);

            return result;
        }
        #endregion

        #region 获取所有的节点
        public IList<BookType> SelectAllBookType()
        {
            IList<BookType> bookTypeList = new List<BookType>();

            using (SqlDataReader dr = base.ExecuteReader(StoredProcedureName.sp_SelectAllBookType, null))
            {
                while (dr.Read())
                {
                    BookType bookType = new BookType();
                    bookType.BookTypeID = dr.GetInt32(dr.GetOrdinal("BookTypeID"));
                    bookType.BookTypeName = dr.GetString(dr.GetOrdinal("BookTypeName"));
                    bookType.ParentID = dr.GetInt32(dr.GetOrdinal("ParentID"));
                    bookType.OrderBy = dr.GetInt32(dr.GetOrdinal("OrderBy"));
                    bookTypeList.Add(bookType);
                }
            }

            return bookTypeList;
        }
        #endregion

        #region 获取根节点
        public IList<BookType> SelectRootBookType()
        {
            IList<BookType> bookTypeList = new List<BookType>();

            foreach(BookType bookType in GetBookTypeListCached())
            {
                if(bookType.ParentID==-1)
                  bookTypeList.Add(bookType);
            }

            return bookTypeList;
        }
        #endregion

        #region 获取子节点
        public IList<BookType> SelectSubBooType(int parentID)
        {
            IList<BookType> bookTypeList = new List<BookType>();

            foreach (BookType bookType in GetBookTypeListCached())
            {
                if(bookType.ParentID==parentID)
                   bookTypeList.Add(bookType);
            }

            return bookTypeList;
        }
        #endregion

        #region 获取ParentID=parentID的所有节点的BookTypeID数组
        public void GetBookTypeIDList(ArrayList bookTypeIDList, int parentID)
        {
            foreach (BookType bookType in GetBookTypeListCached())
            {
                if (bookType.ParentID == parentID)
                {
                    bookTypeIDList.Add(bookType.BookTypeID);
                    GetBookTypeIDList(bookTypeIDList, bookType.BookTypeID);
                }
            }
        }
        #endregion


        #region 反复读取数据库(不使用）
        #region 获取根节点
        //public IList<BookType> SelectRootBookType()
        //{
        //    IList<BookType> bookTypeList = new List<BookType>();

        //    using (SqlDataReader dr = SQLHelper.ExecuteReader(Config.SQLServerConnStr, CommandType.StoredProcedure, StoredProcedureName.sp_SelectRootBookType.ToString(), null))
        //    {
        //        while (dr.Read())
        //        {
        //            BookType bookType = new BookType();
        //            bookType.BookTypeID = dr.GetInt32(dr.GetOrdinal("BookTypeID"));
        //            bookType.BookTypeName = dr.GetString(dr.GetOrdinal("BookTypeName"));
        //            bookType.ParentID = dr.GetInt32(dr.GetOrdinal("ParentID"));
        //            bookType.OrderBy = dr.GetInt32(dr.GetOrdinal("OrderBy"));
        //            bookTypeList.Add(bookType);
        //        }
        //    }

        //    return bookTypeList;
        //}
        #endregion

        #region 获取子节点
        //public IList<BookType> SelectSubBooType(int parentID)
        //{
        //    IList<BookType> bookTypeList = new List<BookType>();
        //    SqlParameter parms = new SqlParameter("@ParentID", parentID);

        //    using (SqlDataReader dr = SQLHelper.ExecuteReader(Config.SQLServerConnStr, CommandType.StoredProcedure, StoredProcedureName.sp_SelectSubBooType.ToString(), parms))
        //    {
        //        while (dr.Read())
        //        {
        //            BookType bookType = new BookType();
        //            bookType.BookTypeID = dr.GetInt32(dr.GetOrdinal("BookTypeID"));
        //            bookType.BookTypeName = dr.GetString(dr.GetOrdinal("BookTypeName"));
        //            bookType.ParentID = dr.GetInt32(dr.GetOrdinal("ParentID"));
        //            bookType.OrderBy = dr.GetInt32(dr.GetOrdinal("OrderBy"));
        //            bookTypeList.Add(bookType);
        //        }
        //    }

        //    return bookTypeList;
        //}
        #endregion

        #region 获取ParentID=parentID的所有节点的BookTypeID数组
        //public void GetBookTypeIDList(ArrayList bookTypeIDList, int parentID)
        //{
        //    SqlParameter parm = new SqlParameter("@ParentID", parentID);
        //    using (SqlDataReader dr = SQLHelper.ExecuteReader(Config.SQLServerConnStr, CommandType.StoredProcedure, StoredProcedureName.sp_SelectSubBooType.ToString(), parm))
        //    {
        //        while (dr.Read())
        //        {
        //            int bookTypeID=dr.GetInt32(dr.GetOrdinal("BookTypeID"));
        //            bookTypeIDList.Add(bookTypeID);
        //            GetBookTypeIDList(bookTypeIDList, bookTypeID);
        //        }
        //    }
        //}
        #endregion

        #endregion
    }
}
