2626import org .jetlinks .community .utils .ReactorUtils ;
2727import org .jetlinks .core .metadata .DataType ;
2828import org .jetlinks .core .metadata .types .*;
29+ import org .jetlinks .core .utils .Reactors ;
30+ import org .jetlinks .reactor .ql .supports .filter .BetweenFilter ;
31+ import org .jetlinks .reactor .ql .supports .filter .LikeFilter ;
32+ import org .jetlinks .reactor .ql .utils .CastUtils ;
33+ import org .jetlinks .reactor .ql .utils .CompareUtils ;
2934import reactor .core .publisher .Mono ;
3035
3136import java .util .*;
@@ -41,41 +46,82 @@ public enum FixedTermTypeSupport implements TermTypeSupport {
4146 public boolean isSupported (DataType type ) {
4247 return !type .getType ().equals (ArrayType .ID ) && super .isSupported (type );
4348 }
49+
50+ @ Override
51+ public boolean matchBlocking (Object expect , Object actual ) {
52+ return CompareUtils .compare (expect , actual ) == 0 ;
53+ }
4454 },
4555 neq ("不等于" , "neq" ) {
4656 @ Override
4757 public boolean isSupported (DataType type ) {
4858 return !type .getType ().equals (ArrayType .ID ) && super .isSupported (type );
4959 }
5060
61+ @ Override
62+ public boolean matchBlocking (Object expect , Object actual ) {
63+ return CompareUtils .compare (expect , actual ) != 0 ;
64+ }
5165 },
5266 notnull ("不为空" , "notnull" , false ) {
5367 @ Override
5468 protected String createDefaultDesc (String property , Object expect , Object actual ) {
5569 return String .format ("%s%s" , property , getName ());
5670 }
71+
5772 @ Override
58- protected Object getExpectRef ( String col ) {
59- return 1 ;
73+ public boolean matchBlocking ( Object expect , Object actual ) {
74+ return actual != null ;
6075 }
6176 },
6277 isnull ("为空" , "isnull" , false ) {
6378 @ Override
6479 public String createDefaultDesc (String property , Object expect , Object actual ) {
6580 return String .format ("%s%s" , property , getName ());
6681 }
82+
6783 @ Override
68- protected Object getExpectRef ( String col ) {
69- return 1 ;
84+ public boolean matchBlocking ( Object expect , Object actual ) {
85+ return actual == null ;
7086 }
87+
7188 },
7289
73- gt ("大于" , "gt" , DateTimeType .ID , ShortType .ID , IntType .ID , LongType .ID , FloatType .ID , DoubleType .ID ),
74- gte ("大于等于" , "gte" , DateTimeType .ID , ShortType .ID , IntType .ID , LongType .ID , FloatType .ID , DoubleType .ID ),
75- lt ("小于" , "lt" , DateTimeType .ID , ShortType .ID , IntType .ID , LongType .ID , FloatType .ID , DoubleType .ID ),
76- lte ("小于等于" , "lte" , DateTimeType .ID , ShortType .ID , IntType .ID , LongType .ID , FloatType .ID , DoubleType .ID ),
90+ gt ("大于" , "gt" , DateTimeType .ID , ShortType .ID , IntType .ID , LongType .ID , FloatType .ID , DoubleType .ID ) {
91+ @ Override
92+ public boolean matchBlocking (Object expect , Object actual ) {
93+ return CompareUtils .compare (expect , actual ) > 0 ;
94+ }
95+ },
96+ gte ("大于等于" , "gte" , DateTimeType .ID , ShortType .ID , IntType .ID , LongType .ID , FloatType .ID , DoubleType .ID ) {
97+ @ Override
98+ public boolean matchBlocking (Object expect , Object actual ) {
99+ return CompareUtils .compare (expect , actual ) >= 0 ;
100+ }
101+ },
102+ lt ("小于" , "lt" , DateTimeType .ID , ShortType .ID , IntType .ID , LongType .ID , FloatType .ID , DoubleType .ID ) {
103+ @ Override
104+ public boolean matchBlocking (Object expect , Object actual ) {
105+ return CompareUtils .compare (expect , actual ) < 0 ;
106+ }
107+ },
108+ lte ("小于等于" , "lte" , DateTimeType .ID , ShortType .ID , IntType .ID , LongType .ID , FloatType .ID , DoubleType .ID ) {
109+ @ Override
110+ public boolean matchBlocking (Object expect , Object actual ) {
111+ return CompareUtils .compare (expect , actual ) <= 0 ;
112+ }
113+ },
77114
78115 btw ("在...之间" , "btw" , DateTimeType .ID , ShortType .ID , IntType .ID , LongType .ID , FloatType .ID , DoubleType .ID ) {
116+ @ Override
117+ public boolean matchBlocking (Object expect , Object actual ) {
118+ List <Object > castArray = ConverterUtils .convertToList (expect );
119+ if (castArray .size () > 1 ) {
120+ return BetweenFilter .predicate (actual , castArray .get (0 ), castArray .get (1 ));
121+ }
122+ return false ;
123+ }
124+
79125 @ Override
80126 protected Object convertValue (Object val , Term term ) {
81127 return val ;
@@ -106,6 +152,16 @@ protected String createValueDesc(Object expect) {
106152 public String createDefaultDesc (String property , Object expect , Object actual ) {
107153 return String .format ("%s不在%s之间" , property , createValueDesc (expect ));
108154 }
155+
156+ @ Override
157+ public boolean matchBlocking (Object expect , Object actual ) {
158+ List <Object > castArray = ConverterUtils .convertToList (expect );
159+ if (castArray .size () > 1 ) {
160+ return !BetweenFilter .predicate (actual , castArray .get (0 ), castArray .get (1 ));
161+ }
162+ return true ;
163+ }
164+
109165 },
110166 in ("在...之中" , "in" , StringType .ID , ShortType .ID , IntType .ID , LongType .ID , FloatType .ID , DoubleType .ID , EnumType .ID ) {
111167 @ Override
@@ -122,6 +178,14 @@ protected String createValueDesc(Object expect) {
122178 public String createDefaultDesc (String property , Object expect , Object actual ) {
123179 return String .format ("%s在%s之中" , property , createValueDesc (expect ));
124180 }
181+
182+ @ Override
183+ public boolean matchBlocking (Object expect , Object actual ) {
184+ return ConverterUtils
185+ .convertToList (expect )
186+ .stream ()
187+ .anyMatch (o -> CompareUtils .compare (o , actual ) == 0 );
188+ }
125189 },
126190 nin ("不在...之中" , "nin" , StringType .ID , ShortType .ID , IntType .ID , LongType .ID , FloatType .ID , DoubleType .ID , EnumType .ID ) {
127191 @ Override
@@ -138,6 +202,14 @@ protected String createValueDesc(Object expect) {
138202 public String createDefaultDesc (String property , Object expect , Object actual ) {
139203 return String .format ("%s不在%s之中" , property , createValueDesc (expect ));
140204 }
205+
206+ @ Override
207+ public boolean matchBlocking (Object expect , Object actual ) {
208+ return ConverterUtils
209+ .convertToList (expect )
210+ .stream ()
211+ .noneMatch (o -> CompareUtils .compare (o , actual ) == 0 );
212+ }
141213 },
142214 contains_all ("全部包含在...之中" , "contains_all" , ArrayType .ID ) {
143215 @ Override
@@ -154,6 +226,17 @@ protected String createValueDesc(Object expect) {
154226 public String createDefaultDesc (String property , Object expect , Object actual ) {
155227 return String .format ("%s全部包含在%s之中" , property , createValueDesc (expect ));
156228 }
229+
230+ @ Override
231+ public boolean matchBlocking (Object expect , Object actual ) {
232+ if (actual == null ) {
233+ return false ;
234+ }
235+ TreeSet <Object > left =
236+ CastUtils .castCollection (expect , new TreeSet <>(CompareUtils ::compare ));
237+
238+ return left .containsAll (ConverterUtils .convertToList (actual ));
239+ }
157240 },
158241 contains_any ("任意包含在...之中" , "contains_any" , ArrayType .ID ) {
159242 @ Override
@@ -170,6 +253,17 @@ protected String createValueDesc(Object expect) {
170253 public String createDefaultDesc (String property , Object expect , Object actual ) {
171254 return String .format ("%s任意包含在%s之中" , property , createValueDesc (expect ));
172255 }
256+
257+ @ Override
258+ public boolean matchBlocking (Object expect , Object actual ) {
259+ TreeSet <Object > left =
260+ CastUtils .castCollection (expect , new TreeSet <>(CompareUtils ::compare ));
261+
262+ return ConverterUtils
263+ .convertToList (actual )
264+ .stream ()
265+ .anyMatch (val -> CompareUtils .contains (left , val ));
266+ }
173267 },
174268 not_contains ("不包含在...之中" , "not_contains" , ArrayType .ID ) {
175269 @ Override
@@ -186,6 +280,17 @@ protected String createValueDesc(Object expect) {
186280 public String createDefaultDesc (String property , Object expect , Object actual ) {
187281 return String .format ("%s不包含在%s之中" , property , createValueDesc (expect ));
188282 }
283+
284+ @ Override
285+ public boolean matchBlocking (Object expect , Object actual ) {
286+ TreeSet <Object > left =
287+ CastUtils .castCollection (expect , new TreeSet <>(CompareUtils ::compare ));
288+
289+ return ConverterUtils
290+ .convertToList (actual )
291+ .stream ()
292+ .noneMatch (val -> CompareUtils .contains (left , val ));
293+ }
189294 },
190295
191296 like ("包含字符" , "str_like" , StringType .ID ) {
@@ -201,12 +306,25 @@ protected Object convertValue(Object val, Term term) {
201306 }
202307 return val ;
203308 }
309+
310+ @ Override
311+ public boolean matchBlocking (Object expect , Object actual ) {
312+ if (expect instanceof String && !((String ) expect ).contains ("%" )) {
313+ expect = "%" + expect + "%" ;
314+ }
315+ return LikeFilter .doTest (false , actual , expect );
316+ }
204317 },
205318 nlike ("不包含字符" , "str_nlike" , StringType .ID ) {
206319 @ Override
207320 protected Object convertValue (Object val , Term term ) {
208321 return like .convertValue (val , term );
209322 }
323+
324+ @ Override
325+ public boolean matchBlocking (Object expect , Object actual ) {
326+ return LikeFilter .doTest (true , actual , expect );
327+ }
210328 },
211329
212330 // gt(math.sub(column,now()),?)
@@ -225,6 +343,13 @@ protected String createValueDesc(Object expect) {
225343 public String createDefaultDesc (String property , Object expect , Object actual ) {
226344 return String .format ("%s距离当前时间大于%s秒" , property , expect );
227345 }
346+
347+ @ Override
348+ public boolean matchBlocking (Object expect , Object actual ) {
349+ long cast = CastUtils .castNumber (expect ).longValue ();
350+ long now = System .currentTimeMillis ();
351+ return cast > now / 1000 ;
352+ }
228353 },
229354 time_lt_now ("距离当前时间小于...秒" , "time_lt_now" , DateTimeType .ID ) {
230355 @ Override
@@ -241,6 +366,13 @@ protected String createValueDesc(Object expect) {
241366 public String createDefaultDesc (String property , Object expect , Object actual ) {
242367 return String .format ("%s距离当前时间小于%s秒" , property , expect );
243368 }
369+
370+ @ Override
371+ public boolean matchBlocking (Object expect , Object actual ) {
372+ long cast = CastUtils .castNumber (expect ).longValue ();
373+ long now = System .currentTimeMillis ();
374+ return cast < now / 1000 ;
375+ }
244376 };
245377
246378 private final String text ;
@@ -353,31 +485,9 @@ public String createDesc(String property, Object expect, Object actual) {
353485 );
354486 }
355487
356- protected Object getExpectRef (String col ){
357- return NativeSql .of (col );
358- }
359- private Function <Object , Mono <Boolean >> createFilter () {
360- Term term = new Term ();
361- term .setTermType (getType ());
362- term .setColumn ("_actual" );
363- term .setValue (getExpectRef ("_expect" ));
364- return ReactorUtils .createFilter (Collections .singletonList (term ));
365- }
366-
367- private volatile Function <Object , Mono <Boolean >> filter ;
368-
369488 @ Override
370489 public Mono <Boolean > match (Object expect , Object actual ) {
371- if (filter == null ) {
372- synchronized (this ) {
373- if (filter == null ) {
374- filter = createFilter ();
375- }
376- }
377- }
378- Map <String , Object > val = new HashMap <>();
379- val .put ("_actual" , actual );
380- val .put ("_expect" , expect );
381- return filter .apply (val );
490+ return matchBlocking (expect , actual ) ? Reactors .ALWAYS_TRUE : Reactors .ALWAYS_FALSE ;
382491 }
492+
383493}
0 commit comments