|
| 1 | +package cn.veasion.db.criteria; |
| 2 | + |
| 3 | +import cn.veasion.db.DbException; |
| 4 | +import cn.veasion.db.FilterException; |
| 5 | +import cn.veasion.db.base.Filter; |
| 6 | +import cn.veasion.db.base.Operator; |
| 7 | +import cn.veasion.db.query.EQ; |
| 8 | +import cn.veasion.db.query.EntityQuery; |
| 9 | +import cn.veasion.db.query.JoinQueryParam; |
| 10 | +import cn.veasion.db.utils.FieldUtils; |
| 11 | + |
| 12 | +import java.lang.reflect.Field; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.Collection; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.HashSet; |
| 17 | +import java.util.Iterator; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.Objects; |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +/** |
| 23 | + * QueryCriteriaConvert |
| 24 | + * |
| 25 | + * @author luozhuowei |
| 26 | + * @date 2021/12/15 |
| 27 | + */ |
| 28 | +public class QueryCriteriaConvert { |
| 29 | + |
| 30 | + private Object object; |
| 31 | + private EntityQuery query; |
| 32 | + private JoinCriteria[] array; |
| 33 | + private Set<Class<?>> joined = new HashSet<>(); |
| 34 | + private Map<Class<?>, EntityQuery> joinClassMap = new HashMap<>(); |
| 35 | + |
| 36 | + public QueryCriteriaConvert(Object object) { |
| 37 | + this(object, null); |
| 38 | + } |
| 39 | + |
| 40 | + public QueryCriteriaConvert(Object object, Class<?> entityClass) { |
| 41 | + Objects.requireNonNull(object, "参数不能为空"); |
| 42 | + this.object = object; |
| 43 | + this.query = new EQ(entityClass, "t"); |
| 44 | + this.handleFilters(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * 获取查询对象 |
| 49 | + */ |
| 50 | + public EntityQuery getEntityQuery() { |
| 51 | + return this.query; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * 判断类型是否存在关联 |
| 56 | + */ |
| 57 | + public boolean hasJoin(Class<?> clazz) { |
| 58 | + return joined.contains(clazz); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * 获取关联类型查询对象 |
| 63 | + */ |
| 64 | + public EntityQuery getJoinEntityQuery(Class<?> clazz) { |
| 65 | + return joinClassMap.get(clazz); |
| 66 | + } |
| 67 | + |
| 68 | + public Map<Class<?>, EntityQuery> getJoinClassMap() { |
| 69 | + return joinClassMap; |
| 70 | + } |
| 71 | + |
| 72 | + private void handleFilters() { |
| 73 | + JoinCriteriaMulti joinCriteriaMulti = object.getClass().getAnnotation(JoinCriteriaMulti.class); |
| 74 | + if (joinCriteriaMulti != null) { |
| 75 | + array = joinCriteriaMulti.value(); |
| 76 | + } |
| 77 | + initJoinClassMap(); |
| 78 | + Map<String, Field> fields = FieldUtils.fields(object.getClass()); |
| 79 | + for (Field field : fields.values()) { |
| 80 | + QueryCriteria annotation = field.getAnnotation(QueryCriteria.class); |
| 81 | + if (annotation == null) { |
| 82 | + continue; |
| 83 | + } |
| 84 | + Object value = FieldUtils.getValue(object, field.getName(), true); |
| 85 | + if (value == null) { |
| 86 | + continue; |
| 87 | + } |
| 88 | + boolean skipEmpty = annotation.skipEmpty(); |
| 89 | + if (skipEmpty) { |
| 90 | + if (value instanceof String && "".equals(value)) { |
| 91 | + continue; |
| 92 | + } else if (value instanceof Collection && ((Collection<?>) value).isEmpty()) { |
| 93 | + continue; |
| 94 | + } else if (value instanceof Object[] && ((Object[]) value).length == 0) { |
| 95 | + continue; |
| 96 | + } |
| 97 | + } |
| 98 | + String fieldName = "".equals(annotation.field()) ? field.getName() : annotation.field(); |
| 99 | + Operator operator = annotation.value(); |
| 100 | + Class<?> relationClass = annotation.relation(); |
| 101 | + |
| 102 | + if (relationClass != Void.class) { |
| 103 | + checkJoin(relationClass); |
| 104 | + } |
| 105 | + |
| 106 | + String[] orFields = annotation.orFields(); |
| 107 | + if (orFields.length > 0) { |
| 108 | + query.addFilter(Filter.leftBracket()); |
| 109 | + for (int i = 0; i < orFields.length; i++) { |
| 110 | + Filter filter = getFilter(orFields[i], operator, value); |
| 111 | + if (relationClass != Void.class) { |
| 112 | + filter.fieldAs(joinClassMap.get(relationClass).getTableAs()); |
| 113 | + } |
| 114 | + query.addFilter(filter); |
| 115 | + if (i < orFields.length - 1) { |
| 116 | + query.addFilter(Filter.or()); |
| 117 | + } |
| 118 | + } |
| 119 | + query.addFilter(Filter.rightBracket()); |
| 120 | + } else { |
| 121 | + Filter filter = getFilter(fieldName, operator, value); |
| 122 | + if (relationClass != Void.class) { |
| 123 | + filter.fieldAs(joinClassMap.get(relationClass).getTableAs()); |
| 124 | + } |
| 125 | + query.addFilter(filter); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private void initJoinClassMap() { |
| 131 | + if (array == null || array.length == 0) return; |
| 132 | + for (JoinCriteria joinCriteria : array) { |
| 133 | + Class<?> join = joinCriteria.join(); |
| 134 | + if (join != Void.class) { |
| 135 | + joinClassMap.put(join, new EQ(join, FieldUtils.firstCase(join.getSimpleName(), true))); |
| 136 | + } |
| 137 | + } |
| 138 | + joinClassMap.put(Void.class, query); |
| 139 | + for (JoinCriteria joinCriteria : array) { |
| 140 | + if (joinCriteria.staticJoin()) { |
| 141 | + checkJoin(joinCriteria.join()); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private void checkJoin(Class<?> joinClass) { |
| 147 | + if (joined.contains(joinClass)) { |
| 148 | + return; |
| 149 | + } |
| 150 | + if (array == null || array.length == 0) { |
| 151 | + throw new DbException("@JoinCriteriaMulti 中关联类未找到:" + joinClass.getSimpleName()); |
| 152 | + } |
| 153 | + Set<JoinCriteria> joinCriteriaSet = new HashSet<>(Arrays.asList(array)); |
| 154 | + do { |
| 155 | + for (JoinCriteria joinCriteria : joinCriteriaSet) { |
| 156 | + if (joinClass == joinCriteria.join()) { |
| 157 | + EntityQuery main = joinClassMap.get(joinCriteria.value()); |
| 158 | + EntityQuery join = joinClassMap.get(joinCriteria.join()); |
| 159 | + JoinQueryParam joinQueryParam = main.join(joinCriteria.joinType(), join); |
| 160 | + joined.add(joinClass); |
| 161 | + String[] onFields = joinCriteria.onFields(); |
| 162 | + for (int i = 0; i < onFields.length; i += 2) { |
| 163 | + joinQueryParam.on(onFields[i], onFields[i + 1]); |
| 164 | + } |
| 165 | + joinClass = joinCriteria.value(); |
| 166 | + joinCriteriaSet.remove(joinCriteria); |
| 167 | + break; |
| 168 | + } |
| 169 | + } |
| 170 | + } while (joinClass != Void.class && !joined.contains(joinClass)); |
| 171 | + } |
| 172 | + |
| 173 | + public static Filter getFilter(String field, Operator operator, Object value) { |
| 174 | + if (Operator.EQ.equals(operator)) { |
| 175 | + return Filter.eq(field, value); |
| 176 | + } else if (Operator.NEQ.equals(operator)) { |
| 177 | + return Filter.neq(field, value); |
| 178 | + } else if (Operator.GT.equals(operator)) { |
| 179 | + return Filter.gt(field, value); |
| 180 | + } else if (Operator.GTE.equals(operator)) { |
| 181 | + return Filter.gte(field, value); |
| 182 | + } else if (Operator.LT.equals(operator)) { |
| 183 | + return Filter.lt(field, value); |
| 184 | + } else if (Operator.LTE.equals(operator)) { |
| 185 | + return Filter.lte(field, value); |
| 186 | + } else if (Operator.IN.equals(operator)) { |
| 187 | + if (value instanceof Collection) { |
| 188 | + return Filter.in(field, (Collection<?>) value); |
| 189 | + } else if (value instanceof Object[]) { |
| 190 | + return Filter.in(field, (Object[]) value); |
| 191 | + } else { |
| 192 | + throw new FilterException(field + " 字段 Operator.IN 类型必须是集合或者数组"); |
| 193 | + } |
| 194 | + } else if (Operator.NOT_IN.equals(operator)) { |
| 195 | + if (value instanceof Collection) { |
| 196 | + return Filter.notIn(field, (Collection<?>) value); |
| 197 | + } else if (value instanceof Object[]) { |
| 198 | + return Filter.notIn(field, (Object[]) value); |
| 199 | + } else { |
| 200 | + throw new FilterException(field + " 字段 Operator.IN 类型必须是集合或者数组"); |
| 201 | + } |
| 202 | + } else if (Operator.LIKE.equals(operator)) { |
| 203 | + return Filter.like(field, value); |
| 204 | + } else if (Operator.BETWEEN.equals(operator)) { |
| 205 | + if (value instanceof Collection) { |
| 206 | + Iterator<?> iterator = ((Collection<?>) value).iterator(); |
| 207 | + return Filter.between(field, iterator.next(), iterator.next()); |
| 208 | + } else if (value instanceof Object[]) { |
| 209 | + Object[] objects = (Object[]) value; |
| 210 | + return Filter.between(field, objects[0], objects[1]); |
| 211 | + } else { |
| 212 | + throw new FilterException(field + " 字段 Operator.BETWEEN 类型必须是集合或者数组"); |
| 213 | + } |
| 214 | + } else if (Operator.NULL.equals(operator)) { |
| 215 | + return Filter.isNull(field); |
| 216 | + } else if (Operator.NOT_NULL.equals(operator)) { |
| 217 | + return Filter.isNotNull(field); |
| 218 | + } else { |
| 219 | + throw new FilterException(field + " 不支持 Operator." + operator.name()); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | +} |
0 commit comments