forked from rongchao/epmet-cloud-rizhao
7 changed files with 354 additions and 9 deletions
@ -0,0 +1,34 @@ |
|||||
|
package com.epmet.excel; |
||||
|
|
||||
|
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
import lombok.Data; |
||||
|
|
||||
|
import javax.validation.constraints.NotBlank; |
||||
|
|
||||
|
/** |
||||
|
* @Author zxc |
||||
|
* @DateTime 2022/11/1 11:18 |
||||
|
*/ |
||||
|
@Data |
||||
|
public class IcDangerAreaImportExcel { |
||||
|
|
||||
|
@NotBlank(message = "风险地区为必填项") |
||||
|
@ExcelProperty("风险地区") |
||||
|
private String allName; |
||||
|
|
||||
|
@NotBlank(message = "风险等级为必填项") |
||||
|
@ExcelProperty("风险等级") |
||||
|
private String dangerLevel; |
||||
|
|
||||
|
@Data |
||||
|
public static class IcDangerAreaError{ |
||||
|
@ExcelProperty("风险地区") |
||||
|
private String name; |
||||
|
|
||||
|
@ExcelProperty("风险等级") |
||||
|
private String dangerLevel; |
||||
|
|
||||
|
@ExcelProperty("错误信息") |
||||
|
private String errorInfo; |
||||
|
} |
||||
|
} |
@ -0,0 +1,130 @@ |
|||||
|
package com.epmet.excel.handler; |
||||
|
|
||||
|
import com.alibaba.excel.context.AnalysisContext; |
||||
|
import com.alibaba.excel.read.listener.ReadListener; |
||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||
|
import com.epmet.commons.tools.constant.NumConstant; |
||||
|
import com.epmet.commons.tools.exception.EpmetErrorCode; |
||||
|
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.validator.ValidatorUtils; |
||||
|
import com.epmet.dao.IcDangerAreaDao; |
||||
|
import com.epmet.entity.IcDangerAreaEntity; |
||||
|
import com.epmet.entity.IcNatEntity; |
||||
|
import com.epmet.enums.DangerLevelEnum; |
||||
|
import com.epmet.excel.IcDangerAreaImportExcel; |
||||
|
import com.epmet.excel.data.IcNatImportExcelData; |
||||
|
import com.epmet.service.IcDangerAreaService; |
||||
|
import lombok.Data; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.collections4.CollectionUtils; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
/** |
||||
|
* 核酸检测excel导入监听器 |
||||
|
*/ |
||||
|
@Data |
||||
|
@Slf4j |
||||
|
public class IcDangerAreaExcelImportListener implements ReadListener<IcDangerAreaImportExcel> { |
||||
|
|
||||
|
public static final int MAX_THRESHOLD = 200; |
||||
|
private String customerId = ""; |
||||
|
private Map<String,String> dangerLevelMap = null; |
||||
|
private List<IcDangerAreaEntity> datas = new ArrayList<>(); |
||||
|
private List<IcDangerAreaImportExcel.IcDangerAreaError> errorRows = new ArrayList<>(); |
||||
|
|
||||
|
@Autowired |
||||
|
private IcDangerAreaService icDangerAreaService; |
||||
|
@Autowired |
||||
|
private IcDangerAreaDao icDangerAreaDao; |
||||
|
|
||||
|
public IcDangerAreaExcelImportListener(Map<String,String> dangerLevelMap, String customerId, IcDangerAreaService icDangerAreaService, IcDangerAreaDao icDangerAreaDao) { |
||||
|
this.dangerLevelMap = dangerLevelMap; |
||||
|
this.customerId = customerId; |
||||
|
this.icDangerAreaService = icDangerAreaService; |
||||
|
this.icDangerAreaDao = icDangerAreaDao; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void invoke(IcDangerAreaImportExcel data, AnalysisContext context) { |
||||
|
try { |
||||
|
LambdaQueryWrapper<IcDangerAreaEntity> qw = new LambdaQueryWrapper<>(); |
||||
|
qw.eq(IcDangerAreaEntity::getAllName,data.getAllName()); |
||||
|
IcDangerAreaEntity icDangerAreaEntity = icDangerAreaDao.selectOne(qw); |
||||
|
if (null != icDangerAreaEntity){ |
||||
|
throw new EpmetException(EpmetErrorCode.DANGER_AREA_ERROR.getCode()); |
||||
|
} |
||||
|
ValidatorUtils.validateEntity(data); |
||||
|
IcDangerAreaEntity entity = ConvertUtils.sourceToTarget(data, IcDangerAreaEntity.class); |
||||
|
String[] split = data.getAllName().split("-"); |
||||
|
for (int i = NumConstant.ZERO; i < split.length; i++) { |
||||
|
switch (i){ |
||||
|
case NumConstant.ZERO: |
||||
|
entity.setProvince(split[i]); |
||||
|
break; |
||||
|
case NumConstant.ONE: |
||||
|
entity.setCity(split[i]); |
||||
|
break; |
||||
|
case NumConstant.TWO: |
||||
|
entity.setDistrict(split[i]); |
||||
|
break; |
||||
|
default: |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
entity.setDangerLevel(dangerLevelMap.get(entity.getDangerLevel())); |
||||
|
entity.setCustomerId(customerId); |
||||
|
datas.add(entity); |
||||
|
if (datas.size() == MAX_THRESHOLD) { |
||||
|
execPersist(); |
||||
|
} |
||||
|
} catch (Exception e) { |
||||
|
String errorMsg = null; |
||||
|
if (e instanceof ValidateException) { |
||||
|
errorMsg = ((ValidateException) e).getMsg(); |
||||
|
} else { |
||||
|
log.error("【风险地区信息导入】出错:{}", e.getStackTrace()); |
||||
|
errorMsg = ((EpmetException)e).getInternalMsg(); |
||||
|
log.error("【风险地区信息导入】出错:{}", ExceptionUtils.getErrorStackTrace(e)); |
||||
|
} |
||||
|
IcDangerAreaImportExcel.IcDangerAreaError errorRow = new IcDangerAreaImportExcel.IcDangerAreaError(); |
||||
|
errorRow.setName(data.getAllName()); |
||||
|
errorRow.setErrorInfo(errorMsg); |
||||
|
errorRows.add(errorRow); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
|
// 最后几条达不到阈值,这里必须再调用一次
|
||||
|
execPersist(); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 执行持久化 |
||||
|
*/ |
||||
|
private void execPersist() { |
||||
|
try { |
||||
|
if (CollectionUtils.isNotEmpty(datas)){ |
||||
|
icDangerAreaService.insertBatch(datas); |
||||
|
} |
||||
|
} finally { |
||||
|
datas.clear(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取错误行 |
||||
|
* @return |
||||
|
*/ |
||||
|
public List<IcDangerAreaImportExcel.IcDangerAreaError> getErrorRows() { |
||||
|
return errorRows; |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue