package com.ssm.manage.page;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang.StringUtils;

import java.io.Serializable;

public class BasePage implements Serializable {
    @ApiModelProperty(value = "页码,默认为1", example = "1")
    private int pageNum = 1;
    @ApiModelProperty(value = "每页显示数量，默认10，最大50", example = "10")
    private int pageSize = 10;
    @ApiModelProperty(value = "排序方式，desc/asc,默认desc", example = "desc")
    private String order = "desc";
    @ApiModelProperty(value = "排序字段，默认id", example = "id")
    private String sort = "id";
    @ApiModelProperty(value = "排序字段，默认id", example = "id")
    private String sorts = "company_id";
    @ApiModelProperty(hidden = true)
    private String orderAndSort;

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        if (pageNum < 1) {
            pageNum = 0;
        }
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        if (pageSize > 100) {
            pageSize = pageSize;
        }
        this.pageSize = pageSize;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }


    public String getOrderAndSort() {
        return columnCnvert(sort, order);
    }

    public String columnCnvert(String sort, String order) {
        String sortx;
        if (StringUtils.isNotBlank(sort)) {
            order = order == null ? "desc" : order;
            sortx = sort.toLowerCase();
            for (int i = sort.length() - 1; i >= 0; i--) {
                if (!sort.substring(i, i + 1).equals(sortx.substring(i, i + 1))) {
                    sortx = sortx.substring(0, i) + "_" + sortx.substring(i);
                }
            }
            sortx += "  " + order;
        } else {
            sortx = "";
        }
        return sortx;
    }
}
