forked from rongchao/epmet-cloud-rizhao
				
			
				 14 changed files with 503 additions and 49 deletions
			
			
		@ -0,0 +1,61 @@ | 
				
			|||||
 | 
					package com.epmet.excel.data; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					import com.alibaba.excel.annotation.ExcelProperty; | 
				
			||||
 | 
					import com.alibaba.excel.annotation.write.style.ColumnWidth; | 
				
			||||
 | 
					import com.epmet.dto.form.AddIcNatFormDTO; | 
				
			||||
 | 
					import lombok.Data; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					import javax.validation.constraints.NotBlank; | 
				
			||||
 | 
					import javax.validation.constraints.NotNull; | 
				
			||||
 | 
					import java.util.Date; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					/** | 
				
			||||
 | 
					 * 核酸检测信息导入excel数据 | 
				
			||||
 | 
					 */ | 
				
			||||
 | 
					@Data | 
				
			||||
 | 
					public class IcNatImportExcelData { | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    @NotBlank(message = "姓名为必填项") | 
				
			||||
 | 
					    @ExcelProperty("姓名") | 
				
			||||
 | 
					    private String name; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    @NotBlank(message = "手机号为必填项") | 
				
			||||
 | 
					    @ExcelProperty("手机号") | 
				
			||||
 | 
					    private String mobile; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    @NotBlank(message = "身份证号为必填项") | 
				
			||||
 | 
					    @ExcelProperty("身份证号") | 
				
			||||
 | 
					    private String idCard; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    @NotNull(message = "检测时间为必填项") | 
				
			||||
 | 
					    @ExcelProperty("检测时间") | 
				
			||||
 | 
					    private Date natTime; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    @NotBlank(message = "检测地点为必填项") | 
				
			||||
 | 
					    @ExcelProperty("检测地点") | 
				
			||||
 | 
					    private String natAddress; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    @NotBlank(message = "检测结果为必填项") | 
				
			||||
 | 
					    @ExcelProperty("检测结果") | 
				
			||||
 | 
					    private String natResultZh; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    @Data | 
				
			||||
 | 
					    public static class ErrorRow { | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					        @ExcelProperty("姓名") | 
				
			||||
 | 
					        @ColumnWidth(20) | 
				
			||||
 | 
					        private String name; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					        @ExcelProperty("手机号") | 
				
			||||
 | 
					        @ColumnWidth(20) | 
				
			||||
 | 
					        private String mobile; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					        @ColumnWidth(20) | 
				
			||||
 | 
					        @ExcelProperty("身份证号") | 
				
			||||
 | 
					        private String idCard; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					        @ColumnWidth(60) | 
				
			||||
 | 
					        @ExcelProperty("错误信息") | 
				
			||||
 | 
					        private String errorInfo; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					} | 
				
			||||
@ -0,0 +1,126 @@ | 
				
			|||||
 | 
					package com.epmet.excel.handler; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					import com.alibaba.excel.context.AnalysisContext; | 
				
			||||
 | 
					import com.alibaba.excel.read.listener.ReadListener; | 
				
			||||
 | 
					import com.epmet.commons.tools.exception.ExceptionUtils; | 
				
			||||
 | 
					import com.epmet.commons.tools.exception.ValidateException; | 
				
			||||
 | 
					import com.epmet.commons.tools.utils.ConvertUtils; | 
				
			||||
 | 
					import com.epmet.commons.tools.validator.ValidatorUtils; | 
				
			||||
 | 
					import com.epmet.entity.IcNatEntity; | 
				
			||||
 | 
					import com.epmet.excel.data.IcNatImportExcelData; | 
				
			||||
 | 
					import com.epmet.service.impl.IcNatServiceImpl; | 
				
			||||
 | 
					import lombok.extern.slf4j.Slf4j; | 
				
			||||
 | 
					import org.apache.commons.lang3.StringUtils; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					import java.util.ArrayList; | 
				
			||||
 | 
					import java.util.List; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					/** | 
				
			||||
 | 
					 * 核酸检测excel导入监听器 | 
				
			||||
 | 
					 */ | 
				
			||||
 | 
					@Slf4j | 
				
			||||
 | 
					public class IcNatExcelImportListener implements ReadListener<IcNatImportExcelData> { | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 最大条数阈值 | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    public static final int MAX_THRESHOLD = 2; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    private String currentUserId; | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 当前组织ID | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    private String currentAgencyId; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    private String currentAgencyPids; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 数据 | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    private List<IcNatEntity> datas = new ArrayList<>(); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 错误项列表 | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    private List<IcNatImportExcelData.ErrorRow> errorRows = new ArrayList<>(); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    private IcNatServiceImpl icNatService; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    public IcNatExcelImportListener(String currentUserId, String currentAgencyId, String currentAgencyPids, IcNatServiceImpl icNatService) { | 
				
			||||
 | 
					        this.currentUserId = currentUserId; | 
				
			||||
 | 
					        this.currentAgencyId = currentAgencyId; | 
				
			||||
 | 
					        this.currentAgencyPids = currentAgencyPids; | 
				
			||||
 | 
					        this.icNatService = icNatService; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    @Override | 
				
			||||
 | 
					    public void invoke(IcNatImportExcelData data, AnalysisContext context) { | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					        try { | 
				
			||||
 | 
					            // 先校验数据
 | 
				
			||||
 | 
					            ValidatorUtils.validateEntity(data); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					            String natResult = "0"; | 
				
			||||
 | 
					            String natResultZh = data.getNatResultZh(); | 
				
			||||
 | 
					            if (StringUtils.isNotBlank(natResultZh)) { | 
				
			||||
 | 
					                natResult = natResultZh.equals("阴性") ? "0" : "1"; | 
				
			||||
 | 
					            } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					            IcNatEntity icNatEntity = ConvertUtils.sourceToTarget(data, IcNatEntity.class); | 
				
			||||
 | 
					            icNatEntity.setAgencyId(currentAgencyId); | 
				
			||||
 | 
					            icNatEntity.setPids(currentAgencyPids); | 
				
			||||
 | 
					            icNatEntity.setUserId(currentUserId); | 
				
			||||
 | 
					            icNatEntity.setUserType("other"); | 
				
			||||
 | 
					            icNatEntity.setNatResult(natResult); | 
				
			||||
 | 
					            icNatEntity.setAttachmentType(""); | 
				
			||||
 | 
					            icNatEntity.setAttachmentUrl(""); | 
				
			||||
 | 
					            datas.add(icNatEntity); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					            if (datas.size() == MAX_THRESHOLD) { | 
				
			||||
 | 
					                execPersist(); | 
				
			||||
 | 
					            } | 
				
			||||
 | 
					        } catch (Exception e) { | 
				
			||||
 | 
					            String errorMsg = null; | 
				
			||||
 | 
					            if (e instanceof ValidateException) { | 
				
			||||
 | 
					                errorMsg = ((ValidateException) e).getMsg(); | 
				
			||||
 | 
					            } else { | 
				
			||||
 | 
					                errorMsg = "未知错误"; | 
				
			||||
 | 
					                log.error("【核酸检测信息导入】出错:{}", ExceptionUtils.getErrorStackTrace(e)); | 
				
			||||
 | 
					            } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					            IcNatImportExcelData.ErrorRow errorRow = new IcNatImportExcelData.ErrorRow(); | 
				
			||||
 | 
					            errorRow.setName(data.getName()); | 
				
			||||
 | 
					            errorRow.setMobile(data.getMobile()); | 
				
			||||
 | 
					            errorRow.setIdCard(data.getIdCard()); | 
				
			||||
 | 
					            errorRow.setErrorInfo(errorMsg); | 
				
			||||
 | 
					            errorRows.add(errorRow); | 
				
			||||
 | 
					        } | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    @Override | 
				
			||||
 | 
					    public void doAfterAllAnalysed(AnalysisContext context) { | 
				
			||||
 | 
					        // 最后几条达不到阈值,这里必须再调用一次
 | 
				
			||||
 | 
					        execPersist(); | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 执行持久化 | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    private void execPersist() { | 
				
			||||
 | 
					        try { | 
				
			||||
 | 
					            if (datas != null && datas.size() > 0) { | 
				
			||||
 | 
					                icNatService.batchPersist(datas); | 
				
			||||
 | 
					            } | 
				
			||||
 | 
					        } finally { | 
				
			||||
 | 
					            datas.clear(); | 
				
			||||
 | 
					        } | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 获取错误行 | 
				
			||||
 | 
					     * @return | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    public List<IcNatImportExcelData.ErrorRow> getErrorRows() { | 
				
			||||
 | 
					        return errorRows; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					} | 
				
			||||
					Loading…
					
					
				
		Reference in new issue