diff --git a/src/test/java/com/supervision/demo/IndexTest.java b/src/test/java/com/supervision/demo/IndexTest.java index f40c154..c5589fb 100644 --- a/src/test/java/com/supervision/demo/IndexTest.java +++ b/src/test/java/com/supervision/demo/IndexTest.java @@ -2,17 +2,28 @@ package com.supervision.demo; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.supervision.common.constant.IndexRuleConstants; +import com.supervision.police.domain.ComDictionary; import com.supervision.police.domain.ModelAtomicIndex; +import com.supervision.police.domain.ModelIndex; import com.supervision.police.domain.NotePrompt; +import com.supervision.police.service.ComDictionaryService; import com.supervision.police.service.ModelAtomicIndexService; +import com.supervision.police.service.ModelIndexService; import com.supervision.police.service.NotePromptService; +import com.supervision.police.vo.ModelIndexReqVO; import lombok.extern.slf4j.Slf4j; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import java.util.ArrayList; -import java.util.List; +import java.io.FileOutputStream; +import java.util.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -24,7 +35,11 @@ public class IndexTest { @Autowired private ModelAtomicIndexService modelAtomicIndexService; @Autowired + private ModelIndexService modelIndexService; + @Autowired private NotePromptService notePromptService; + @Autowired + private ComDictionaryService comDictionaryService; /** * 图谱生成原子指标历史数据关联提示词 @@ -69,4 +84,73 @@ public class IndexTest { }); log.info("未匹配的:{}。共:{}个", unMatched, unMatched.size()); } + + /** + * 导出指标数据excel + */ + @Test + public void exportIndex() { + //将结果生成Excel保存到桌面 + Map data = (Map) modelIndexService.selectAll(new ModelIndexReqVO(), 1, 100000).getData(); + List dicts = comDictionaryService.list(); + System.out.println(data); + List list = (List) data.get("result"); + list.sort(Comparator.comparing(ModelIndex::getIndexType)); + List> result = new ArrayList<>(); + list.forEach(index -> { + Map indexMap = new HashMap<>(); + indexMap.put("指标名称", index.getName()); + indexMap.put("指标得分", index.getIndexScore()); + indexMap.put("指标类型", comDictionaryService.getName(dicts, "index_type", index.getIndexType())); + List> atomicIndexMapList = new ArrayList<>(); + index.getAtomicIndexList().forEach(atomicIndex -> { + Map atomicIndexMap = new HashMap<>(); + atomicIndexMap.put("原子指标名称", atomicIndex.getName()); + atomicIndexMap.put("指标来源", comDictionaryService.getName(dicts, "index_source", atomicIndex.getIndexSource())); + atomicIndexMapList.add(atomicIndexMap); + }); + if (!atomicIndexMapList.isEmpty()) { + indexMap.put("原子指标", atomicIndexMapList); + } + result.add(indexMap); + }); + System.out.println(result); + // 使用Apache POI生成Excel文件 + try (Workbook workbook = new XSSFWorkbook()) { + Sheet sheet = workbook.createSheet("Index Data"); + int rowNum = 0; + Row headerRow = sheet.createRow(rowNum++); + String[] headers = {"指标类型", "指标名称", "指标得分", "原子指标名称", "指标来源"}; + for (int i = 0; i < headers.length; i++) { + Cell cell = headerRow.createCell(i); + cell.setCellValue(headers[i]); + } + + for (Map indexMap : result) { + List> atomicIndexMapList = (List>) indexMap.get("原子指标"); + int startRow = rowNum; + for (Map atomicIndexMap : atomicIndexMapList) { + Row row = sheet.createRow(rowNum++); + row.createCell(3).setCellValue(atomicIndexMap.get("原子指标名称")); + row.createCell(4).setCellValue(atomicIndexMap.get("指标来源")); + } + if (atomicIndexMapList.size() > 1) { + sheet.addMergedRegion(new CellRangeAddress(startRow, rowNum - 1, 0, 0)); + sheet.addMergedRegion(new CellRangeAddress(startRow, rowNum - 1, 1, 1)); + sheet.addMergedRegion(new CellRangeAddress(startRow, rowNum - 1, 2, 2)); + } + Row firstRow = sheet.getRow(startRow); + firstRow.createCell(0).setCellValue((String) indexMap.get("指标类型")); + firstRow.createCell(1).setCellValue((String) indexMap.get("指标名称")); + firstRow.createCell(2).setCellValue((Integer) indexMap.get("指标得分")); + } + + // 将Excel文件保存到桌面 + try (FileOutputStream fileOut = new FileOutputStream(System.getProperty("user.home") + "/Desktop/IndexData.xlsx")) { + workbook.write(fileOut); + } + } catch (Exception e) { + e.printStackTrace(); + } + } }