parent
22156abd9e
commit
13a18e5358
@ -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<Boolean> 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()); |
||||
} |
||||
} |
||||
} |
@ -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; |
||||
|
||||
} |
@ -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<DrugLite> drugLiteR = remoteCommDrugService.getByName("海洛因"); |
||||
DrugLite heroin = drugLiteR.getData(); // 获取默认的海洛因数据
|
||||
R<List<DrugLite>> innerGetAllR = remoteCommDrugService.innerGetAll(); // 获取所有的毒品信息
|
||||
List<DrugLite> drugLiteList = innerGetAllR.getData(); |
||||
Map<String, DrugLite> drugLiteMap = drugLiteList.stream().collect(Collectors.toMap(DrugLite::getName, Function.identity())); |
||||
// 防止检材列表
|
||||
List<EntrustmentIdentificationMaterial> entrustmentIdentificationMaterialList = new ArrayList<>(); |
||||
// 读取excel数据
|
||||
List<Map<String, String>> data = ExcelUtils.readExcel(file); |
||||
for (Map<String, String> 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); |
||||
} |
||||
} |
@ -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<String, String>> 的格式 |
||||
* 每一行数据是一个 Map,键是列头(表头),值是该单元格的数据。 |
||||
* |
||||
* @param file 上传的 Excel 文件 |
||||
* @return 解析后的 Excel 数据列表 |
||||
* @throws Exception 可能抛出的异常,如文件读取异常 |
||||
*/ |
||||
public static List<Map<String, String>> readExcel(MultipartFile file) throws Exception { |
||||
// 存储最终结果,每一行的 Excel 数据以 Map 的形式存储
|
||||
List<Map<String, String>> 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<String> 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<String, String> 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 ""; // 其他类型返回空字符串
|
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue