/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */
package com.chushang.common.mybatis.utils;

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

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

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

	/**
	 * 分页
	 * @param rows        列表数据
	 * @param total  总记录数
	 * @param pageSize    每页记录数
	 * @param currPage    当前页数
	 */
	public PageResult(List<?> rows, long total, long pageSize, long currPage) {
		this.rows = rows;
		this.total = (int)total;
		this.pageSize = (int)pageSize;
		this.currPage = (int)currPage;
		this.totalPage = (int)Math.ceil((double)total/pageSize);
	}
	public PageResult(List<?> rows, IPage<?> page) {
		this.rows = rows;
		if (null != page){
			this.total = (int)page.getTotal();
			this.pageSize = (int)page.getSize();
			this.currPage = (int)page.getCurrent();
			this.totalPage = (int)Math.ceil((double)total/pageSize);
		}
	}

	/**
	 * 分页
	 */
	public PageResult(IPage<?> page) {
		this.rows = page.getRecords();
		this.total = (int)page.getTotal();
		this.pageSize = (int)page.getSize();
		this.currPage = (int)page.getCurrent();
		this.totalPage = (int)page.getPages();
	}

}
