package cn.iocoder.yudao.framework.common.pojo;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
@Data
@ApiModel("分页结果")
public final class PageResult<T> implements Serializable {

    @ApiModelProperty(value = "数据", required = true)
    private List<T> list;

    @ApiModelProperty(value = "总量", required = true, example = "0")
    private Long total = 0L;

    @ApiModelProperty(value = "每页记录数", required = true, example = "10")
    private Long rows = 10L;

    @ApiModelProperty(value = "当前页数", required = true, example = "1")
    private Long page = 1L;

    @ApiModelProperty(value = "总页数", required = true, example = "0")
    private Long pages = 0L;

    public PageResult() {}

    public PageResult(List<T> list, long total) {
        this.list = list;
        this.total = total;
    }

    public PageResult(List<T> list, long total, long rows, long page, long pages) {
        this.list = list;
        this.rows = rows;
        this.page = page;
        this.pages = pages;
        this.total = total;
    }

    public PageResult(long total) {
        this.list = new ArrayList<>();
        this.total = total;
    }

    public static <T> PageResult<T> empty() {
        return new PageResult<>();
    }

    public static <T> PageResult<T> empty(long total) {
        return new PageResult<>(total);
    }

    /****
     * 分页转换
     * @param pager
     * @return
     */
    public static <T> PageResult<T> of(IPage<T> pager){
        return new PageResult<>(pager.getRecords(), pager.getTotal(), pager.getSize(), pager.getCurrent(), pager.getPages());
    }

    /****
     * 分页转换
     * @param pager
     * @return
     */
    public static <T> PageResult<T> of(PageInfo<T> pager){
        return new PageResult<>(pager.getList(), pager.getTotal(), pager.getPageSize(), pager.getPageNum(), pager.getPages());
    }
}
