Kaynağa Gözat

[task-6226] 设备管理看板中专业组支持过滤和排序配置

huangws 14 saat önce
ebeveyn
işleme
fbb9c00257

+ 146 - 0
ibps-provider-root/modules/provider-business/src/main/java/com/lc/ibps/business/service/impl/StatisticServiceImpl.java

@@ -32,6 +32,7 @@ import java.time.YearMonth;
 import java.time.format.DateTimeFormatter;
 import java.time.format.TextStyle;
 import java.util.*;
+import java.util.stream.Collectors;
 
 @Service
 public class StatisticServiceImpl implements StatisticService {
@@ -266,6 +267,11 @@ public class StatisticServiceImpl implements StatisticService {
         equipDTO.setOriginalAssets(getEquipAssetsByDept());
 
         list.add(equipDTO);
+        //根据配置获取是否需要对部门进行排序和过滤
+        String dashboradDeptStr = getDashboradDeptStr();
+        if (!Objects.equals(dashboradDeptStr, "")) {
+            list = filterAndSortByDept(list, dashboradDeptStr);
+        }
         return list;
     }
 
@@ -1423,4 +1429,144 @@ public class StatisticServiceImpl implements StatisticService {
         }
         return resultMap;
     }
+
+    public String getDashboradDeptStr() {
+        String retStr = "";
+        String sqlStr = "select id_,setting from t_ipcc where id_ = '1'";
+
+        try {
+            List<Map<String, Object>> retList = (List<Map<String, Object>>) commonDao.query(sqlStr);
+
+            if (retList != null && !retList.isEmpty()) {
+                Object settingObj = retList.get(0).get("setting");
+                if (settingObj == null) {
+                    return retStr;
+                }
+                String settingJson = settingObj.toString();
+
+                String targetKey = "\"dashboradDeptStr\":\"";
+                int startIndex = settingJson.indexOf(targetKey);
+
+                if (startIndex != -1) {
+                    // 移动索引到值的开始位置
+                    startIndex += targetKey.length();
+                    // 找到值的结束位置(下一个双引号)
+                    int endIndex = settingJson.indexOf("\"", startIndex);
+
+                    if (endIndex != -1) {
+                        retStr = settingJson.substring(startIndex, endIndex);
+                    }
+                }
+
+            }
+        } catch (Exception e) {
+            logger.error("get ipcc config error!",e);
+            return retStr;
+        }
+
+        return retStr;
+    }
+
+    /**
+     * 根据部门字符串筛选并排序 List 中包含 org 属性的数据
+     * 2026/05/13 by huangws
+     * @param list             原始数据列表
+     * @param dashboradDeptStr 部门顺序字符串,如 "临床检验组,生化免疫组"
+     * @return 处理后的列表
+     */
+    public static List<EquipmentDashBoardDTO> filterAndSortByDept(List<EquipmentDashBoardDTO> list, String dashboradDeptStr) {
+        // 1. 参数校验
+        if (list == null || list.isEmpty() || dashboradDeptStr == null || dashboradDeptStr.trim().isEmpty()) {
+            return list;
+        }
+
+        // 2. 将字符串转换为有序的 List 和 Set
+        List<String> orderedDepts = Arrays.stream(dashboradDeptStr.split(","))
+                .map(String::trim)
+                .filter(s -> !s.isEmpty())
+                .collect(Collectors.toList());
+
+        Set<String> deptSet = new HashSet<>(orderedDepts);
+
+        // 3. 遍历最外层的 DTO
+        for (EquipmentDashBoardDTO dto : list) {
+            if (dto == null) continue;
+
+            // 4. 获取 DTO 的所有字段进行反射处理
+            Field[] fields = dto.getClass().getDeclaredFields();
+            for (Field field : fields) {
+                // 只处理 List 类型的字段
+                if (List.class.isAssignableFrom(field.getType())) {
+                    field.setAccessible(true); // 允许访问私有字段
+                    try {
+                        List<?> originalList = (List<?>) field.get(dto);
+                        if (originalList == null || originalList.isEmpty()) {
+                            continue;
+                        }
+
+                        // 5. 判断列表中的元素是否包含 "org" 属性
+                        Object firstItem = originalList.get(0);
+                        boolean hasOrgField = false;
+                        boolean isMapType = false;
+
+                        if (firstItem instanceof Map) {
+                            hasOrgField = ((Map<?, ?>) firstItem).containsKey("org");
+                            isMapType = true;
+                        } else {
+                            try {
+                                firstItem.getClass().getDeclaredField("org");
+                                hasOrgField = true;
+                            } catch (NoSuchFieldException e) {
+                                hasOrgField = false;
+                            }
+                        }
+
+                        // 如果不包含 org,跳过这个字段
+                        if (!hasOrgField) {
+                            continue;
+                        }
+
+                        // 6. 执行筛选和排序
+                        // 使用 linkedHashMap 保持插入顺序
+                        Map<String, Object> tempMap = new LinkedHashMap<>();
+
+                        for (Object item : originalList) {
+                            String orgValue = null;
+                            if (isMapType) {
+                                Object val = ((Map<?, ?>) item).get("org");
+                                if (val != null) orgValue = val.toString();
+                            } else {
+                                try {
+                                    Field orgField = item.getClass().getDeclaredField("org");
+                                    orgField.setAccessible(true);
+                                    Object val = orgField.get(item);
+                                    if (val != null) orgValue = val.toString();
+                                } catch (Exception ignored) {}
+                            }
+
+                            // 只有当 org 值在目标字符串中存在时,才放入临时 Map
+                            if (orgValue != null && deptSet.contains(orgValue)) {
+                                tempMap.put(orgValue, item);
+                            }
+                        }
+
+                        // 7. 按照目标顺序重新构建 List
+                        List<Object> sortedList = new ArrayList<>();
+                        for (String targetDept : orderedDepts) {
+                            if (tempMap.containsKey(targetDept)) {
+                                sortedList.add(tempMap.get(targetDept));
+                            }
+                        }
+
+                        // 8. 将排序后的 List 设回 DTO
+                        field.set(dto, sortedList);
+
+                    } catch (IllegalAccessException e) {
+                        logger.error("sort arrayList error!",e);
+                    }
+                }
+            }
+        }
+        return list;
+    }
 }