package com.nbport.zgwl.common;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonView;
import lombok.Data;

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

/**
 * 分页查询返回结果包装类
 *
 * @author mac
 */
@Data
public class PageResult<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 当前第几页
     */
    @JsonView(Views.Public.class)
    private long pageNo;

    /**
     * 当前页条数
     */
    @JsonView(Views.Public.class)
    private long pageSize;

    /**
     * 总条数
     */
    @JsonView(Views.Public.class)
    private long totalSize;

    /**
     * 当前总共有几页
     */
    @JsonView(Views.Public.class)
    private long totalPage;

    /**
     * 分页数据
     */
    @JsonView(Views.Public.class)
    private List<T> data;

    public PageResult() {
    }

    public PageResult(Page<T> page) {
        this.pageNo = page.getCurrent();
        this.pageSize = page.getSize();
        this.totalSize = page.getTotal();
        this.totalPage = page.getPages();
        this.data = page.getRecords();
    }

    public PageResult(Page<?> page, List<T> list) {
        this.pageNo = page.getCurrent();
        this.pageSize = page.getSize();
        this.totalSize = page.getTotal();
        this.totalPage = page.getPages();
        this.data = list;
    }

    public PageResult(Page<?> page, Class<T> cls) {
        this.pageNo = page.getCurrent();
        this.pageSize = page.getSize();
        this.totalSize = page.getTotal();
        this.totalPage = page.getPages();
        this.data = BeanUtil.copyToList(page.getRecords(), cls);
    }
}
