7 changed files with 444 additions and 5 deletions
			
			
		@ -0,0 +1,154 @@ | 
				
			|||
package com.epmet.model; | 
				
			|||
 | 
				
			|||
import com.alibaba.excel.context.AnalysisContext; | 
				
			|||
import com.alibaba.excel.event.AnalysisEventListener; | 
				
			|||
import com.epmet.commons.tools.constant.NumConstant; | 
				
			|||
import com.epmet.commons.tools.enums.DictTypeEnum; | 
				
			|||
import com.epmet.commons.tools.exception.EpmetException; | 
				
			|||
import com.epmet.commons.tools.redis.common.bean.AgencyInfoCache; | 
				
			|||
import com.epmet.commons.tools.utils.ConvertUtils; | 
				
			|||
import com.epmet.commons.tools.utils.Result; | 
				
			|||
import com.epmet.commons.tools.validator.ValidatorUtils; | 
				
			|||
import com.epmet.dto.result.IcCoverageCategoryDictListResultDTO; | 
				
			|||
import com.epmet.entity.IcDangerousChemicalsEntity; | 
				
			|||
import com.epmet.feign.EpmetAdminOpenFeignClient; | 
				
			|||
import com.epmet.service.CoverageService; | 
				
			|||
import com.epmet.service.IcDangerousChemicalsService; | 
				
			|||
import lombok.Data; | 
				
			|||
import lombok.extern.slf4j.Slf4j; | 
				
			|||
import org.apache.commons.collections4.CollectionUtils; | 
				
			|||
import org.apache.commons.collections4.MapUtils; | 
				
			|||
import org.apache.commons.lang3.StringUtils; | 
				
			|||
 | 
				
			|||
import java.util.ArrayList; | 
				
			|||
import java.util.HashMap; | 
				
			|||
import java.util.List; | 
				
			|||
import java.util.Map; | 
				
			|||
import java.util.stream.Collectors; | 
				
			|||
 | 
				
			|||
/** | 
				
			|||
 * @Author zxc | 
				
			|||
 * @DateTime 2022/6/20 10:31 | 
				
			|||
 * @DESC | 
				
			|||
 */ | 
				
			|||
@Data | 
				
			|||
@Slf4j | 
				
			|||
public class DangerousChemicalsImportListener extends AnalysisEventListener<DangerousChemicalsModel> { | 
				
			|||
 | 
				
			|||
    private IcDangerousChemicalsService icDangerousChemicalsService; | 
				
			|||
    private EpmetAdminOpenFeignClient adminOpenFeignClient; | 
				
			|||
    private CoverageService coverageService; | 
				
			|||
    private AgencyInfoCache agencyInfo; | 
				
			|||
 | 
				
			|||
    private List<DangerousChemicalsModel.RowRemarkMessage> errorRows = new ArrayList<>(); | 
				
			|||
    private List<DangerousChemicalsModel.RowRemarkMessage> otherRows = new ArrayList<>(); | 
				
			|||
    private List<IcDangerousChemicalsEntity> insertList = new ArrayList<>(); | 
				
			|||
 | 
				
