All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kasinf.framework.rest.support.SearchOperator Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
package com.kasinf.framework.rest.support;


import com.kasinf.framework.rest.exception.SearchException;
import lombok.Getter;
import org.springframework.util.StringUtils;

import java.util.Arrays;

/**
 * @author lkhsh
 * 查询操作符
 */
@Getter
public enum SearchOperator {
    /**
     * 定义的查询操作符
     */
    eq("等于", "="),
    ne("不等于", "!="),
    gt("大于", ">"),
    ge("大于等于", ">="),
    lt("小于", "<"),
    le("小于等于", "<="),
    start("前缀模糊匹配", "like"),
    notStart("前缀模糊不匹配", "not like"),
    end("后缀模糊匹配", "like"),
    notEnd("后缀模糊不匹配", "not like"),
    like("模糊匹配", "like"),
    notLike("不匹配", "not like"),
    isNull("空", "is null"),
    isNotNull("非空", "is not null"),
    in("包含", "in"),
    notIn("不包含", "not in"),
    custom("自定义默认的", null),
    yesterday("昨天的", "between"),
    thisWeek("本周的","between"),
    lastWeek("上周的","between"),
    thisMonth("本月的","between"),
    thisYear("今年的","between"),
    specific("指定日期","between");

    private final String info;
    private final String symbol;

    SearchOperator(final String info, String symbol) {
        this.info = info;
        this.symbol = symbol;
    }

    public static String toStringAllOperator() {
        return Arrays.toString(SearchOperator.values());
    }

    /**
     * 操作符是否允许为空
     * @param operator 操作符
     * @return boolean
     */
    public static boolean isAllowBlankValue(final SearchOperator operator) {
        return operator == SearchOperator.isNotNull || operator == SearchOperator.isNull;
    }

    /**
     * 根据操作符获取本对象
     * @param symbol 操作符
     * @return SearchOperator
     * @throws SearchException 抛出{@link SearchException}
     */
    public static SearchOperator valueBySymbol(String symbol) throws SearchException {
        symbol = formatSymbol(symbol);
        for (SearchOperator operator : values()) {
            if (operator.getSymbol().equals(symbol)) {
                return operator;
            }
        }

        throw new SearchException("SearchOperator not method curd operator symbol : " + symbol);
    }

    private static String formatSymbol(String symbol) {
        if (StringUtils.isEmpty(symbol)) {
            return symbol;
        }
        return symbol.trim().toLowerCase().replace("  ", " ");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy