package com.usoft.fin.grpc.entity;

/**
 * 分页参数
 * @author wangmh
 * @date 2018-08-25 15:42
 **/
public class PageInfo {

    /**
     * 总条数
     */
    private int totalCount;

    /**
     * 每页条数，默认值为20
     */
    private int pageSize;

    /**
     * 当前页码，从1开始，默认值为1
     */
    private int pageNumber;

    /**
     * 默认页数
     */
    private static final int DEFULT_PAGE_NUMBER = 1;

    /**
     * 每页默认大小
     */
    private static final int DEFULT_PAGE_SIZE = 20;

    public PageInfo(int pageNumber, int pageSize, int count) {
        this.totalCount = count;
        this.pageNumber = pageNumber < 1 ? DEFULT_PAGE_NUMBER : pageNumber;
        this.pageSize = pageSize < 1 ? DEFULT_PAGE_SIZE : pageSize;
    }

    public int getTotalPage() {
        return totalCount / pageSize + totalCount % pageSize == 0 ? 0 : 1;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    /**
     * 获取当前分页在总数的位置，从0开始
     * @return
     */
    public int getStartIndex() {
        return (pageNumber - 1) * pageSize;
    }

    /**
     * 是否为第一页
     * @return
     */
    public boolean isFirst() {
        return pageNumber == 1;
    }

    /**
     * 是否为最后一页
     * @return
     */
    public boolean isLast() {
        return getTotalPage() == pageNumber;
    }
}
