package com.kcim.common.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.annotation.JsonInclude;

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

/**
 * 分页工具类
 *
 * @author Mark sunlightcs@gmail.com
 */
public class PageUtils implements Serializable {
	private static final long serialVersionUID = 1L;
	/**
	 * 总记录数
	 */
	private int totalCount;
	/**
	 * 每页记录数
	 */
	private int pageSize;
	/**
	 * 总页数
	 */
	private int totalPage;
	/**
	 * 当前页数
	 */
	private int current;
	/**
	 * 列表数据
	 */
	private List<?> list;

	/**
	 * 归集后的数据总额
	 */
	@JsonInclude(JsonInclude.Include.NON_NULL)
	private BigDecimal totalAmount;


	public PageUtils(int totalCount, int pageSize, int totalPage, int current, List<?> list, BigDecimal totalAmount, BigDecimal departmentAmount) {
		this.totalCount = totalCount;
		this.pageSize = pageSize;
		this.totalPage = totalPage;
		this.current = current;
		this.list = list;
		this.totalAmount = totalAmount;
		this.departmentAmount = departmentAmount;
	}

	public PageUtils(List<?> list, int totalCount, Integer pageSize, Integer current, BigDecimal totalAmount, BigDecimal departmentAmount) {
		this.totalCount = totalCount;
		this.pageSize = pageSize;
//		this.totalPage = (int)Math.ceil((double)totalCount/pageSize);
		this.current = current;
		this.list = list;
		this.totalAmount = totalAmount;
		this.departmentAmount = departmentAmount;



	}

	public BigDecimal getDepartmentAmount() {
		return departmentAmount;
	}

	public void setDepartmentAmount(BigDecimal departmentAmount) {
		this.departmentAmount = departmentAmount;
	}

	@JsonInclude(JsonInclude.Include.NON_NULL)
	private BigDecimal departmentAmount;
	/**
	 * 分页
	 * @param list        列表数据
	 * @param totalCount  总记录数
	 * @param pageSize    每页记录数
	 * @param current    当前页数
	 */
	public PageUtils(List<?> list, int totalCount, int pageSize, int current) {
		this.list = list;
		this.totalCount = totalCount;
		this.pageSize = pageSize;
		this.current = current ;
		this.totalPage = (int)Math.ceil((double)totalCount/pageSize);
	}

	/**
	 * 分页
	 * @param list        列表数据
	 * @param totalCount  总记录数
	 * @param pageSize    每页记录数
	 * @param current    当前页数
	 * @param totalAmount  总金额
	 */
	public PageUtils(List<?> list, int totalCount, int pageSize, int current,BigDecimal totalAmount) {
		this.list = list;
		this.totalCount = totalCount;
		this.pageSize = pageSize;
		this.current = current ;
		this.totalPage = (int)Math.ceil((double)totalCount/pageSize);
		this.totalAmount = totalAmount;

	}

	/**
	 * 分页
	 */
	public PageUtils(IPage<?> page) {
		this.list = page.getRecords();
		this.totalCount = (int)page.getTotal();
		this.pageSize = (int)page.getSize();
		this.current = (int)page.getCurrent();
		this.totalPage = (int)page.getPages();
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getPageSize() {
		return pageSize;
	}

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

	public int getTotalPage() {
		return totalPage;
	}

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

	public int getCurrent() {
		return current;
	}

	public void setCurrent(int current) {
		this.current = current;
	}

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

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

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public BigDecimal getTotalAmount() {
		return totalAmount;
	}

	public void setTotalAmount(BigDecimal totalAmount) {
		this.totalAmount = totalAmount;
	}
	public static <T> List<T> startPage(List<T> list,int current,int pageSize){
		if(list ==null){
			return new ArrayList<>();
		}
		if(list.size() == 0){
			return  list;
		}
		if(current ==0){
			current = 1;
		}
		//记录总数
		int count = list.size();
		//页数
		int pageCount = 0;
		if(count % pageSize==0){
			pageCount = count / pageSize;
		}else {
			pageCount = count / pageSize + 1;
		}
		int fromIndex = 0;
		int toIndex = 0;
		if(current>pageCount){
			return new ArrayList<>();
		}
		if(current != pageCount){
			fromIndex = (current - 1)*pageSize;
			toIndex = fromIndex + pageSize;
		}else {
			fromIndex = (current - 1)*pageSize;
			toIndex = count;
		}
		return list.subList(fromIndex,toIndex);
	}

	public static <T> List<T> getPageList(List<T> list,int current,int pageSize){
		if(list ==null){
			return new ArrayList<>();
		}
		if(list.size() == 0){
			return  list;
		}
		if(current ==0){
			current = 1;
		}
		//记录总数
		int count = list.size();
		//页数
		int pageCount = 0;
		if(count % pageSize==0){
			pageCount = count / pageSize;
		}else {
			pageCount = count / pageSize + 1;
		}
		int fromIndex = 0;
		int toIndex = 0;
		if(current>pageCount){
			return new ArrayList<>();
		}
		if(current != pageCount){
			fromIndex = (current - 1)*pageSize;
			toIndex = fromIndex + pageSize;
		}else {
			fromIndex = (current - 1)*pageSize;
			toIndex = count;
		}
		return list.subList(fromIndex,toIndex);
	}

}
