forked from rongchao/epmet-cloud-rizhao
7 changed files with 456 additions and 13 deletions
@ -0,0 +1,106 @@ |
|||
package com.epmet.excel; |
|||
|
|||
import com.alibaba.excel.annotation.ExcelProperty; |
|||
import com.alibaba.excel.annotation.write.style.ColumnWidth; |
|||
import lombok.Data; |
|||
|
|||
import javax.validation.constraints.NotBlank; |
|||
|
|||
|
|||
/** |
|||
* 企事业单位导入 |
|||
*/ |
|||
@Data |
|||
public class EnterpriseImportExcelDTO { |
|||
|
|||
/** |
|||
* 场所类型名称 |
|||
*/ |
|||
@NotBlank(message = "场所类型不能为空") |
|||
@ExcelProperty(value = "*场所类型") |
|||
private String placeTypeName; |
|||
|
|||
|
|||
@NotBlank(message = "场所区域不能为空") |
|||
@ExcelProperty(value = "*场所区域") |
|||
private String gridName; |
|||
|
|||
/** |
|||
* 场所名称 |
|||
*/ |
|||
@NotBlank(message = "场所名称不能为空") |
|||
@ExcelProperty(value = "*场所名称") |
|||
private String placeOrgName; |
|||
|
|||
/** |
|||
* 场所地址 |
|||
*/ |
|||
@NotBlank(message = "场所地址不能为空") |
|||
@ExcelProperty(value = "*场所地址") |
|||
private String address; |
|||
|
|||
/** |
|||
* 规模名称 字典value,场所规模【 |
|||
* 0:10人以下 |
|||
* 1:10-20人 |
|||
* 2:21-40人 |
|||
* 3:41-100人 |
|||
* 4:100人以上】 |
|||
*/ |
|||
@NotBlank(message = "规模不能为空") |
|||
@ExcelProperty(value = "*规模") |
|||
private String scaleName; |
|||
|
|||
/** |
|||
* 场所负责人 |
|||
*/ |
|||
@NotBlank(message = "负责人不能为空") |
|||
@ExcelProperty(value = "*负责人") |
|||
private String personInCharge; |
|||
|
|||
/** |
|||
* 负责人电话 |
|||
*/ |
|||
@NotBlank(message = "联系电话不能为空") |
|||
@ExcelProperty(value = "*联系电话") |
|||
private String mobile; |
|||
|
|||
@Data |
|||
public static class ErrorRow { |
|||
|
|||
@ColumnWidth(20) |
|||
@ExcelProperty(value = "场所类型") |
|||
private String placeTypeName; |
|||
|
|||
@ColumnWidth(20) |
|||
@ExcelProperty(value = "场所区域") |
|||
private String gridName; |
|||
|
|||
@ColumnWidth(20) |
|||
@ExcelProperty(value = "场所名称") |
|||
private String placeOrgName; |
|||
|
|||
/** |
|||
* 场所地址 |
|||
*/ |
|||
@ColumnWidth(30) |
|||
@ExcelProperty(value = "场所地址") |
|||
private String address; |
|||
|
|||
@ColumnWidth(20) |
|||
@ExcelProperty(value = "规模") |
|||
private String scaleName; |
|||
|
|||
@ColumnWidth(20) |
|||
@ExcelProperty(value = "负责人") |
|||
private String personInCharge; |
|||
|
|||
@ColumnWidth(20) |
|||
@ExcelProperty(value = "联系电话") |
|||
private String mobile; |
|||
|
|||
@ColumnWidth(60) |
|||
@ExcelProperty("错误信息") |
|||
private String errorInfo; |
|||
} |
|||
} |
@ -0,0 +1,136 @@ |
|||
package com.epmet.excel.handler; |
|||
|
|||
import com.alibaba.excel.context.AnalysisContext; |
|||
import com.alibaba.excel.read.listener.ReadListener; |
|||
import com.epmet.commons.tools.constant.StrConstant; |
|||
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.entity.IcEnterpriseEntity; |
|||
import com.epmet.excel.EnterpriseImportExcelDTO; |
|||
import com.epmet.service.impl.IcEnterpriseServiceImpl; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
@Slf4j |
|||
public class IcEnterpriseExcelImportListener implements ReadListener<EnterpriseImportExcelDTO> { |
|||
//最大条数阈值
|
|||
public static final int MAX_THRESHOLD = 200; |
|||
private String currentCustomerId; |
|||
/** |
|||
* 当前操作用户 |
|||
*/ |
|||
private CustomerStaffInfoCacheResult staffInfo; |
|||
private IcEnterpriseServiceImpl icEnterpriseServiceImpl; |
|||
private Map<String, String> placeTypeMap; |
|||
private Map<String, String> scaleMap; |
|||
private Map<String,String> gridMap; |
|||
//数据
|
|||
private List<IcEnterpriseEntity> datas = new ArrayList<>(); |
|||
|
|||
//错误项列表
|
|||
private List<EnterpriseImportExcelDTO.ErrorRow> errorRows = new ArrayList<>(); |
|||
|
|||
|
|||
public IcEnterpriseExcelImportListener(String currentCustomerId, |
|||
CustomerStaffInfoCacheResult staffInfoCacheResult, |
|||
IcEnterpriseServiceImpl icEnterpriseService, |
|||
Map<String, String> placeTypeMap, |
|||
Map<String, String> scaleMap, |
|||
Map<String,String> gridMap) { |
|||
this.currentCustomerId = currentCustomerId; |
|||
this.staffInfo = staffInfoCacheResult; |
|||
this.icEnterpriseServiceImpl = icEnterpriseService; |
|||
this.placeTypeMap=placeTypeMap; |
|||
this.scaleMap=scaleMap; |
|||
this.gridMap=gridMap; |
|||
} |
|||
|
|||
@Override |
|||
public void invoke(EnterpriseImportExcelDTO data, AnalysisContext context) { |
|||
|
|||
try { |
|||
// 不能为空先校验数据
|
|||
ValidatorUtils.validateEntity(data); |
|||
|
|||
// 去除前后空格
|
|||
// todo
|
|||
// 根据网格名是否能找到网格id
|
|||
// todo
|
|||
if(!placeTypeMap.containsKey(data.getPlaceTypeName())){ |
|||
throw new ValidateException(String.format("%s场所类型不存在",data.getPlaceTypeName())); |
|||
} |
|||
if(!scaleMap.containsKey(data.getScaleName())){ |
|||
throw new ValidateException(String.format("%s规模不存在",data.getScaleName())); |
|||
} |
|||
IcEnterpriseEntity enterpriseEntity = ConvertUtils.sourceToTarget(data, IcEnterpriseEntity.class); |
|||
enterpriseEntity.setCustomerId(currentCustomerId); |
|||
//网格id
|
|||
enterpriseEntity.setGridId("todo"); |
|||
|
|||
enterpriseEntity.setAgencyId(staffInfo.getAgencyId()); |
|||
enterpriseEntity.setAgencyPids(staffInfo.getAgencyPIds()); |
|||
//场所类型
|
|||
enterpriseEntity.setPlaceType(placeTypeMap.get(data.getPlaceTypeName())); |
|||
enterpriseEntity.setLongitude(StrConstant.EPMETY_STR); |
|||
enterpriseEntity.setLatitude(StrConstant.EPMETY_STR); |
|||
//规模
|
|||
enterpriseEntity.setScale(scaleMap.get(data.getScaleName())); |
|||
enterpriseEntity.setSourceType("import"); |
|||
datas.add(enterpriseEntity); |
|||
|
|||
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)); |
|||
} |
|||
EnterpriseImportExcelDTO.ErrorRow errorRow = new EnterpriseImportExcelDTO.ErrorRow(); |
|||
errorRow.setPlaceTypeName(data.getPlaceTypeName()); |
|||
errorRow.setGridName(data.getGridName()); |
|||
errorRow.setPlaceOrgName(data.getPlaceOrgName()); |
|||
errorRow.setAddress(data.getAddress()); |
|||
errorRow.setScaleName(data.getScaleName()); |
|||
errorRow.setPersonInCharge(data.getPersonInCharge()); |
|||
errorRow.setMobile(data.getMobile()); |
|||
errorRow.setErrorInfo(errorMsg); |
|||
errorRows.add(errorRow); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void doAfterAllAnalysed(AnalysisContext context) { |
|||
// 最后几条达不到阈值,这里必须再调用一次
|
|||
execPersist(); |
|||
} |
|||
|
|||
/** |
|||
* 执行持久化 |
|||
*/ |
|||
private void execPersist() { |
|||
try { |
|||
icEnterpriseServiceImpl.insertBatch(datas); |
|||
} finally { |
|||
datas.clear(); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 获取错误行 |
|||
* |
|||
* @return |
|||
*/ |
|||
public List<EnterpriseImportExcelDTO.ErrorRow> getErrorRows() { |
|||
return errorRows; |
|||
} |
|||
} |
Loading…
Reference in new issue