package CoreFS.SD03;

import CoreFS.SA01.CoreException;

public class CoreFilePageBean {
	private int allRow; // 总记录数
	private int totalPage; // 总页数
	private int currentPage; // 当前页
	private int pageSize; // 每页记录数
	private int totSize; // 总长度
	private String path;// 文件路径
	private int startPos;
	private int endPos;

	private boolean isFirstPage; // 是否为第一页
	private boolean isLastPage; // 是否为最后一页
	private boolean hasPreviousPage; // 是否有前一页
	private boolean hasNextPage; // 是否有下一页

	public CoreFilePageBean(String path, int pageSize) {
		this.path = path;
		this.pageSize = pageSize;
	}

	public CoreFilePageBean() {

	}

	/** */
	/**
	 * 初始化分页信息
	 */
	public void init() {
		this.isFirstPage = isFirstPage();
		this.isLastPage = isLastPage();
		this.hasPreviousPage = isHasPreviousPage();
		this.hasNextPage = isHasNextPage();
	}

	public boolean isFirstPage() {
		return currentPage == 1; // 如是当前页是第1页
	}

	public boolean isLastPage() {
		return currentPage == totalPage; // 如果当前页是最后一页
	}

	public boolean isHasPreviousPage() {
		return currentPage > 1; // 只要当前页不是第1页
	}

	public boolean isHasNextPage() {
		return currentPage < totalPage; // 只要当前页不是最后1页
	}

	public int getAllRow() {
		return allRow;
	}

	public void setAllRow(int allRow) {
		this.allRow = allRow;
	}

	public int getTotalPage() {
		this.totalPage = (this.totSize / this.pageSize) + 1;
		return totalPage;
	}

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

	public int getCurrentPage() {
		this.currentPage = currentPage >= 1 ? currentPage : 1;
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

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

	public int getTotSize() {
		return totSize;
	}

	public void setTotSize(int totSize) {
		this.totSize = totSize;
	}

	public int getStartPos() {
		this.startPos = (this.currentPage - 1) * this.pageSize;
		startPos = startPos < 0 ? 0 : startPos;
		return startPos;
	}

	public void setStartPos(int startPos) {
		this.startPos = startPos;
	}

	public int getEndPos() {

		this.endPos = this.currentPage * this.pageSize - 1;
		endPos = endPos > totSize ? totSize : endPos;
		return endPos;
	}

	public void setEndPos(int endPos) {
		this.endPos = endPos;
	}

	public boolean goable() {
		boolean goable = true;
		// 检查当前页
		if (this.currentPage > this.totalPage || this.currentPage < 1) {
			goable = false;
			throw new CoreException("当前页大于最大页 或者当前页为 < 1");
		}
			
		// 检查起始位置
		if (this.startPos < 0 || this.startPos > this.endPos)
		{
			goable = false;
			throw new CoreException("当前开始位置<0 或者当前开始位置超过结束位置");
		}	

		// 检查结束位置
		if (this.endPos < 0 || this.endPos > this.totSize)
		{
			goable = false;
			throw new CoreException("当前结束位置<0 或者当前位置超过最大长度");
		}
		return goable;
	}

}
