更新了生成统计Excel的功能

master
杨海航 3 months ago
parent 230aebe7bf
commit 3ef0c6fe03
  1. 8
      src/main/java/digital/laboratory/platform/entrustment/controller/EntrustMaterialCheckoutResultController.java
  2. 13
      src/main/java/digital/laboratory/platform/entrustment/dto/ResultExcelDTO.java
  3. 5
      src/main/java/digital/laboratory/platform/entrustment/service/EntrustMaterialCheckoutResultService.java
  4. 4
      src/main/java/digital/laboratory/platform/entrustment/service/EntrustmentIdentificationMaterialService.java
  5. 431
      src/main/java/digital/laboratory/platform/entrustment/service/impl/EntrustMaterialCheckoutResultServiceImpl.java
  6. 22
      src/main/java/digital/laboratory/platform/entrustment/service/impl/EntrustmentIdentificationMaterialServiceImpl.java
  7. 4
      src/main/java/digital/laboratory/platform/entrustment/service/impl/EntrustmentServiceImpl.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.common.core.util.R;
import digital.laboratory.platform.entrustment.convert.EntrustMaterialCheckoutResultConvert; import digital.laboratory.platform.entrustment.convert.EntrustMaterialCheckoutResultConvert;
import digital.laboratory.platform.entrustment.dto.EntrustMaterialCheckoutResultDTO; 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.BaseQuery;
import digital.laboratory.platform.entrustment.query.EntrustMaterialCheckoutResultQuery; import digital.laboratory.platform.entrustment.query.EntrustMaterialCheckoutResultQuery;
import digital.laboratory.platform.entrustment.service.EntrustMaterialCheckoutResultService; import digital.laboratory.platform.entrustment.service.EntrustMaterialCheckoutResultService;
@ -104,9 +105,8 @@ public class EntrustMaterialCheckoutResultController {
} }
@ApiOperation("导出Excel") @ApiOperation("导出Excel")
@GetMapping("/getExcelByResult") @PostMapping("/getExcelByResult")
public void getResultData(HttpServletResponse response, Integer entrustType, String oldResult, LocalDateTime startTime, LocalDateTime endTime) throws IOException { public R getResultData(HttpServletResponse response, @RequestBody ResultExcelDTO excelDTO) throws IOException {
entrustMaterialCheckoutResultService.getExcelByResult(response, entrustType, oldResult, startTime, endTime); return entrustMaterialCheckoutResultService.getExcelByResult(response, excelDTO);
} }
} }

@ -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;
}

@ -3,7 +3,9 @@ package digital.laboratory.platform.entrustment.service;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService; 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.EntrustMaterialCheckoutResultDTO;
import digital.laboratory.platform.entrustment.dto.ResultExcelDTO;
import digital.laboratory.platform.entrustment.entity.EntrustMaterialCheckoutResult; import digital.laboratory.platform.entrustment.entity.EntrustMaterialCheckoutResult;
import digital.laboratory.platform.entrustment.query.EntrustMaterialCheckoutResultQuery; import digital.laboratory.platform.entrustment.query.EntrustMaterialCheckoutResultQuery;
import digital.laboratory.platform.entrustment.vo.DetectionRateVO; import digital.laboratory.platform.entrustment.vo.DetectionRateVO;
@ -71,7 +73,6 @@ public interface EntrustMaterialCheckoutResultService extends IService<EntrustMa
*/ */
DetectionRateVO getDetectionRateByMaterial(Integer year, String orgId); DetectionRateVO getDetectionRateByMaterial(Integer year, String orgId);
List<EntrustmentIdentificationMaterialVO> 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;
} }

@ -114,4 +114,8 @@ public interface EntrustmentIdentificationMaterialService extends IService<Entru
String printMaterialArchives(String entrustmentId) throws Exception; String printMaterialArchives(String entrustmentId) throws Exception;
void conformityDrugs(List<EntrustmentIdentificationMaterial> list); void conformityDrugs(List<EntrustmentIdentificationMaterial> list);
List<EntrustmentIdentificationMaterial> sortByAcceptNo(List<EntrustmentIdentificationMaterial> materials);
List<EntrustmentIdentificationMaterialVO> sortVoByAcceptNo(List<EntrustmentIdentificationMaterialVO> materials);
} }

