chenrui
2025-04-02 c82ecd33f521c502ce4f895fc1ac1dcea68c56d6
basic-server/src/main/java/com/ruoyi/basic/service/impl/StandardMethodServiceImpl.java
@@ -171,58 +171,76 @@
    }
    public void addStructureTest(List<Object> structureTestObjectIdList, List<StandardMethod> standardMethodList) {
        // 用于存储新增、更新和删除的数据
        List<StandardMethod> updateList = new ArrayList<>();
        List<Integer> deleteListId = new ArrayList<>();
        List<StandardMethod> addList = new ArrayList<>();
        // 先将 Excel 数据按 key 分组
        // 先将 Excel 数据按 code 分组,并合并 field
        Map<String, StandardMethod> excelDataMap = new HashMap<>();
        for (StandardMethod excelMethod : standardMethodList) {
            String key = generateKey(excelMethod);
            excelDataMap.put(key, excelMethod);
            if (excelDataMap.containsKey(key)) {
                // 如果已存在相同的 code,则合并 field
                StandardMethod existingMethod = excelDataMap.get(key);
                String mergedField = mergeFields(existingMethod.getField(), excelMethod.getField());
                existingMethod.setField(mergedField);
            } else {
                // 否则直接添加到 map 中
                excelDataMap.put(key, excelMethod);
            }
        }
//        if (!structureTestObjectIdList.isEmpty()) {
            // 以 excel 中的组名查询数据库中的分组
            List<StandardMethod> allDbMethods = new ArrayList<>();
            for (Object j : structureTestObjectIdList) {
                List<StandardMethod> standardMethods = baseMapper.selectList(new LambdaQueryWrapper<StandardMethod>()
                        .like(StandardMethod::getStructureTestObjectId, "\"" + j + "\""));
                allDbMethods.addAll(standardMethods);
            }
        // 查询数据库中的分组数据
        List<StandardMethod> allDbMethods = new ArrayList<>();
        for (Object j : structureTestObjectIdList) {
            List<StandardMethod> standardMethods = baseMapper.selectList(
                    new LambdaQueryWrapper<StandardMethod>()
                            .apply("JSON_CONTAINS(structure_test_object_id, {0})", j) // MySQL JSON 查询
            );
            allDbMethods.addAll(standardMethods);
        }
            // 将数据库数据按 key 分组
            Map<String, StandardMethod> dbDataMap = new HashMap<>();
            for (StandardMethod dbMethod : allDbMethods) {
                String key = generateKey(dbMethod);
        // 将数据库数据按 code 分组,并合并 field
        Map<String, StandardMethod> dbDataMap = new HashMap<>();
        for (StandardMethod dbMethod : allDbMethods) {
            String key = generateKey(dbMethod);
            if (dbDataMap.containsKey(key)) {
                // 如果已存在相同的 code,则合并 field
                StandardMethod existingMethod = dbDataMap.get(key);
                String mergedField = mergeFields(existingMethod.getField(), dbMethod.getField());
                existingMethod.setField(mergedField);
            } else {
                // 否则直接添加到 map 中
                dbDataMap.put(key, dbMethod);
            }
        }
            // 处理更新和新增
            for (Map.Entry<String, StandardMethod> entry : excelDataMap.entrySet()) {
                String key = entry.getKey();
                StandardMethod excelMethod = entry.getValue();
                StandardMethod dbMethod = dbDataMap.get(key);
                if (dbMethod != null) {
                    // 更新
                    excelMethod.setId(dbMethod.getId());
                    updateList.add(excelMethod);
                } else {
                    // 新增
                    addList.add(excelMethod);
                }
        // 处理更新和新增
        for (Map.Entry<String, StandardMethod> entry : excelDataMap.entrySet()) {
            String key = entry.getKey();
            StandardMethod excelMethod = entry.getValue();
            StandardMethod dbMethod = dbDataMap.get(key);
            if (dbMethod != null) {
                // 更新
                excelMethod.setId(dbMethod.getId());
                updateList.add(excelMethod);
            } else {
                // 新增
                addList.add(excelMethod);
            }
        }
            // 处理删除
            for (Map.Entry<String, StandardMethod> entry : dbDataMap.entrySet()) {
                String key = entry.getKey();
                if (!excelDataMap.containsKey(key)) {
                    StandardMethod dbMethod = entry.getValue();
                    deleteListId.add(dbMethod.getId());
                }
        // 处理删除
        for (Map.Entry<String, StandardMethod> entry : dbDataMap.entrySet()) {
            String key = entry.getKey();
            if (!excelDataMap.containsKey(key)) {
                StandardMethod dbMethod = entry.getValue();
                deleteListId.add(dbMethod.getId());
            }
        }
            // 执行批量操作
        if (!addList.isEmpty()) {
            // 新增
            baseMapper.insertBatchSomeColumn(addList);
@@ -241,8 +259,26 @@
        }
    }
    private String mergeFields(String field1, String field2) {
        if (field1 == null || field1.isEmpty()) {
            return field2;
        }
        if (field2 == null || field2.isEmpty()) {
            return field1;
        }
        // 使用 Set 去重
        String[] fields1 = field1.split(",");
        String[] fields2 = field2.split(",");
        Set<String> uniqueFields = new HashSet<>(Arrays.asList(fields1));
        uniqueFields.addAll(Arrays.asList(fields2));
        // 返回逗号拼接的结果
        return String.join(",", uniqueFields);
    }
    private String generateKey(StandardMethod method) {
        return method.getStructureTestObjectId() + "-" + method.getCode() + "-" + method.getField();
        return method.getCode();
    }
    // 格式化数据