forked from luyan/epmet-cloud-lingshan
				
			
				 11 changed files with 472 additions and 14 deletions
			
			
		@ -0,0 +1,48 @@ | 
				
			|||
package com.epmet.excel.data; | 
				
			|||
 | 
				
			|||
import com.alibaba.excel.annotation.ExcelProperty; | 
				
			|||
import com.alibaba.excel.annotation.write.style.ColumnWidth; | 
				
			|||
import lombok.Data; | 
				
			|||
 | 
				
			|||
import javax.validation.constraints.NotBlank; | 
				
			|||
 | 
				
			|||
/** | 
				
			|||
 * @Description | 
				
			|||
 * @Author yzm | 
				
			|||
 * @Date 2022/9/27 9:41 | 
				
			|||
 */ | 
				
			|||
@Data | 
				
			|||
public class IcNatCompareRecordExcelData { | 
				
			|||
    @NotBlank(message = "姓名为必填项") | 
				
			|||
    @ExcelProperty("姓名") | 
				
			|||
    private String name; | 
				
			|||
 | 
				
			|||
    @NotBlank(message = "身份证号为必填项") | 
				
			|||
    @ExcelProperty("身份证号") | 
				
			|||
    private String idCard; | 
				
			|||
 | 
				
			|||
    @NotBlank(message = "手机号为必填项") | 
				
			|||
    @ExcelProperty("手机号") | 
				
			|||
    private String mobile; | 
				
			|||
 | 
				
			|||
    @Data | 
				
			|||
    public static class ErrorRow { | 
				
			|||
 | 
				
			|||
        @ExcelProperty("姓名") | 
				
			|||
        @ColumnWidth(20) | 
				
			|||
        private String name; | 
				
			|||
 | 
				
			|||
        @ColumnWidth(20) | 
				
			|||
        @ExcelProperty("身份证号") | 
				
			|||
        private String idCard; | 
				
			|||
 | 
				
			|||
        @ExcelProperty("手机号") | 
				
			|||
        @ColumnWidth(20) | 
				
			|||
        private String mobile; | 
				
			|||
 | 
				
			|||
        @ColumnWidth(60) | 
				
			|||
        @ExcelProperty("错误信息") | 
				
			|||
        private String errorInfo; | 
				
			|||
    } | 
				
			|||
} | 
				
			|||
 | 
				
			|||
@ -0,0 +1,152 @@ | 
				
			|||
package com.epmet.excel.handler; | 
				
			|||
 | 
				
			|||
import com.alibaba.excel.context.AnalysisContext; | 
				
			|||
import com.alibaba.excel.read.listener.ReadListener; | 
				
			|||
import com.epmet.commons.tools.constant.NumConstant; | 
				
			|||
import com.epmet.commons.tools.constant.StrConstant; | 
				
			|||
import com.epmet.commons.tools.dto.result.CustomerStaffInfoCacheResult; | 
				
			|||
import com.epmet.commons.tools.dto.result.YtHsjcResDTO; | 
				
			|||
import com.epmet.commons.tools.dto.result.YtHsjcResDetailDTO; | 
				
			|||
import com.epmet.commons.tools.exception.EpmetException; | 
				
			|||
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.utils.DateUtils; | 
				
			|||
import com.epmet.commons.tools.utils.ObjectUtil; | 
				
			|||
import com.epmet.commons.tools.utils.YtHsResUtils; | 
				
			|||
import com.epmet.commons.tools.validator.ValidatorUtils; | 
				
			|||
import com.epmet.entity.IcNatCompareRecordEntity; | 
				
			|||
import com.epmet.excel.data.IcNatCompareRecordExcelData; | 
				
			|||
import com.epmet.service.impl.IcNatCompareRecordServiceImpl; | 
				
			|||
import lombok.extern.slf4j.Slf4j; | 
				
			|||
import org.apache.commons.collections4.CollectionUtils; | 
				
			|||
import org.apache.commons.lang3.StringUtils; | 
				
			|||
 | 
				
			|||
import java.util.ArrayList; | 
				
			|||
import java.util.Date; | 
				
			|||
import java.util.List; | 
				
			|||
 | 
				
			|||
/** | 
				
			|||
 * @Description | 
				
			|||
 * @Author yzm | 
				
			|||
 * @Date 2022/9/27 9:42 | 
				
			|||
 */ | 
				
			|||
@Slf4j | 
				
			|||
public class IcNatCompareRecordExcelImportListener implements ReadListener<IcNatCompareRecordExcelData> { | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * 最大条数阈值 | 
				
			|||
     */ | 
				
			|||
    public static final int MAX_THRESHOLD = 200; | 
				
			|||
    /** | 
				
			|||
     * 当前操作用户 | 
				
			|||
     */ | 
				
			|||
    private CustomerStaffInfoCacheResult staffInfo; | 
				
			|||
    private String customerId; | 
				
			|||
    private IcNatCompareRecordServiceImpl icNatCompareRecordService; | 
				
			|||
    /** | 
				
			|||
     * 数据 | 
				
			|||
     */ | 
				
			|||
    private List<IcNatCompareRecordEntity> datas = new ArrayList<>(); | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * 错误项列表 | 
				
			|||
     */ | 
				
			|||
    private List<IcNatCompareRecordExcelData.ErrorRow> errorRows = new ArrayList<>(); | 
				
			|||
    private Date importTime; | 
				
			|||
    private String importDate; | 
				
