소스 검색

公共 - 查询返回指定属性结果

yucheng 3 년 전
부모
커밋
58aaafcb32

+ 1 - 1
src/main/java/com/persagy/proxy/adm/request/AdmResponse.java

@@ -58,7 +58,7 @@ public class AdmResponse {
             return failure("调用中台查询接口出错:" + dmpResult.getMessage());
         }
         // 转换为结果
-        List<T> admVOs = AdmEntityTransferUtil.toAdmMultiEntity(dmpResult.getData(), clazz);
+        List<T> admVOs = AdmEntityTransferUtil.toAdmMultiEntity(dmpResult.getData(), admRequest.getProjection(), clazz);
         Long total = dmpResult.getCount() == null ? null : dmpResult.getCount().longValue();
         Integer pageNo = admRequest == null ? null : admRequest.getPageNumber();
         Integer pageSize = admRequest == null ? null : admRequest.getPageSize();

+ 2 - 17
src/main/java/com/persagy/proxy/adm/service/impl/AbstractAdmBaseServiceImpl.java

@@ -55,21 +55,6 @@ public class AbstractAdmBaseServiceImpl<T> implements IAdmBaseService<T> {
     }
 
     /**
-     * 拷贝属性
-     * @param dmpData 数据中台对象
-     * @param srcProp 数据中台属性名
-     * @param desProp 对应的数据中心属性名
-     */
-    private void copyProperty(JSONObject dmpData, String srcProp, String desProp) {
-        Object value = dmpData.get(srcProp);
-        if(value == null) {
-            return;
-        }
-        dmpData.put(desProp, value);
-        dmpData.remove(srcProp);
-    }
-
-    /**
      * 新增
      * @param context
      * @param clazz
@@ -88,7 +73,7 @@ public class AbstractAdmBaseServiceImpl<T> implements IAdmBaseService<T> {
             throw new RuntimeException("调用中台查询接口出错:" + response.getMessage());
         }
         // 转换为结果
-        return AdmEntityTransferUtil.toAdmMultiEntity(response.getData(), clazz);
+        return AdmEntityTransferUtil.toAdmMultiEntity(response.getData(), null, clazz);
     }
 
     /**
@@ -110,7 +95,7 @@ public class AbstractAdmBaseServiceImpl<T> implements IAdmBaseService<T> {
             throw new RuntimeException("调用中台查询接口出错:" + response.getMessage());
         }
         // 转换为结果
-        return AdmEntityTransferUtil.toAdmMultiEntity(response.getData(), clazz);
+        return AdmEntityTransferUtil.toAdmMultiEntity(response.getData(), null, clazz);
     }
 
     /**

+ 40 - 18
src/main/java/com/persagy/proxy/adm/utils/AdmEntityTransferUtil.java

@@ -1,12 +1,15 @@
 package com.persagy.proxy.adm.utils;
 
+import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.ArrayUtil;
 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.utils.JsonNodeUtils;
 
+import java.util.Iterator;
 import java.util.List;
+import java.util.Set;
 
 /**
  * ADM通用查询条件
@@ -40,9 +43,9 @@ public class AdmEntityTransferUtil {
      * @param <T>
      * @return
      */
