package com.xxx.base.springframework.mybatisplus.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

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

/**
 * @description: 封装分页结果集
 * @author: huzhiwen
 * @create: 2019-01-26 20:39
 **/
@AllArgsConstructor
@NoArgsConstructor
@Data
public class PageResult<T> implements Serializable {

    private static final long serialVersionUID = -244633338430857286L;

//    @ApiModelProperty(value = "要查找第几页", name = "page", dataType = "Integer")
    private Integer page = 1;
//    @ApiModelProperty(value = "每页显示多少条", name = "pageSize", dataType = "Integer")
    private Integer pageSize = 20;
//    @ApiModelProperty(value = "总页数", name = "totalPage", dataType = "Integer")
    private Integer totalPage = 0;
//    @ApiModelProperty(value = "总记录数", name = "totalRows", dataType = "Long")
    private Long totalRows = 0L;
//    @ApiModelProperty(value = "结果集", name = "rows", dataType = "List<T>")
    private List<T> rows;

    public PageResult(IPage<T> page) {
        setRows(page.getRecords());
        setTotalRows(page.getTotal());
        setPage((int) page.getCurrent());
        setPageSize((int) page.getSize());
        setTotalPage((int) page.getPages());
    }

    public PageResult(IPage page, List<T> list) {
        setRows(list);
        setTotalRows(page.getTotal());
        setPage((int) page.getCurrent());
        setPageSize((int) page.getSize());
        setTotalPage((int) page.getPages());
    }

    public PageResult(Integer page, Integer pageSize) {
        setPage(page);
        setPageSize(pageSize);
    }
}
