package com.base.biz.aop;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.util.CollectionUtils;

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

/**
 * 分页查询
 *
 * @author lx
 * @date 2019/11/5 0005 10:58
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class PageInfo<T> extends Page<T> implements Serializable {

    private static final long serialVersionUID = 13333123L;

    @SuppressWarnings("rawtypes")
    public static final PageInfo EMPTY = new PageInfo();

    @SuppressWarnings("unchecked")
    public static <T> PageInfo<T> empty() {
        return (PageInfo<T>) EMPTY;
    }

    /**
     * 当前页码
     */
    private int page;
    /**
     * 页容
     */
    private int pageSize;

    /**
     * 结果集数据用这里,不用管records和没有注释的字段
     */
    private List<T> list = new ArrayList<>();

    public PageInfo(int pageSize, int page){
        super(page,pageSize);
        this.pageSize = pageSize;
        this.page = page;
    }

    public PageInfo(){}

    public static <T> PageInfo<T> of(Object t){
        PageInfo<T> pageInfo = new PageInfo<>();
        if (t instanceof com.github.pagehelper.PageInfo) {
            com.github.pagehelper.PageInfo page = (com.github.pagehelper.PageInfo) t;
            pageInfo.setPage(page.getPages());
            pageInfo.setTotal(page.getTotal());
            pageInfo.setPageSize(page.getPageSize());

            List<T> list = page.getList();
            pageInfo.setList(list);
        } else if(t instanceof PageInfo) {
            pageInfo = (PageInfo)t;
            List<T> list = CollectionUtils.isEmpty(pageInfo.getRecords()) ? pageInfo.getList() : pageInfo.getRecords();
            pageInfo.setRecords(null);
            if(!CollectionUtils.isEmpty(list)) {
                pageInfo.setList(list);
            }
            return pageInfo;
        } else if (t instanceof com.github.pagehelper.Page) {
            com.github.pagehelper.Page page = (com.github.pagehelper.Page) t;
            pageInfo.setPage(page.getPages());
            pageInfo.setTotal(page.getTotal());
            pageInfo.setPageSize(page.getPageSize());
            pageInfo.setList(page);
        } else if (t instanceof Page){
            Page page = (Page) t;
            pageInfo.setPage((int) page.getPages());
            pageInfo.setTotal(page.getTotal());
            pageInfo.setPageSize((int) page.getSize());
            List<T> list = page.getRecords();
            pageInfo.setList(list);
        }
        return pageInfo;
    }

    public static <T> PageInfo<T> listOf(List<T> t){
        PageInfo<T> pageInfo = new PageInfo<>();
        if(!CollectionUtils.isEmpty(t)){
            pageInfo.setTotal(t.size());
            pageInfo.setList(t);
        }
        return pageInfo;
    }

    public static <R> PageInfo<R> newOf(com.github.pagehelper.PageInfo page) {
        PageInfo<R> pageInfo = new PageInfo<R>();
        pageInfo.setPage(page.getPages());
        pageInfo.setTotal(page.getTotal());
        pageInfo.setPageSize(page.getPageSize());
        return pageInfo;
    }

    
    public static <R> PageInfo<R> newOf(com.github.pagehelper.Page page,List<R> list) {
        PageInfo<R> pageInfo = new PageInfo<R>();
        pageInfo.setPage(page.getPages());
        pageInfo.setTotal(page.getTotal());
        pageInfo.setPageSize(page.getPageSize());
        pageInfo.setList(list);
        return pageInfo;
    }
}