package com.kym.entity.common;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageSerializable;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Collection;
import java.util.List;

/**
 * @author skyline
 * @description 精简分页对象
 * @date 2023-08-30 15:01
 */
@Data
@NoArgsConstructor
public class PageBean<T> extends PageSerializable<T> {
    public static final int DEFAULT_NAVIGATE_PAGES = 8;
    /**
     * 当前页
     */
    private int pageNum;
    /**
     * 每页的数量
     */
    private int pageSize;
    /**
     * 当前页的数量
     */
    private int size;

    /**
     * 总页数
     */
    private int pages;

    /**
     * 临时扩展数据
     */
    private Object extraData;

    /**
     * 包装Page对象
     *
     * @param list
     */
    public PageBean(List<? extends T> list) {
        this(list, DEFAULT_NAVIGATE_PAGES);
    }

    /**
     * 包装Page对象
     *
     * @param list          page结果
     * @param navigatePages 页码数量
     */
    public PageBean(List<? extends T> list, int navigatePages) {
        super(list);
        if (list instanceof Page) {
            Page page = (Page) list;
            this.pageNum = page.getPageNum();
            this.pageSize = page.getPageSize();

            this.pages = page.getPages();
            this.size = page.size();
            //由于结果是>startRow的，所以实际的需要+1
        } else if (list instanceof Collection) {
            this.pageNum = 1;
            this.pageSize = list.size();

            this.pages = this.pageSize > 0 ? 1 : 0;
            this.size = list.size();
        }
    }

    public PageBean<T> setExtraData(Object extraData) {
        this.extraData = extraData;
        return this;
    }

    public PageBean<T> setList(PageBean<?> pageBean){
        this.total = pageBean.total;
        this.pageSize = pageBean.pageSize;
        this.pageNum = pageBean.pageNum;
        this.size = pageBean.size;
        this.extraData = pageBean.extraData;
        return this;
    }
}
