diff --git a/src/main/java/digital/laboratory/platform/entrustment/controller/EntrustmentController.java b/src/main/java/digital/laboratory/platform/entrustment/controller/EntrustmentController.java index e6876df..bc99deb 100644 --- a/src/main/java/digital/laboratory/platform/entrustment/controller/EntrustmentController.java +++ b/src/main/java/digital/laboratory/platform/entrustment/controller/EntrustmentController.java @@ -2397,7 +2397,6 @@ public class EntrustmentController { } @ApiOperation("预览鉴定事项确认书,只在送检单位填写完委托与检材后才能进行预览") @GetMapping("/previewEntrustLetterPDF") - public void previewEntrustLetterPDF(String entrustId, HttpServletResponse servletResponse) throws Exception { entrustmentService.previewEntrustLetterPDF(entrustId, servletResponse); } diff --git a/src/main/java/digital/laboratory/platform/entrustment/controller/ExcelUploadController.java b/src/main/java/digital/laboratory/platform/entrustment/controller/ExcelUploadController.java new file mode 100644 index 0000000..35c1733 --- /dev/null +++ b/src/main/java/digital/laboratory/platform/entrustment/controller/ExcelUploadController.java @@ -0,0 +1,40 @@ +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 io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; +import java.util.List; +import java.util.Map; + +/** + * @author ChenJiangBao + * @version 1.0 + * @description: excel上传接口控制器 + * @date 2025/4/16 9:48 + */ +@RestController +@RequestMapping("/excel") +@Api(tags = "20-推送数据到LabsCare平台接口管理", description = "推送数据到LabsCare平台接口管理") +public class ExcelUploadController { + + @Resource + private ExcelUploadService excelUploadService; + + @ApiOperation("上传两社人员名单excel文件") + @PostMapping("/uploadHairInspect") + public R uploadHairInspectExcel(@RequestPart("file") MultipartFile file, @RequestParam("entrustId") String entrustId) { + try { + Boolean success = excelUploadService.uploadHairInspectExcel(file, entrustId); + return R.ok(success); + } catch (Exception e) { + e.printStackTrace(); + return R.failed(e.getMessage()); + } + } +} diff --git a/src/main/java/digital/laboratory/platform/entrustment/enums/EntrustIdentificationSituationType.java b/src/main/java/digital/laboratory/platform/entrustment/enums/EntrustIdentificationSituationType.java index 4733275..39b96c8 100644 --- a/src/main/java/digital/laboratory/platform/entrustment/enums/EntrustIdentificationSituationType.java +++ b/src/main/java/digital/laboratory/platform/entrustment/enums/EntrustIdentificationSituationType.java @@ -10,7 +10,8 @@ public enum EntrustIdentificationSituationType { AFRESF_IDENT(2, "重新鉴定"), PRIMARY_SCREEN(3, "初筛(不要报告、只要结果)"), MYANMAR_PERSON(4, "涉缅人员"), - VOLUNTARY_PERSON(5, "自愿戒治人员") + VOLUNTARY_PERSON(5, "自愿戒治人员"), + TWO_AGENCY(6, "两社人员") ; private final Integer code; diff --git a/src/main/java/digital/laboratory/platform/entrustment/service/ExcelUploadService.java b/src/main/java/digital/laboratory/platform/entrustment/service/ExcelUploadService.java new file mode 100644 index 0000000..86dfb03 --- /dev/null +++ b/src/main/java/digital/laboratory/platform/entrustment/service/ExcelUploadService.java @@ -0,0 +1,21 @@ +package digital.laboratory.platform.entrustment.service; + +import org.springframework.web.multipart.MultipartFile; + +/** + * @author ChenJiangBao + * @version 1.0 + * @description: excel上传服务层接口 + * @date 2025/4/16 11:55 + */ +public interface ExcelUploadService { + + /** + * 上传两社人员名单excel文件 + * @param file + * @param entrustId + * @return + */ + Boolean uploadHairInspectExcel(MultipartFile file, String entrustId) throws Exception; + +} 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 new file mode 100644 index 0000000..9588d55 --- /dev/null +++ b/src/main/java/digital/laboratory/platform/entrustment/service/impl/ExcelUploadServiceImpl.java @@ -0,0 +1,90 @@ +package digital.laboratory.platform.entrustment.service.impl; + +import cn.hutool.core.collection.CollUtil; +import digital.laboratory.platform.common.core.exception.CheckedException; +import digital.laboratory.platform.common.core.util.R; +import digital.laboratory.platform.entrustment.entity.Entrustment; +import digital.laboratory.platform.entrustment.entity.EntrustmentIdentificationMaterial; +import digital.laboratory.platform.entrustment.enums.AnalysisOptionEnums; +import digital.laboratory.platform.entrustment.enums.EntrustIdentificationSituationType; +import digital.laboratory.platform.entrustment.service.EntrustmentIdentificationMaterialService; +import digital.laboratory.platform.entrustment.service.EntrustmentService; +import digital.laboratory.platform.entrustment.service.ExcelUploadService; +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.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 EntrustmentService entrustmentService; + + @Resource + private EntrustmentIdentificationMaterialService entrustmentIdentificationMaterialService; + + @Resource + private RemoteCommDrugService remoteCommDrugService; + + /** + * 上传两社人员名单excel文件 + * @param file + * @param entrustId + * @return + */ + @Override + public Boolean uploadHairInspectExcel(MultipartFile file, String entrustId) throws Exception { + + // 校验委托是否合法 + Entrustment entrustment = entrustmentService.getById(entrustId); + if (entrustment == null) { + throw new CheckedException(String.format("委托id为 %s 的委托信息不存在!")); + } + if (!entrustment.getOldIdentificationResult().equals(EntrustIdentificationSituationType.TWO_AGENCY.getDesc())) { + throw new CheckedException("当前不支持两社人员之外的委托信息进行导入!"); + } + // 默认取海洛因的毒品信息 + R drugLiteR = remoteCommDrugService.getByName("海洛因"); + DrugLite heroin = drugLiteR.getData(); // 获取默认的海洛因数据 + R> innerGetAllR = remoteCommDrugService.innerGetAll(); // 获取所有的毒品信息 + List drugLiteList = innerGetAllR.getData(); + Map drugLiteMap = drugLiteList.stream().collect(Collectors.toMap(DrugLite::getName, Function.identity())); + // 防止检材列表 + List entrustmentIdentificationMaterialList = new ArrayList<>(); + // 读取excel数据 + List> data = ExcelUtils.readExcel(file); + for (Map datum : data) { + EntrustmentIdentificationMaterial entrustmentIdentificationMaterial = new EntrustmentIdentificationMaterial(); + entrustmentIdentificationMaterial.setEntrustmentId(entrustment.getId()); + entrustmentIdentificationMaterial.setType("1"); + entrustmentIdentificationMaterial.setTypeName("生物样本"); + entrustmentIdentificationMaterial.setName(datum.get("姓名") + "的毛发"); + entrustmentIdentificationMaterial.setBiologyGender(datum.get("性别")); + entrustmentIdentificationMaterial.setBiologyType(EntrustBiologyType.HAIR.getDesc()); + entrustmentIdentificationMaterial.setMaterialAge(Integer.valueOf(datum.get("年龄"))); + entrustmentIdentificationMaterial.setRemark(datum.get("身份证号")); + entrustmentIdentificationMaterial.setDrawPlace(datum.get("采样单位")); + entrustmentIdentificationMaterial.setAnalysisOption(AnalysisOptionEnums.QUALITATIVE.getCode()); // 默认设置定性 + entrustmentIdentificationMaterial.setCandidateDrugs(CollUtil.newArrayList(drugLiteMap.containsKey(datum.get("曾吸毒种类")) ? drugLiteMap.get(datum.get("曾吸毒种类")) : heroin)); + entrustmentIdentificationMaterialList.add(entrustmentIdentificationMaterial); + } + + return entrustmentIdentificationMaterialService.saveBatch(entrustmentIdentificationMaterialList); + } +} diff --git a/src/main/java/digital/laboratory/platform/entrustment/utils/ExcelUtils.java b/src/main/java/digital/laboratory/platform/entrustment/utils/ExcelUtils.java new file mode 100644 index 0000000..d48abb7 --- /dev/null +++ b/src/main/java/digital/laboratory/platform/entrustment/utils/ExcelUtils.java @@ -0,0 +1,98 @@ +package digital.laboratory.platform.entrustment.utils; + +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.springframework.web.multipart.MultipartFile; + +import java.io.InputStream; +import java.util.*; + +/** + * @author ChenJiangBao + * @version 1.0 + * @description: 使用 Apache POI 来读取 Excel 文件 + * @date 2025/4/16 9:55 + */ +public class ExcelUtils { + + /** + * 读取 Excel 文件并将其内容转化为 List> 的格式 + * 每一行数据是一个 Map,键是列头(表头),值是该单元格的数据。 + * + * @param file 上传的 Excel 文件 + * @return 解析后的 Excel 数据列表 + * @throws Exception 可能抛出的异常,如文件读取异常 + */ + public static List> readExcel(MultipartFile file) throws Exception { + // 存储最终结果,每一行的 Excel 数据以 Map 的形式存储 + List> result = new ArrayList<>(); + + // 使用 try-with-resources 语法自动关闭流 + try (InputStream is = file.getInputStream(); // 获取文件输入流 + Workbook workbook = new XSSFWorkbook(is)) { // 使用 XSSFWorkbook 读取 Excel 文件(.xlsx 格式) + + // 获取第一个工作表 + Sheet sheet = workbook.getSheetAt(0); + if (sheet == null) return result; // 如果没有工作表,则返回空列表 + + // 获取表头(第一行),表头行存储列名 + Row headerRow = sheet.getRow(0); + if (headerRow == null) return result; // 如果没有表头,则返回空列表 + + // 存储表头列名 + List headers = new ArrayList<>(); + for (Cell cell : headerRow) { + // 去除表头列名的多余空格 + headers.add(cell.getStringCellValue().trim()); + } + + // 从第二行开始读取数据 + for (int i = 1; i <= sheet.getLastRowNum(); i++) { + // 获取当前行 + Row row = sheet.getRow(i); + if (row == null) continue; // 如果当前行为空,则跳过 + + // 存储当前行数据的 Map,键是表头,值是单元格数据 + Map rowData = new LinkedHashMap<>(); + for (int j = 0; j < headers.size(); j++) { + // 获取当前单元格 + Cell cell = row.getCell(j); + // 将列名和单元格数据放入 Map 中 + rowData.put(headers.get(j), getCellValue(cell)); + } + // 将当前行的 Map 加入到结果列表中 + result.add(rowData); + } + } + + return result; // 返回解析后的数据列表 + } + + /** + * 获取单元格的值,处理不同类型的单元格 + * + * @param cell 当前单元格 + * @return 单元格中的值 + */ + private static String getCellValue(Cell cell) { + if (cell == null) return ""; // 如果单元格为空,返回空字符串 + + // 根据单元格的类型来处理其值 + switch (cell.getCellType()) { + case STRING: + return cell.getStringCellValue().trim(); // 字符串类型,返回去除空格后的字符串 + case NUMERIC: + // 数字类型,判断是否为日期格式 + if (DateUtil.isCellDateFormatted(cell)) { + return cell.getDateCellValue().toString(); // 返回日期的字符串表示 + } + return String.valueOf((long) cell.getNumericCellValue()); // 返回数字 + case BOOLEAN: + return String.valueOf(cell.getBooleanCellValue()); // 布尔值类型,返回布尔值的字符串表示 + case FORMULA: + return cell.getCellFormula(); // 公式类型,返回公式的字符串表示 + default: + return ""; // 其他类型返回空字符串 + } + } +}