|
@@ -1,12 +1,15 @@
|
|
|
package com.persagy.dmp.rwd.digital.utils;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
|
import cn.hutool.core.util.ArrayUtil;
|
|
|
import cn.hutool.core.util.ReflectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.OrderItem;
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.fasterxml.jackson.databind.node.ArrayNode;
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
+import com.persagy.dmp.basic.model.QueryCriteria;
|
|
|
import com.persagy.dmp.basic.model.QueryOperator;
|
|
|
import com.persagy.dmp.basic.utils.JsonNodeUtils;
|
|
|
import com.persagy.dmp.common.model.entity.BaseEntity;
|
|
@@ -26,11 +29,42 @@ public class ObjectDigitalCriteriaHelper {
|
|
|
private static final Set<String> DIGITAL_FIELDS = ReflectUtil.getFieldMap(ObjectDigital.class).keySet();
|
|
|
|
|
|
/**
|
|
|
+ * 对象查询条件预处理:主要处理infos扩展内容
|
|
|
+ * @param criteria
|
|
|
+ */
|
|
|
+ public static void presetDigitalCriteria(QueryCriteria criteria) {
|
|
|
+ // 处理查询条件
|
|
|
+ ensureExtraCond(criteria.getCriteria());
|
|
|
+ // 处理排序条件
|
|
|
+ ensureOrders(criteria.getOrders());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理排序条件
|
|
|
+ * @param orders
|
|
|
+ */
|
|
|
+ private static void ensureOrders(List<OrderItem> orders) {
|
|
|
+ if(CollUtil.isEmpty(orders)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for(OrderItem order:orders) {
|
|
|
+ String column = order.getColumn();
|
|
|
+ // 基础属性不用处理
|
|
|
+ if(StrUtil.isBlank(column) || DIGITAL_FIELDS.contains(column)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 非基础属性转json
|
|
|
+ column = StrUtil.format("#JSON#json_extract({}, '$.{}')", ObjectDigital.EXTRA_COLUMN, column);
|
|
|
+ order.setColumn(column);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 拼接扩展条件
|
|
|
* 暂用#JSON#表示是json条件,不处理驼峰
|
|
|
* @param criteria
|
|
|
*/
|
|
|
- public static void ensureExtraCond(ObjectNode criteria) {
|
|
|
+ private static void ensureExtraCond(ObjectNode criteria) {
|
|
|
if(criteria == null || criteria.isNull()) {
|
|
|
return;
|
|
|
}
|
|
@@ -196,33 +230,55 @@ public class ObjectDigitalCriteriaHelper {
|
|
|
return;
|
|
|
}
|
|
|
// 处理relationFrom
|
|
|
- JsonNode relationFrom = criteria.get(QueryOperator.RELATION_FROM.getIndex());
|
|
|
- if(relationFrom != null && relationFrom.isObject() && relationFrom.size() > 0) {
|
|
|
- StringBuilder inSql = new StringBuilder("(select obj_from from dt_relation where 1=1 ");
|
|
|
- addWhereCond(relationFrom, inSql, "graphId");
|
|
|
- addWhereCond(relationFrom, inSql, "graphCode");
|
|
|
- addWhereCond(relationFrom, inSql, "relCode");
|
|
|
- addWhereCond(relationFrom, inSql, "objTo");
|
|
|
- inSql.append(")");
|
|
|
- ObjectNode inObj = criteria.putObject(BaseEntity.PROP_ID);
|
|
|
- inObj.put(QueryOperator.IN_SQL.getIndex(), inSql.toString());
|
|
|
- // 处理完成后删除
|
|
|
- criteria.remove(QueryOperator.RELATION_FROM.getIndex());
|
|
|
- }
|
|
|
+ ensureRelationIds(criteria, true);
|
|
|
// 处理relationTo
|
|
|
- JsonNode relationTo = criteria.get(QueryOperator.RELATION_TO.getIndex());
|
|
|
- if(relationTo != null && relationTo.isObject() && relationTo.size() > 0) {
|
|
|
- StringBuilder inSql = new StringBuilder("(select obj_to from dt_relation where 1=1 ");
|
|
|
- addWhereCond(relationTo, inSql, "graphId");
|
|
|
- addWhereCond(relationTo, inSql, "graphCode");
|
|
|
- addWhereCond(relationTo, inSql, "relCode");
|
|
|
- addWhereCond(relationTo, inSql, "objFrom");
|
|
|
- inSql.append(")");
|
|
|
- ObjectNode inObj = criteria.putObject(BaseEntity.PROP_ID);
|
|
|
- inObj.put(QueryOperator.IN_SQL.getIndex(), inSql.toString());
|
|
|
- // 处理完成后删除
|
|
|
- criteria.remove(QueryOperator.RELATION_TO.getIndex());
|
|
|
+ ensureRelationIds(criteria, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理关系条件
|
|
|
+ * @param criteria
|
|
|
+ * @param isFrom
|
|
|
+ */
|
|
|
+ private static void ensureRelationIds(ObjectNode criteria, boolean isFrom) {
|
|
|
+ String flagKey = isFrom ? QueryOperator.RELATION_FROM.getIndex() : QueryOperator.RELATION_TO.getIndex();
|
|
|
+ String selectKey = isFrom ? "objFrom" : "objTo";
|
|
|
+ String whereKey = isFrom ? "objTo" : "objFrom";
|
|
|
+ JsonNode relationCond = criteria.get(flagKey);
|
|
|
+ // 没有条件不需要处理
|
|
|
+ if(relationCond == null || relationCond.size() <= 0 || !relationCond.isObject()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ /** 20210827 relation条件支持复杂条件 - 使用Wrapper方式只能先查询出来,可以改为拼接sql提升效率,但需要大量扩展,不便于维护。
|
|
|
+ // 查询关系的条件
|
|
|
+ QueryWrapper<ObjectRelation> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.select(QueryCriteriaHelper.toUnderlineCase(selectKey));
|
|
|
+ // 添加所属项目条件
|
|
|
+ ConditionUtil.ensureProjectCriteria(wrapper);
|
|
|
+ QueryCriteriaHelper.toWrapper(wrapper, (ObjectNode) relationCond);
|
|
|
+ List<ObjectRelation> relations = SpringHelper.getBean(IObjectRelationService.class).queryByCondition(wrapper);
|
|
|
+ List<String> idList = CollUtil.getFieldValues(relations, selectKey, String.class);
|
|
|
+ // 回写到criteria中
|
|
|
+ ObjectNode inObj = criteria.putObject(BaseEntity.PROP_ID);
|
|
|
+ ArrayNode inArray = inObj.putArray(QueryOperator.IN.getIndex());
|
|
|
+ if(CollUtil.isEmpty(idList)) {
|
|
|
+ inArray.add("0");
|
|
|
+ } else {
|
|
|
+ idList.forEach(id -> inArray.add(id));
|
|
|
}
|
|
|
+ // 处理完成后删除
|
|
|
+ criteria.remove(flagKey);
|
|
|
+ **/
|
|
|
+ StringBuilder inSql = new StringBuilder("(select "+selectKey+" from dt_relation where 1=1 ");
|
|
|
+ addWhereCond(relationCond, inSql, "graphId");
|
|
|
+ addWhereCond(relationCond, inSql, "graphCode");
|
|
|
+ addWhereCond(relationCond, inSql, "relCode");
|
|
|
+ addWhereCond(relationCond, inSql, whereKey);
|
|
|
+ inSql.append(")");
|
|
|
+ ObjectNode inObj = criteria.putObject(BaseEntity.PROP_ID);
|
|
|
+ inObj.put(QueryOperator.IN_SQL.getIndex(), inSql.toString());
|
|
|
+ // 处理完成后删除
|
|
|
+ criteria.remove(flagKey);
|
|
|
}
|
|
|
|
|
|
/**
|