Browse Source

解决属性排序的问题

lixing 3 years ago
parent
commit
3f21352ffe

+ 38 - 1
src/main/java/com/persagy/apm/report/common/utils/DataUtils.java

@@ -198,7 +198,7 @@ public class DataUtils {
      * 根据自定义规则对列表排序
      * 如果存在多个要排序的字段,排序方式同数据库排序方式
      *
-     * @param list 要排序的列表
+     * @param list          要排序的列表
      * @param comparatorMap 要排序的属性 -> 正序/倒序(-1:从高到低,1: 从低到高)
      * @author lixing
      * @version V1.0 2021/7/29 9:52 上午
@@ -335,4 +335,41 @@ public class DataUtils {
             }
         };
     }
+
+    /**
+     * double值保留指定位数的小数
+     *
+     * @param d1    要处理的double值
+     * @param scale 要保留的小数位数
+     * @return 保留指定小数位的数值字符串
+     * @author lixing
+     * @version V1.0 2021/9/1 10:07 下午
+     */
+    public static String doubleScale(Double d1, int scale) {
+        if (d1 == null) {
+            return null;
+        }
+        BigDecimal bigDecimal = new BigDecimal(d1);
+        return bigDecimal.setScale(scale, BigDecimal.ROUND_HALF_UP).toString();
+    }
+
+    public static void main(String[] args) {
+        List<Map<String, Double>> list = new ArrayList<>();
+        Map<String, Double> map1 = new HashMap<>();
+        map1.put("a", 1d);
+        map1.put("b", 2d);
+
+        Map<String, Double> map2 = new HashMap<>();
+        map2.put("a", 2d);
+        map2.put("b", 1d);
+
+        list.add(map1);
+        list.add(map2);
+
+        Map<String, Integer> sortMap = new HashMap<>();
+        sortMap.put("a", 1);
+        sort(list, sortMap);
+
+        System.out.println(list);
+    }
 }

+ 14 - 10
src/main/java/com/persagy/apm/report/paragraphs/service/impl/env/AreaEnvBuilder.java

@@ -79,29 +79,33 @@ public class AreaEnvBuilder extends AreaIndicatorsParagraphBuilder {
             projectCount = itemMapList.get(0).get(AreaEnvContentCodes.projectsCount.name());
 
             // 项目达标率最差的指标
-            List<Map<String, String>> worstMap = itemMapList.stream().filter(
+            List<Map<String, Object>> worstMap = itemMapList.stream().filter(
                     map -> map.get(AreaEnvContentCodes.unqualifiedCount.name()) != null).
-                    collect(Collectors.toList());
+                    map(map -> ParagraphUtils.convertMapMemberType(
+                            map, AreaEnvContentCodes.unqualifiedCount.name(), Integer.class)
+                    ).collect(Collectors.toList());
             if (!CollectionUtils.isEmpty(worstMap)) {
                 Map<String, Integer> worstSortMap = new HashMap<>();
                 worstSortMap.put(AreaEnvContentCodes.unqualifiedCount.name(), -1);
                 DataUtils.sort(worstMap, worstSortMap);
-                worstIndicator = worstMap.get(0).get(ItemAttrConstants.NAME);
-                worstIndicatorNoncomplianceProjectCount = worstMap.get(0).get(
-                        AreaEnvContentCodes.unqualifiedCount.name());
+                worstIndicator = (String) worstMap.get(0).get(ItemAttrConstants.NAME);
+                worstIndicatorNoncomplianceProjectCount = String.valueOf(
+                        worstMap.get(0).get(AreaEnvContentCodes.unqualifiedCount.name()));
             }
 
             // 项目达标率最好的指标
-            List<Map<String, String>> bestMap = itemMapList.stream().filter(
+            List<Map<String, Object>> bestMap = itemMapList.stream().filter(
                     map -> map.get(AreaEnvContentCodes.qualifiedCount.name()) != null).
-                    collect(Collectors.toList());
+                    map(map -> ParagraphUtils.convertMapMemberType(
+                            map, AreaEnvContentCodes.qualifiedCount.name(), Integer.class)
+                    ).collect(Collectors.toList());
             if (!CollectionUtils.isEmpty(bestMap)) {
                 Map<String, Integer> bestSortMap = new HashMap<>();
                 bestSortMap.put(AreaEnvContentCodes.qualifiedCount.name(), -1);
                 DataUtils.sort(bestMap, bestSortMap);
-                bestIndicator = bestMap.get(0).get(ItemAttrConstants.NAME);
-                bestIndicatorComplianceProjectCount = bestMap.get(0).get(
-                        AreaEnvContentCodes.qualifiedCount.name());
+                bestIndicator = (String) bestMap.get(0).get(ItemAttrConstants.NAME);
+                bestIndicatorComplianceProjectCount = String.valueOf(
+                        bestMap.get(0).get(AreaEnvContentCodes.qualifiedCount.name()));
             }
         }
 

+ 12 - 9
src/main/java/com/persagy/apm/report/paragraphs/service/impl/env/ProjectEnvBuilder.java

@@ -4,8 +4,6 @@ import com.alibaba.excel.util.CollectionUtils;
 import com.google.gson.Gson;
 import com.google.gson.JsonSyntaxException;
 import com.persagy.apm.report.common.utils.DataUtils;
-import com.persagy.apm.report.userconfig.function.model.Function;
-import com.persagy.apm.report.userconfig.function.service.IFunctionService;
 import com.persagy.apm.report.detail.enums.AttrValueTypeEnum;
 import com.persagy.apm.report.detail.model.vo.AttrValueVO;
 import com.persagy.apm.report.detail.model.vo.GroupVO;
@@ -19,6 +17,8 @@ import com.persagy.apm.report.outline.model.ReportOutline;
 import com.persagy.apm.report.paragraphs.constants.ItemAttrConstants;
 import com.persagy.apm.report.paragraphs.service.impl.ProjectParagraphBuilder;
 import com.persagy.apm.report.paragraphs.utils.ParagraphUtils;
+import com.persagy.apm.report.userconfig.function.model.Function;
+import com.persagy.apm.report.userconfig.function.service.IFunctionService;
 import org.mockito.internal.util.collections.Sets;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -73,26 +73,29 @@ public class ProjectEnvBuilder extends ProjectParagraphBuilder {
                     item -> ParagraphUtils.item2Map(item, attrCodeSet)).collect(Collectors.toList());
 
             // 报警次数最多的指标
-            List<Map<String, String>> alarmCountMap = itemMapList.stream().filter(
+            List<Map<String, Object>> alarmCountMap = itemMapList.stream().filter(
                     map -> map.get(ProjectEnvContentCodes.alarmCount.name()) != null).
-                    collect(Collectors.toList());
+                    map(map -> ParagraphUtils.convertMapMemberType(
+                            map, ProjectEnvContentCodes.alarmCount.name(), Integer.class)
+                    ).collect(Collectors.toList());
             if (!CollectionUtils.isEmpty(alarmCountMap)) {
                 Map<String, Integer> alarmCountSortMap = new HashMap<>();
                 alarmCountSortMap.put(ProjectEnvContentCodes.alarmCount.name(), -1);
                 DataUtils.sort(alarmCountMap, alarmCountSortMap);
-                maxAlarmIndicator = alarmCountMap.get(0).get(ItemAttrConstants.NAME);
+                maxAlarmIndicator = (String) alarmCountMap.get(0).get(ItemAttrConstants.NAME);
             }
 
-
             // 处理率最低的指标
-            List<Map<String, String>> handleProportionMap = itemMapList.stream().filter(
+            List<Map<String, Object>> handleProportionMap = itemMapList.stream().filter(
                     map -> map.get(ProjectEnvContentCodes.handleProportion.name()) != null).
-                    collect(Collectors.toList());
+                    map(map -> ParagraphUtils.convertMapMemberType(
+                            map, ProjectEnvContentCodes.handleProportion.name(), Double.class)
+                    ).collect(Collectors.toList());
             if (!CollectionUtils.isEmpty(handleProportionMap)) {
                 Map<String, Integer> handleProportionSortMap = new HashMap<>();
                 handleProportionSortMap.put(ProjectEnvContentCodes.handleProportion.name(), 1);
                 DataUtils.sort(handleProportionMap, handleProportionSortMap);
-                minHandleProportionIndicator = handleProportionMap.get(0).get(ItemAttrConstants.NAME);
+                minHandleProportionIndicator = (String) handleProportionMap.get(0).get(ItemAttrConstants.NAME);
             }
         }
 

+ 1 - 1
src/main/java/com/persagy/apm/report/paragraphs/service/impl/iot/ProjectIotBuilder.java

@@ -58,7 +58,7 @@ public class ProjectIotBuilder extends ProjectParagraphBuilder {
                 if (attrDoubleValue != null) {
                     attrDoubleValue = DataUtils.doubleMultiply(attrDoubleValue, 100d);
                 }
-                String attrValueStr = attrDoubleValue == null ? "--" : String.valueOf(attrDoubleValue);
+                String attrValueStr = attrDoubleValue == null ? "--" : DataUtils.doubleScale(attrDoubleValue, 2);
                 String isQualifiedStr = attrMap.get(ProjectIotContentCodes.isQualified.name()).getValue();
                 isQualifiedStr = isQualifiedStr == null ? "" : isQualifiedStr;
                 String itemInfo = name + ":" +

+ 31 - 1
src/main/java/com/persagy/apm/report/paragraphs/utils/ParagraphUtils.java

@@ -4,6 +4,7 @@ import com.persagy.apm.report.detail.model.vo.AttrValueVO;
 import com.persagy.apm.report.detail.model.vo.ItemVO;
 import com.persagy.apm.report.paragraphs.constants.ItemAttrConstants;
 import com.persagy.apm.report.paragraphs.service.impl.ParagraphBuilder;
+import org.springframework.util.CollectionUtils;
 
 import java.util.HashMap;
 import java.util.Map;
@@ -19,7 +20,7 @@ public class ParagraphUtils {
     /**
      * 将条目转换为map
      *
-     * @param itemVO 条目对象
+     * @param itemVO  条目对象
      * @param codeSet 需要转换的属性集
      * @return 根据条目转换成的map对象
      * @author lixing
@@ -35,4 +36,33 @@ public class ParagraphUtils {
         }
         return itemMap;
     }
+
+    public static Map<String, Object> convertMapMemberType(Map<String, String> map, String memberName, Class<?> memberType) {
+        if (CollectionUtils.isEmpty(map) || memberName == null || memberType == null) {
+            return null;
+        }
+        Map<String, Object> anotherMap = new HashMap<>();
+
+        try {
+            for (String key : map.keySet()) {
+                if (memberName.equals(key)) {
+                    String s = map.get(key);
+                    if (memberType.equals(Integer.class)) {
+                        anotherMap.put(key, Integer.valueOf(s));
+                    } else if (memberType.equals(Double.class)) {
+                        anotherMap.put(key, Double.valueOf(s));
+                    } else {
+                        anotherMap.put(key, map.get(key));
+                    }
+
+                } else {
+                    anotherMap.put(key, map.get(key));
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        return anotherMap;
+    }
 }

+ 2 - 2
src/main/java/com/persagy/apm/report/userconfig/statisticsitems/enums/QualifiedEnum.java

@@ -13,8 +13,8 @@ public enum QualifiedEnum {
     /**
      * 是否合格
      */
-    QUALIFIED(0d, "合格"),
-    NOT_QUALIFIED(1d, "不合格");
+    QUALIFIED(1d, "合格"),
+    NOT_QUALIFIED(0d, "不合格");
 
     @Setter
     @Getter