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

namespace STMes
{
    public class DataPage
    {
        private DBProxy _dbProxy;
        private string _dbName;
        private string _priority;
        private IDataParameter[] Params;
        private object[] Values;

        public bool _valid;
        public string _errMsg;


        private string countSql;
        private string pageSql;

        public DataPage(DBProxy dbProxy, string dbName, string priority)
        {
            _dbProxy = dbProxy;
            _dbName = dbName;
            _priority = priority;
        }

        public DataPage InitDataPage(string sql, int pageIndex, int pageSize, IDataParameter[] Params, object[] Values)
        {
            this.PageIndex = pageIndex;
            this.PageSize = pageSize;

            this.Params = Params;
            this.Values = Values;

            countSql = DataCountSql(sql);
            pageSql = DataPageSql(sql, pageIndex, pageSize);
            ExcuteQueryData();

            return this;

        }

        public DataPage InitDataPage(string sql, int pageIndex, int pageSize)
        {
            this.PageIndex = pageIndex;
            this.PageSize = pageSize;

            countSql = DataCountSql(sql);
            pageSql = DataPageSql(sql, pageIndex, pageSize);
            ExcuteQueryData();
            return this;

        }
        private string DataCountSql(string sql)
        {
            string strSql = string.Format("select count(*) from ( {0})", sql);
            return strSql;
        }
        private string DataPageSql(string sql, int pageIndex, int pageSize)
        {
            int startIndex = (pageIndex - 1) * pageSize;
            int endIndex = (pageIndex - 1) * pageSize + pageSize;
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append(" SELECT * FROM (SELECT DATAPAGE_TABLE_A.*, rownum DATAPAGE_FIELD_ROWNUM FROM ");
            sbSql.AppendFormat(" ( {0} ) DATAPAGE_TABLE_A", sql);
            sbSql.AppendFormat(" where rownum <={0} ) DATAPAGE_TABLE_B where DATAPAGE_FIELD_ROWNUM>{1}", endIndex, startIndex);
            return sbSql.ToString();
        }

        private void ExcuteQueryData()
        {
            DataSet ds = null;

            try
            {

                // pageCount
                DataSet dsPage = _dbProxy.ExecuteQuery(_dbName, _priority, countSql, CommandType.Text, Params, Values, out _errMsg);
                if (dsPage != null)
                {
                    this.RecordCount = int.Parse(dsPage.Tables[0].Rows[0][0].ToString());
                    ComputePageCount();
                }
                // page
                ds = _dbProxy.ExecuteQuery(_dbName, _priority, pageSql, CommandType.Text, Params, Values, out _errMsg);

            }
            catch (Exception ex)
            {
                _valid = false;
                _errMsg = ex.Message;
            }

            this.Result = ds;
        }
        #region 总记录数
        private int _RecordCount = 0;
        /// <summary>
        /// 总记录记录数
        /// </summary>
        public int RecordCount
        {
            private set { _RecordCount = value; }
            get
            {
                return _RecordCount;
            }
        }
        #endregion

        #region 一页的记录数
        private Int32 _PageSize = 20;
        /// <summary>
        /// 一页的记录数
        /// </summary>
        public Int32 PageSize
        {
            private set { _PageSize = value; }
            get
            {
                return _PageSize;
            }
        }
        #endregion

        #region 一共有多少页
        private Int32 _PagerCount = 0;
        /// <summary>
        /// 一共有多少页，根据总记录数和一页的记录数自动计算
        /// </summary>
        public Int32 PageCount
        {
            private set { _PagerCount = value; }
            get
            {
                return _PagerCount;

            }
        }
        #endregion

        #region 当前的页号
        private Int32 _PageIndex = 1;
        /// <summary>
        /// 当前的页号
        /// </summary>
        public Int32 PageIndex
        {
            private set { _PageIndex = value; }
            get
            {
                return _PageIndex;
            }
        }
        #endregion




        private DataSet _Result;
        public DataSet Result
        {
            private set { _Result = value; }
            get
            {
                return _Result;
            }

        }

        #region 计算页数
        /// <summary>
        /// 通过总记录数、一页的记录数计算页数
        /// </summary>
        private Int32 ComputePageCount()
        {
            Int32 recordCount = this.RecordCount;
            Int32 pageSize = this.PageSize;

            Int32 tmpPageCount = 1;

            if (recordCount > 0)
            {
                //计算页数
                tmpPageCount = recordCount / pageSize + (recordCount % pageSize == 0 ? 0 : 1);
            }

            this.PageCount = tmpPageCount;
            return tmpPageCount;
        }
        #endregion
    }



}
