From 3ef0c6fe032b83eaeee69529d4859d149057d6f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=B5=B7=E8=88=AA?= <11918452+yang-haihang@user.noreply.gitee.com> Date: Tue, 18 Mar 2025 14:52:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86=E7=94=9F=E6=88=90?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1Excel=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...trustMaterialCheckoutResultController.java | 8 +- .../entrustment/dto/ResultExcelDTO.java | 13 + .../EntrustMaterialCheckoutResultService.java | 5 +- ...rustmentIdentificationMaterialService.java | 4 + ...rustMaterialCheckoutResultServiceImpl.java | 431 +++++++++--------- ...mentIdentificationMaterialServiceImpl.java | 22 + .../service/impl/EntrustmentServiceImpl.java | 4 +- 7 files changed, 265 insertions(+), 222 deletions(-) create mode 100644 src/main/java/digital/laboratory/platform/entrustment/dto/ResultExcelDTO.java diff --git a/src/main/java/digital/laboratory/platform/entrustment/controller/EntrustMaterialCheckoutResultController.java b/src/main/java/digital/laboratory/platform/entrustment/controller/EntrustMaterialCheckoutResultController.java index 1e1a6da..161aa2c 100644 --- a/src/main/java/digital/laboratory/platform/entrustment/controller/EntrustMaterialCheckoutResultController.java +++ b/src/main/java/digital/laboratory/platform/entrustment/controller/EntrustMaterialCheckoutResultController.java @@ -7,6 +7,7 @@ import digital.laboratory.platform.common.core.exception.CheckedException; import digital.laboratory.platform.common.core.util.R; import digital.laboratory.platform.entrustment.convert.EntrustMaterialCheckoutResultConvert; import digital.laboratory.platform.entrustment.dto.EntrustMaterialCheckoutResultDTO; +import digital.laboratory.platform.entrustment.dto.ResultExcelDTO; import digital.laboratory.platform.entrustment.query.BaseQuery; import digital.laboratory.platform.entrustment.query.EntrustMaterialCheckoutResultQuery; import digital.laboratory.platform.entrustment.service.EntrustMaterialCheckoutResultService; @@ -104,9 +105,8 @@ public class EntrustMaterialCheckoutResultController { } @ApiOperation("导出Excel") - @GetMapping("/getExcelByResult") - public void getResultData(HttpServletResponse response, Integer entrustType, String oldResult, LocalDateTime startTime, LocalDateTime endTime) throws IOException { - entrustMaterialCheckoutResultService.getExcelByResult(response, entrustType, oldResult, startTime, endTime); + @PostMapping("/getExcelByResult") + public R getResultData(HttpServletResponse response, @RequestBody ResultExcelDTO excelDTO) throws IOException { + return entrustMaterialCheckoutResultService.getExcelByResult(response, excelDTO); } - } diff --git a/src/main/java/digital/laboratory/platform/entrustment/dto/ResultExcelDTO.java b/src/main/java/digital/laboratory/platform/entrustment/dto/ResultExcelDTO.java new file mode 100644 index 0000000..4fd74aa --- /dev/null +++ b/src/main/java/digital/laboratory/platform/entrustment/dto/ResultExcelDTO.java @@ -0,0 +1,13 @@ +package digital.laboratory.platform.entrustment.dto; + +import lombok.Data; + +import java.time.LocalDateTime; + +@Data +public class ResultExcelDTO { + Integer entrustType; + String oldResult; + LocalDateTime startTime; + LocalDateTime endTime; +} diff --git a/src/main/java/digital/laboratory/platform/entrustment/service/EntrustMaterialCheckoutResultService.java b/src/main/java/digital/laboratory/platform/entrustment/service/EntrustMaterialCheckoutResultService.java index 2f8f57f..69e9f36 100644 --- a/src/main/java/digital/laboratory/platform/entrustment/service/EntrustMaterialCheckoutResultService.java +++ b/src/main/java/digital/laboratory/platform/entrustment/service/EntrustMaterialCheckoutResultService.java @@ -3,7 +3,9 @@ package digital.laboratory.platform.entrustment.service; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.IService; +import digital.laboratory.platform.common.core.util.R; import digital.laboratory.platform.entrustment.dto.EntrustMaterialCheckoutResultDTO; +import digital.laboratory.platform.entrustment.dto.ResultExcelDTO; import digital.laboratory.platform.entrustment.entity.EntrustMaterialCheckoutResult; import digital.laboratory.platform.entrustment.query.EntrustMaterialCheckoutResultQuery; import digital.laboratory.platform.entrustment.vo.DetectionRateVO; @@ -71,7 +73,6 @@ public interface EntrustMaterialCheckoutResultService extends IService getResultData(Integer entrustType, String oldResult, LocalDateTime startTime, LocalDateTime endTime); - void getExcelByResult(HttpServletResponse response, Integer entrustType, String oldResult, LocalDateTime startTime, LocalDateTime endTime) throws IOException; + R getExcelByResult(HttpServletResponse response, ResultExcelDTO excelDTO) throws IOException; } diff --git a/src/main/java/digital/laboratory/platform/entrustment/service/EntrustmentIdentificationMaterialService.java b/src/main/java/digital/laboratory/platform/entrustment/service/EntrustmentIdentificationMaterialService.java index b9e6175..b411d6c 100644 --- a/src/main/java/digital/laboratory/platform/entrustment/service/EntrustmentIdentificationMaterialService.java +++ b/src/main/java/digital/laboratory/platform/entrustment/service/EntrustmentIdentificationMaterialService.java @@ -114,4 +114,8 @@ public interface EntrustmentIdentificationMaterialService extends IService list); + + List sortByAcceptNo(List materials); + + List sortVoByAcceptNo(List materials); } diff --git a/src/main/java/digital/laboratory/platform/entrustment/service/impl/EntrustMaterialCheckoutResultServiceImpl.java b/src/main/java/digital/laboratory/platform/entrustment/service/impl/EntrustMaterialCheckoutResultServiceImpl.java index 7d84cb3..bcd8c63 100644 --- a/src/main/java/digital/laboratory/platform/entrustment/service/impl/EntrustMaterialCheckoutResultServiceImpl.java +++ b/src/main/java/digital/laboratory/platform/entrustment/service/impl/EntrustMaterialCheckoutResultServiceImpl.java @@ -1,8 +1,8 @@ package digital.laboratory.platform.entrustment.service.impl; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.date.LocalDateTimeUtil; import cn.hutool.core.util.StrUtil; -import cn.hutool.log.Log; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.StringUtils; @@ -10,10 +10,12 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import digital.laboratory.platform.common.core.exception.CheckedException; +import digital.laboratory.platform.common.core.util.R; import digital.laboratory.platform.entrustment.convert.DrugLiteConvert; import digital.laboratory.platform.entrustment.convert.EntrustMaterialCheckoutResultConvert; import digital.laboratory.platform.entrustment.dto.CheckoutResultExcelDTO; import digital.laboratory.platform.entrustment.dto.EntrustMaterialCheckoutResultDTO; +import digital.laboratory.platform.entrustment.dto.ResultExcelDTO; import digital.laboratory.platform.entrustment.entity.EntrustMaterialCheckoutResult; import digital.laboratory.platform.entrustment.entity.Entrustment; import digital.laboratory.platform.entrustment.entity.EntrustmentIdentificationMaterial; @@ -39,6 +41,7 @@ import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.math.BigDecimal; +import java.math.RoundingMode; import java.net.URLEncoder; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @@ -517,122 +520,115 @@ public class EntrustMaterialCheckoutResultServiceImpl extends ServiceImpl getResultData(Integer entrustType, String oldResult, LocalDateTime startTime, LocalDateTime endTime) { + public List getResultData(ResultExcelDTO excelDTO) { - LambdaQueryWrapper qw = new LambdaQueryWrapper<>(); + LocalDateTime startTime = excelDTO.getStartTime(); + LocalDateTime endTime = excelDTO.getEndTime(); + String oldResult = excelDTO.getOldResult(); + Integer entrustType = excelDTO.getEntrustType(); + // 创建查询条件包装器 + LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); + // 设置时间范围过滤条件 if (startTime != null && endTime != null) { - qw.ge(startTime != null, EntrustMaterialCheckoutResult::getCreateTime, startTime) - .le(endTime != null, EntrustMaterialCheckoutResult::getCreateTime, endTime); - } else { - qw.ge(EntrustMaterialCheckoutResult::getCreateTime, this.getStartTime(null)); - } - // 查询所有符合时间条件的检测结果 - List resultList = this.list(qw - .isNotNull(EntrustMaterialCheckoutResult::getQualitativeResult) - .ne(EntrustMaterialCheckoutResult::getQualitativeResult, "")); + wrapper.between(Entrustment::getAcceptTime, startTime, endTime); - // 如果没有查询到数据,则直接返回空列表 - if (CollUtil.isEmpty(resultList)) { - return Collections.emptyList(); + } else { + wrapper.ge(Entrustment::getAcceptTime, this.getStartTime(null)); } - // 按委托 ID 进行分组 - Map> resultMap = resultList.stream() - .collect(Collectors.groupingBy(EntrustMaterialCheckoutResult::getEntrustId)); - - // 获取所有的委托 ID - Set entrustIdList = resultMap.keySet(); - - LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); - + // 处理 oldResult 过滤条件 if (StringUtils.isNotBlank(oldResult)) { - if (oldResult.equals("委托")) { - String types[] = {"首次鉴定", "补充鉴定", "重新鉴定"}; - wrapper.in(Entrustment::getOldIdentificationResult, types); + if ("委托".equals(oldResult)) { + wrapper.in(Entrustment::getOldIdentificationResult, "首次鉴定", "补充鉴定", "重新鉴定"); } else { wrapper.eq(Entrustment::getOldIdentificationResult, oldResult); } } - // 批量查询符合条件的委托信息,避免多次查询数据库 - List entrustmentList = entrustmentService.list(wrapper - .in(Entrustment::getId, entrustIdList) - .eq(entrustType != null, Entrustment::getEntrustmentType, entrustType)); + // 查询符合条件的 Entrustment 列表,避免重复查询数据库 + List entrustmentList = entrustmentService.list( + wrapper.eq(entrustType != null, Entrustment::getEntrustmentType, entrustType) + + ); + + System.out.println("委托的数量:" + entrustmentList.size()); + // 如果无符合条件的委托信息,则返回空列表 if (CollUtil.isEmpty(entrustmentList)) { return Collections.emptyList(); } - // 获取所有符合条件的委托 ID - Set filteredEntrustIds = entrustmentList.stream() + // 获取符合条件的委托 ID 集合,提高查询效率 + Set entrustIds = entrustmentList.stream() .map(Entrustment::getId) .collect(Collectors.toSet()); - // 获取所有符合条件的委托map + // 将 Entrustment 列表转换为 Map,提高后续查询速度 Map entrustMap = entrustmentList.stream() .collect(Collectors.toMap(Entrustment::getId, Function.identity())); - // 过滤出符合条件的检测结果 - List newResultList = resultList.stream() - .filter(result -> filteredEntrustIds.contains(result.getEntrustId())) - .collect(Collectors.toList()); + // 查询所有符合条件的 EntrustmentIdentificationMaterial 数据 + List materialList = entrustmentIdentificationMaterialService.list( + Wrappers.lambdaQuery() + .in(EntrustmentIdentificationMaterial::getEntrustmentId, entrustIds) + ); + + // 若无符合条件的材料,则直接返回空列表 + if (CollUtil.isEmpty(materialList)) { + return Collections.emptyList(); + } - // 提取所有 resultId 批量查询 EntrustmentIdentificationMaterial,避免循环查询数据库 - Set resultIds = newResultList.stream() - .map(EntrustMaterialCheckoutResult::getId) + // 获取材料 ID 集合,提高查询效率 + Set materialIds = materialList.stream() + .map(EntrustmentIdentificationMaterial::getId) .collect(Collectors.toSet()); - // 批量查询所有相关的材料信息 - List materialList = entrustmentIdentificationMaterialService.list(Wrappers - .lambdaQuery() - .in(EntrustmentIdentificationMaterial::getId, resultIds) - .last("ORDER BY " + - "CAST(SUBSTRING_INDEX(accept_no, '-', 1) AS UNSIGNED) ASC, " + // 年份排序 - "CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(accept_no, '-', -2), '-', 1) AS UNSIGNED) ASC, " + // 流水号排序 - "CAST(SUBSTRING_INDEX(accept_no, '-', -1) AS UNSIGNED) ASC")); // 子流水号排序 - - - // 将材料信息转换为 Map 以便快速查找 - Map materialMap = materialList.stream() - .collect(Collectors.toMap(EntrustmentIdentificationMaterial::getId, Function.identity())); - - // 构建返回的 VO 对象列表 - List voList = newResultList.stream() - .map(result -> { - EntrustmentIdentificationMaterial material = materialMap.get(result.getId()); - if (material == null) { - return null; - } - EntrustmentIdentificationMaterialVO vo = new EntrustmentIdentificationMaterialVO(); - BeanUtils.copyProperties(material, vo); - vo.setDrugLites(DrugLiteConvert.getDrugLites(result.getQualitativeResult())); - Entrustment entrust = entrustMap.get(vo.getEntrustmentId()); - String oldIdentificationResult = entrust.getOldIdentificationResult(); - if (oldIdentificationResult.equals("首次鉴定") || oldIdentificationResult.equals("补充鉴定") || oldIdentificationResult.equals("重新鉴定")) { - vo.setOldIdentificationResult("委托"); - } else { - vo.setOldIdentificationResult(oldIdentificationResult); - } - vo.setOrgName(entrust.getClientOrgName()); - return vo; - }) - .filter(Objects::nonNull) // 过滤掉可能为空的数据 - .collect(Collectors.toList()); - - // 按 acceptNo 进行排序 - voList.sort(Comparator.comparing((EntrustmentIdentificationMaterial m) -> { - String[] parts = m.getAcceptNo().split("-"); - return new int[]{Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2])}; - }, Comparator.comparingInt((int[] arr) -> arr[0]) // 按年份排序 - .thenComparingInt(arr -> arr[1]) // 按流水号排序 - .thenComparingInt(arr -> arr[2]))); // 按子流水号排序 - return voList; + // 查询所有符合条件的检测结果数据 + List resultList = this.list( + Wrappers.lambdaQuery() + .in(EntrustMaterialCheckoutResult::getId, materialIds) + ); + + // 将检测结果转换为 Map,避免后续 O(n^2) 复杂度查询 + Map resultMap = resultList.stream() + .collect(Collectors.toMap(EntrustMaterialCheckoutResult::getId, Function.identity())); + + // 构造返回的 VO 列表 + List voList = materialList.stream().map(material -> { + EntrustmentIdentificationMaterialVO vo = new EntrustmentIdentificationMaterialVO(); + BeanUtils.copyProperties(material, vo); + + // 设置检测结果 + vo.setDrugLites(Optional.ofNullable(resultMap.get(material.getId())) + .map(EntrustMaterialCheckoutResult::getQualitativeResult) + .map(DrugLiteConvert::getDrugLites) + .orElse(null)); + + // 设置委托信息 + Entrustment entrust = entrustMap.get(material.getEntrustmentId()); + if (entrust != null) { + String oldIdentificationResult = entrust.getOldIdentificationResult(); + vo.setOldIdentificationResult( + ("首次鉴定".equals(oldIdentificationResult) || "补充鉴定".equals(oldIdentificationResult) || "重新鉴定".equals(oldIdentificationResult)) + ? "委托" + : oldIdentificationResult + ); + vo.setOrgName(entrust.getClientOrgName()); + } + return vo; + }).collect(Collectors.toList()); + + // 按 acceptNo 进行排序并返回结果 + return entrustmentIdentificationMaterialService.sortVoByAcceptNo(voList); } public Map> getResultDataMap( - Integer entrustType, String oldResult, LocalDateTime startTime, LocalDateTime endTime) { - + ResultExcelDTO excelDTO) { + LocalDateTime startTime = excelDTO.getStartTime(); + LocalDateTime endTime = excelDTO.getEndTime(); + String oldResult = excelDTO.getOldResult(); + Integer entrustType = excelDTO.getEntrustType(); try { // 1. 构建查询条件 LambdaQueryWrapper qw = new LambdaQueryWrapper<>(); @@ -766,165 +762,172 @@ public class EntrustMaterialCheckoutResultServiceImpl extends ServiceImpl resultData = this.getResultData(entrustType, oldResult, startTime, endTime); - Map> resultDataMap = this.getResultDataMap(entrustType, oldResult, startTime, endTime); + public R getExcelByResult(HttpServletResponse response, ResultExcelDTO excelDTO) throws IOException { + // 获取查询数据 + List resultData = this.getResultData(excelDTO); + Map> resultDataMap = this.getResultDataMap(excelDTO); + + LocalDateTime startTime = excelDTO.getStartTime(); + LocalDateTime endTime = excelDTO.getEndTime(); + String oldResult = excelDTO.getOldResult(); + Integer entrustType = excelDTO.getEntrustType(); + if (resultData.isEmpty() || resultDataMap.isEmpty()) { - throw new RuntimeException("没有符合条件的数据!"); + return R.failed("没有符合条件的数据!"); } + Workbook workbook = new XSSFWorkbook(); - Sheet sheet = null; - if (entrustType == 0) { - sheet = workbook.createSheet("常规毒品"); - } else { - if (StringUtils.isNotBlank(oldResult)) { - sheet = workbook.createSheet("生物样本-" + oldResult); - } else { - sheet = workbook.createSheet("生物样本"); - } - } - String[] InVivoHeadList = new String[]{"检材类型", "检材编号", "检材名称", "检材性状", "检材质量", "年龄", "性别", "委托单位", "受理日期", "检出结果"}; - String[] InVitroHeadList = new String[]{"检材类型", "检材编号", "检材名称", "检材性状", "检材质量", "委托单位", "受理日期", "检出结果"}; - String[] headList; - if (entrustType == 0) { - headList = InVitroHeadList; - } else { - headList = InVivoHeadList; - } + createDataSheet(workbook, entrustType, oldResult, resultData); + createStatisticsSheet(workbook, resultDataMap, entrustType, oldResult, startTime, endTime); + createDrugSheets(workbook, resultDataMap, entrustType); - Row row = sheet.createRow(0); - for (int i = 0; i < headList.length; i++) { - Cell cell = row.createCell(i); - cell.setCellValue(headList[i]); + workbook.write(response.getOutputStream()); + return R.ok("导出成功!"); + } + + private void createDataSheet(Workbook workbook, Integer entrustType, String oldResult, List resultData) { + // 确定表名 + String sheetName = (entrustType == 0) ? "常规毒品" : (StringUtils.isNotBlank(oldResult) ? "生物样本-" + oldResult : "生物样本"); + Sheet sheet = workbook.createSheet(sheetName); + + // 定义表头 + String[] inVivoHeaders = {"检材类型", "检材编号", "检材名称", "检材性状", "检材质量", "年龄", "性别", "委托单位", "受理日期", "检出结果"}; + String[] inVitroHeaders = {"检材类型", "检材编号", "检材名称", "检材性状", "检材质量", "委托单位", "受理日期", "检出结果"}; + String[] headers = (entrustType == 0) ? inVitroHeaders : inVivoHeaders; + + // 创建表头行 + Row headerRow = sheet.createRow(0); + for (int i = 0; i < headers.length; i++) { + headerRow.createCell(i).setCellValue(headers[i]); + sheet.setColumnWidth(i, 20 * 256); } + int masculineSize = 0; + // 填充数据 for (int i = 0; i < resultData.size(); i++) { EntrustmentIdentificationMaterialVO vo = resultData.get(i); - Row contentRow = sheet.createRow(i + 1); - contentRow.createCell(0).setCellValue(vo.getOldIdentificationResult()); - contentRow.createCell(1).setCellValue(vo.getAcceptNo()); - contentRow.createCell(2).setCellValue(vo.getName()); - contentRow.createCell(3).setCellValue(vo.getFormName()); - contentRow.createCell(4).setCellValue(vo.getQuantity() + vo.getUnit()); - if (entrustType == 1) { - contentRow.createCell(5).setCellValue(vo.getMaterialAge()); - contentRow.createCell(6).setCellValue(vo.getBiologyGender()); - contentRow.createCell(7).setCellValue(vo.getOrgName()); - contentRow.createCell(8).setCellValue(vo.getAcceptTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); - List drugLites = vo.getDrugLites(); - if (CollUtil.isNotEmpty(drugLites)) { - StringBuilder drugName = new StringBuilder(); - for (DrugLite drugLite : drugLites) { - drugName.append(drugLite.getName()).append(","); - } - contentRow.createCell(9).setCellValue(drugName.substring(0, drugName.length() - 1)); - } else { - contentRow.createCell(9).setCellValue(""); - } - } else { - contentRow.createCell(5).setCellValue(vo.getOrgName()); - contentRow.createCell(6).setCellValue(vo.getAcceptTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); - List drugLites = vo.getDrugLites(); - if (CollUtil.isNotEmpty(drugLites)) { - StringBuilder drugName = new StringBuilder(); - for (DrugLite drugLite : drugLites) { - drugName.append(drugLite.getName()).append(","); - } - contentRow.createCell(7).setCellValue(drugName.substring(0, drugName.length() - 1)); - } else { - contentRow.createCell(7).setCellValue(""); - } + Row row = sheet.createRow(i + 1); + fillRowData(row, vo, entrustType); + if (CollUtil.isNotEmpty(vo.getDrugLites())) { + masculineSize++; } } + Row lastRow = sheet.createRow(resultData.size() + 2); + lastRow.createCell(0).setCellValue("检材总数量:" + resultData.size()); + lastRow.createCell(1).setCellValue("阳性总数量:" + masculineSize); + BigDecimal result = new BigDecimal(masculineSize) + .multiply(new BigDecimal(100)) + .divide(new BigDecimal(resultData.size()), 2, RoundingMode.HALF_UP); + lastRow.createCell(2).setCellValue("总检出率:" + result.toString() + "%"); + } - Sheet statisticsSheet = workbook.createSheet("统计"); - - Row statisticsSheetRow = statisticsSheet.createRow(0); - - String[] statisticsHeadList = new String[]{"化合物种类", "类型", "检出数量", "总数量", "检出率"}; - - for (int i = 0; i < statisticsHeadList.length; i++) { - Cell cell = statisticsSheetRow.createCell(i); - cell.setCellValue(statisticsHeadList[i]); - } - Set drugSet = resultDataMap.keySet(); + private void createStatisticsSheet(Workbook workbook, Map> resultDataMap, Integer entrustType, String oldResult, LocalDateTime startTime, LocalDateTime endTime) { + Sheet sheet = workbook.createSheet("统计"); - int materialSize = 0; + String[] headers = {"化合物种类", "类型", "检出数量", "总数量", "检出率"}; - for (String s : drugSet) { - materialSize += resultDataMap.get(s).size(); + // 创建表头 + Row headerRow = sheet.createRow(0); + for (int i = 0; i < headers.length; i++) { + headerRow.createCell(i).setCellValue(headers[i]); + sheet.setColumnWidth(i, 20 * 256); } + // 获取总数量 Map sizeMap = this.getSizeMap(entrustType, oldResult, startTime, endTime); - System.out.println(sizeMap); - - for (String drugName : drugSet) { - List materialVOList = resultDataMap.get(drugName); + // 填充统计数据 + int rowIndex = 1; + for (Map.Entry> entry : resultDataMap.entrySet()) { + String drugName = entry.getKey(); + List materials = entry.getValue(); + Row row = sheet.createRow(rowIndex++); if (StringUtils.isNotBlank(oldResult)) { - Row sheetRow = statisticsSheet.createRow(statisticsSheet.getLastRowNum() + 1); - sheetRow.createCell(0).setCellValue(drugName); - sheetRow.createCell(1).setCellValue(oldResult); - sheetRow.createCell(2).setCellValue(materialVOList.size()); - sheetRow.createCell(3).setCellValue(sizeMap.get(oldResult)); - sheetRow.createCell(4).setCellValue(String.format("%.2f", materialVOList.size() * 1.0 / sizeMap.get(oldResult) * 100) + "%"); + + row.createCell(0).setCellValue(drugName); + row.createCell(1).setCellValue(oldResult); + row.createCell(2).setCellValue(materials.size()); + row.createCell(3).setCellValue(sizeMap.getOrDefault(oldResult, 0)); + row.createCell(4).setCellValue(String.format("%.2f%%", materials.size() * 100.0 / sizeMap.getOrDefault(oldResult, 1))); } else { - Map> map = materialVOList.stream().collect(Collectors.groupingBy(EntrustmentIdentificationMaterialVO::getOldIdentificationResult)); - Set keySet = map.keySet(); - for (String key : keySet) { - System.out.println(key); - int size = map.get(key).size(); - Row sheetRow = statisticsSheet.createRow(statisticsSheet.getLastRowNum() + 1); - sheetRow.createCell(0).setCellValue(drugName); - sheetRow.createCell(1).setCellValue(key); - sheetRow.createCell(2).setCellValue(size); - sheetRow.createCell(3).setCellValue(Integer.valueOf(sizeMap.get(key))); - sheetRow.createCell(4).setCellValue(String.format("%.2f", size * 1.0 / sizeMap.get(key) * 100) + "%"); + Map> groupedData = materials.stream().collect(Collectors.groupingBy(EntrustmentIdentificationMaterialVO::getOldIdentificationResult)); + for (Map.Entry> group : groupedData.entrySet()) { + row.createCell(0).setCellValue(drugName); + row.createCell(1).setCellValue(group.getKey()); + row.createCell(2).setCellValue(group.getValue().size()); + row.createCell(3).setCellValue(sizeMap.getOrDefault(group.getKey(), 0)); + row.createCell(4).setCellValue(String.format("%.2f%%", group.getValue().size() * 100.0 / sizeMap.getOrDefault(group.getKey(), 1))); } } - } + } + + private void createDrugSheets(Workbook workbook, Map> resultDataMap, Integer entrustType) { + for (Map.Entry> entry : resultDataMap.entrySet()) { + String drugName = entry.getKey().replaceAll("[^\u4e00-\u9fa5a-zA-Z0-9]", ""); + Sheet sheet = workbook.createSheet(drugName); - for (String drugName : drugSet) { - List materialVOList = resultDataMap.get(drugName); - String title = drugName.replaceAll("[^\\u4e00-\\u9fa5a-zA-Z0-9]", ""); - sheet = workbook.createSheet(title); - Row row0 = sheet.createRow(0); - for (int i = 0; i < headList.length; i++) { - Cell cell = row0.createCell(i); - cell.setCellValue(headList[i]); + // 定义表头 + String[] headers = (entrustType == 0) ? new String[]{"检材类型", "检材编号", "检材名称", "检材性状", "检材质量", "委托单位", "受理日期", "检出结果"} + : new String[]{"检材类型", "检材编号", "检材名称", "检材性状", "检材质量", "年龄", "性别", "委托单位", "受理日期", "检出结果"}; + + // 创建表头行 + Row headerRow = sheet.createRow(0); + for (int i = 0; i < headers.length; i++) { + headerRow.createCell(i).setCellValue(headers[i]); + sheet.setColumnWidth(i, 20 * 256); } - for (int i = 0; i < materialVOList.size(); i++) { - EntrustmentIdentificationMaterialVO vo = materialVOList.get(i); - Row contentRow = sheet.createRow(i + 1); - contentRow.createCell(0).setCellValue(vo.getOldIdentificationResult()); - contentRow.createCell(1).setCellValue(vo.getAcceptNo()); - contentRow.createCell(2).setCellValue(vo.getName()); - contentRow.createCell(3).setCellValue(vo.getFormName()); - contentRow.createCell(4).setCellValue(vo.getQuantity() + vo.getUnit()); - if (entrustType == 1) { - contentRow.createCell(5).setCellValue(vo.getMaterialAge()); - contentRow.createCell(6).setCellValue(vo.getBiologyGender()); - contentRow.createCell(7).setCellValue(vo.getOrgName()); - contentRow.createCell(8).setCellValue(vo.getAcceptTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); - contentRow.createCell(9).setCellValue(drugName); - } else { - contentRow.createCell(5).setCellValue(vo.getOrgName()); - contentRow.createCell(6).setCellValue(vo.getAcceptTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); - contentRow.createCell(7).setCellValue(drugName); - } + // 填充数据 + int rowIndex = 1; + for (EntrustmentIdentificationMaterialVO vo : entry.getValue()) { + Row row = sheet.createRow(rowIndex++); + fillRowData(row, vo, entrustType); } } - workbook.write(response.getOutputStream()); } + private void fillRowData(Row row, EntrustmentIdentificationMaterialVO vo, Integer entrustType) { + row.createCell(0).setCellValue(vo.getOldIdentificationResult()); + row.createCell(1).setCellValue(vo.getAcceptNo()); + row.createCell(2).setCellValue(vo.getName()); + row.createCell(3).setCellValue(vo.getFormName()); + row.createCell(4).setCellValue(vo.getQuantity() + vo.getUnit()); + + if (entrustType == 1) { + row.createCell(5).setCellValue(vo.getMaterialAge() != null || vo.getMaterialAge() != 0 ? vo.getMaterialAge().toString() + "岁" : "未知"); + row.createCell(6).setCellValue(StringUtils.isNotBlank(vo.getBiologyGender()) ? vo.getBiologyGender() : "未知"); + row.createCell(7).setCellValue(vo.getOrgName()); + row.createCell(8).setCellValue(LocalDateTimeUtil.format(vo.getAcceptTime(), "yyyy-MM-dd")); + List drugLites = vo.getDrugLites(); + if (drugLites != null && !drugLites.isEmpty()) { + String result = drugLites.stream() + .map(DrugLite::getName) + .collect(Collectors.joining(",")); // 自动去除最后的逗号 + row.createCell(9).setCellValue(result); + } else { + row.createCell(9).setCellValue("未检出"); + } + } else { + row.createCell(5).setCellValue(vo.getOrgName()); + row.createCell(6).setCellValue(LocalDateTimeUtil.format(vo.getAcceptTime(), "yyyy-MM-dd")); + List drugLites = vo.getDrugLites(); + if (drugLites != null && !drugLites.isEmpty()) { + String result = drugLites.stream() + .map(DrugLite::getName) + .collect(Collectors.joining(",")); // 自动去除最后的逗号 + row.createCell(7).setCellValue(result); + } else { + row.createCell(7).setCellValue("未检出"); + } + } + } + + public Map getSizeMap(Integer entrustType, String oldResult, LocalDateTime startTime, LocalDateTime endTime) { // 构造查询条件,查询 EntrustmentIdentificationMaterial 数据 LambdaQueryWrapper qw = new LambdaQueryWrapper<>(); LocalDateTime queryStartTime = Optional.ofNullable(startTime).orElseGet(() -> this.getStartTime(null)); - if (endTime != null) { qw.between(EntrustmentIdentificationMaterial::getAcceptTime, queryStartTime, endTime); } else { diff --git a/src/main/java/digital/laboratory/platform/entrustment/service/impl/EntrustmentIdentificationMaterialServiceImpl.java b/src/main/java/digital/laboratory/platform/entrustment/service/impl/EntrustmentIdentificationMaterialServiceImpl.java index 9571dd1..b81e151 100644 --- a/src/main/java/digital/laboratory/platform/entrustment/service/impl/EntrustmentIdentificationMaterialServiceImpl.java +++ b/src/main/java/digital/laboratory/platform/entrustment/service/impl/EntrustmentIdentificationMaterialServiceImpl.java @@ -1627,4 +1627,26 @@ public class EntrustmentIdentificationMaterialServiceImpl extends ServiceImpl sortByAcceptNo(List materials){ + materials.sort(Comparator.comparing((EntrustmentIdentificationMaterial m) -> { + String[] parts = m.getAcceptNo().split("-"); + return new int[]{Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2])}; + }, Comparator.comparingInt((int[] arr) -> arr[0]) // 按年份排序 + .thenComparingInt(arr -> arr[1]) // 按流水号排序 + .thenComparingInt(arr -> arr[2]))); // 按子流水号排序 + return materials; + } + + @Override + public List sortVoByAcceptNo(List materials){ + materials.sort(Comparator.comparing((EntrustmentIdentificationMaterial m) -> { + String[] parts = m.getAcceptNo().split("-"); + return new int[]{Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2])}; + }, Comparator.comparingInt((int[] arr) -> arr[0]) // 按年份排序 + .thenComparingInt(arr -> arr[1]) // 按流水号排序 + .thenComparingInt(arr -> arr[2]))); // 按子流水号排序 + return materials; + } } diff --git a/src/main/java/digital/laboratory/platform/entrustment/service/impl/EntrustmentServiceImpl.java b/src/main/java/digital/laboratory/platform/entrustment/service/impl/EntrustmentServiceImpl.java index 51ed0d8..49dd73c 100644 --- a/src/main/java/digital/laboratory/platform/entrustment/service/impl/EntrustmentServiceImpl.java +++ b/src/main/java/digital/laboratory/platform/entrustment/service/impl/EntrustmentServiceImpl.java @@ -2459,8 +2459,8 @@ public class EntrustmentServiceImpl extends ServiceImpl