package com.sckw.core.model.page;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;

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

/**
 * @Description 分页结果相关
 * @Author zk
 * @Date 2019/5/14
 */
public class PageResult implements Serializable {

    /**
     * 当前页数
     */
    protected int page;
    /**
     * 每页显示条数
     */
    protected int pageSize;
    /**
     * 总条数
     */
    protected long size;
    /**
     * 总页数
     */
    protected int pages;
    protected List list;

    public PageResult(){

    }

    public PageResult(int page, int pageSize, List list){
        this.page = page;
        this.pageSize = pageSize;
        if(list == null){
            this.size = 0;
            this.list = null;
        }else{
            this.size = list.size();
            this.list = list.subList(pageSize * (page - 1),pageSize * page > size ? (int) size : pageSize * page);
        }
    }

    public PageResult(int page, int pageSize, List list, int size){
        this.page = page;
        this.pageSize = pageSize;
        if(list == null){
            this.size = 0;
            this.list = null;
        }else{
            this.size = size;
            this.list = list;
        }
    }

    public PageResult(PageInfo pageInfo){
        this.page = pageInfo.getPageNum();
        this.pageSize = pageInfo.getPageSize();
        this.size = pageInfo.getTotal();
        this.pages = pageInfo.getPages();
        this.list = pageInfo.getList();
    }

    public static <T> PageResult build(Integer page, Integer pageSize, Long totalCount, List<T> records) {
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPageSize(pageSize);
        pageResult.setSize(totalCount);
        pageResult.setPages((int) ((totalCount + pageSize - 1) / pageSize));
        pageResult.setList(records);
        return pageResult;
    }

    public static <T> PageResult of(IPage page, List<T> records) {
        PageResult pageResult = new PageResult();
        pageResult.setPage((int)page.getCurrent());
        pageResult.setPageSize((int) page.getSize());
        pageResult.setSize(page.getTotal());
        // 修正总页数计算
        pageResult.setPages((int) ((page.getTotal() + page.getSize() - 1) / page.getSize()));
        pageResult.setList(records);
        return pageResult;
    }

    public void setPageInfo(PageInfo pageInfo){
        this.page = pageInfo.getPageNum();
        this.pageSize = pageInfo.getPageSize();
        this.size = pageInfo.getTotal();
        this.pages = pageInfo.getPages();
        this.list = pageInfo.getList();
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPageSize() {
        return pageSize;
    }

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

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public int getPages() {
        return pages;
    }

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

    public List getList() {
        return list;
    }

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

    public static int getPage(Map record) {
        return record != null && record.get("page") != null ? Integer.parseInt(record.get("page").toString()) : 1;
    }

    public static int getPageSize(Map record) {
        return record != null && record.get("pageSize") != null ? Integer.parseInt(record.get("pageSize").toString()) : 10;
    }

}