package com.ruoyi.biemo.core.page;


import java.util.Collections;
import java.util.List;

public class Page<T>  {
    private static final long serialVersionUID = 8545996863226528798L;
    protected List<T> rows;
    protected long total;
    protected Integer pageSize;
    protected Integer pageNum;
    protected boolean optimizeCountSql;
    protected boolean isSearchCount;
    protected boolean hitCount;
    protected String countId;
    protected Long maxLimit;
    protected Integer code = 200;
    protected String msg = "查询成功";

    public Page() {
        this.rows = Collections.emptyList();
        this.total = 0L;
        this.pageSize = 10;
        this.pageNum = 1;
        this.optimizeCountSql = true;
        this.isSearchCount = true;
        this.hitCount = false;
    }

    public Page(Integer pageNum, Integer pageSize) {
        this(pageNum, pageSize, 0L);
    }

    public Page(Integer pageNum, Integer pageSize, long total) {
        this(pageNum, pageSize, total, true);
    }

    public Page(Integer pageNum, Integer pageSize, boolean isSearchCount) {
        this(pageNum, pageSize, 0L, isSearchCount);
    }

    public Page(Integer pageNum, Integer pageSize, long total, boolean isSearchCount) {
        this.rows = Collections.emptyList();
        this.total = 0L;
        this.pageSize = 10;
        this.pageNum = 1;
        this.optimizeCountSql = true;
        this.isSearchCount = true;
        this.hitCount = false;
        if (pageNum > 1L) {
            this.pageNum = pageNum;
        }

        this.pageSize = pageSize;
        this.total = total;
        this.isSearchCount = isSearchCount;
    }

    long getPages() {
        if (this.getPageSize() == 0) {
            return 0;
        } else {
            long pages = this.getTotal() / this.getPageSize();
            if (this.getTotal() % this.getPageSize() != 0) {
                ++pages;
            }

            return pages;
        }
    }
    public boolean hasPrevious() {
        return this.pageNum > 1L;
    }

    public boolean hasNext() {
        return this.pageNum < this.getPages();
    }

    public List<T> getRows() {
        return this.rows;
    }
    public Page<T> setRows(List<T> rows) {
        this.rows = rows;
        return this;
    }
    public long getTotal() {
        return this.total;
    }

    public Page<T> setTotal(long total) {
        this.total = total;
        return this;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public String countId() {
        return this.getCountId();
    }

    public Long maxLimit() {
        return this.getMaxLimit();
    }

    public boolean isSearchCount() {
        return this.total < 0L ? false : this.isSearchCount;
    }

    public Page<T> setSearchCount(boolean isSearchCount) {
        this.isSearchCount = isSearchCount;
        return this;
    }

    public Page<T> setOptimizeCountSql(boolean optimizeCountSql) {
        this.optimizeCountSql = optimizeCountSql;
        return this;
    }
    public void hitCount(boolean hit) {
        this.hitCount = hit;
    }

    public void setHitCount(boolean hit) {
        this.hitCount = hit;
    }
    public boolean isHitCount() {
        return this.hitCount;
    }

    public String getCountId() {
        return this.countId;
    }

    public void setCountId(final String countId) {
        this.countId = countId;
    }

    public Long getMaxLimit() {
        return this.maxLimit;
    }

    public void setMaxLimit(final Long maxLimit) {
        this.maxLimit = maxLimit;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