			|||
    public IcNatCompareRecordExcelImportListener(String customerId, CustomerStaffInfoCacheResult staffInfo,String importDate,Date importTime, IcNatCompareRecordServiceImpl icNatCompareRecordService) { | 
				
			|||
        this.customerId = customerId; | 
				
			|||
        this.staffInfo = staffInfo; | 
				
			|||
        this.icNatCompareRecordService = icNatCompareRecordService; | 
				
			|||
        this.importDate=importDate; | 
				
			|||
        this.importTime=importTime; | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
 | 
				
			|||
    @Override | 
				
			|||
    public void invoke(IcNatCompareRecordExcelData data, AnalysisContext analysisContext) { | 
				
			|||
        try { | 
				
			|||
            // log.warn("有数据吗?"+JSON.toJSONString(data));
 | 
				
			|||
            // 不能为空先校验数据
 | 
				
			|||
            ValidatorUtils.validateEntity(data); | 
				
			|||
            // 去除空格
 | 
				
			|||
            ObjectUtil.objectToTrim(data); | 
				
			|||
            IcNatCompareRecordEntity compareRecordEntity = ConvertUtils.sourceToTarget(data, IcNatCompareRecordEntity.class); | 
				
			|||
            compareRecordEntity.setCustomerId(customerId); | 
				
			|||
            compareRecordEntity.setLatestNatTime(null); | 
				
			|||
            compareRecordEntity.setNatAddress(StrConstant.EPMETY_STR); | 
				
			|||
            compareRecordEntity.setNatResult(StrConstant.EPMETY_STR); | 
				
			|||
            // 调用烟台api获取核酸检测结果
 | 
				
			|||
            YtHsjcResDTO hsjcResDTO = YtHsResUtils.hsjc(data.getIdCard(), 1, 1); | 
				
			|||
            if (null != hsjcResDTO && CollectionUtils.isNotEmpty(hsjcResDTO.getData()) && null != hsjcResDTO.getData().get(0)) { | 
				
			|||
                YtHsjcResDetailDTO ytHsjcResDetailDTO = hsjcResDTO.getData().get(0); | 
				
			|||
                String testTime = ytHsjcResDetailDTO.getTest_time(); | 
				
			|||
                compareRecordEntity.setLatestNatTime(DateUtils.parse(testTime, DateUtils.DATE_PATTERN)); | 
				
			|||
                compareRecordEntity.setNatAddress(StringUtils.isNotBlank(ytHsjcResDetailDTO.getSampling_org_pcr()) ? ytHsjcResDetailDTO.getSample_result_pcr() : StrConstant.EPMETY_STR); | 
				
			|||
 | 
				
			|||
                // "sample_result_pcr":"2",// 核酸检测结果 1:阳性,2:阴性
 | 
				
			|||
                String sample_result_pcr = ytHsjcResDetailDTO.getSample_result_pcr(); | 
				
			|||
                if (NumConstant.ONE_STR.equals(sample_result_pcr)) { | 
				
			|||
                    compareRecordEntity.setNatResult(NumConstant.ONE_STR); | 
				
			|||
                } else if (NumConstant.TWO_STR.equals(sample_result_pcr)) { | 
				
			|||
                    compareRecordEntity.setNatResult(NumConstant.ZERO_STR); | 
				
			|||
                } | 
				
			|||
                compareRecordEntity.setContactAddress(StringUtils.isNotBlank(ytHsjcResDetailDTO.getAddress()) ? ytHsjcResDetailDTO.getAddress() : StrConstant.EPMETY_STR); | 
				
			|||
            } | 
				
			|||
            datas.add(compareRecordEntity); | 
				
			|||
 | 
				
			|||
            if (datas.size() == MAX_THRESHOLD) { | 
				
			|||
                execPersist(); | 
				
			|||
            } | 
				
			|||
        } catch (Exception e) { | 
				
			|||
            String errorMsg = null; | 
				
			|||
            if (e instanceof ValidateException) { | 
				
			|||
                errorMsg = ((ValidateException) e).getMsg(); | 
				
			|||
            } else if (e instanceof EpmetException) { | 
				
			|||
                errorMsg = ((EpmetException) e).getMsg(); | 
				
			|||
            } else { | 
				
			|||
                errorMsg = "未知错误"; | 
				
			|||
                log.error("【未做核酸比对导入】出错:{}", ExceptionUtils.getErrorStackTrace(e)); | 
				
			|||
            } | 
				
			|||
            IcNatCompareRecordExcelData.ErrorRow errorRow = new IcNatCompareRecordExcelData.ErrorRow(); | 
				
			|||
            errorRow.setIdCard(data.getIdCard()); | 
				
			|||
            errorRow.setName(data.getName()); | 
				
			|||
            errorRow.setMobile(data.getMobile()); | 
				
			|||
            errorRow.setErrorInfo(errorMsg); | 
				
			|||
            errorRows.add(errorRow); | 
				
			|||
        } | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    @Override | 
				
			|||
    public void doAfterAllAnalysed(AnalysisContext analysisContext) { | 
				
			|||
        // 最后几条达不到阈值,这里必须再调用一次
 | 
				
			|||
        execPersist(); | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * 执行持久化 | 
				
			|||
     */ | 
				
			|||
    private void execPersist() { | 
				
			|||
        // ic_nat_compare_record、ic_nat_compare_rec_relation
 | 
				
			|||
        try { | 
				
			|||
            if (datas != null && datas.size() > 0) { | 
				
			|||
                icNatCompareRecordService.batchPersist(datas, staffInfo, this); | 
				
			|||
            } | 
				
			|||
        } finally { | 
				
			|||
            datas.clear(); | 
				
			|||
        } | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * 获取错误行 | 
				
			|||
     * | 
				
			|||
     * @return | 
				
			|||
     */ | 
				
			|||
    public List<IcNatCompareRecordExcelData.ErrorRow> getErrorRows() { | 
				
			|||
        return errorRows; | 
				
			|||
    } | 
				
			|||
} | 
				
			|||
 | 
				
			|||
					Loading…
					
					
				
		Reference in new issue