package com.zksy.common.utils.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * @Author sy
 * @Date 2022/04/11
 * @Description
 */
@ApiModel
public class PageRes {
    @ApiModelProperty(name = "currPage", value = "当前页数", dataType = "Integer", example = "1")
    private Integer currPage;
    @ApiModelProperty(name = "pageSize", value = "分页大小", dataType = "Integer", example = "10")
    private Integer pageSize;
    @ApiModelProperty(name = "totalPage", value = "总页数", dataType = "Integer", example = "1")
    private Integer totalPage;
    @ApiModelProperty(name = "totalRecord", value = "总记录数", dataType = "Integer", example = "1")
    private Integer totalRecord;
    @ApiModelProperty(name = "startRow", value = "开始行数", dataType = "Integer", example = "0", hidden = true)
    private Integer startRow;

    public PageRes() {
        this.currPage = Constant.Page.CURR_PAGE_DEFAULT;
        this.pageSize = Constant.Page.PAGE_SIZE_DEFAULT;
        this.startRow = Constant.Page.START_ROW_DEFAULT;
        this.totalPage = Constant.Page.TOTAL_PAGE_DEFAULT;
        this.totalRecord = Constant.Page.TOTAL_RECORD_DEFAULT;
    }

    public PageRes(Integer currPage) {
        this.currPage = currPage;
        this.pageSize = Constant.Page.PAGE_SIZE_DEFAULT;
        this.startRow = Constant.Page.START_ROW_DEFAULT;
        this.totalPage = Constant.Page.TOTAL_PAGE_DEFAULT;
        this.totalRecord = Constant.Page.TOTAL_RECORD_DEFAULT;
    }

    public PageRes(Integer currPage, Integer pageSize) {
        this.currPage = currPage;
        this.pageSize = (pageSize != null && pageSize > 0) ? pageSize : Constant.Page.PAGE_SIZE_DEFAULT;
        this.startRow = Constant.Page.START_ROW_DEFAULT;
        this.totalPage = Constant.Page.TOTAL_PAGE_DEFAULT;
        this.totalRecord = Constant.Page.TOTAL_RECORD_DEFAULT;
        computeStartRow(this.currPage);
    }

    public PageRes(PageReq pageReq) {
        this.currPage = Constant.Page.CURR_PAGE_DEFAULT;
        this.pageSize = Constant.Page.PAGE_SIZE_DEFAULT;
        this.startRow = Constant.Page.START_ROW_DEFAULT;
        this.totalPage = Constant.Page.TOTAL_PAGE_DEFAULT;
        this.totalRecord = Constant.Page.TOTAL_RECORD_DEFAULT;
        this.setCurrPage(pageReq.getPageNumber());
        this.setPageSize(pageReq.getPageSize());
    }

    public Integer getCurrPage() {
        return currPage;
    }

    public void setCurrPage(Integer currPage) {
        this.currPage = currPage;
        //1.得到总记录数，计算总页数
        computeTotalPage(this.totalRecord);
        //计算其实记录数
        computeStartRow(currPage);
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        if (pageSize != null && pageSize > 0) {
            this.pageSize = pageSize;
        } else {
            pageSize = Constant.Page.PAGE_SIZE_DEFAULT;
        }
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public Integer getTotalRecord() {
        return totalRecord;
    }

    public void setTotalRecord(Integer totalRecord) {
        this.totalRecord = totalRecord;
        //1.得到总记录数，计算总页数
        computeTotalPage(totalRecord);
        //计算其实记录数
        computeStartRow(this.currPage);
    }

    public Integer getStartRow() {
        return startRow;
    }

    public void setStartRow(Integer startRow) {
        this.startRow = startRow;
    }

    /**
     * 根据总记录数计算总页数
     *
     * @param totalRecord
     */
    private void computeTotalPage(Integer totalRecord) {
        if (totalRecord > 0) {
            if (totalRecord < pageSize) {
                totalPage = 1;
            } else if (totalRecord % pageSize > 0) {
                totalPage = (totalRecord / pageSize) + 1;
            } else {
                totalPage = totalRecord / pageSize;
            }
        } else {
            totalPage = 0;
        }
    }

    /**
     * 根据当前页和总记录数计算起始记录数
     *
     * @param currPage
     */
    private void computeStartRow(Integer currPage) {
        if (totalPage == 0 || currPage <= 0) {
            startRow = 0;
        } else {
            startRow = (currPage - 1) * pageSize;
        }
		/*else if(currPage < totalPage){
			startRow = (currPage - 1) * pageSize;
		}else{
			startRow = (totalPage - 1) * pageSize;
		}*/
    }
}
