refactor: 优化条件匹配逻辑
This commit is contained in:
parent
6a0fff01b6
commit
5dca15e76a
|
|
@ -26,6 +26,11 @@ import org.jetlinks.community.utils.ConverterUtils;
|
|||
import org.jetlinks.community.utils.ReactorUtils;
|
||||
import org.jetlinks.core.metadata.DataType;
|
||||
import org.jetlinks.core.metadata.types.*;
|
||||
import org.jetlinks.core.utils.Reactors;
|
||||
import org.jetlinks.reactor.ql.supports.filter.BetweenFilter;
|
||||
import org.jetlinks.reactor.ql.supports.filter.LikeFilter;
|
||||
import org.jetlinks.reactor.ql.utils.CastUtils;
|
||||
import org.jetlinks.reactor.ql.utils.CompareUtils;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.*;
|
||||
|
|
@ -41,6 +46,11 @@ public enum FixedTermTypeSupport implements TermTypeSupport {
|
|||
public boolean isSupported(DataType type) {
|
||||
return !type.getType().equals(ArrayType.ID) && super.isSupported(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matchBlocking(Object expect, Object actual) {
|
||||
return CompareUtils.compare(expect, actual) == 0;
|
||||
}
|
||||
},
|
||||
neq("不等于", "neq") {
|
||||
@Override
|
||||
|
|
@ -48,15 +58,20 @@ public enum FixedTermTypeSupport implements TermTypeSupport {
|
|||
return !type.getType().equals(ArrayType.ID) && super.isSupported(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matchBlocking(Object expect, Object actual) {
|
||||
return CompareUtils.compare(expect, actual) != 0;
|
||||
}
|
||||
},
|
||||
notnull("不为空", "notnull", false) {
|
||||
@Override
|
||||
protected String createDefaultDesc(String property, Object expect, Object actual) {
|
||||
return String.format("%s%s", property, getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object getExpectRef(String col) {
|
||||
return 1;
|
||||
public boolean matchBlocking(Object expect, Object actual) {
|
||||
return actual != null;
|
||||
}
|
||||
},
|
||||
isnull("为空", "isnull", false) {
|
||||
|
|
@ -64,18 +79,49 @@ public enum FixedTermTypeSupport implements TermTypeSupport {
|
|||
public String createDefaultDesc(String property, Object expect, Object actual) {
|
||||
return String.format("%s%s", property, getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object getExpectRef(String col) {
|
||||
return 1;
|
||||
public boolean matchBlocking(Object expect, Object actual) {
|
||||
return actual == null;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
gt("大于", "gt", DateTimeType.ID, ShortType.ID, IntType.ID, LongType.ID, FloatType.ID, DoubleType.ID) {
|
||||
@Override
|
||||
public boolean matchBlocking(Object expect, Object actual) {
|
||||
return CompareUtils.compare(expect, actual) > 0;
|
||||
}
|
||||
},
|
||||
gte("大于等于", "gte", DateTimeType.ID, ShortType.ID, IntType.ID, LongType.ID, FloatType.ID, DoubleType.ID) {
|
||||
@Override
|
||||
public boolean matchBlocking(Object expect, Object actual) {
|
||||
return CompareUtils.compare(expect, actual) >= 0;
|
||||
}
|
||||
},
|
||||
lt("小于", "lt", DateTimeType.ID, ShortType.ID, IntType.ID, LongType.ID, FloatType.ID, DoubleType.ID) {
|
||||
@Override
|
||||
public boolean matchBlocking(Object expect, Object actual) {
|
||||
return CompareUtils.compare(expect, actual) < 0;
|
||||
}
|
||||
},
|
||||
lte("小于等于", "lte", DateTimeType.ID, ShortType.ID, IntType.ID, LongType.ID, FloatType.ID, DoubleType.ID) {
|
||||
@Override
|
||||
public boolean matchBlocking(Object expect, Object actual) {
|
||||
return CompareUtils.compare(expect, actual) <= 0;
|
||||
}
|
||||
},
|
||||
|
||||
gt("大于", "gt", DateTimeType.ID, ShortType.ID, IntType.ID, LongType.ID, FloatType.ID, DoubleType.ID),
|
||||
gte("大于等于", "gte", DateTimeType.ID, ShortType.ID, IntType.ID, LongType.ID, FloatType.ID, DoubleType.ID),
|
||||
lt("小于", "lt", DateTimeType.ID, ShortType.ID, IntType.ID, LongType.ID, FloatType.ID, DoubleType.ID),
|
||||
lte("小于等于", "lte", DateTimeType.ID, ShortType.ID, IntType.ID, LongType.ID, FloatType.ID, DoubleType.ID),
|
||||
|
||||
btw("在...之间", "btw", DateTimeType.ID, ShortType.ID, IntType.ID, LongType.ID, FloatType.ID, DoubleType.ID) {
|
||||
@Override
|
||||
public boolean matchBlocking(Object expect, Object actual) {
|
||||
List<Object> castArray = ConverterUtils.convertToList(expect);
|
||||
if (castArray.size() > 1) {
|
||||
return BetweenFilter.predicate(actual, castArray.get(0), castArray.get(1));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object convertValue(Object val, Term term) {
|
||||
return val;
|
||||
|
|
@ -106,6 +152,16 @@ public enum FixedTermTypeSupport implements TermTypeSupport {
|
|||
public String createDefaultDesc(String property, Object expect, Object actual) {
|
||||
return String.format("%s不在%s之间", property, createValueDesc(expect));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matchBlocking(Object expect, Object actual) {
|
||||
List<Object> castArray = ConverterUtils.convertToList(expect);
|
||||
if (castArray.size() > 1) {
|
||||
return !BetweenFilter.predicate(actual, castArray.get(0), castArray.get(1));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
},
|
||||
in("在...之中", "in", StringType.ID, ShortType.ID, IntType.ID, LongType.ID, FloatType.ID, DoubleType.ID, EnumType.ID) {
|
||||
@Override
|
||||
|
|
@ -122,6 +178,14 @@ public enum FixedTermTypeSupport implements TermTypeSupport {
|
|||
public String createDefaultDesc(String property, Object expect, Object actual) {
|
||||
return String.format("%s在%s之中", property, createValueDesc(expect));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matchBlocking(Object expect, Object actual) {
|
||||
return ConverterUtils
|
||||
.convertToList(expect)
|
||||
.stream()
|
||||
.anyMatch(o -> CompareUtils.compare(o, actual) == 0);
|
||||
}
|
||||
},
|
||||
nin("不在...之中", "nin", StringType.ID, ShortType.ID, IntType.ID, LongType.ID, FloatType.ID, DoubleType.ID, EnumType.ID) {
|
||||
@Override
|
||||
|
|
@ -138,6 +202,14 @@ public enum FixedTermTypeSupport implements TermTypeSupport {
|
|||
public String createDefaultDesc(String property, Object expect, Object actual) {
|
||||
return String.format("%s不在%s之中", property, createValueDesc(expect));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matchBlocking(Object expect, Object actual) {
|
||||
return ConverterUtils
|
||||
.convertToList(expect)
|
||||
.stream()
|
||||
.noneMatch(o -> CompareUtils.compare(o, actual) == 0);
|
||||
}
|
||||
},
|
||||
contains_all("全部包含在...之中", "contains_all", ArrayType.ID) {
|
||||
@Override
|
||||
|
|
@ -154,6 +226,17 @@ public enum FixedTermTypeSupport implements TermTypeSupport {
|
|||
public String createDefaultDesc(String property, Object expect, Object actual) {
|
||||
return String.format("%s全部包含在%s之中", property, createValueDesc(expect));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matchBlocking(Object expect, Object actual) {
|
||||
if (actual == null) {
|
||||
return false;
|
||||
}
|
||||
TreeSet<Object> left =
|
||||
CastUtils.castCollection(expect, new TreeSet<>(CompareUtils::compare));
|
||||
|
||||
return left.containsAll(ConverterUtils.convertToList(actual));
|
||||
}
|
||||
},
|
||||
contains_any("任意包含在...之中", "contains_any", ArrayType.ID) {
|
||||
@Override
|
||||
|
|
@ -170,6 +253,17 @@ public enum FixedTermTypeSupport implements TermTypeSupport {
|
|||
public String createDefaultDesc(String property, Object expect, Object actual) {
|
||||
return String.format("%s任意包含在%s之中", property, createValueDesc(expect));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matchBlocking(Object expect, Object actual) {
|
||||
TreeSet<Object> left =
|
||||
CastUtils.castCollection(expect, new TreeSet<>(CompareUtils::compare));
|
||||
|
||||
return ConverterUtils
|
||||
.convertToList(actual)
|
||||
.stream()
|
||||
.anyMatch(val -> CompareUtils.contains(left, val));
|
||||
}
|
||||
},
|
||||
not_contains("不包含在...之中", "not_contains", ArrayType.ID) {
|
||||
@Override
|
||||
|
|
@ -186,6 +280,17 @@ public enum FixedTermTypeSupport implements TermTypeSupport {
|
|||
public String createDefaultDesc(String property, Object expect, Object actual) {
|
||||
return String.format("%s不包含在%s之中", property, createValueDesc(expect));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matchBlocking(Object expect, Object actual) {
|
||||
TreeSet<Object> left =
|
||||
CastUtils.castCollection(expect, new TreeSet<>(CompareUtils::compare));
|
||||
|
||||
return ConverterUtils
|
||||
.convertToList(actual)
|
||||
.stream()
|
||||
.noneMatch(val -> CompareUtils.contains(left, val));
|
||||
}
|
||||
},
|
||||
|
||||
like("包含字符", "str_like", StringType.ID) {
|
||||
|
|
@ -201,12 +306,25 @@ public enum FixedTermTypeSupport implements TermTypeSupport {
|
|||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matchBlocking(Object expect, Object actual) {
|
||||
if (expect instanceof String && !((String) expect).contains("%")) {
|
||||
expect = "%" + expect + "%";
|
||||
}
|
||||
return LikeFilter.doTest(false, actual, expect);
|
||||
}
|
||||
},
|
||||
nlike("不包含字符", "str_nlike", StringType.ID) {
|
||||
@Override
|
||||
protected Object convertValue(Object val, Term term) {
|
||||
return like.convertValue(val, term);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matchBlocking(Object expect, Object actual) {
|
||||
return LikeFilter.doTest(true, actual, expect);
|
||||
}
|
||||
},
|
||||
|
||||
// gt(math.sub(column,now()),?)
|
||||
|
|
@ -225,6 +343,13 @@ public enum FixedTermTypeSupport implements TermTypeSupport {
|
|||
public String createDefaultDesc(String property, Object expect, Object actual) {
|
||||
return String.format("%s距离当前时间大于%s秒", property, expect);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matchBlocking(Object expect, Object actual) {
|
||||
long cast = CastUtils.castNumber(expect).longValue();
|
||||
long now = System.currentTimeMillis();
|
||||
return cast > now / 1000;
|
||||
}
|
||||
},
|
||||
time_lt_now("距离当前时间小于...秒", "time_lt_now", DateTimeType.ID) {
|
||||
@Override
|
||||
|
|
@ -241,6 +366,13 @@ public enum FixedTermTypeSupport implements TermTypeSupport {
|
|||
public String createDefaultDesc(String property, Object expect, Object actual) {
|
||||
return String.format("%s距离当前时间小于%s秒", property, expect);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matchBlocking(Object expect, Object actual) {
|
||||
long cast = CastUtils.castNumber(expect).longValue();
|
||||
long now = System.currentTimeMillis();
|
||||
return cast < now / 1000;
|
||||
}
|
||||
};
|
||||
|
||||
private final String text;
|
||||
|
|
@ -353,31 +485,9 @@ public enum FixedTermTypeSupport implements TermTypeSupport {
|
|||
);
|
||||
}
|
||||
|
||||
protected Object getExpectRef(String col){
|
||||
return NativeSql.of(col);
|
||||
}
|
||||
private Function<Object, Mono<Boolean>> createFilter() {
|
||||
Term term = new Term();
|
||||
term.setTermType(getType());
|
||||
term.setColumn("_actual");
|
||||
term.setValue(getExpectRef("_expect"));
|
||||
return ReactorUtils.createFilter(Collections.singletonList(term));
|
||||
}
|
||||
|
||||
private volatile Function<Object, Mono<Boolean>> filter;
|
||||
|
||||
@Override
|
||||
public Mono<Boolean> match(Object expect, Object actual) {
|
||||
if (filter == null) {
|
||||
synchronized (this) {
|
||||
if (filter == null) {
|
||||
filter = createFilter();
|
||||
}
|
||||
}
|
||||
}
|
||||
Map<String, Object> val = new HashMap<>();
|
||||
val.put("_actual", actual);
|
||||
val.put("_expect", expect);
|
||||
return filter.apply(val);
|
||||
return matchBlocking(expect, actual) ? Reactors.ALWAYS_TRUE : Reactors.ALWAYS_FALSE;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
2
pom.xml
2
pom.xml
|
|
@ -33,7 +33,7 @@
|
|||
<!-- https://github.com/hs-web/reactor-excel -->
|
||||
<reactor.excel.version>1.0.6</reactor.excel.version>
|
||||
<!-- https://github.com/jetlinks/reactor-ql -->
|
||||
<reactor.ql.version>1.0.19</reactor.ql.version>
|
||||
<reactor.ql.version>1.0.20-SNAPSHOT</reactor.ql.version>
|
||||
<!-- https://github.com/jetlinks/jetlinks-plugin -->
|
||||
<jetlinks.plugin.version>1.0.5-SNAPSHOT</jetlinks.plugin.version>
|
||||
<!-- https://github.com/jetlinks/jetlinks-sdk -->
|
||||
|
|
|
|||
Loading…
Reference in New Issue