package com.hzya.frame.page;

import org.apache.ibatis.session.RowBounds;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Page<E> extends ArrayList<E> implements Serializable {


	@SuppressWarnings("unused")
	private static final int NO_SQL_COUNT = -1;
	@SuppressWarnings("unused")
	private static final int SQL_COUNT = 0;
    private int pageNum;
    private int pageSize;
    private int startRow;
    private int endRow;
    private long total;
    private int pages;

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

    public Page(int pageNum, int pageSize, boolean count) {
        this(pageNum, pageSize, count?0:-1);
    }

    public Page(int pageNum, int pageSize, int total) {
        super(pageSize > -1?pageSize:0);
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = (long)total;
        this.startRow = pageNum > 0?(pageNum - 1) * pageSize:0;
        this.endRow = pageNum * pageSize;
    }

    public Page(RowBounds rowBounds, boolean count) {
        this(rowBounds, count?0:-1);
    }

    public Page(RowBounds rowBounds, int total) {
        super(rowBounds.getLimit() > -1?rowBounds.getLimit():0);
        this.pageSize = rowBounds.getLimit();
        this.startRow = rowBounds.getOffset();
        this.total = (long)total;
        this.endRow = this.startRow + this.pageSize;
    }

    public List<E> getResult() {
        return this;
    }

    public int getPages() {
        return this.pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public int getEndRow() {
        return this.endRow;
    }

    public void setEndRow(int endRow) {
        this.endRow = endRow;
    }

    public int getPageNum() {
        return this.pageNum;
    }

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

    public int getPageSize() {
        return this.pageSize;
    }

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

    public int getStartRow() {
        return this.startRow;
    }

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

    public long getTotal() {
        return this.total;
    }

    public void setTotal(long total) {
        this.total = total;
        if(this.pageSize > 0) {
            this.pages = (int)(total / (long)this.pageSize + (long)(total % (long)this.pageSize == 0L?0:1));
        } else {
            this.pages = (int)total;
        }

    }

    public boolean isCount() {
        return this.total > -1L;
    }

    public String toString() {
        return "Page{pageNum=" + this.pageNum + ", pageSize=" + this.pageSize + ", startRow=" + this.startRow + ", endRow=" + this.endRow + ", total=" + this.total + ", pages=" + this.pages + '}';
    }
}