@ -1,8 +1,8 @@
package digital.laboratory.platform.entrustment.service.impl; package digital.laboratory.platform.entrustment.service.impl;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils; 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.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import digital.laboratory.platform.common.core.exception.CheckedException; 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.DrugLiteConvert;
import digital.laboratory.platform.entrustment.convert.EntrustMaterialCheckoutResultConvert; import digital.laboratory.platform.entrustment.convert.EntrustMaterialCheckoutResultConvert;
import digital.laboratory.platform.entrustment.dto.CheckoutResultExcelDTO; import digital.laboratory.platform.entrustment.dto.CheckoutResultExcelDTO;
import digital.laboratory.platform.entrustment.dto.EntrustMaterialCheckoutResultDTO; 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.EntrustMaterialCheckoutResult;
import digital.laboratory.platform.entrustment.entity.Entrustment; import digital.laboratory.platform.entrustment.entity.Entrustment;
import digital.laboratory.platform.entrustment.entity.EntrustmentIdentificationMaterial; import digital.laboratory.platform.entrustment.entity.EntrustmentIdentificationMaterial;
@ -39,6 +41,7 @@ import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
@ -517,122 +520,115 @@ public class EntrustMaterialCheckoutResultServiceImpl extends ServiceImpl<Entrus
return materialPage; return materialPage;
} }
public List<EntrustmentIdentificationMaterialVO> getResultData(Integer entrustType, String oldResult, LocalDateTime startTime, LocalDateTime endTime) { public List<EntrustmentIdentificationMaterialVO> getResultData(ResultExcelDTO excelDTO) {
LambdaQueryWrapper<EntrustMaterialCheckoutResult> qw = new LambdaQueryWrapper<>(); LocalDateTime startTime = excelDTO.getStartTime();
LocalDateTime endTime = excelDTO.getEndTime();
String oldResult = excelDTO.getOldResult();
Integer entrustType = excelDTO.getEntrustType();
// 创建查询条件包装器
LambdaQueryWrapper<Entrustment> wrapper = new LambdaQueryWrapper<>();
// 设置时间范围过滤条件
if (startTime != null && endTime != null) { if (startTime != null && endTime != null) {
qw.ge(startTime != null, EntrustMaterialCheckoutResult::getCreateTime, startTime) wrapper.between(Entrustment::getAcceptTime, startTime, endTime);
.le(endTime != null, EntrustMaterialCheckoutResult::getCreateTime, endTime);
} else {
qw.ge(EntrustMaterialCheckoutResult::getCreateTime, this.getStartTime(null));
}
// 查询所有符合时间条件的检测结果
List<EntrustMaterialCheckoutResult> resultList = this.list(qw
.isNotNull(EntrustMaterialCheckoutResult::getQualitativeResult)
.ne(EntrustMaterialCheckoutResult::getQualitativeResult, ""));
// 如果没有查询到数据,则直接返回空列表 } else {
if (CollUtil.isEmpty(resultList)) { wrapper.ge(Entrustment::getAcceptTime, this.getStartTime(null));
return Collections.emptyList();
} }
// 按委托 ID 进行分组 // 处理 oldResult 过滤条件
Map<String, List<EntrustMaterialCheckoutResult>> resultMap = resultList.stream()
.collect(Collectors.groupingBy(EntrustMaterialCheckoutResult::getEntrustId));
// 获取所有的委托 ID
Set<String> entrustIdList = resultMap.keySet();
LambdaQueryWrapper<Entrustment> wrapper = new LambdaQueryWrapper<>();
if (StringUtils.isNotBlank(oldResult)) { if (StringUtils.isNotBlank(oldResult)) {
if (oldResult.equals("委托")) { if ("委托".equals(oldResult)) {
String types[] = {"首次鉴定", "补充鉴定", "重新鉴定"}; wrapper.in(Entrustment::getOldIdentificationResult, "首次鉴定", "补充鉴定", "重新鉴定");
wrapper.in(Entrustment::getOldIdentificationResult, types);
} else { } else {
wrapper.eq(Entrustment::getOldIdentificationResult, oldResult); wrapper.eq(Entrustment::getOldIdentificationResult, oldResult);
} }
} }
// 批量查询符合条件的委托信息,避免多次查询数据库
List<Entrustment> entrustmentList = entrustmentService.list(wrapper
.in(Entrustment::getId, entrustIdList)
.eq(entrustType != null, Entrustment::getEntrustmentType, entrustType));
// 查询符合条件的 Entrustment 列表,避免重复查询数据库
List<Entrustment> entrustmentList = entrustmentService.list(
wrapper.eq(entrustType != null, Entrustment::getEntrustmentType, entrustType)
);
System.out.println("委托的数量:" + entrustmentList.size());
// 如果无符合条件的委托信息,则返回空列表
if (CollUtil.isEmpty(entrustmentList)) { if (CollUtil.isEmpty(entrustmentList)) {
return Collections.emptyList(); return Collections.emptyList();
} }
// 获取所有符合条件的委托 ID // 获取符合条件的委托 ID 集合,提高查询效率
Set<String> filteredEntrustIds = entrustmentList.stream() Set<String> entrustIds = entrustmentList.stream()
.map(Entrustment::getId) .map(Entrustment::getId)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
// 获取所有符合条件的委托map // 将 Entrustment 列表转换为 Map,提高后续查询速度
Map<String, Entrustment> entrustMap = entrustmentList.stream() Map<String, Entrustment> entrustMap = entrustmentList.stream()
.collect(Collectors.toMap(Entrustment::getId, Function.identity())); .collect(Collectors.toMap(Entrustment::getId, Function.identity()));
// 过滤出符合条件的检测结果 // 查询所有符合条件的 EntrustmentIdentificationMaterial 数据
List<EntrustMaterialCheckoutResult> newResultList = resultList.stream() List<EntrustmentIdentificationMaterial> materialList = entrustmentIdentificationMaterialService.list(
.filter(result -> filteredEntrustIds.contains(result.getEntrustId())) Wrappers.<EntrustmentIdentificationMaterial>lambdaQuery()
.collect(Collectors.toList()); .in(EntrustmentIdentificationMaterial::getEntrustmentId, entrustIds)
);
// 若无符合条件的材料,则直接返回空列表
if (CollUtil.isEmpty(materialList)) {
return Collections.emptyList();
}
// 提取所有 resultId 批量查询 EntrustmentIdentificationMaterial,避免循环查询数据库 // 获取材料 ID 集合,提高查询效率
Set<String> resultIds = newResultList.stream() Set<String> materialIds = materialList.stream()
.map(EntrustMaterialCheckoutResult::getId) .map(EntrustmentIdentificationMaterial::getId)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
// 批量查询所有相关的材料信息 // 查询所有符合条件的检测结果数据
List<EntrustmentIdentificationMaterial> materialList = entrustmentIdentificationMaterialService.list(Wrappers List<EntrustMaterialCheckoutResult> resultList = this.list(
.<EntrustmentIdentificationMaterial>lambdaQuery() Wrappers.<EntrustMaterialCheckoutResult>lambdaQuery()
.in(EntrustmentIdentificationMaterial::getId, resultIds) .in(EntrustMaterialCheckoutResult::getId, materialIds)
.last("ORDER BY " + );
"CAST(SUBSTRING_INDEX(accept_no, '-', 1) AS UNSIGNED) ASC, " + // 年份排序
"CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(accept_no, '-', -2), '-', 1) AS UNSIGNED) ASC, " + // 流水号排序 // 将检测结果转换为 Map,避免后续 O(n^2) 复杂度查询
"CAST(SUBSTRING_INDEX(accept_no, '-', -1) AS UNSIGNED) ASC")); // 子流水号排序 Map<String, EntrustMaterialCheckoutResult> resultMap = resultList.stream()
.collect(Collectors.toMap(EntrustMaterialCheckoutResult::getId, Function.identity()));
// 将材料信息转换为 Map 以便快速查找 // 构造返回的 VO 列表
Map<String, EntrustmentIdentificationMaterial> materialMap = materialList.stream() List<EntrustmentIdentificationMaterialVO> voList = materialList.stream().map(material -> {
.collect(Collectors.toMap(EntrustmentIdentificationMaterial::getId, Function.identity())); EntrustmentIdentificationMaterialVO vo = new EntrustmentIdentificationMaterialVO();
BeanUtils.copyProperties(material, vo);
// 构建返回的 VO 对象列表
List<EntrustmentIdentificationMaterialVO> voList = newResultList.stream() // 设置检测结果
.map(result -> { vo.setDrugLites(Optional.ofNullable(resultMap.get(material.getId()))
EntrustmentIdentificationMaterial material = materialMap.get(result.getId()); .map(EntrustMaterialCheckoutResult::getQualitativeResult)
if (material == null) { .map(DrugLiteConvert::getDrugLites)
return null; .orElse(null));
}
EntrustmentIdentificationMaterialVO vo = new EntrustmentIdentificationMaterialVO(); // 设置委托信息
BeanUtils.copyProperties(material, vo); Entrustment entrust = entrustMap.get(material.getEntrustmentId());
vo.setDrugLites(DrugLiteConvert.getDrugLites(result.getQualitativeResult())); if (entrust != null) {
Entrustment entrust = entrustMap.get(vo.getEntrustmentId()); String oldIdentificationResult = entrust.getOldIdentificationResult();
String oldIdentificationResult = entrust.getOldIdentificationResult(); vo.setOldIdentificationResult(
if (oldIdentificationResult.equals("首次鉴定") || oldIdentificationResult.equals("补充鉴定") || oldIdentificationResult.equals("重新鉴定")) { ("首次鉴定".equals(oldIdentificationResult) || "补充鉴定".equals(oldIdentificationResult) || "重新鉴定".equals(oldIdentificationResult))
vo.setOldIdentificationResult("委托"); ? "委托"
} else { : oldIdentificationResult
vo.setOldIdentificationResult(oldIdentificationResult); );
} vo.setOrgName(entrust.getClientOrgName());
vo.setOrgName(entrust.getClientOrgName()); }
return vo; return vo;
}) }).collect(Collectors.toList());
.filter(Objects::nonNull) // 过滤掉可能为空的数据
.collect(Collectors.toList()); // 按 acceptNo 进行排序并返回结果
return entrustmentIdentificationMaterialService.sortVoByAcceptNo(voList);
// 按 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;
} }
public Map<String, List<EntrustmentIdentificationMaterialVO>> getResultDataMap( public Map<String, List<EntrustmentIdentificationMaterialVO>> 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 { try {
// 1. 构建查询条件 // 1. 构建查询条件
LambdaQueryWrapper<Entrustment> qw = new LambdaQueryWrapper<>(); LambdaQueryWrapper<Entrustment> qw = new LambdaQueryWrapper<>();
@ -766,165 +762,172 @@ public class EntrustMaterialCheckoutResultServiceImpl extends ServiceImpl<Entrus
} }
@Override @Override
public void getExcelByResult(HttpServletResponse response, Integer entrustType, String oldResult, LocalDateTime public R getExcelByResult(HttpServletResponse response, ResultExcelDTO excelDTO) throws IOException {
startTime, LocalDateTime endTime) throws IOException { // 获取查询数据
List<EntrustmentIdentificationMaterialVO> resultData = this.getResultData(entrustType, oldResult, startTime, endTime); List<EntrustmentIdentificationMaterialVO> resultData = this.getResultData(excelDTO);
Map<String, List<EntrustmentIdentificationMaterialVO>> resultDataMap = this.getResultDataMap(entrustType, oldResult, startTime, endTime); Map<String, List<EntrustmentIdentificationMaterialVO>> 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()) { if (resultData.isEmpty() || resultDataMap.isEmpty()) {
throw new RuntimeException("没有符合条件的数据!"); return R.failed("没有符合条件的数据!");
} }
Workbook workbook = new XSSFWorkbook(); 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("生物样本");
} createDataSheet(workbook, entrustType, oldResult, resultData);
} createStatisticsSheet(workbook, resultDataMap, entrustType, oldResult, startTime, endTime);
String[] InVivoHeadList = new String[]{"检材类型", "检材编号", "检材名称", "检材性状", "检材质量", "年龄", "性别", "委托单位", "受理日期", "检出结果"}; createDrugSheets(workbook, resultDataMap, entrustType);
String[] InVitroHeadList = new String[]{"检材类型", "检材编号", "检材名称", "检材性状", "检材质量", "委托单位", "受理日期", "检出结果"};
String[] headList;
if (entrustType == 0) {
headList = InVitroHeadList;
} else {
headList = InVivoHeadList;
}
Row row = sheet.createRow(0); workbook.write(response.getOutputStream());
for (int i = 0; i < headList.length; i++) { return R.ok("导出成功!");
Cell cell = row.createCell(i); }
cell.setCellValue(headList[i]);
private void createDataSheet(Workbook workbook, Integer entrustType, String oldResult, List<EntrustmentIdentificationMaterialVO> 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++) { for (int i = 0; i < resultData.size(); i++) {
EntrustmentIdentificationMaterialVO vo = resultData.get(i); EntrustmentIdentificationMaterialVO vo = resultData.get(i);
Row contentRow = sheet.createRow(i + 1); Row row = sheet.createRow(i + 1);
contentRow.createCell(0).setCellValue(vo.getOldIdentificationResult()); fillRowData(row, vo, entrustType);
contentRow.createCell(1).setCellValue(vo.getAcceptNo()); if (CollUtil.isNotEmpty(vo.getDrugLites())) {
contentRow.createCell(2).setCellValue(vo.getName()); masculineSize++;
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<DrugLite> 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<DrugLite> 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 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("统计"); private void createStatisticsSheet(Workbook workbook, Map<String, List<EntrustmentIdentificationMaterialVO>> resultDataMap, Integer entrustType, String oldResult, LocalDateTime startTime, LocalDateTime endTime) {
Sheet sheet = 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<String> drugSet = resultDataMap.keySet();
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<String, Integer> sizeMap = this.getSizeMap(entrustType, oldResult, startTime, endTime); Map<String, Integer> sizeMap = this.getSizeMap(entrustType, oldResult, startTime, endTime);
System.out.println(sizeMap); // 填充统计数据
int rowIndex = 1;
for (String drugName : drugSet) { for (Map.Entry<String, List<EntrustmentIdentificationMaterialVO>> entry : resultDataMap.entrySet()) {
List<EntrustmentIdentificationMaterialVO> materialVOList = resultDataMap.get(drugName); String drugName = entry.getKey();
List<EntrustmentIdentificationMaterialVO> materials = entry.getValue();
Row row = sheet.createRow(rowIndex++);
if (StringUtils.isNotBlank(oldResult)) { if (StringUtils.isNotBlank(oldResult)) {
Row sheetRow = statisticsSheet.createRow(statisticsSheet.getLastRowNum() + 1);
sheetRow.createCell(0).setCellValue(drugName); row.createCell(0).setCellValue(drugName);
sheetRow.createCell(1).setCellValue(oldResult); row.createCell(1).setCellValue(oldResult);
sheetRow.createCell(2).setCellValue(materialVOList.size()); row.createCell(2).setCellValue(materials.size());
sheetRow.createCell(3).setCellValue(sizeMap.get(oldResult)); row.createCell(3).setCellValue(sizeMap.getOrDefault(oldResult, 0));
sheetRow.createCell(4).setCellValue(String.format("%.2f", materialVOList.size() * 1.0 / sizeMap.get(oldResult) * 100) + "%"); row.createCell(4).setCellValue(String.format("%.2f%%", materials.size() * 100.0 / sizeMap.getOrDefault(oldResult, 1)));
} else { } else {
Map<String, List<EntrustmentIdentificationMaterialVO>> map = materialVOList.stream().collect(Collectors.groupingBy(EntrustmentIdentificationMaterialVO::getOldIdentificationResult)); Map<String, List<EntrustmentIdentificationMaterialVO>> groupedData = materials.stream().collect(Collectors.groupingBy(EntrustmentIdentificationMaterialVO::getOldIdentificationResult));
Set<String> keySet = map.keySet(); for (Map.Entry<String, List<EntrustmentIdentificationMaterialVO>> group : groupedData.entrySet()) {
for (String key : keySet) { row.createCell(0).setCellValue(drugName);
System.out.println(key); row.createCell(1).setCellValue(group.getKey());
int size = map.get(key).size(); row.createCell(2).setCellValue(group.getValue().size());
Row sheetRow = statisticsSheet.createRow(statisticsSheet.getLastRowNum() + 1); row.createCell(3).setCellValue(sizeMap.getOrDefault(group.getKey(), 0));
sheetRow.createCell(0).setCellValue(drugName); row.createCell(4).setCellValue(String.format("%.2f%%", group.getValue().size() * 100.0 / sizeMap.getOrDefault(group.getKey(), 1)));
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) + "%");
} }
} }
} }
}
private void createDrugSheets(Workbook workbook, Map<String, List<EntrustmentIdentificationMaterialVO>> resultDataMap, Integer entrustType) {
for (Map.Entry<String, List<EntrustmentIdentificationMaterialVO>> entry : resultDataMap.entrySet()) {
String drugName = entry.getKey().replaceAll("[^\u4e00-\u9fa5a-zA-Z0-9]", "");
Sheet sheet = workbook.createSheet(drugName);
for (String drugName : drugSet) { // 定义表头
List<EntrustmentIdentificationMaterialVO> materialVOList = resultDataMap.get(drugName); String[] headers = (entrustType == 0) ? new String[]{"检材类型", "检材编号", "检材名称", "检材性状", "检材质量", "委托单位", "受理日期", "检出结果"}
String title = drugName.replaceAll("[^\\u4e00-\\u9fa5a-zA-Z0-9]", ""); : new String[]{"检材类型", "检材编号", "检材名称", "检材性状", "检材质量", "年龄", "性别", "委托单位", "受理日期", "检出结果"};
sheet = workbook.createSheet(title);
Row row0 = sheet.createRow(0); // 创建表头行
for (int i = 0; i < headList.length; i++) { Row headerRow = sheet.createRow(0);
Cell cell = row0.createCell(i); for (int i = 0; i < headers.length; i++) {
cell.setCellValue(headList[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); int rowIndex = 1;
Row contentRow = sheet.createRow(i + 1); for (EntrustmentIdentificationMaterialVO vo : entry.getValue()) {
contentRow.createCell(0).setCellValue(vo.getOldIdentificationResult()); Row row = sheet.createRow(rowIndex++);
contentRow.createCell(1).setCellValue(vo.getAcceptNo()); fillRowData(row, vo, entrustType);
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);
}
} }
} }
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<DrugLite> 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<DrugLite> 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<String, Integer> getSizeMap(Integer entrustType, String oldResult, LocalDateTime startTime, LocalDateTime endTime) { public Map<String, Integer> getSizeMap(Integer entrustType, String oldResult, LocalDateTime startTime, LocalDateTime endTime) {
// 构造查询条件,查询 EntrustmentIdentificationMaterial 数据 // 构造查询条件,查询 EntrustmentIdentificationMaterial 数据
LambdaQueryWrapper<EntrustmentIdentificationMaterial> qw = new LambdaQueryWrapper<>(); LambdaQueryWrapper<EntrustmentIdentificationMaterial> qw = new LambdaQueryWrapper<>();
LocalDateTime queryStartTime = Optional.ofNullable(startTime).orElseGet(() -> this.getStartTime(null)); LocalDateTime queryStartTime = Optional.ofNullable(startTime).orElseGet(() -> this.getStartTime(null));
if (endTime != null) { if (endTime != null) {
qw.between(EntrustmentIdentificationMaterial::getAcceptTime, queryStartTime, endTime); qw.between(EntrustmentIdentificationMaterial::getAcceptTime, queryStartTime, endTime);
} else { } else {

@ -1627,4 +1627,26 @@ public class EntrustmentIdentificationMaterialServiceImpl extends ServiceImpl<En
} }
return cj; return cj;
} }
@Override
public List<EntrustmentIdentificationMaterial> sortByAcceptNo(List<EntrustmentIdentificationMaterial> 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<EntrustmentIdentificationMaterialVO> sortVoByAcceptNo(List<EntrustmentIdentificationMaterialVO> 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;
}
} }

@ -2459,8 +2459,8 @@ public class EntrustmentServiceImpl extends ServiceImpl<EntrustmentMapper, Entru
// HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); // HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
// // 让异步线程继承主线程的上下文 // // 让异步线程继承主线程的上下文
// RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); // RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
applicationContext.publishEvent(new PushDataToLabsCareEvent(this, entrust.getId(), 1)); // applicationContext.publishEvent(new PushDataToLabsCareEvent(this, entrust.getId(), 1));
applicationContext.publishEvent(new PushDataToLabsCareEvent(this, entrust.getId(), 2)); // applicationContext.publishEvent(new PushDataToLabsCareEvent(this, entrust.getId(), 2));
}); });
//同步第三方禁毒系统的受理功能 开始-------------------------------------------------------------- //同步第三方禁毒系统的受理功能 开始--------------------------------------------------------------

Loading…
Cancel
Save