using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ZXDT.DBUtility;


namespace ZXDT.CallCenter.DAL
{
    public class PagerDAL
    {
        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public static DataTable GetListPager(string TableName, string PK, string Fields, string WhereStr, string OrderStr, int PageSize, int PageIndex, bool IsCount, out int RecordCount)
        {
            DataTable dt = DbHelperSQL.RunPagination(TableName, PK, Fields, WhereStr, OrderStr, PageSize, PageIndex, IsCount, out RecordCount);
            return dt;
        }
        public static DataTable GetTableList(string TableName, string PK, string Fields, string strWhere, string orderby, int PageSize, int PageIndex, out int RecordCount)
        {
            StringBuilder strSqlCount = new StringBuilder();
            strSqlCount.AppendFormat("SELECT count(1) FROM {0} ", TableName);
            strSqlCount.Append(" WHERE 1=1 " + strWhere);

            
            RecordCount=  DbHelperSQL.GetSingle(strSqlCount.ToString()).ToInt32() ;
            StringBuilder strSql = new StringBuilder();
                strSql.Append("SELECT * FROM ( ");
                strSql.Append(" SELECT ROW_NUMBER() OVER (");

                strSql.Append("order by " + orderby);

                strSql.AppendFormat(")AS Row, {0}  from {1}  ", Fields.Replace("\r\n"," "), TableName);

                strSql.Append(" WHERE 1=1 " + strWhere);
                strSql.Append(" ) TT");
                strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", (PageIndex-1)* PageSize+1, PageIndex*PageSize);
                DataSet ds= DbHelperSQL.Query(strSql.ToString());
            if (ds != null && ds.Tables[0] != null)
            {
                return ds.Tables[0];
            }
            return null;
        }
    }
}
