package com.ads.common.data;

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

import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 * 
 * @author 作者 GXS
 * @date 创建时间 2017年11月24日 下午4:34:03
 * @description 对Page<E>结果进行包装
 *              <p/>
 *              新增分页的多项属性
 * 
 */

@SuppressWarnings({ "rawtypes", "unchecked" })
public class PageInfo<T> implements Serializable {
	
	private static final long serialVersionUID = 1L;

	// 当前页
	private int pageNum;

	// 每页的数量
	private int pageSize;

	// 总记录数
	private long total;

	// 总页数
	private int pages;

	// 结果集
	private List<T> list;

	// 是否为第一页
	private boolean isFirstPage = false;

	// 是否为最后一页
	private boolean isLastPage = false;

	public PageInfo() {
	}

	/**
	 * 包装Page对象
	 *
	 * @param list
	 */
	public PageInfo(List<T> list) {
		if (list instanceof IPage) {
			IPage page = (IPage) list;
			this.pageNum = (int)page.getCurrent();
			this.pageSize = (int)page.getSize();

			this.pages = (int)page.getPages();
			this.list = page.getRecords();
			this.total = page.getTotal();
		} else if (list instanceof Collection) {
			this.pageNum = 1;
			this.pageSize = list.size();

			this.pages = 1;
			this.list = list;
			this.total = list.size();
		}
		if (list instanceof Collection) {
			// 判断页面边界
			judgePageBoudary();
		}
	}

	/**
	 * 分页
	 */
	public void pageUtils(IPage<?> page) {
		this.total = page.getTotal();
		this.pageSize = (int)page.getSize();
		this.pageNum = (int)page.getCurrent();
		this.pages = (int)page.getPages();
	}

	/**
	 * 判定页面边界
	 */
	private void judgePageBoudary() {
		isFirstPage = pageNum == 1;
		isLastPage = pageNum == pages;
	}

	public int getPageNum() {
		return pageNum;
	}

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

	public int getPageSize() {
		return pageSize;
	}

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

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public int getPages() {
		return pages;
	}

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

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	public boolean isIsFirstPage() {
		return isFirstPage;
	}

	public void setIsFirstPage(boolean isFirstPage) {
		this.isFirstPage = isFirstPage;
	}

	public boolean isIsLastPage() {
		return isLastPage;
	}

	public void setIsLastPage(boolean isLastPage) {
		this.isLastPage = isLastPage;
	}

	@Override
	public String toString() {
		final StringBuffer sb = new StringBuffer("PageInfo{");
		sb.append("pageNum=").append(pageNum);
		sb.append(", pageSize=").append(pageSize);
		sb.append(", total=").append(total);
		sb.append(", pages=").append(pages);
		sb.append(", list=").append(list);
		sb.append(", isFirstPage=").append(isFirstPage);
		sb.append(", isLastPage=").append(isLastPage);
		sb.append(", navigatepageNums=");
		sb.append('}');
		return sb.toString();
	}
}
