SubCalcUtils.java
8.33 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package com.hotent.runtime.utils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.JsonUtil;
import com.hotent.base.util.StringUtil;
import com.hotent.base.util.string.StringPool;
import java.math.BigDecimal;
/**
* @author moka
* */
public class SubCalcUtils {
private final static String OP_EQUALS = "1";
private final static String OP_NOT_EQUALS = "2";
private final static String OP_GREAT_THAN = "3";
private final static String OP_LESS_THAN = "4";
private final static String OP_GREAT_EQUALS = "7";
private final static String OP_LESS_EQUALS = "8";
private final static String OP_CONTAINS = "9";
private final static String OP_ISNULL = "11";
private final static String OP_NOTNULL = "12";
/**
* 判断子表中某一行满足某个条件, 默认为false
*
* 若令条件为A,B,C为符合条件,包含则是A || B || C,即其中一个条件(如A)不满足,则直接返回true
* */
public static boolean subFieldContain(JsonNode subArray, String field, String op, Object value, String dataType) {
//默认为false
if(!(subArray instanceof ArrayNode)){
return false;
}
ArrayNode arrayNode = (ArrayNode) subArray;
if (arrayNode.isEmpty()) {
return false;
}
for (JsonNode node : arrayNode) {
ObjectNode row = (ObjectNode) node;
boolean rowResult = SubCalcUtils.subRowCalc(row, field, op, value, dataType);
if (rowResult) {
return true;
}
}
return false;
}
/**
* 判断子表中是否全部不满足条件的
*
* 不包含正 等价于 全为反
* 若令条件为A,B,C为符合条件,不包含则是!A and !B and !C,即其中一个条件(如!A)不满足,则直接返回false
* */
public static boolean subFieldNotContain(JsonNode subArray, String field, String op, Object value, String dateType) {
if(!(subArray instanceof ArrayNode)){
return false;
}
ArrayNode arrayNode = (ArrayNode) subArray;
if (arrayNode.isEmpty()) {
return false;
}
for (JsonNode node : arrayNode) {
ObjectNode row = (ObjectNode) node;
boolean rowResult = SubCalcUtils.subRowCalc(row, field, op, value, dateType);
if (rowResult) {
return false;
}
}
return true;
}
private static boolean subRowCalc(ObjectNode row, String field, String op, Object value, String dateType) {
switch (dateType) {
case "string":
return subRowStringCalc(row, field, op, BeanUtils.isEmpty(value) ? "" : value.toString());
case "number":
return subRowNumberCalc(row, field, op, BeanUtils.isEmpty(value) ? BigDecimal.ZERO : value);
case "date":
return subRowDateCalc(row, field, op, BeanUtils.isEmpty(value) ? "" : value.toString());
default:
throw new IllegalArgumentException("unknown date type");
}
}
/**
* 1: ==
* 2: !=
* 3: >
* 4: <
* 7: >=
* 8: <=
* 9: belong to(value split by comma and length is 2)
* */
private static boolean subRowDateCalc(ObjectNode row, String field, String op, String value) {
String dateValue = JsonUtil.getString(row, field, StringPool.EMPTY);
if (OP_ISNULL.equals(op)){
return BeanUtils.isEmpty(dateValue);
}else if (OP_NOTNULL.equals(op)){
return BeanUtils.isNotEmpty(dateValue);
}
if (OP_CONTAINS.equals(op)) {
return subRowDateRegionCalc(dateValue, value);
}
int diff = dateValue.compareTo(value);
return getSubRowCalcResult(diff, op);
}
private static boolean subRowDateRegionCalc(String dateValue, String dateRegion) {
if (StringUtil.isEmpty(dateRegion) || StringUtil.isEmpty(dateValue)) {
return false;
}
String[] split = dateRegion.split(StringPool.COMMA);
if (2 == split.length) {
return (dateValue.compareTo(split[0]) >= 0 && dateValue.compareTo(split[1]) <= 0);
}
throw new IllegalArgumentException("illegal date argument, the date region must contains one comma");
}
/**
* 1: ==
* 2: !=
* 3: >
* 4: <
* 7: >=
* 8: <=
* 9: belong to(value split by comma)
* */
private static boolean subRowNumberCalc(ObjectNode row, String field, String op, Object value) {
JsonNode fieldNode = row.get(field);
if (OP_ISNULL.equals(op)){
return BeanUtils.isEmpty(fieldNode);
}else if (OP_NOTNULL.equals(op)){
return BeanUtils.isNotEmpty(fieldNode);
}
BigDecimal decimalValue;
if (value instanceof BigDecimal) {
decimalValue = ((BigDecimal) value);
} else if (value instanceof Integer) {
decimalValue = BigDecimal.valueOf((Integer) value);
} else if (value instanceof Long) {
decimalValue = BigDecimal.valueOf(((Long) value));
} else if (value instanceof Double) {
decimalValue = BigDecimal.valueOf(((Double) value));
} else if (value instanceof String && OP_CONTAINS.equals(op)) {
return calcNumberContains(value.toString(), row.get(field));
} else {
throw new IllegalArgumentException("unknown value type");
}
// numberValue 可能为 TextNode 则直接读取数据
if (fieldNode instanceof TextNode ){
int diff = new BigDecimal(fieldNode.asInt()).compareTo(decimalValue);
return getSubRowCalcResult(diff, op);
}
// numberValue 可能为BigDecimal或Int
BigDecimal curValue;
try {
curValue = fieldNode.decimalValue();
} catch (Exception e) {
curValue = BigDecimal.ZERO;
}
int diff = curValue.compareTo(decimalValue);
return getSubRowCalcResult(diff, op);
}
private static boolean getSubRowCalcResult(int diff, String op) {
switch (op) {
case OP_EQUALS:
return diff == 0;
case OP_NOT_EQUALS:
return diff != 0;
case OP_GREAT_THAN:
return diff > 0;
case OP_LESS_THAN:
return diff < 0;
case OP_GREAT_EQUALS:
return diff >= 0;
case OP_LESS_EQUALS:
return diff <= 0;
default:
throw new IllegalArgumentException("unknown operation type");
}
}
private static boolean calcNumberContains(String valueStr, JsonNode node) {
String[] split = valueStr.split(StringPool.COMMA);
BigDecimal curValue;
try {
curValue = node.decimalValue();
} catch (Exception e) {
curValue = BigDecimal.ZERO;
}
for (String numberStr : split) {
BigDecimal decimal = BigDecimal.valueOf(Long.parseLong(numberStr));
if (curValue.compareTo(decimal) == 0) {
return true;
}
}
return false;
}
/**
* 1: ==
* 2: !=
* 9: contains(split by comma)
* */
private static boolean subRowStringCalc(ObjectNode row, String field, String op, String value) {
String fieldValue = JsonUtil.getString(row, field, StringPool.EMPTY);
switch (op) {
case OP_EQUALS:
return fieldValue.equals(value);
case OP_NOT_EQUALS:
return !fieldValue.equals(value);
case OP_CONTAINS:
String[] split = value.split(StringPool.COMMA);
for (String s : split) {
if (fieldValue.equals(s)) {
return true;
}
}
return false;
case OP_ISNULL:
return StringUtil.isEmpty(fieldValue);
case OP_NOTNULL:
return StringUtil.isNotEmpty(fieldValue);
default:
throw new IllegalArgumentException("unknown operation type");
}
}
}