package cn.com.qmth.stmms.admin.utils;

import java.util.List;

public class PageUtil {

    /**
     * 开始分页
     * 
     * @param list
     * @param pageNum
     *            页码
     * @param pageSize
     *            每页多少条数据
     * @return
     */
    public static List<?> startPage(List<?> list, Integer pageNum, Integer pageSize) {
        if (list == null) {
            return null;
        }
        if (list.size() == 0) {
            return null;
        }

        Integer count = list.size(); // 记录总数
        Integer pageCount = pageCount(list, pageNum, pageSize);

        int fromIndex = 0; // 开始索引
        int toIndex = 0; // 结束索引

        if (pageCount < pageNum) {
            return null;
        }
        if (pageCount - pageNum != 0) {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = fromIndex + pageSize;
        } else {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = count;
        }

        List<?> pageList = list.subList(fromIndex, toIndex);

        return pageList;
    }

    public static Integer pageCount(List<?> list, Integer pageNum, Integer pageSize) {
        if (list == null) {
            return 0;
        }
        if (list.size() == 0) {
            return 0;
        }
        Integer count = list.size(); // 记录总数
        Integer pageCount = 0; // 页数
        if (count % pageSize == 0) {
            pageCount = count / pageSize;
        } else {
            pageCount = count / pageSize + 1;
        }

        return pageCount;
    }
}
