package com.sckw.core.model.page;

import com.github.pagehelper.PageInfo;
import com.sckw.core.utils.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页结果泛型类
 *
 * @author xucaiqin
 * @date 2023-07-12 08:42:26
 */
public class PageRes<T> {

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

    public PageRes() {
        this.list = new ArrayList<>();
    }

    public PageRes(int page, int pageSize, long size, int pages, List<T> list) {
        this.page = page;
        this.pageSize = pageSize;
        this.size = size;
        this.pages = pages;
        this.list = list;
    }

    public PageRes(PageInfo<T> pageInfo) {
        this.page = pageInfo.getPageNum();
        this.pageSize = pageInfo.getPageSize();
        this.size = pageInfo.getTotal();
        this.pages = pageInfo.getPages();
        this.list = CollectionUtils.isEmpty(pageInfo.getList()) ? new ArrayList<>() : pageInfo.getList();
    }

    public PageRes(PageInfo<T> pageInfo, List<T> list) {
        this.page = pageInfo.getPageNum();
        this.pageSize = pageInfo.getPageSize();
        this.size = pageInfo.getTotal();
        this.pages = pageInfo.getPages();
        this.list = list;
    }

    public PageRes(List<T> list) {
        PageInfo<T> pageInfo = new PageInfo<>(list);
        this.page = pageInfo.getPageNum();
        this.pageSize = pageInfo.getPageSize();
        this.size = pageInfo.getTotal();
        this.pages = pageInfo.getPages();
        this.list = pageInfo.getList();
    }

    /**
     * 构建返回数据
     *
     * @param pagInfo pageHelper的分页对象
     * @param list    实际返回的数据集
     * @return
     */
    public static <T> PageRes<T> build(PageInfo<?> pagInfo, List<T> list) {
        PageRes<T> tPageRes = new PageRes<>();
        tPageRes.setPage(pagInfo.getPageNum());
        tPageRes.setPageSize(pagInfo.getPageSize());
        tPageRes.setSize(pagInfo.getTotal());
        tPageRes.setPages(pagInfo.getPages());
        tPageRes.setList(list);
        return tPageRes;
    }

    /**
     * 手动对list进行分页数据处理
     *
     * @param page     当前页
     * @param pageSize 每页大小
     * @param list     总数据
     * @return
     */
    public static <T> PageRes<T> handPage(int page, int pageSize, List<T> list) {
        PageRes<T> tPageRes = new PageRes<>();
        tPageRes.setPage(page);
        tPageRes.setPageSize(pageSize);
        tPageRes.setSize(list.size());
        tPageRes.setPages(list.size() / pageSize + 1);
        tPageRes.setList(list.stream().skip((long) (page - 1) * pageSize).limit(pageSize).toList());
        return tPageRes;
    }

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

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

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

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

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

    public int getPage() {
        return page;
    }

    public int getPageSize() {
        return pageSize;
    }

    public long getSize() {
        return size;
    }

    public int getPages() {
        return pages;
    }

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