liding
2025-03-26 e713714eb3796ac4d67495f823aa633535b3ad15
basic-server/src/main/java/com/ruoyi/basic/service/impl/StandardMethodServiceImpl.java
@@ -1,7 +1,10 @@
package com.ruoyi.basic.service.impl;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
@@ -131,22 +134,115 @@
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void inputExcel(MultipartFile file) throws IOException {
        // 存储检测对象List
        List<Object> structureTestObjectIdList = new ArrayList<>();
        Set<Object> structureTestObjectIdSet = new HashSet<>();
        List<StandardMethod> result = new ArrayList<>();
        ExcelUtil.readBySax(file.getInputStream(), 0, (i, l, list) -> {
            // 去除第一行表头
            if (l == 0) {
                return;
        EasyExcel.read(file.getInputStream(), StandardMethod.class, new AnalysisEventListener<StandardMethod>() {
            @Override
            public void invoke(StandardMethod data, AnalysisContext context) {
                // 去除第一行表头,EasyExcel 自动处理表头,这里无需额外处理
                // 存储唯一检测对象
                String structureTestObjectId = data.getStructureTestObjectId();
                Object idObj = getFirstIdFromJson(structureTestObjectId);
                if (idObj != null) {
                    structureTestObjectIdSet.add(idObj);
                }
                result.add(data);
            }
            // 存储唯一检测对象
            if (!structureTestObjectIdList.contains(list.get(2))) {
                structureTestObjectIdList.add(list.get(2));
            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                // 解析完成后执行添加操作
                addStructureTest(new ArrayList<>(structureTestObjectIdSet), result);
            }
            StandardMethod standardMethod = formatData(list);
            result.add(standardMethod);
        });
        addStructureTest(structureTestObjectIdList, result);
        }).sheet().doRead();
    }
    private Object getFirstIdFromJson(String json) {
        // 简单解析 JSON 数组获取第一个元素
        if (json != null && json.startsWith("[") && json.contains(",")) {
            return json.substring(2, json.indexOf(","));
        } else if (json != null && json.startsWith("[")) {
            return json.substring(1, json.length() - 1);
        }
        return null;
    }
    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 分组
        Map<String, StandardMethod> excelDataMap = new HashMap<>();
        for (StandardMethod excelMethod : standardMethodList) {
            String key = generateKey(excelMethod);
            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);
            }
            // 将数据库数据按 key 分组
            Map<String, StandardMethod> dbDataMap = new HashMap<>();
            for (StandardMethod dbMethod : allDbMethods) {
                String key = generateKey(dbMethod);
                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 : dbDataMap.entrySet()) {
                String key = entry.getKey();
                if (!excelDataMap.containsKey(key)) {
                    StandardMethod dbMethod = entry.getValue();
                    deleteListId.add(dbMethod.getId());
                }
            }
        if (!addList.isEmpty()) {
            // 新增
            baseMapper.insertBatchSomeColumn(addList);
        }
        if (!deleteListId.isEmpty()) {
            // 删除
            baseMapper.deleteBatchIds(deleteListId);
        }
        if (!updateList.isEmpty()) {
            // 更新
            updateList.forEach(i -> {
                baseMapper.updateById(i);
            });
        }
    }
    private String generateKey(StandardMethod method) {
        return method.getStructureTestObjectId() + "-" + method.getCode() + "-" + method.getField();
    }
    // 格式化数据
@@ -185,74 +281,6 @@
        return standardMethod;
    }
    // 新增数据
    public void addStructureTest(List<Object> structureTestObjectIdList, List<StandardMethod> standardMethodList) {
        List<StandardMethod> updateList = new ArrayList<>();
        List<Integer> deleteListId = new ArrayList<>();
        List<StandardMethod> addList = new ArrayList<>();
        if (!structureTestObjectIdList.isEmpty()) {
            // 循环excel里面的分组
            structureTestObjectIdList.forEach(j -> {
                // 以excel中的组名查询数据库中的分组
                List<StandardMethod> standardMethods = baseMapper.selectList(Wrappers.<StandardMethod>lambdaQuery()
                        .like(StandardMethod::getStructureTestObjectId, "\"" + j + "\""));
                // 将结果循环匹配
                for (int i = 0; i < standardMethods.size(); i++) {
                    boolean isExistence = false;
                    for (int i1 = 0; i1 < standardMethodList.size(); i1++) {
                        // 更新
                        if (standardMethods.get(i).getStructureTestObjectId().equals(standardMethodList.get(i1).getStructureTestObjectId())
                                && standardMethods.get(i).getCode().equals(standardMethodList.get(i1).getCode())
                                && standardMethods.get(i).getField().equals(standardMethodList.get(i1).getField())) {
                            // 给excel数据赋值id做更新
                            standardMethodList.get(i1).setId(standardMethods.get(i).getId());
                            // 更新
                            updateList.add(standardMethodList.get(i1));
                            isExistence = true;
                            break;
                        }
                    }
                    // 删除
                    if (!isExistence) {
                        deleteListId.add(standardMethods.get(i).getId());
                    }
                }
                for (int i = 0; i < standardMethodList.size(); i++) {
                    if (standardMethodList.get(i).getStructureTestObjectId().contains("\"" + j + "\"")) {
                        boolean isExistence = false;
                        for (int i1 = 0; i1 < standardMethods.size(); i1++) {
                            if (standardMethods.get(i1).getStructureTestObjectId().equals(standardMethodList.get(i).getStructureTestObjectId())
                                    && standardMethods.get(i1).getCode().equals(standardMethodList.get(i).getCode())
                                    && standardMethods.get(i1).getField().equals(standardMethodList.get(i).getField())) {
                                isExistence = true;
                                break;
                            }
                        }
                        // 新增
                        if (!isExistence) {
                            addList.add(standardMethodList.get(i));
                        }
                    }
                }
            });
        }
        if (!addList.isEmpty()) {
            // 新增
            baseMapper.insertBatchSomeColumn(addList);
        }
        if (!deleteListId.isEmpty()) {
            // 删除
            baseMapper.deleteBatchIds(deleteListId);
        }
        if (!updateList.isEmpty()) {
            // 更新
            updateList.forEach(i -> {
                baseMapper.updateById(i);
            });
        }
    }
}