forked from luyan/epmet-cloud-lingshan
				
			
				 10 changed files with 396 additions and 283 deletions
			
			
		@ -0,0 +1,177 @@ | 
				
			|||
package com.epmet.excel.handler; | 
				
			|||
 | 
				
			|||
import com.alibaba.excel.context.AnalysisContext; | 
				
			|||
import com.alibaba.excel.read.listener.ReadListener; | 
				
			|||
import com.epmet.commons.tools.dto.result.CustomerStaffInfoCacheResult; | 
				
			|||
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.constant.IcResiUserConstant; | 
				
			|||
import com.epmet.entity.IcTripReportRecordEntity; | 
				
			|||
import com.epmet.excel.IcPsTripReportRecordExcel; | 
				
			|||
import com.epmet.excel.data.IcTripReportExcelData; | 
				
			|||
import com.epmet.excel.error.IcPsTripReportRecordErrorExcel; | 
				
			|||
import com.epmet.service.impl.IcTripReportRecordServiceImpl; | 
				
			|||
import lombok.extern.slf4j.Slf4j; | 
				
			|||
import org.apache.commons.lang3.StringUtils; | 
				
			|||
 | 
				
			|||
import java.util.ArrayList; | 
				
			|||
import java.util.List; | 
				
			|||
import java.util.Map; | 
				
			|||
import java.util.concurrent.atomic.AtomicBoolean; | 
				
			|||
 | 
				
			|||
 | 
				
			|||
/** | 
				
			|||
 * 磐石-行程上报excel导入监听器 | 
				
			|||
 */ | 
				
			|||
@Slf4j | 
				
			|||
public class IcPsTripReportExcelImportListener implements ReadListener<IcPsTripReportRecordExcel> { | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * 最大条数阈值 | 
				
			|||
     */ | 
				
			|||
    public static final int MAX_THRESHOLD = 200; | 
				
			|||
    /** | 
				
			|||
     * 当前操作用户 | 
				
			|||
     */ | 
				
			|||
    private CustomerStaffInfoCacheResult staffInfo; | 
				
			|||
    private String customerId; | 
				
			|||
    //字典表数据
 | 
				
			|||
    private Map<String, String> trafficTypeMap; | 
				
			|||
    private Map<String, String> sojournHistoryMap; | 
				
			|||
    private Map<String, String> isolateTypeMap; | 
				
			|||
    private Map<String, String> tripDataTypeMap; | 
				
			|||
    private Map<String, String> areaMap; | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * 数据 | 
				
			|||
     */ | 
				
			|||
    private List<IcTripReportRecordEntity> datas = new ArrayList<>(); | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * 错误项列表 | 
				
			|||
     */ | 
				
			|||
    private List<IcPsTripReportRecordErrorExcel> errorRows = new ArrayList<>(); | 
				
			|||
 | 
				
			|||
    private IcTripReportRecordServiceImpl tripReportRecordService; | 
				
			|||
 | 
				
