package cn.cecep.talroad.utils;
/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */


import com.github.pagehelper.PageInfo;
import lombok.Data;

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

/*
 *@Author: liuwenshuang
 *@date:   2018/10/29
 */


@Data
public class PageUtil implements Serializable {
	private static final long serialVersionUID = 1L;
	//总记录数
	private Long totalCount;
	//每页记录数
	private int pageSize;
	//总页数
	private int totalPage;
	//当前页数
	private int currPage;
	//是否为最后一页
	private boolean isLastPage;
	//列表数据
	private List<?> list;

	/**
	 * 分页
	 * @param list        列表数据
	 * @param totalCount  总记录数
	 * @param pageSize    每页记录数
	 * @param currPage    当前页数
	 */
	public PageUtil(List<?> list, Long totalCount, int pageSize, int currPage) {
		this.list = list;
		this.totalCount = totalCount;
		this.pageSize = pageSize;
		this.currPage = currPage;
		this.totalPage = (int)Math.ceil((double)totalCount/pageSize);
		if(currPage >= totalPage) {
			isLastPage = true;
		} else {
			isLastPage = false;
		}
	}

	/**
	 * 分页2版
	 * @param pageNum    当前页数
	 * @param pageSize   每页记录数
	 * @param sourceList 列表数据
	 */
	public PageUtil(List<?> sourceList, int pageNum, int pageSize) {
		if (sourceList == null || sourceList.isEmpty()) {
			return;
		}
		int size = sourceList.size();
		// 总记录条数
		this.totalCount = Long.valueOf(String.valueOf(sourceList.size()));

		// 每页显示多少条记录
		this.pageSize = pageSize;

		//获取总页数
		this.totalPage = size / this.pageSize;
		if (this.totalCount % this.pageSize != 0) {
			this.totalPage = this.totalPage + 1;
		}

		// 当前第几页数据
		this.currPage = this.totalPage < pageNum ? this.totalPage : pageNum;

		// 起始索引
		int fromIndex = this.pageSize * (this.currPage - 1);

		// 结束索引
		int toIndex = this.pageSize * this.currPage > this.totalCount ? size : this.pageSize * this.currPage;

		this.list = sourceList.subList(fromIndex, toIndex);
	}


	/**
	 * 分页
	 */
//	public PageUtil(Page<?> page) {
//		this.list = page.getRecords();
//		this.totalCount = Long.valueOf(page.getTotal());
//		this.pageSize = page.getSize();
//		this.currPage = page.getCurrent();
//		//this.totalPage = (int)page.getPages();
//		this.totalPage = (int)Math.ceil((double)totalCount/pageSize);
//		if(currPage >= totalPage) {
//			isLastPage = true;
//		} else {
//			isLastPage = false;
//		}
//	}

	/**
	 * 分页
	 */
	public PageUtil(PageInfo page) {
		this.list = page.getList();
		this.totalCount = page.getTotal();
		this.pageSize = page.getSize();
		this.currPage = page.getPageNum();
		this.totalPage = page.getPages();
		if(currPage >= totalPage) {
			isLastPage = true;
		} else {
			isLastPage = false;
		}
	}
	/**
	 * 分页2版
	 */
	public PageUtil(List<?> list) {
		PageInfo page = new PageInfo<>(list);
		this.list = page.getList();
		this.totalCount = page.getTotal();
		this.pageSize = page.getSize();
		this.currPage = page.getPageNum();
		this.totalPage = page.getPages();
		if(currPage >= totalPage) {
			isLastPage = true;
		} else {
			isLastPage = false;
		}
	}

}
