ExpressionTest.java
2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package cn.runsa.crmapp.common;
import com.alibaba.fastjson.JSON;
import cn.runsa.crmapp.common.expression.ExpGroup;
import cn.runsa.crmapp.common.expression.Expression;
import cn.runsa.crmapp.common.expression.ExpressionBuilder;
import cn.runsa.crmapp.common.expression.LogicTranslator;
public class ExpressionTest {
public static void main(String[] args) {
String str = "{\"logic\":\"or\",\"groups\":[{\"logic\":\"and\",\"exps\":[{\"key\":\"birthday\",\"op\":\"gt\",\"type\":\"function\",\"function\":\"WEEK\",\"val\":\"30\"},{\"key\":\"birthday\",\"op\":\"lt\",\"type\":\"function\",\"function\":\"YEAR\",\"val\":\"1980\"}]},{\"logic\":\"and\",\"exps\":[{\"key\":\"cstType\",\"op\":\"eq\",\"val\":\"1\"},{\"key\":\"sex\",\"op\":\"ne\",\"val\":\"0\"}]}]}";
ExpGroup group = JSON.parseObject(str, ExpGroup.class);
ExpressionBuilder builder = new ExpressionBuilder(new LogicTranslator(){
@Override
public String translate(String logic) {
// TODO Auto-generated method stub
return logic;
}
}){
@Override
public String parseExpression(Expression exp) {
StringBuilder sb = new StringBuilder();
if(exp.getType() == null){
sb.append(exp.getKey());
switch (exp.getOp()) {
case eq:
sb.append(" = ");
break;
case ne:
sb.append(" != ");
break;
case gt:
sb.append(" > ");
break;
case lt:
sb.append(" < ");
break;
case ge:
sb.append(" >= ");
break;
case le:
sb.append(" <= ");
break;
}
sb.append(exp.getVal());
}else if("function".equals(exp.getType())){
sb.append(exp.getFunction());
sb.append("(");
sb.append(exp.getKey());
sb.append(")");
switch (exp.getOp()) {
case eq:
sb.append(" = ");
break;
case ne:
sb.append(" != ");
break;
case gt:
sb.append(" > ");
break;
case lt:
sb.append(" < ");
break;
case ge:
sb.append(" >= ");
break;
case le:
sb.append(" <= ");
break;
}
sb.append(exp.getVal());
}
return sb.toString();
}
};
String stat = builder.buildStatement(group);
System.out.println(stat);
}
}