From de0494d7a6f99f50fa33b9487dd2ffefca82e8b6 Mon Sep 17 00:00:00 2001 From: chenrui <1187576398@qq.com> Date: 星期二, 08 四月 2025 17:44:47 +0800 Subject: [PATCH] 标准库功能修改 --- basic-server/src/main/java/com/ruoyi/basic/service/impl/StandardMethodServiceImpl.java | 226 ++++++++++++++++++++++++++++++++++++-------------------- 1 files changed, 145 insertions(+), 81 deletions(-) diff --git a/basic-server/src/main/java/com/ruoyi/basic/service/impl/StandardMethodServiceImpl.java b/basic-server/src/main/java/com/ruoyi/basic/service/impl/StandardMethodServiceImpl.java index 44566bd..4b893f1 100644 --- a/basic-server/src/main/java/com/ruoyi/basic/service/impl/StandardMethodServiceImpl.java +++ b/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,151 @@ @Transactional(rollbackFor = Exception.class) @Override public void inputExcel(MultipartFile file) throws IOException { + // 瀛樺偍妫�娴嬪璞ist - 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) { + // 瑙f瀽瀹屾垚鍚庢墽琛屾坊鍔犳搷浣� + 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 鏁版嵁鎸� code 鍒嗙粍锛屽苟鍚堝苟 field + Map<String, StandardMethod> excelDataMap = new HashMap<>(); + for (StandardMethod excelMethod : standardMethodList) { + String key = generateKey(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); + } + } + + // 鏌ヨ鏁版嵁搴撲腑鐨勫垎缁勬暟鎹� + 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); + } + + // 灏嗘暟鎹簱鏁版嵁鎸� 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 : 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 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.getCode(); } // 鏍煎紡鍖栨暟鎹� @@ -185,74 +317,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 -> { - // 浠xcel涓殑缁勫悕鏌ヨ鏁版嵁搴撲腑鐨勫垎缁� - 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())) { - // 缁檈xcel鏁版嵁璧嬪�糹d鍋氭洿鏂� - 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); - }); - } - } } -- Gitblit v1.9.3