			|||
    public IcPsTripReportExcelImportListener(String customerId, CustomerStaffInfoCacheResult staffInfo, IcTripReportRecordServiceImpl tripReportRecordService, | 
				
			|||
           Map<String, String> trafficTypeMap, Map<String, String> sojournHistoryMap, Map<String, String> isolateTypeMap, Map<String, String> tripDataTypeMap, Map<String, String> areaMap) { | 
				
			|||
        this.customerId=customerId; | 
				
			|||
        this.staffInfo = staffInfo; | 
				
			|||
        this.tripReportRecordService = tripReportRecordService; | 
				
			|||
        this.trafficTypeMap = trafficTypeMap; | 
				
			|||
        this.sojournHistoryMap = sojournHistoryMap; | 
				
			|||
        this.isolateTypeMap = isolateTypeMap; | 
				
			|||
        this.tripDataTypeMap = tripDataTypeMap; | 
				
			|||
        this.areaMap = areaMap; | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    @Override | 
				
			|||
    public void invoke(IcPsTripReportRecordExcel data, AnalysisContext context) { | 
				
			|||
 | 
				
			|||
        try { | 
				
			|||
            // 先校验数据
 | 
				
			|||
            ValidatorUtils.validateEntity(data); | 
				
			|||
 | 
				
			|||
            AtomicBoolean bl = new AtomicBoolean(false); | 
				
			|||
            StringBuffer errMsg = new StringBuffer(""); | 
				
			|||
            IcTripReportRecordEntity e = ConvertUtils.sourceToTarget(data, IcTripReportRecordEntity.class); | 
				
			|||
            e.setCustomerId(customerId); | 
				
			|||
            e.setAgencyId(staffInfo.getAgencyId()); | 
				
			|||
            e.setPids(staffInfo.getAgencyPIds()); | 
				
			|||
            e.setUserType(IcResiUserConstant.USER_TYPE_IMPORT); | 
				
			|||
            if (trafficTypeMap.containsKey(data.getTrafficType())) { | 
				
			|||
                e.setTrafficType(trafficTypeMap.get(data.getTrafficType())); | 
				
			|||
            } | 
				
			|||
            if ("其他".equals(data.getTrafficType()) && StringUtils.isBlank(data.getTrafficTypeExplain())) { | 
				
			|||
                errMsg.append("返回方式为其他时,请补充“其他返回方式”;"); | 
				
			|||
                bl.set(true); | 
				
			|||
            } | 
				
			|||
            if (StringUtils.isNotBlank(data.getSojournHistory())) { | 
				
			|||
                e.setSojournHistory(sojournHistoryMap.get(data.getSojournHistory())); | 
				
			|||
            } else { | 
				
			|||
                String[] str = e.getSourceAddress().split("-"); | 
				
			|||
                if (str.length < 3) { | 
				
			|||
                    errMsg.append("数据不完整,‘来源地’信息填写格式错误;"); | 
				
			|||
                    bl.set(true); | 
				
			|||
                } | 
				
			|||
                e.setSojournHistory("3");//无风险
 | 
				
			|||
                if (areaMap.containsKey(str[2])) { | 
				
			|||
                    e.setSojournHistory(areaMap.get(str[2])); | 
				
			|||
                } | 
				
			|||
            } | 
				
			|||
            if (StringUtils.isNotBlank(data.getIsolateType())) { | 
				
			|||
                e.setIsolateType(isolateTypeMap.get(data.getIsolateType())); | 
				
			|||
            } | 
				
			|||
            if (StringUtils.isNotBlank(data.getTripDataType())) { | 
				
			|||
                e.setTripDataType(tripDataTypeMap.get(data.getTripDataType())); | 
				
			|||
            } | 
				
			|||
            e.setIsArriveCheck("是".equals(data.getIsArriveCheck()) ? "1" : "0"); | 
				
			|||
            e.setIsArrive("是".equals(data.getIsArrive()) ? "1" : "0"); | 
				
			|||
            //必要字段没值的
 | 
				
			|||
            if (StringUtils.isEmpty(e.getSourceAddress()) || StringUtils.isEmpty(e.getPresentAddress()) | 
				
			|||
                    || StringUtils.isEmpty(e.getTrafficType())) { | 
				
			|||
                errMsg.append("数据不完整,请检查‘来源地’、‘在曹居住地点’、‘返回方式’信息是否填写完整;"); | 
				
			|||
                bl.set(true); | 
				
			|||
            } | 
				
			|||
            if (bl.get()) { | 
				
			|||
                IcPsTripReportRecordErrorExcel errorRow = new IcPsTripReportRecordErrorExcel(); | 
				
			|||
                errorRow.setName(data.getName()); | 
				
			|||
                errorRow.setMobile(data.getMobile()); | 
				
			|||
                errorRow.setIdCard(data.getIdCard()); | 
				
			|||
                errorRow.setErrorInfo(errMsg.toString()); | 
				
			|||
                errorRows.add(errorRow); | 
				
			|||
                return; | 
				
			|||
            } | 
				
			|||
            datas.add(e); | 
				
			|||
 | 
				
			|||
            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)); | 
				
			|||
            } | 
				
			|||
 | 
				
			|||
            IcPsTripReportRecordErrorExcel errorRow = new IcPsTripReportRecordErrorExcel(); | 
				
			|||
            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) { | 
				
			|||
                tripReportRecordService.batchPersist(datas); | 
				
			|||
            } | 
				
			|||
        } finally { | 
				
			|||
            datas.clear(); | 
				
			|||
        } | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * 获取错误行 | 
				
			|||
     * @return | 
				
			|||
     */ | 
				
			|||
    public List<IcPsTripReportRecordErrorExcel> getErrorRows() { | 
				
			|||
        return errorRows; | 
				
			|||
    } | 
				
			|||
} | 
				
			|||
					Loading…
					
					
				
		Reference in new issue