-    public static <T> List<T> toAdmMultiEntity(List<Object> array, Class<T> clazz) {
+    public static <T> List<T> toAdmMultiEntity(List<Object> array, List<String> projection, Class<T> clazz) {
         ArrayNode dataNode = JsonNodeUtils.toArrayNode(array, null, null);
-        return toAdmMultiEntity(dataNode, clazz);
+        return toAdmMultiEntity(dataNode, projection, clazz);
     }
 
     /**
@@ -52,18 +55,15 @@ public class AdmEntityTransferUtil {
      * @param <T>
      * @return
      */
-    public static <T> List<T> toAdmMultiEntity(ArrayNode array, Class<T> clazz) {
+    public static <T> List<T> toAdmMultiEntity(ArrayNode array, List<String> projection, Class<T> clazz) {
         if(ArrayUtil.isEmpty(array)) {
             return null;
         }
-        // 转换属性名 TODO 目前只有创建时间和修改时间需要转换,后续有其他时通过建立映射在此转换即可
+        // 转换属性名
         for(int i = 0, j = array.size();i < j;i++) {
             JsonNode node = array.get(i);
             if(node != null && node.isObject()) {
-                copyProperty((ObjectNode) node, "creationTime", "createTime");
-                copyProperty((ObjectNode) node, "modifiedTime", "lastUpdate");
-                copyProperty((ObjectNode) node, "objType", "objectType");
-                removeProperty((ObjectNode)node);
+                resetAdmProperty((ObjectNode) node, projection);
             }
         }
         return JsonNodeUtils.toEntity(array, clazz, "infos");
@@ -76,13 +76,21 @@ public class AdmEntityTransferUtil {
      * @param <T>
      * @return
      */
-    public static <T> T toAdmSingleEntity(ObjectNode node, Class<T> clazz) {
+    public static <T> T toAdmSingleEntity(ObjectNode node, List<String> projection, Class<T> clazz) {
+        resetAdmProperty(node, projection);
+        return JsonNodeUtils.toEntity(node, clazz, "infos");
+    }
+
+    /**
+     * 处理dmp转adm对象的属性名
+     * @param node
+     */
+    private static void resetAdmProperty(ObjectNode node, List<String> projection) {
         // 转换属性名 TODO 目前只有创建时间和修改时间需要转换,后续有其他时通过建立映射在此转换即可
         copyProperty(node, "creationTime", "createTime");
         copyProperty(node, "modifiedTime", "lastUpdate");
         copyProperty(node, "objectType", "objType");
-        removeProperty(node);
-        return JsonNodeUtils.toEntity(node, clazz, "infos");
+        removeProperty(node, projection);
     }
 
     /**
@@ -107,12 +115,26 @@ public class AdmEntityTransferUtil {
      * 删除掉数据中心不需要的属性
      * @param dmpData
      */
-    private static void removeProperty(ObjectNode dmpData){
-        dmpData.remove( "creator");
-        dmpData.remove( "groupCode");
-        dmpData.remove( "valid");
-        dmpData.remove( "createApp");
-        dmpData.remove( "virtualCodes");
-        dmpData.remove( "updateApp");
+    private static void removeProperty(ObjectNode dmpData, List<String> projection){
+        if(CollUtil.isNotEmpty(projection)) {
+            // 转换为Set,提供遍历效率
+            Set<String> fieldSet = CollUtil.newHashSet(projection);
+            Iterator<String> fields =  dmpData.fieldNames();
+            while (fields.hasNext()) {
+                String field = fields.next();
+                // 返回结果中包含的不处理
+                if(fieldSet.contains(field)) {
+                    continue;
+                }
+                // 去掉不包含的属性
+                fields.remove();
+            }
+        } else {
+            // 返回全部时,去掉不需要属性
+            List<String> removeList = CollUtil.newArrayList("creator", "groupCode", "valid", "createApp", "virtualCodes", "updateApp");
+            for(String removeField:removeList) {
+                dmpData.remove(removeField);
+            }
+        }
     }
 }

+ 1 - 3
src/main/java/com/persagy/proxy/adm/utils/AdmQueryCriteriaHelper.java

@@ -3,7 +3,6 @@ package com.persagy.proxy.adm.utils;
 import cn.hutool.core.map.MapUtil;
 import cn.hutool.core.util.ArrayUtil;
 import cn.hutool.core.util.StrUtil;
-import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.node.ArrayNode;
 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
@@ -61,7 +60,7 @@ public class AdmQueryCriteriaHelper {
         copyPageInfo(admCriteria, dmpCriteria);
         // 处理filters
         dmpCriteria.setCriteria(toCriteria(admCriteria.getName(), admCriteria.getFilters()));
-        if(StringUtils.isNotEmpty(admCriteria.getName())){
+        if(StrUtil.isNotEmpty(admCriteria.getName())){
             // 处理name
             dmpCriteria.getCriteria().put("objType", admCriteria.getName());
         }
@@ -187,7 +186,6 @@ public class AdmQueryCriteriaHelper {
      * @param express
      */
     private static void ensureAndExpress(String name, ObjectNode criteria, String express) {
-
         String cond = StrUtil.getContainsStr(express, COND_STR);
         if(StrUtil.isBlank(cond)) {
             return;