package com.agileboot.common.mybatis.core.page;

import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;

/**
 * 分页模型类
 *
 * @author valarchie
 */
@Data
public class PageR<T> {
    private List<T> rows;  // 列表数据
    private Long total;    // 总记录数
    private Long current;  // 当前页数
    private Long size;     // 每页记录数
    private Long pages;    // 总页数

    private int code;
    private String msg;

    public PageR() {
        this.code = HttpStatus.HTTP_OK;
        this.msg = "查询成功";
        this.total = 0L;
        this.current = 0L;
        this.size = 0L;
        this.pages = 0L;
    }

    public PageR(List<T> list) {
        this.code = HttpStatus.HTTP_OK;
        this.rows = list;
        this.total = (long) list.size();
        this.msg = "查询成功";
    }

    public PageR(IPage<T> page) {
        this.code = HttpStatus.HTTP_OK;
        this.msg = "查询成功";
        this.rows = page.getRecords();
        this.total = page.getTotal();
        this.current = page.getCurrent();
        this.size = page.getSize();
        this.pages = page.getPages();
    }

    public PageR(IPage<?> page, List<T> list) {
        this.code = HttpStatus.HTTP_OK;
        this.msg = "查询成功";
        if (CollectionUtils.isEmpty(list)) {
            this.total = 0L;
            this.current = 0L;
            this.size = 0L;
            this.pages = 0L;
        } else {
            this.rows = list;
            this.total = page.getTotal();
            this.current = page.getCurrent();
            this.size = page.getSize();
            this.pages = page.getPages();
        }
    }

}
