更新了生成统计Excel的功能

master
杨海航 2 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.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);
}
}

@ -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.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<EntrustMa
*/
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;
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;
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<Entrus
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) {
qw.ge(startTime != null, EntrustMaterialCheckoutResult::getCreateTime, startTime)
.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, ""));
wrapper.between(Entrustment::getAcceptTime, startTime, endTime);
// 如果没有查询到数据,则直接返回空列表
if (CollUtil.isEmpty(resultList)) {
return Collections.emptyList();
} else {
wrapper.ge(Entrustment::getAcceptTime, this.getStartTime(null));
}
// 按委托 ID 进行分组
Map<String, List<EntrustMaterialCheckoutResult>> resultMap = resultList.stream()
.collect(Collectors.groupingBy(EntrustMaterialCheckoutResult::getEntrustId));
// 获取所有的委托 ID
Set<String> entrustIdList = resultMap.keySet();
LambdaQueryWrapper<Entrustment> 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<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)) {
return Collections.emptyList();
}
// 获取所有符合条件的委托 ID
Set<String> filteredEntrustIds = entrustmentList.stream()
// 获取符合条件的委托 ID 集合,提高查询效率
Set<String> entrustIds = entrustmentList.stream()
.map(Entrustment::getId)
.collect(Collectors.toSet());
// 获取所有符合条件的委托map
// 将 Entrustment 列表转换为 Map,提高后续查询速度
Map<String, Entrustment> entrustMap = entrustmentList.stream()
.collect(Collectors.toMap(Entrustment::getId, Function.identity()));
// 过滤出符合条件的检测结果
List<EntrustMaterialCheckoutResult> newResultList = resultList.stream()
.filter(result -> filteredEntrustIds.contains(result.getEntrustId()))
.collect(Collectors.toList());
// 查询所有符合条件的 EntrustmentIdentificationMaterial 数据
List<EntrustmentIdentificationMaterial> materialList = entrustmentIdentificationMaterialService.list(
Wrappers.<EntrustmentIdentificationMaterial>lambdaQuery()
.in(EntrustmentIdentificationMaterial::getEntrustmentId, entrustIds)
);
// 若无符合条件的材料,则直接返回空列表
if (CollUtil.isEmpty(materialList)) {
return Collections.emptyList();
}
// 提取所有 resultId 批量查询 EntrustmentIdentificationMaterial,避免循环查询数据库
Set<String> resultIds = newResultList.stream()
.map(EntrustMaterialCheckoutResult::getId)
// 获取材料 ID 集合,提高查询效率
Set<String> materialIds = materialList.stream()
.map(EntrustmentIdentificationMaterial::getId)
.collect(Collectors.toSet());
// 批量查询所有相关的材料信息
List<EntrustmentIdentificationMaterial> materialList = entrustmentIdentificationMaterialService.list(Wrappers
.<EntrustmentIdentificationMaterial>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<String, EntrustmentIdentificationMaterial> materialMap = materialList.stream()
.collect(Collectors.toMap(EntrustmentIdentificationMaterial::getId, Function.identity()));
// 构建返回的 VO 对象列表
List<EntrustmentIdentificationMaterialVO> 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<EntrustMaterialCheckoutResult> resultList = this.list(
Wrappers.<EntrustMaterialCheckoutResult>lambdaQuery()
.in(EntrustMaterialCheckoutResult::getId, materialIds)
);
// 将检测结果转换为 Map,避免后续 O(n^2) 复杂度查询
Map<String, EntrustMaterialCheckoutResult> resultMap = resultList.stream()
.collect(Collectors.toMap(EntrustMaterialCheckoutResult::getId, Function.identity()));
// 构造返回的 VO 列表
List<EntrustmentIdentificationMaterialVO> 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<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 {
// 1. 构建查询条件
LambdaQueryWrapper<Entrustment> qw = new LambdaQueryWrapper<>();
@ -766,165 +762,172 @@ public class EntrustMaterialCheckoutResultServiceImpl extends ServiceImpl<Entrus
}
@Override
public void getExcelByResult(HttpServletResponse response, Integer entrustType, String oldResult, LocalDateTime
startTime, LocalDateTime endTime) throws IOException {
List<EntrustmentIdentificationMaterialVO> resultData = this.getResultData(entrustType, oldResult, startTime, endTime);
Map<String, List<EntrustmentIdentificationMaterialVO>> resultDataMap = this.getResultDataMap(entrustType, oldResult, startTime, endTime);
public R getExcelByResult(HttpServletResponse response, ResultExcelDTO excelDTO) throws IOException {
// 获取查询数据
List<EntrustmentIdentificationMaterialVO> resultData = this.getResultData(excelDTO);
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()) {
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<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++) {
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<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 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<String> drugSet = resultDataMap.keySet();
private void createStatisticsSheet(Workbook workbook, Map<String, List<EntrustmentIdentificationMaterialVO>> 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<String, Integer> sizeMap = this.getSizeMap(entrustType, oldResult, startTime, endTime);
System.out.println(sizeMap);
for (String drugName : drugSet) {
List<EntrustmentIdentificationMaterialVO> materialVOList = resultDataMap.get(drugName);
// 填充统计数据
int rowIndex = 1;
for (Map.Entry<String, List<EntrustmentIdentificationMaterialVO>> entry : resultDataMap.entrySet()) {
String drugName = entry.getKey();
List<EntrustmentIdentificationMaterialVO> 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<String, List<EntrustmentIdentificationMaterialVO>> map = materialVOList.stream().collect(Collectors.groupingBy(EntrustmentIdentificationMaterialVO::getOldIdentificationResult));
Set<String> 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<String, List<EntrustmentIdentificationMaterialVO>> groupedData = materials.stream().collect(Collectors.groupingBy(EntrustmentIdentificationMaterialVO::getOldIdentificationResult));
for (Map.Entry<String, List<EntrustmentIdentificationMaterialVO>> 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<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 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<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) {
// 构造查询条件,查询 EntrustmentIdentificationMaterial 数据
LambdaQueryWrapper<EntrustmentIdentificationMaterial> qw = new LambdaQueryWrapper<>();
LocalDateTime queryStartTime = Optional.ofNullable(startTime).orElseGet(() -> this.getStartTime(null));
if (endTime != null) {
qw.between(EntrustmentIdentificationMaterial::getAcceptTime, queryStartTime, endTime);
} else {

@ -1627,4 +1627,26 @@ public class EntrustmentIdentificationMaterialServiceImpl extends ServiceImpl<En
}
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();
// // 让异步线程继承主线程的上下文
// RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
applicationContext.publishEvent(new PushDataToLabsCareEvent(this, entrust.getId(), 1));
applicationContext.publishEvent(new PushDataToLabsCareEvent(this, entrust.getId(), 2));
// applicationContext.publishEvent(new PushDataToLabsCareEvent(this, entrust.getId(), 1));
// applicationContext.publishEvent(new PushDataToLabsCareEvent(this, entrust.getId(), 2));
});
//同步第三方禁毒系统的受理功能 开始--------------------------------------------------------------

Loading…
Cancel
Save