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

namespace FOUPOS.BLL
{
    public class PageFunction
    {
        #region GetTotalPage
        /// <summary>
        /// 返回总页数
        /// </summary>
        /// <param name="PageSize">每页记录数</param>
        /// <param name="RecordCount">总记录数</param>
        /// <returns></returns>
        public static int GetTotalPage(int PageSize, int RecordCount)
        {
            if (RecordCount > 0)
            {
                return Math.Abs((RecordCount + PageSize - 1) / PageSize);
            }
            else
            {
                return 0;
            }
        }
        #endregion
        #region GetPageContent
        /// <summary>
        /// 获取结果集
        /// </summary>
        /// <param name="ds">数据集</param>
        /// <param name="CurrentPage">当前页</param>
        /// <param name="PageSize">每页记录数</param>
        /// /// <param name="RecordCount">总记录数</param>
        /// <returns></returns>
        public static DataTable GetPageContent(DataSet ds, int CurrentPage, int PageSize, int RecordCount)
        {
            int TotalPage = GetTotalPage(PageSize, RecordCount);
            DataTable dt = new DataTable();
            dt = ds.Tables[0].Clone();//把ds.Tables[0]的结构传递给dt
            if (TotalPage < 2)
            {
                dt = ds.Tables[0];
            }
            else
            {
                if (CurrentPage <= 0)
                    CurrentPage = 1;
                int start = (CurrentPage - 1) * PageSize;
                int end = start + PageSize;
                if (end > RecordCount)
                    end = RecordCount;

                for (int i = start; i < end; i++)
                {
                    dt.Rows.Add(ds.Tables[0].Rows[i].ItemArray);
                }
            }
            return dt;
        }
        #endregion
    }
}
