package com.trade.common.core.dto;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;

/**
 * 查询辅助类
 */
public class BaseSearch {
    private String fastSearch;
    /**
     * 客户编号
     */
    private String customerIdentifierNo;
    /**
     * 企业名称
     */
    private String enterpriseName;
    /**
     * 企业统一社会信用代码
     */
    private String certNo;
    /**
     * 页码（首页从1开始）
     */
    private int page = 1;
    /**
     * 页大小
     */
    private int rows = 30;
    /**
     * 排序字段
     */
    private String sidx = "gmt_create";
    /**
     * 排序类型
     */
    private String sort = "desc";

    /**
     * 页码（首页从1开始）
     */
    public int getPage() {
        return page > 1 ? (page) : 1;
    }

    public void setPage(int page) {
        this.page = page;
    }

    /**
     * 页大小
     */
    public int getRows() {
        return rows > 1 ? (rows) : 10;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    /**
     * 排序字段
     */
    public String getSidx() {
        if (sidx == null || sidx.trim().equals("")) {
            sidx = "gmt_create";
        }
        return sidx;
    }

    public void setSidx(String sidx) {
        this.sidx = sidx;
    }

    /**
     * 排序类型
     */
    public String getSort() {
        if (StringUtils.isEmpty(sort)) {
            sort = "desc";
        }
        return sort;
    }

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


    public String getFastSearch() {
        return fastSearch;
    }

    public void setFastSearch(String fastSearch) {
        this.fastSearch = fastSearch;
    }

    public String getCustomerIdentifierNo() {
        return customerIdentifierNo;
    }

    public void setCustomerIdentifierNo(String customerIdentifierNo) {
        this.customerIdentifierNo = customerIdentifierNo;
    }

    public String getEnterpriseName() {
        return enterpriseName;
    }

    public void setEnterpriseName(String enterpriseName) {
        this.enterpriseName = enterpriseName;
    }

    public String getCertNo() {
        return certNo;
    }

    public void setCertNo(String certNo) {
        this.certNo = certNo;
    }

    public <T> Page<T> toMpPage(OrderItem... orderItems) {
        // 1. 分页条件
        Page<T> p = Page.of(page, rows);
        // 2. 排序提交
        if (sidx != null) {
            p.addOrder(new OrderItem(sidx, sort.equals("asc")));
            return p;
        }

        if (orderItems != null) {
            p.addOrder(orderItems);
        }
        return p;
    }

    // 手动传入排序方式
    public <T> Page<T> toMpPage(String defaultSortBy, boolean isAsc) {
        return this.toMpPage(new OrderItem(defaultSortBy, isAsc));
    }

    // 默认 按照 CreateTime 降序排序
    public <T> Page<T> toMpPageDefaultSortByCreateTimeDesc() {
        return this.toMpPage("gmt_create", false);
    }

    // 默认 按照 UpdateTime 降序排序
    public <T> Page<T> toMpPageDefaultSortByUpdateTimeDesc() {
        return this.toMpPage("gmt_update", false);
    }

}
