From da71a8ba41a4e2b0fa2e7aaf8ae4ebeb13c004ef Mon Sep 17 00:00:00 2001 From: chen <2710907404@qq.com> Date: Mon, 14 Jul 2025 14:03:06 +0800 Subject: [PATCH] =?UTF-8?q?20250714=20=E6=9B=B4=E6=96=B0=201.=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=A0=B9=E6=8D=AE=E5=A7=94=E6=89=98=E5=8D=95=E4=BD=8D?= =?UTF-8?q?=E5=8E=BB=E5=AF=BC=E5=87=BA=E5=AF=B9=E5=BA=94=E7=9A=84=E5=A7=94?= =?UTF-8?q?=E6=89=98=E4=BF=A1=E6=81=AF=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ler.java => ExcelOperationController.java} | 20 +- .../enums/EntrustExportHeaderEnum.java | 33 ++ .../EntrustMaterialCheckoutResultService.java | 9 + ...ervice.java => ExcelOperationService.java} | 13 +- ...rustMaterialCheckoutResultServiceImpl.java | 7 + .../impl/ExcelOperationServiceImpl.java | 393 ++++++++++++++++++ .../service/impl/ExcelUploadServiceImpl.java | 230 ---------- 7 files changed, 469 insertions(+), 236 deletions(-) rename src/main/java/digital/laboratory/platform/entrustment/controller/{ExcelUploadController.java => ExcelOperationController.java} (56%) create mode 100644 src/main/java/digital/laboratory/platform/entrustment/enums/EntrustExportHeaderEnum.java rename src/main/java/digital/laboratory/platform/entrustment/service/{ExcelUploadService.java => ExcelOperationService.java} (55%) create mode 100644 src/main/java/digital/laboratory/platform/entrustment/service/impl/ExcelOperationServiceImpl.java delete mode 100644 src/main/java/digital/laboratory/platform/entrustment/service/impl/ExcelUploadServiceImpl.java diff --git a/src/main/java/digital/laboratory/platform/entrustment/controller/ExcelUploadController.java b/src/main/java/digital/laboratory/platform/entrustment/controller/ExcelOperationController.java similarity index 56% rename from src/main/java/digital/laboratory/platform/entrustment/controller/ExcelUploadController.java rename to src/main/java/digital/laboratory/platform/entrustment/controller/ExcelOperationController.java index 2124e68..44ad7f4 100644 --- a/src/main/java/digital/laboratory/platform/entrustment/controller/ExcelUploadController.java +++ b/src/main/java/digital/laboratory/platform/entrustment/controller/ExcelOperationController.java @@ -1,14 +1,18 @@ package digital.laboratory.platform.entrustment.controller; import digital.laboratory.platform.common.core.util.R; -import digital.laboratory.platform.entrustment.service.ExcelUploadService; -import digital.laboratory.platform.entrustment.utils.ExcelUtils; +import digital.laboratory.platform.entrustment.service.ExcelOperationService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; /** * @author ChenJiangBao @@ -19,20 +23,26 @@ import javax.annotation.Resource; @RestController @RequestMapping("/papp/excel") @Api(tags = "20-excel上传接口控制器接口管理", description = "excel上传接口控制器接口管理") -public class ExcelUploadController { +public class ExcelOperationController { @Resource - private ExcelUploadService excelUploadService; + private ExcelOperationService excelOperationService; @ApiOperation("上传两社人员名单excel文件") @PostMapping("/uploadHairInspect") public R uploadHairInspectExcel(@RequestPart("file") MultipartFile file, @RequestParam("entrustId") String entrustId) { try { - Boolean success = excelUploadService.uploadHairInspectExcel(file, entrustId); + Boolean success = excelOperationService.uploadHairInspectExcel(file, entrustId); return R.ok(success); } catch (Exception e) { e.printStackTrace(); return R.failed(e.getMessage()); } } + + @ApiOperation("根据委托单位去导出对应的委托案件信息") + @GetMapping("/exportByEntrustOrg") + public void exportByEntrustOrg(@RequestParam("year") Integer year, HttpServletResponse response) throws IOException { + excelOperationService.exportByEntrustOrg(year, response); + } } diff --git a/src/main/java/digital/laboratory/platform/entrustment/enums/EntrustExportHeaderEnum.java b/src/main/java/digital/laboratory/platform/entrustment/enums/EntrustExportHeaderEnum.java new file mode 100644 index 0000000..5ca5e84 --- /dev/null +++ b/src/main/java/digital/laboratory/platform/entrustment/enums/EntrustExportHeaderEnum.java @@ -0,0 +1,33 @@ +package digital.laboratory.platform.entrustment.enums; + +import lombok.Getter; + +/** + * @author ChenJiangBao + * @version 1.0 + * @description: 委托信息导出excel表头枚举 + * @date 2025/7/14 10:47 + */ +@Getter +public enum EntrustExportHeaderEnum { + CASE_NAME("caseName", "案件名称"), + CASE_BRIEF("caseBrief", "案情简要"), + HAPPEN_TIME("happenTime", "案发时间"), + SUSPECT_INFO("suspectInfo", "嫌疑人信息"), + CLIENT_ORG_NAME("clientOrgName", "委托单位"), + ENTRUST_TIME("entrustmentTime", "送检时间"), + ACCEPT_NO("acceptNo", "受理编号"), + ACCEPT_TIME("acceptTime", "受理时间"), + ENTRUST_REQUIREMENT("entrustRequirement", "鉴定要求"), + MATERIAL_CHARACTER("materialCharacterDesc", "性状描述"), + MATERIAL_COUNT("materialCount", "检材数量"), + MATERIAL_RESULT("materialResult", "检材检测结果"); + + private final String field; // 字段名,用于匹配数据 + private final String header; // Excel列名,用于显示 + + EntrustExportHeaderEnum(String field, String header) { + this.field = field; + this.header = header; + } +} 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 34eab41..2cd5cb7 100644 --- a/src/main/java/digital/laboratory/platform/entrustment/service/EntrustMaterialCheckoutResultService.java +++ b/src/main/java/digital/laboratory/platform/entrustment/service/EntrustMaterialCheckoutResultService.java @@ -9,6 +9,7 @@ import digital.laboratory.platform.entrustment.dto.EntrustMaterialCheckoutResult import digital.laboratory.platform.entrustment.dto.GenerateQuarterlyReportDTO; import digital.laboratory.platform.entrustment.dto.ResultExcelDTO; import digital.laboratory.platform.entrustment.entity.EntrustMaterialCheckoutResult; +import digital.laboratory.platform.entrustment.entity.EntrustmentIdentificationMaterial; import digital.laboratory.platform.entrustment.query.EntrustMaterialCheckoutResultQuery; import digital.laboratory.platform.entrustment.vo.DetectionRateVO; import digital.laboratory.platform.entrustment.vo.EntrustMaterialCheckoutResultVO; @@ -90,6 +91,14 @@ public interface EntrustMaterialCheckoutResultService extends IService materialList); + R pushSuspectDetectionResultTask(DLPUser dlpUser); String generateQuarterlyReportExcel(GenerateQuarterlyReportDTO dto, HttpServletResponse response) throws IOException; diff --git a/src/main/java/digital/laboratory/platform/entrustment/service/ExcelUploadService.java b/src/main/java/digital/laboratory/platform/entrustment/service/ExcelOperationService.java similarity index 55% rename from src/main/java/digital/laboratory/platform/entrustment/service/ExcelUploadService.java rename to src/main/java/digital/laboratory/platform/entrustment/service/ExcelOperationService.java index 86dfb03..21d459f 100644 --- a/src/main/java/digital/laboratory/platform/entrustment/service/ExcelUploadService.java +++ b/src/main/java/digital/laboratory/platform/entrustment/service/ExcelOperationService.java @@ -2,13 +2,16 @@ package digital.laboratory.platform.entrustment.service; import org.springframework.web.multipart.MultipartFile; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + /** * @author ChenJiangBao * @version 1.0 * @description: excel上传服务层接口 * @date 2025/4/16 11:55 */ -public interface ExcelUploadService { +public interface ExcelOperationService { /** * 上传两社人员名单excel文件 @@ -18,4 +21,12 @@ public interface ExcelUploadService { */ Boolean uploadHairInspectExcel(MultipartFile file, String entrustId) throws Exception; + /** + * 根据当前用户的机构id查询属于这个机构的委托信息 + * + * @param year + * @param response + */ + void exportByEntrustOrg(Integer year, HttpServletResponse response) throws IOException; + } 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 7d784be..dc97da2 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 @@ -1472,6 +1472,13 @@ public class EntrustMaterialCheckoutResultServiceImpl extends ServiceImpl materialList) { if (CollUtil.isEmpty(materialList)) { return StrUtil.EMPTY; diff --git a/src/main/java/digital/laboratory/platform/entrustment/service/impl/ExcelOperationServiceImpl.java b/src/main/java/digital/laboratory/platform/entrustment/service/impl/ExcelOperationServiceImpl.java new file mode 100644 index 0000000..63144c7 --- /dev/null +++ b/src/main/java/digital/laboratory/platform/entrustment/service/impl/ExcelOperationServiceImpl.java @@ -0,0 +1,393 @@ +package digital.laboratory.platform.entrustment.service.impl; + +import cn.hutool.core.collection.CollUtil; +import com.baomidou.mybatisplus.core.toolkit.IdWorker; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import digital.laboratory.platform.common.core.exception.CheckedException; +import digital.laboratory.platform.common.core.util.R; +import digital.laboratory.platform.common.mybatis.security.service.DLPUser; +import digital.laboratory.platform.common.security.util.SecurityUtils; +import digital.laboratory.platform.entrustment.entity.*; +import digital.laboratory.platform.entrustment.enums.AnalysisOptionEnums; +import digital.laboratory.platform.entrustment.enums.EntrustExportHeaderEnum; +import digital.laboratory.platform.entrustment.enums.EntrustIdentificationSituationType; +import digital.laboratory.platform.entrustment.enums.EntrustStatusConstants; +import digital.laboratory.platform.entrustment.mapper.EntrustmentMapper; +import digital.laboratory.platform.entrustment.service.*; +import digital.laboratory.platform.entrustment.utils.ExcelUtils; +import digital.laboratory.platform.entrustment.vo.EntrustmentVO; +import digital.laboratory.platform.sys.entity.DrugLite; +import digital.laboratory.platform.sys.enums.entrust.EntrustBiologyType; +import digital.laboratory.platform.sys.feign.RemoteCommDrugService; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.xssf.streaming.SXSSFWorkbook; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.math.BigDecimal; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +/** + * @author ChenJiangBao + * @version 1.0 + * @description: excel上传服务层接口 实现类 + * @date 2025/4/16 11:55 + */ +@Service +public class ExcelOperationServiceImpl implements ExcelOperationService { + + @Resource + private SampleService sampleService; + + @Resource + private CaseEventService caseEventService; + + @Resource + private EntrustmentService entrustmentService; + + @Resource + private EntrustmentMapper entrustmentMapper; + + @Resource + private EntrustmentIdentificationMaterialService entrustmentIdentificationMaterialService; + + @Resource + private EntrustMaterialCheckoutResultService entrustMaterialCheckoutResultService; + + @Resource + private RemoteCommDrugService remoteCommDrugService; + + @Resource + private SuspectService suspectService; + + /** + * 上传两社人员名单Excel文件,并自动生成检材与检测结果数据 + * + * @param file Excel文件(包含人员信息) + * @param entrustId 委托ID + * @return 是否成功导入 + * @throws Exception 发生异常时抛出 + */ + @Override + public Boolean uploadHairInspectExcel(MultipartFile file, String entrustId) throws Exception { + + // 1. 校验委托合法性 + Entrustment entrustment = entrustmentService.getById(entrustId); + if (entrustment == null) { + throw new CheckedException(String.format("委托id为 %s 的委托信息不存在!", entrustId)); + } + if (!entrustment.getOldIdentificationResult().equals(EntrustIdentificationSituationType.TWO_AGENCY.getDesc())) { + throw new CheckedException("当前不支持两社人员之外的委托信息进行导入!"); + } + + // 2. 获取案件与毒品信息 + // 2.1 校验案件信息 + CaseEvent cj = caseEventService.validateCaseInfo(entrustment.getCaseId()); + + // 2.2 获取默认毒品(海洛因) + + + // 2.3 获取所有毒品名称映射(便于匹配检测结果) + R> innerGetAllR = remoteCommDrugService.innerGetAll(); + List drugLiteList = innerGetAllR.getData(); + Map drugLiteMap = drugLiteList.stream() + .collect(Collectors.toMap(DrugLite::getName, Function.identity())); + + // 3. 读取Excel内容(从第一行数据开始读取) + int headIndex = 1; + List> data = ExcelUtils.readExcel(file, headIndex); + + // 3.1 获取当前委托下已有检材数,作为orderNo起始编号 + int orderNo = entrustmentIdentificationMaterialService.lambdaQuery() + .eq(EntrustmentIdentificationMaterial::getEntrustmentId, entrustId) + .list().size() + 1; + + // 3.2 初始化结果对象集合 + List results = new ArrayList<>(); + + ArrayList suspects = new ArrayList<>(); + for (Map datum : data) { + Suspect suspect = new Suspect(); + suspect.setName(datum.get("姓名")); + suspect.setIdNumber(datum.get("身份证号")); + suspects.add(suspect); + } + if (CollUtil.isNotEmpty(suspects)) { + suspectService.remove(Wrappers.lambdaQuery() + .eq(Suspect::getEntrustId, entrustId)); + suspectService.addSuspectList(suspects, entrustId); + } + List entrustmentIdentificationMaterialList = new ArrayList<>(); + + // 4. 遍历Excel数据行,构建检材与检测结果 + for (Map datum : data) { + // 构建检材对象 + EntrustmentIdentificationMaterial material = new EntrustmentIdentificationMaterial(); + material.setEntrustmentId(entrustment.getId()); + material.setCaseId(entrustment.getCaseId()); + material.setName(datum.get("姓名") + "毛发"); + material.setRemark(datum.get("身份证号")); + + // 解析身份证号 + String idNum = datum.get("身份证号"); + if (idNum != null && idNum.length() == 18) { + material.setBiologyGender(this.getSexByIdNum(idNum)); + material.setMaterialAge(Integer.valueOf(this.getAgeByIdNum(idNum))); + } + + material.setDrawPlace(datum.get("采样单位")); + material.setId(IdWorker.get32UUID().toUpperCase()); + + // 检测结果解析 + ArrayList matchedDrugs = new ArrayList<>(); + String[] drugs = datum.get("检测结果").split("、"); + if (drugs.length == 0 || (drugs.length == 1 && drugs[0].trim().isEmpty())) { + matchedDrugs.add(drugLiteMap.get("海洛因")); + } else { + boolean matched = false; + for (String drugName : drugs) { + if (drugLiteMap.containsKey(drugName)) { + matchedDrugs.add(drugLiteMap.get(drugName)); + matched = true; + } + } + if (!matched) { + matchedDrugs.add(drugLiteMap.get("海洛因")); + } + } + material.setCandidateDrugs(matchedDrugs); + + // 设置检材默认属性 + material.setBiologyType(EntrustBiologyType.HAIR.getDesc()); + material.setType("1"); + material.setTypeName("生物样本"); + material.setQuantity(new BigDecimal(50)); + material.setUnit("mg"); + material.setForm("黑色头发"); + material.setFormName("黑色头发"); + material.setOrderNo(orderNo); + material.setImEntrustNumber(String.valueOf(orderNo)); + orderNo++; + material.setPackComplete(true); + material.setRtSampleQuantity(0); + material.setDrawTime(LocalDateTime.now()); + material.setAnalysisOption(AnalysisOptionEnums.QUALITATIVE.getCode()); + + entrustmentIdentificationMaterialService.setMaterialIdentificationNo(material, cj); + material.setSample1No(sampleService.getNewSampleNo(material.getImNo(), 1)); + + entrustmentIdentificationMaterialList.add(material); + } +// entrustMaterialCheckoutResultService.saveBatch(results); + boolean isSave = entrustmentIdentificationMaterialService.saveBatch(entrustmentIdentificationMaterialList); + String entrustReq = entrustmentService.buildEntrustReq(entrustmentIdentificationMaterialList); + entrustment.setEntrustRequirement(entrustReq); + entrustmentService.updateById(entrustment); + // 5. 批量保存检材信息和检测结果 + return isSave; + } + + /** + * 根据当前用户的机构id查询属于这个机构的委托信息 + * + * @param year 年份,用于筛选特定年份的委托信息 + * @param response + */ + @Override + public void exportByEntrustOrg(Integer year, HttpServletResponse response) throws IOException { + DLPUser user = SecurityUtils.getUser(); + // 查询委托单位是当前用户机构id的委托信息 + List entrustmentVOList = entrustmentMapper.getEntrustmentVOList( + Wrappers.query() + .eq("e.client_org_id", user.getOrgId()) + .eq("e.status", EntrustStatusConstants.ENTRUST_STATUS_ACCEPTED.getStatus()) + .likeRight(year != null , "e.accept_time", year) + .orderByDesc("e.accept_time") + ); + + if (CollUtil.isEmpty(entrustmentVOList)) { + throw new CheckedException(String.format("当前用户机构 [%s] 无委托信息!", user.getOrgName())); + } + + // 获取检材信息 + List identificationMaterials = entrustmentIdentificationMaterialService.list( + Wrappers.lambdaQuery() + .in(EntrustmentIdentificationMaterial::getEntrustmentId, + entrustmentVOList.stream().map(EntrustmentVO::getId).collect(Collectors.toList())) + ); + + // 分组 + Map> materialGroupMap = identificationMaterials.stream().collect(Collectors.groupingBy(EntrustmentIdentificationMaterial::getEntrustmentId)); + + + + // 获取嫌疑人信息并根据委托id分组 + Map suspectGroupMap = buildSuspectInfoGroupByEntrustId(entrustmentVOList); + + SXSSFWorkbook workbook = new SXSSFWorkbook(); + Sheet sheet = workbook.createSheet(user.getOrgName() + "已受理委托信息统计"); + + // ====== 合并第一行单元格 (0,0) 到 (0, 列数-1) + List headers = CollUtil.newArrayList(EntrustExportHeaderEnum.values()); + sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, headers.size() - 1)); + + // ====== 设置标题行内容 + Row titleRow = sheet.createRow(0); + Cell titleCell = titleRow.createCell(0); + titleCell.setCellValue(user.getOrgName() + "已受理委托信息统计"); + + // 可选:设置样式(加粗、居中) + CellStyle titleStyle = workbook.createCellStyle(); + Font font = workbook.createFont(); + font.setBold(true); + font.setFontHeightInPoints((short) 14); + titleStyle.setFont(font); + titleStyle.setAlignment(HorizontalAlignment.CENTER); + titleCell.setCellStyle(titleStyle); + + // ====== 第二行为表头 + Row headerRow = sheet.createRow(1); + for (int i = 0; i < headers.size(); i++) { + headerRow.createCell(i).setCellValue(headers.get(i).getHeader()); +// // 设置自动长度 +// sheet.autoSizeColumn(i); + } + + // ====== 第三行开始写入数据 + for (int i = 0; i < entrustmentVOList.size(); i++) { + EntrustmentVO vo = entrustmentVOList.get(i); + Row row = sheet.createRow(i + 2); + + for (int j = 0; j < headers.size(); j++) { + EntrustExportHeaderEnum header = headers.get(j); + Cell cell = row.createCell(j); + + switch (header) { + case CASE_NAME: + cell.setCellValue(vo.getCaseName()); + break; + case CASE_BRIEF: + cell.setCellValue(vo.getCaseBrief()); + break; + case HAPPEN_TIME: + cell.setCellValue(vo.getHappenTime()); + break; + case SUSPECT_INFO: + cell.setCellValue(suspectGroupMap.get(vo.getId())); + break; + case CLIENT_ORG_NAME: + cell.setCellValue(vo.getClientOrgName()); + break; + case ENTRUST_TIME: + cell.setCellValue(vo.getEntrustmentTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"))); + break; + case ACCEPT_NO: + cell.setCellValue(vo.getAcceptNo()); + break; + case ACCEPT_TIME: + cell.setCellValue(vo.getAcceptTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"))); + break; + case ENTRUST_REQUIREMENT: + cell.setCellValue(vo.getEntrustRequirement()); + break; + case MATERIAL_CHARACTER: + cell.setCellValue(entrustMaterialCheckoutResultService.buildMaterialCharacterDesc(materialGroupMap.get(vo.getId()))); + break; + case MATERIAL_COUNT: + cell.setCellValue(vo.getIdentificationMaterialCount()); + break; + case MATERIAL_RESULT: + cell.setCellValue(entrustMaterialCheckoutResultService.buildEntrustMaterialCheckoutResultStr(vo.getId())); + break; + } + } + } + + + // ====== 输出到浏览器 +// response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); +// String filename = user.getOrgName() + "已受理委托信息统计" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + ".xlsx"; +// response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8.name())); + workbook.write(response.getOutputStream()); + workbook.close(); + } + + /** + * 根据委托id分组,拼接嫌疑人姓名和身份证 + * @param entrustmentVOList + * @return + */ + private Map buildSuspectInfoGroupByEntrustId(List entrustmentVOList) { + List suspectList = suspectService.list( + Wrappers.lambdaQuery() + .in(Suspect::getEntrustId, + entrustmentVOList.stream().map(EntrustmentVO::getId).collect(Collectors.toList())) + ); + + // 分组 + Map suspectGroupMap = suspectList + .stream() + .collect( + Collectors.groupingBy( // 按照entrustId分组 + Suspect::getEntrustId, + Collectors.collectingAndThen( // 收集器:将分组后的列表转换为字符串, 拼接嫌疑人姓名和身份证 + Collectors.toList(), + groupList -> groupList + .stream() + .map(item -> String.format("%s : %s", item.getName(), item.getIdNumber())) + .collect(Collectors.joining("、")) + ) + ) + ); + return suspectGroupMap; + } + + + public String getSexByIdNum(String idNumber) { + // 去除空格并转换为大写 + idNumber = idNumber.trim().toUpperCase(); + + // 校验身份证长度 + if (idNumber.length() == 18) { + try { + // 提取性别 + int genderCode = Integer.parseInt(idNumber.substring(16, 17)); + return genderCode % 2 == 0 ? "女" : "男"; + } catch (Exception e) { + + } + } + return ""; + } + + + public String getAgeByIdNum(String idNumber) { + if (idNumber.length() == 18) { + try { + // 提取出生日期并计算年龄 + String birthDateStr = idNumber.substring(6, 14); + LocalDate birthDate = LocalDate.parse(birthDateStr, DateTimeFormatter.BASIC_ISO_DATE); + LocalDate currentDate = LocalDate.now(); + long age = ChronoUnit.YEARS.between(birthDate, currentDate); + return String.valueOf(age); + + } catch (Exception e) { + } + } + return ""; + } +} diff --git a/src/main/java/digital/laboratory/platform/entrustment/service/impl/ExcelUploadServiceImpl.java b/src/main/java/digital/laboratory/platform/entrustment/service/impl/ExcelUploadServiceImpl.java deleted file mode 100644 index 6d7b9af..0000000 --- a/src/main/java/digital/laboratory/platform/entrustment/service/impl/ExcelUploadServiceImpl.java +++ /dev/null @@ -1,230 +0,0 @@ -package digital.laboratory.platform.entrustment.service.impl; - -import cn.hutool.core.collection.CollUtil; -import com.alibaba.fastjson.JSON; -import com.baomidou.mybatisplus.core.toolkit.IdWorker; -import com.baomidou.mybatisplus.core.toolkit.Wrappers; -import digital.laboratory.platform.common.core.exception.CheckedException; -import digital.laboratory.platform.common.core.util.R; -import digital.laboratory.platform.entrustment.entity.*; -import digital.laboratory.platform.entrustment.enums.AnalysisOptionEnums; -import digital.laboratory.platform.entrustment.enums.EntrustIdentificationSituationType; -import digital.laboratory.platform.entrustment.service.*; -import digital.laboratory.platform.entrustment.utils.ExcelUtils; -import digital.laboratory.platform.sys.entity.DrugLite; -import digital.laboratory.platform.sys.enums.entrust.EntrustBiologyType; -import digital.laboratory.platform.sys.feign.RemoteCommDrugService; -import org.springframework.stereotype.Service; -import org.springframework.web.multipart.MultipartFile; - -import javax.annotation.Resource; -import java.math.BigDecimal; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; -import java.time.temporal.ChronoUnit; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.function.Function; -import java.util.stream.Collectors; - -/** - * @author ChenJiangBao - * @version 1.0 - * @description: excel上传服务层接口 实现类 - * @date 2025/4/16 11:55 - */ -@Service -public class ExcelUploadServiceImpl implements ExcelUploadService { - - @Resource - private SampleService sampleService; - - @Resource - private CaseEventService caseEventService; - - @Resource - private EntrustmentService entrustmentService; - - @Resource - private EntrustmentIdentificationMaterialService entrustmentIdentificationMaterialService; - - @Resource - private RemoteCommDrugService remoteCommDrugService; - - @Resource - private EntrustMaterialCheckoutResultService entrustMaterialCheckoutResultService; - - @Resource - private SuspectService suspectService; - - /** - * 上传两社人员名单Excel文件,并自动生成检材与检测结果数据 - * - * @param file Excel文件(包含人员信息) - * @param entrustId 委托ID - * @return 是否成功导入 - * @throws Exception 发生异常时抛出 - */ - @Override - public Boolean uploadHairInspectExcel(MultipartFile file, String entrustId) throws Exception { - - // 1. 校验委托合法性 - Entrustment entrustment = entrustmentService.getById(entrustId); - if (entrustment == null) { - throw new CheckedException(String.format("委托id为 %s 的委托信息不存在!", entrustId)); - } - if (!entrustment.getOldIdentificationResult().equals(EntrustIdentificationSituationType.TWO_AGENCY.getDesc())) { - throw new CheckedException("当前不支持两社人员之外的委托信息进行导入!"); - } - - // 2. 获取案件与毒品信息 - // 2.1 校验案件信息 - CaseEvent cj = caseEventService.validateCaseInfo(entrustment.getCaseId()); - - // 2.2 获取默认毒品(海洛因) - - - // 2.3 获取所有毒品名称映射(便于匹配检测结果) - R> innerGetAllR = remoteCommDrugService.innerGetAll(); - List drugLiteList = innerGetAllR.getData(); - Map drugLiteMap = drugLiteList.stream() - .collect(Collectors.toMap(DrugLite::getName, Function.identity())); - - // 3. 读取Excel内容(从第一行数据开始读取) - int headIndex = 1; - List> data = ExcelUtils.readExcel(file, headIndex); - - // 3.1 获取当前委托下已有检材数,作为orderNo起始编号 - int orderNo = entrustmentIdentificationMaterialService.lambdaQuery() - .eq(EntrustmentIdentificationMaterial::getEntrustmentId, entrustId) - .list().size() + 1; - - // 3.2 初始化结果对象集合 - List results = new ArrayList<>(); - - ArrayList suspects = new ArrayList<>(); - for (Map datum : data) { - Suspect suspect = new Suspect(); - suspect.setName(datum.get("姓名")); - suspect.setIdNumber(datum.get("身份证号")); - suspects.add(suspect); - } - if (CollUtil.isNotEmpty(suspects)){ - suspectService.remove(Wrappers.lambdaQuery() - .eq(Suspect::getEntrustId, entrustId)); - suspectService.addSuspectList(suspects, entrustId); - } - List entrustmentIdentificationMaterialList = new ArrayList<>(); - - // 4. 遍历Excel数据行,构建检材与检测结果 - for (Map datum : data) { - // 构建检材对象 - EntrustmentIdentificationMaterial material = new EntrustmentIdentificationMaterial(); - material.setEntrustmentId(entrustment.getId()); - material.setCaseId(entrustment.getCaseId()); - material.setName(datum.get("姓名") + "毛发"); - material.setRemark(datum.get("身份证号")); - - // 解析身份证号 - String idNum = datum.get("身份证号"); - if (idNum != null && idNum.length() == 18) { - material.setBiologyGender(this.getSexByIdNum(idNum)); - material.setMaterialAge(Integer.valueOf(this.getAgeByIdNum(idNum))); - } - - material.setDrawPlace(datum.get("采样单位")); - material.setId(IdWorker.get32UUID().toUpperCase()); - - // 检测结果解析 - ArrayList matchedDrugs = new ArrayList<>(); - String[] drugs = datum.get("检测结果").split("、"); - if (drugs.length == 0 || (drugs.length == 1 && drugs[0].trim().isEmpty())){ - matchedDrugs.add(drugLiteMap.get("海洛因")); - }else { - boolean matched = false; - for (String drugName : drugs) { - if (drugLiteMap.containsKey(drugName)) { - matchedDrugs.add(drugLiteMap.get(drugName)); - matched = true; - } - } - if (!matched) { - matchedDrugs.add(drugLiteMap.get("海洛因")); - } - } - material.setCandidateDrugs(matchedDrugs); -// if (!matchedDrugs.isEmpty()) { -// EntrustMaterialCheckoutResult result = new EntrustMaterialCheckoutResult(); -// result.setId(material.getId()); -// result.setQualitativeResult(JSON.toJSONString(matchedDrugs)); -// result.setEntrustId(entrustId); -// results.add(result); -// } - - // 设置检材默认属性 - material.setBiologyType(EntrustBiologyType.HAIR.getDesc()); - material.setType("1"); - material.setTypeName("生物样本"); - material.setQuantity(new BigDecimal(50)); - material.setUnit("mg"); - material.setForm("黑色头发"); - material.setFormName("黑色头发"); - material.setOrderNo(orderNo); - material.setImEntrustNumber(String.valueOf(orderNo)); - orderNo++; - material.setPackComplete(true); - material.setRtSampleQuantity(0); - material.setDrawTime(LocalDateTime.now()); - material.setAnalysisOption(AnalysisOptionEnums.QUALITATIVE.getCode()); - - entrustmentIdentificationMaterialService.setMaterialIdentificationNo(material, cj); - material.setSample1No(sampleService.getNewSampleNo(material.getImNo(), 1)); - - entrustmentIdentificationMaterialList.add(material); - } -// entrustMaterialCheckoutResultService.saveBatch(results); - boolean isSave = entrustmentIdentificationMaterialService.saveBatch(entrustmentIdentificationMaterialList); - String entrustReq = entrustmentService.buildEntrustReq(entrustmentIdentificationMaterialList); - entrustment.setEntrustRequirement(entrustReq); - entrustmentService.updateById(entrustment); - // 5. 批量保存检材信息和检测结果 - return isSave; - } - - - public String getSexByIdNum(String idNumber) { - // 去除空格并转换为大写 - idNumber = idNumber.trim().toUpperCase(); - - // 校验身份证长度 - if (idNumber.length() == 18) { - try { - // 提取性别 - int genderCode = Integer.parseInt(idNumber.substring(16, 17)); - return genderCode % 2 == 0 ? "女" : "男"; - } catch (Exception e) { - - } - } - return ""; - } - - - public String getAgeByIdNum(String idNumber) { - if (idNumber.length() == 18) { - try { - // 提取出生日期并计算年龄 - String birthDateStr = idNumber.substring(6, 14); - LocalDate birthDate = LocalDate.parse(birthDateStr, DateTimeFormatter.BASIC_ISO_DATE); - LocalDate currentDate = LocalDate.now(); - long age = ChronoUnit.YEARS.between(birthDate, currentDate); - return String.valueOf(age); - - } catch (Exception e) { - } - } - return ""; - } -}