package cn.com.ty.lift.common.model;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Setter;

import java.io.Serializable;

/**
 * 分页查询请求基类，获取分页参数pageNum,pageSize，构建IPage对象
 *
 * @author wcz
 * @since 2020/02/08
 */
@Setter
public abstract class PageRequest implements Serializable {
    /**
     * 当前页
     */
    private long pageNum  = 1;
    /**
     * 每页显示条数，默认 10
     */
    private long pageSize = 10;
    /**
     * SQL 排序 ASC 数组
     */
    private String[] ascs;
    /**
     * SQL 排序 DESC 数组
     */
    private String[] descs;

    public <T> IPage<T> getPage() {
        return new Page<T>(getPageNum(), getPageSize()).setAsc(ascs).setDesc(descs);
    }

    public PageRequest addAsc(String... ascs){
        this.ascs = ascs;
        return this;
    }

    public PageRequest addDesc(String... descs){
        this.descs = descs;
        return this;
    }

    public long getPageNum() {
        return pageNum > 0 ? pageNum : 1;
    }

    public long getPageSize() {
        return pageSize > 0 ? pageSize : 10;
    }
}
