Explorar el Código

向下兼容-兼容数据中台通用查询参数,并将数据中台查询参数转换为MyBatisPlus查询条件

yucheng hace 3 años
padre
commit
2c2f8a30e8

+ 2 - 0
dc-business/dc-digital-version/src/main/java/com/persagy/dc/dictionary/service/impl/ObjectInfoHistoryServiceImpl.java

@@ -7,6 +7,7 @@ import com.persagy.dc.dictionary.dao.ObjectInfoHistoryMapper;
 import com.persagy.dc.dictionary.entity.ObjectInfoHistory;
 import com.persagy.dc.dictionary.service.IObjectInfoHistoryService;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
 
@@ -16,6 +17,7 @@ import javax.annotation.Resource;
  * @date 2021-06-23
  */
 @Service
+@Transactional(rollbackFor = Exception.class)
 public class ObjectInfoHistoryServiceImpl implements IObjectInfoHistoryService {
 
     @Resource

+ 2 - 0
dc-business/dc-digital-version/src/main/java/com/persagy/dc/dictionary/service/impl/ObjectTypeHistoryServiceImpl.java

@@ -7,6 +7,7 @@ import com.persagy.dc.dictionary.dao.ObjectTypeHistoryMapper;
 import com.persagy.dc.dictionary.entity.ObjectTypeHistory;
 import com.persagy.dc.dictionary.service.IObjectTypeHistoryService;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
 
@@ -16,6 +17,7 @@ import javax.annotation.Resource;
  * @date 2021-06-23
  */
 @Service
+@Transactional(rollbackFor = Exception.class)
 public class ObjectTypeHistoryServiceImpl implements IObjectTypeHistoryService {
 
     @Resource

+ 2 - 0
dc-business/dc-digital-version/src/main/java/com/persagy/dc/dictionary/service/impl/VersionDefineServiceImpl.java

@@ -12,6 +12,7 @@ import com.persagy.dc.dictionary.service.IObjectTypeHistoryService;
 import com.persagy.dc.dictionary.service.IVersionDefineService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
 import java.util.Arrays;
@@ -23,6 +24,7 @@ import java.util.List;
  * @date 2021-06-23
  */
 @Service
+@Transactional(rollbackFor = Exception.class)
 public class VersionDefineServiceImpl implements IVersionDefineService {
 
     @Resource

+ 82 - 0
dc-business/dc-digital/src/main/java/com/persagy/dc/common/model/JsonNodeUtils.java

@@ -0,0 +1,82 @@
+package com.persagy.dc.common.model;
+
+import com.fasterxml.jackson.databind.JsonNode;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * JsonNode 工具
+ * @author Charlie Yu
+ * @date 2021-06-25
+ */
+public class JsonNodeUtils {
+
+    /**
+     * 获取Node值
+     * @param node 值节点
+     * @return
+     */
+    public static Object getNodeValue(JsonNode node) {
+        if(node.isContainerNode() || node.isNull()) {
+            return "0";
+        }
+        if(node.isBigDecimal()) {
+            return node.decimalValue();
+        }
+        if(node.isBigInteger()) {
+            return node.bigIntegerValue();
+        }
+        if(node.isBoolean()) {
+            return node.booleanValue();
+        }
+        if(node.isBinary()) {
+            try {
+                return node.binaryValue();
+            } catch (IOException e) {
+                return null;
+            }
+        }
+        if(node.isDouble()) {
+            return node.doubleValue();
+        }
+        if(node.isFloat()) {
+            return node.floatValue();
+        }
+        if(node.isInt()) {
+            return node.intValue();
+        }
+        if(node.isLong()) {
+            return node.longValue();
+        }
+        if(node.isNumber()) {
+            return node.numberValue();
+        }
+        if(node.isShort()) {
+            return node.shortValue();
+        }
+        if(node.isTextual()) {
+            return node.textValue();
+        }
+        return "0";
+    }
+
+    /**
+     * 获取列表值
+     * @param node 列表值类型节点
+     * @return
+     */
+    public static List getListValue(JsonNode node) {
+        if(!node.isArray()) {
+            return null;
+        }
+        List values = new ArrayList<>(node.size());
+        Iterator<JsonNode> elements = node.elements();
+        while (elements.hasNext()) {
+            values.add(getNodeValue(elements.next()));
+        }
+        return values;
+    }
+}

+ 155 - 0
dc-business/dc-digital/src/main/java/com/persagy/dc/common/model/QueryCriteria.java

@@ -0,0 +1,155 @@
+package com.persagy.dc.common.model;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.OrderItem;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.*;
+import lombok.Data;
+import org.apache.poi.ss.formula.functions.T;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * 通用查询参数 - 仅用于接收参数
+ * 兼容数据中台查询规则,将数据中台查询条件转换为MyBatisPlus查询条件
+ * <pre>
+ * {
+ *     "page": 1,
+ *     "size": 10,
+ *     "onlyCount": false,
+ *     "criteria": {
+ *         "name": "zhangsan",
+ *         "name": [ "zhangsan", "lisi" ],
+ *         "age": {
+ *             "$gt": 18,
+ *             "$lt": 35
+ *         },
+ *         "gender": {
+ *             "$null": false
+ *         }
+ *     },
+ *     "orders": [
+ *            {
+ *     		 	"column": "name",
+ *     		 	"asc": false
+ *            }
+ *     ],
+ *     "withColumns": [
+ *     		"name", "age"
+ *     ]
+ * }
+ * </pre>
+ * @author yaoll
+ * @date 2020-09-04
+ */
+@Data
+public class QueryCriteria {
+
+    /** 分页页码,从 1 开始 */
+    private Long page;
+    /** 每页行数 */
+    private Long size;
+    /** 是否只统计总数 */
+    private boolean onlyCount = false;
+    /** 是否不统计总数 */
+    private boolean withoutCount = false;
+    /** 查询条件 */
+    private ObjectNode criteria;
+    /** 排序条件 */
+    private List<OrderItem> orders;
+    /** 额外返回的扩展字段清单 */
+    private Set<String> withColumns;
+
+    /**
+     * 转换为Page分页信息
+     * @return
+     */
+    public Page toPage() {
+        // 当前页默认为1
+        if(page == null || page < 1) {
+            page = 1L;
+        }
+        // 没有每页行数表示不分页
+        if(size == null || size < 1) {
+            size = Long.MAX_VALUE;
+        }
+        Page pageInfo = new Page();
+        pageInfo.setCurrent(page);
+        pageInfo.setSize(size);
+        pageInfo.setOrders(orders);
+        pageInfo.setSearchCount(!withoutCount);
+        return pageInfo;
+    }
+
+    /**
+     * 转换为查询条件
+     * @param wrapper
+     */
+    public void toWrapper(QueryWrapper<T> wrapper) {
+        if(criteria == null || criteria.isEmpty(null)) {
+            return;
+        }
+        Iterator<String> fields = criteria.fieldNames();
+        // 按字段循环
+        while(fields.hasNext()) {
+            String field = fields.next();
+            JsonNode fieldValue = criteria.get(field);
+            // 值节点
+            if(fieldValue.isValueNode()) {
+                wrapper.eq(field, JsonNodeUtils.getNodeValue(fieldValue));
+                continue;
+            }
+            // 数组
+            if(fieldValue.isArray()) {
+                wrapper.in(field, JsonNodeUtils.getListValue(fieldValue));
+                continue;
+            }
+            // 对象
+            if(fieldValue.isObject()) {
+                Iterator<String> valueOperators = fieldValue.fieldNames();
+                while(valueOperators.hasNext()) {
+                    String valueOperator = valueOperators.next();
+                    JsonNode operatorValue = criteria.get(field);
+                    addCondition(wrapper, field, valueOperator,operatorValue);
+                }
+            }
+        }
+    }
+
+    /**
+     * 按条件类型添加查询条件
+     * @param wrapper
+     * @param field
+     * @param operator
+     * @param valueNode
+     */
+    private void addCondition(QueryWrapper<T> wrapper, String field, String operator, JsonNode valueNode) {
+        if("$ne".equalsIgnoreCase(operator)) {
+            wrapper.ne(field, JsonNodeUtils.getNodeValue(valueNode));
+        } else if("$gt".equalsIgnoreCase(operator)) {
+            wrapper.gt(field, JsonNodeUtils.getNodeValue(valueNode));
+        } else if("$gte".equalsIgnoreCase(operator)) {
+            wrapper.ge(field, JsonNodeUtils.getNodeValue(valueNode));
+        } else if("$lt".equalsIgnoreCase(operator)) {
+            wrapper.lt(field, JsonNodeUtils.getNodeValue(valueNode));
+        } else if("$lte".equalsIgnoreCase(operator)) {
+            wrapper.le(field, JsonNodeUtils.getNodeValue(valueNode));
+        } else if("$in".equalsIgnoreCase(operator)) {
+            wrapper.in(field, JsonNodeUtils.getListValue(valueNode));
+        } else if("$like".equalsIgnoreCase(operator)) {
+            wrapper.like(field, JsonNodeUtils.getNodeValue(valueNode));
+        } else if("$notLike".equalsIgnoreCase(operator)) {
+            wrapper.notLike(field, JsonNodeUtils.getNodeValue(valueNode));
+        } else if("$null".equalsIgnoreCase(operator)) {
+            boolean nullFlag = valueNode.booleanValue();
+            if(nullFlag) {
+                wrapper.isNull(field);
+            } else {
+                wrapper.isNotNull(field);
+            }
+        }
+    }
+}