package com.bohuikeji.frame.module.admin.entity.back;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.bohuikeji.frame.module.admin.base.EntityBase;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;

import java.util.Collections;
import java.util.List;

/**
 * 数据分页(不要使用mpd的分页)
 * @author 10552
 */
@ApiModel(value="分页对象", description="分页对象")
public class DataPage <T extends EntityBase<T>> implements IPage<T> {
    /**
     * 查询数据列表
     */
    @ApiModelProperty(value = "结果数据集",hidden = true)
    @ApiParam(hidden = true)
    private List<T> records = Collections.emptyList();
    /**
     * 总数
     */
    @ApiModelProperty(value = "总数",example = "0",hidden = true)
    private long total = 0;
    /**
     * 每页显示条数，默认 10
     */
    @ApiModelProperty(value = "每页显示条数，默认 10",example = "10")
    private long size = 10;
    /**
     * 当前页
     */
    @ApiModelProperty(value = "当前页",required = true,example = "1")
    private long current = 1;
    /**
     * SQL 排序 ASC 数组
     */
    @ApiModelProperty(value = "SQL 排序 ASC 数组",hidden = true)
    private String[] ascs;
    /**
     * SQL 排序 DESC 数组
     */
    @ApiModelProperty(value = "SQL 排序 DESC 数组",hidden = true)
    private String[] descs;
    /**
     * 自动优化 COUNT SQL
     */
    @ApiModelProperty(value = "自动优化 COUNT SQL",hidden = true)
    private boolean optimizeCountSql = true;
    /**
     * 是否进行 count 查询
     */
   /* @ApiModelProperty(value = "是否进行 count 查询",hidden = true)
    private boolean isSearchCount = true;*/

    public DataPage() {
        // to do nothing
    }

    /**
     * 分页构造函数
     *
     * @param current 当前页
     * @param size    每页显示条数
     */
    public DataPage(long current, long size) {
        this(current, size, 0);
    }

    public DataPage(long current, long size, long total) {
        this(current, size, total, true);
    }

    public DataPage(long current, long size, boolean isSearchCount) {
        this(current, size, 0, isSearchCount);
    }

    public DataPage(long current, long size, long total, boolean isSearchCount) {
        if (current > 1) {
            this.current = current;
        }
        this.size = size;
        this.total = total;
        //this.isSearchCount = isSearchCount;
    }

    /**
     * 是否存在上一页
     *
     * @return true / false
     */
    public boolean hasPrevious() {
        return this.current > 1;
    }

    /**
     * 是否存在下一页
     *
     * @return true / false
     */
    public boolean hasNext() {
        return this.current < this.getPages();
    }

    @Override
    public List<T> getRecords() {
        return this.records;
    }

    @Override
    public DataPage<T> setRecords(List<T> records) {
        this.records = records;
        return this;
    }

    @Override
    public long getTotal() {
        return this.total;
    }

    @Override
    public DataPage<T> setTotal(long total) {
        this.total = total;
        return this;
    }

    @Override
    public long getSize() {
        return this.size;
    }

    @Override
    public DataPage<T> setSize(long size) {
        this.size = size;
        return this;
    }

    @Override
    public long getCurrent() {
        return this.current;
    }

    @Override
    public DataPage<T> setCurrent(long current) {
        this.current = current;
        return this;
    }

    @Override
    public String[] ascs() {
        return ascs;
    }

    public DataPage<T> setAscs(List<String> ascs) {
        if (CollectionUtils.isNotEmpty(ascs)) {
            this.ascs = ascs.toArray(new String[0]);
        }
        return this;
    }


    /**
     * 升序
     *
     * @param ascs 多个升序字段
     */
    public DataPage<T> setAsc(String... ascs) {
        this.ascs = ascs;
        return this;
    }

    @Override
    public String[] descs() {
        return descs;
    }

    public DataPage<T> setDescs(List<String> descs) {
        if (CollectionUtils.isNotEmpty(descs)) {
            this.descs = descs.toArray(new String[0]);
        }
        return this;
    }

    /**
     * 降序
     *
     * @param descs 多个降序字段
     */
    public DataPage<T> setDesc(String... descs) {
        this.descs = descs;
        return this;
    }

    @Override
    public boolean optimizeCountSql() {
        return optimizeCountSql;
    }

   /* @Override
    public boolean isSearchCount() {
        if (total < 0) {
            return false;
        }
        return isSearchCount;
    }*/

   /* public DataPage<T> setSearchCount(boolean isSearchCount) {
        this.isSearchCount = isSearchCount;
        return this;
    }*/

    public DataPage<T> setOptimizeCountSql(boolean optimizeCountSql) {
        this.optimizeCountSql = optimizeCountSql;
        return this;
    }
}