			|||
    public DangerousChemicalsImportListener(AgencyInfoCache agencyInfo, IcDangerousChemicalsService icDangerousChemicalsService, CoverageService coverageService,EpmetAdminOpenFeignClient adminOpenFeignClient){ | 
				
			|||
        this.agencyInfo = agencyInfo; | 
				
			|||
        this.icDangerousChemicalsService = icDangerousChemicalsService; | 
				
			|||
        this.coverageService = coverageService; | 
				
			|||
        this.adminOpenFeignClient = adminOpenFeignClient; | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    @Override | 
				
			|||
    public void invoke(DangerousChemicalsModel data, AnalysisContext context) { | 
				
			|||
        Integer rowNum = context.readRowHolder().getRowIndex() + NumConstant.ONE; | 
				
			|||
        if (StringUtils.isBlank(data.getName())){ | 
				
			|||
            DangerousChemicalsModel.RowRemarkMessage row = ConvertUtils.sourceToTarget(data, DangerousChemicalsModel.RowRemarkMessage.class); | 
				
			|||
            row.setRowNum(rowNum); | 
				
			|||
            row.setErrorInfo("企业名称不能为空"); | 
				
			|||
            errorRows.add(row); | 
				
			|||
            return; | 
				
			|||
        } | 
				
			|||
        if (StringUtils.isBlank(data.getCategoryName())){ | 
				
			|||
            DangerousChemicalsModel.RowRemarkMessage row = ConvertUtils.sourceToTarget(data, DangerousChemicalsModel.RowRemarkMessage.class); | 
				
			|||
            row.setRowNum(rowNum); | 
				
			|||
            row.setErrorInfo("企业类型不能为空"); | 
				
			|||
            errorRows.add(row); | 
				
			|||
            return; | 
				
			|||
        } | 
				
			|||
        if (StringUtils.isBlank(data.getAddress())){ | 
				
			|||
            DangerousChemicalsModel.RowRemarkMessage row = ConvertUtils.sourceToTarget(data, DangerousChemicalsModel.RowRemarkMessage.class); | 
				
			|||
            row.setRowNum(rowNum); | 
				
			|||
            row.setErrorInfo("经营地址不能为空"); | 
				
			|||
            errorRows.add(row); | 
				
			|||
            return; | 
				
			|||
        } | 
				
			|||
        if (data.getName().trim().length() > NumConstant.FIFTY){ | 
				
			|||
            DangerousChemicalsModel.RowRemarkMessage row = ConvertUtils.sourceToTarget(data, DangerousChemicalsModel.RowRemarkMessage.class); | 
				
			|||
            row.setRowNum(rowNum); | 
				
			|||
            row.setErrorInfo("企业名称长度超出50字限制"); | 
				
			|||
            errorRows.add(row); | 
				
			|||
        } | 
				
			|||
        if (data.getPrincipalMobile().trim().length() > NumConstant.FIFTY){ | 
				
			|||
            DangerousChemicalsModel.RowRemarkMessage row = ConvertUtils.sourceToTarget(data, DangerousChemicalsModel.RowRemarkMessage.class); | 
				
			|||
            row.setRowNum(rowNum); | 
				
			|||
            row.setErrorInfo("联系电话长度超出50字限制"); | 
				
			|||
            errorRows.add(row); | 
				
			|||
        } | 
				
			|||
        if (data.getAddress().trim().length() > NumConstant.FIFTY){ | 
				
			|||
            DangerousChemicalsModel.RowRemarkMessage row = ConvertUtils.sourceToTarget(data, DangerousChemicalsModel.RowRemarkMessage.class); | 
				
			|||
            row.setRowNum(rowNum); | 
				
			|||
            row.setErrorInfo("经营地址长度超出50字限制"); | 
				
			|||
            errorRows.add(row); | 
				
			|||
        } | 
				
			|||
        IcDangerousChemicalsEntity e = ConvertUtils.sourceToTarget(data, IcDangerousChemicalsEntity.class); | 
				
			|||
        e.setSourceType("import"); | 
				
			|||
        e.setAgencyId(agencyInfo.getId()); | 
				
			|||
        e.setCustomerId(agencyInfo.getCustomerId()); | 
				
			|||
        e.setAgencyIdPath(agencyInfo.getPids().equals(NumConstant.ZERO_STR) || agencyInfo.getPids().equals("") ? agencyInfo.getId() : agencyInfo.getPids().concat(":").concat(agencyInfo.getId())); | 
				
			|||
        insertList.add(e); | 
				
			|||
        if (insertList.size() == NumConstant.ONE_HUNDRED){ | 
				
			|||
            execPersist(); | 
				
			|||
        } | 
				
			|||
 | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    @Override | 
				
			|||
    public void doAfterAllAnalysed(AnalysisContext context) { | 
				
			|||
        execPersist(); | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * Desc: 数据库插入 | 
				
			|||
     */ | 
				
			|||
    private void execPersist() { | 
				
			|||
        try { | 
				
			|||
            if (CollectionUtils.isNotEmpty(insertList)) { | 
				
			|||
                //危化品种类字典
 | 
				
			|||
                Result<Map<String, String>> statusRes = adminOpenFeignClient.dictMap(DictTypeEnum.IC_DANGER_TYPE.getCode()); | 
				
			|||
                if (!statusRes.success()){ | 
				
			|||
                    throw new EpmetException("获取IC_DANGER_TYPE字典表失败"); | 
				
			|||
                } | 
				
			|||
                Map<String, String> statusMap = statusRes.success() && MapUtils.isNotEmpty(statusRes.getData()) ? statusRes.getData() : new HashMap<>(16); | 
				
			|||
                //企业类别字典数据
 | 
				
			|||
                List<IcCoverageCategoryDictListResultDTO> dictList = coverageService.dictMap(agencyInfo.getCustomerId(), "dangerous_chemicals"); | 
				
			|||
                Map<String, String> dictMap = dictList.stream().collect(Collectors.toMap(IcCoverageCategoryDictListResultDTO::getCategoryKey, IcCoverageCategoryDictListResultDTO::getCategoryName)); | 
				
			|||
                insertList.forEach(i -> { | 
				
			|||
                    statusMap.forEach((k,v) -> { | 
				
			|||
                        if (i.getDangerTypeName().equals(v)){ | 
				
			|||
                            i.setDangerType(k); | 
				
			|||
                        } | 
				
			|||
                    }); | 
				
			|||
                    dictMap.forEach((k,v) -> { | 
				
			|||
                        if (i.getCategoryName().equals(v)){ | 
				
			|||
                            i.setCategory(k); | 
				
			|||
                        } | 
				
			|||
                    }); | 
				
			|||
                }); | 
				
			|||
                icDangerousChemicalsService.insertBatch(insertList); | 
				
			|||
            } | 
				
			|||
        } finally { | 
				
			|||
            insertList.clear(); | 
				
			|||
        } | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * 获取错误行 | 
				
			|||
     * @return | 
				
			|||
     */ | 
				
			|||
    public List<DangerousChemicalsModel.RowRemarkMessage> getErrorRows() { | 
				
			|||
        return errorRows; | 
				
			|||
    } | 
				
			|||
} | 
				
			|||
@ -0,0 +1,91 @@ | 
				
			|||
package com.epmet.model; | 
				
			|||
 | 
				
			|||
import com.alibaba.excel.annotation.ExcelProperty; | 
				
			|||
import com.alibaba.excel.annotation.write.style.ColumnWidth; | 
				
			|||
import com.epmet.commons.tools.constant.NumConstant; | 
				
			|||
import lombok.Data; | 
				
			|||
import org.hibernate.validator.constraints.Length; | 
				
			|||
 | 
				
			|||
import javax.validation.constraints.NotBlank; | 
				
			|||
 | 
				
			|||
/** | 
				
			|||
 * @Author zxc | 
				
			|||
 * @DateTime 2022/6/20 10:33 | 
				
			|||
 * @DESC | 
				
			|||
 */ | 
				
			|||
@Data | 
				
			|||
public class DangerousChemicalsModel { | 
				
			|||
 | 
				
			|||
    @NotBlank(message = "企业名称为必填项") | 
				
			|||
    @Length(max = 50,message = "企业名称长度超出50字限制") | 
				
			|||
    @ExcelProperty(value = "企业名称") | 
				
			|||
    private String name; | 
				
			|||
 | 
				
			|||
    @NotBlank(message = "企业类型为必填项") | 
				
			|||
    @ExcelProperty(value = "企业类型") | 
				
			|||
    private String categoryName; | 
				
			|||
 | 
				
			|||
    @ExcelProperty(value = "周边安全间距(公里)") | 
				
			|||
    private String safeDistance = NumConstant.ZERO_STR; | 
				
			|||
 | 
				
			|||
    @ExcelProperty(value = "危化品种类") | 
				
			|||
    private String dangerTypeName; | 
				
			|||
 | 
				
			|||
    @ExcelProperty(value = "负责人") | 
				
			|||
    private String principalName; | 
				
			|||
 | 
				
			|||
    @Length(max = 50,message = "联系电话长度超出50字限制") | 
				
			|||
    @ExcelProperty(value = "联系电话") | 
				
			|||
    private String principalMobile; | 
				
			|||
 | 
				
			|||
    @Length(max = 50,message = "经营地址长度超出50字限制") | 
				
			|||
    @ExcelProperty(value = "经营地址") | 
				
			|||
    private String address; | 
				
			|||
 | 
				
			|||
    @ExcelProperty(value = "备注") | 
				
			|||
    private String remark; | 
				
			|||
 | 
				
			|||
    @Data | 
				
			|||
    public static class RowRemarkMessage { | 
				
			|||
 | 
				
			|||
        @ExcelProperty(value = "行号") | 
				
			|||
        private Integer rowNum; | 
				
			|||
 | 
				
			|||
        @ColumnWidth(20) | 
				
			|||
        @ExcelProperty(value = "企业名称") | 
				
			|||
        private String name; | 
				
			|||
 | 
				
			|||
        @ColumnWidth(20) | 
				
			|||
        @ExcelProperty(value = "企业类型") | 
				
			|||
        private String categoryName; | 
				
			|||
 | 
				
			|||
        @ColumnWidth(20) | 
				
			|||
        @ExcelProperty(value = "周边安全间距(公里)") | 
				
			|||
        private String safeDistance = NumConstant.ZERO_STR; | 
				
			|||
 | 
				
			|||
        @ColumnWidth(20) | 
				
			|||
        @ExcelProperty(value = "危化品种类") | 
				
			|||
        private String dangerTypeName; | 
				
			|||
 | 
				
			|||
        @ColumnWidth(20) | 
				
			|||
        @ExcelProperty(value = "负责人") | 
				
			|||
        private String principalName; | 
				
			|||
 | 
				
			|||
        @ColumnWidth(20) | 
				
			|||
        @ExcelProperty(value = "联系电话") | 
				
			|||
        private String principalMobile; | 
				
			|||
 | 
				
			|||
        @ColumnWidth(40) | 
				
			|||
        @ExcelProperty(value = "经营地址") | 
				
			|||
        private String address; | 
				
			|||
 | 
				
			|||
        @ColumnWidth(60) | 
				
			|||
        @ExcelProperty(value = "备注") | 
				
			|||
        private String remark; | 
				
			|||
 | 
				
			|||
        @ColumnWidth(60) | 
				
			|||
        @ExcelProperty("错误信息") | 
				
			|||
        private String errorInfo; | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
} | 
				
			|||
					Loading…
					
					
				
		Reference in new issue