21 changed files with 767 additions and 666 deletions
@ -0,0 +1,111 @@ |
|||||
|
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.IcPartyActivityEntity; |
||||
|
import com.epmet.excel.IcPartyActivityImportExcel; |
||||
|
import com.epmet.service.impl.IcPartyActivityServiceImpl; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* @Description |
||||
|
* @Author yzm |
||||
|
* @Date 2023/2/20 15:36 |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
public class IcPartyActivityImportListener implements ReadListener<IcPartyActivityImportExcel> { |
||||
|
/** |
||||
|
* 最大条数阈值 |
||||
|
*/ |
||||
|
public static final int MAX_THRESHOLD = 200; |
||||
|
/** |
||||
|
* 当前操作用户 |
||||
|
*/ |
||||
|
private CustomerStaffInfoCacheResult staffInfo; |
||||
|
private String customerId; |
||||
|
/** |
||||
|
* 数据 |
||||
|
*/ |
||||
|
private List<IcPartyActivityEntity> datas = new ArrayList<>(); |
||||
|
|
||||
|
/** |
||||
|
* 错误项列表 |
||||
|
*/ |
||||
|
private List<IcPartyActivityImportExcel.ErrorRow> errorRows = new ArrayList<>(); |
||||
|
private IcPartyActivityServiceImpl icPartyActivityService; |
||||
|
|
||||
|
public IcPartyActivityImportListener(String customerId, CustomerStaffInfoCacheResult staffInfo, IcPartyActivityServiceImpl icPartyActivityService) { |
||||
|
this.customerId=customerId; |
||||
|
this.staffInfo = staffInfo; |
||||
|
this.icPartyActivityService = icPartyActivityService; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
@Override |
||||
|
public void invoke(IcPartyActivityImportExcel data, AnalysisContext context) { |
||||
|
try { |
||||
|
// 先校验数据
|
||||
|
ValidatorUtils.validateEntity(data); |
||||
|
IcPartyActivityEntity e = ConvertUtils.sourceToTarget(data, IcPartyActivityEntity.class); |
||||
|
e.setCustomerId(customerId); |
||||
|
e.setAgencyId(staffInfo.getAgencyId()); |
||||
|
e.setPids(staffInfo.getAgencyPIds()); |
||||
|
e.setContent(StrConstant.EPMETY_STR); |
||||
|
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)); |
||||
|
} |
||||
|
|
||||
|
IcPartyActivityImportExcel.ErrorRow errorRow = ConvertUtils.sourceToTarget(data,IcPartyActivityImportExcel.ErrorRow.class); |
||||
|
errorRow.setErrorInfo(errorMsg); |
||||
|
errorRows.add(errorRow); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void doAfterAllAnalysed(AnalysisContext analysisContext) { |
||||
|
// 最后几条达不到阈值,这里必须再调用一次
|
||||
|
execPersist(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 执行持久化 |
||||
|
*/ |
||||
|
private void execPersist() { |
||||
|
try { |
||||
|
if (datas != null && datas.size() > 0) { |
||||
|
icPartyActivityService.batchPersist(datas); |
||||
|
} |
||||
|
} finally { |
||||
|
datas.clear(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取错误行 |
||||
|
* @return |
||||
|
*/ |
||||
|
public List<IcPartyActivityImportExcel.ErrorRow> getErrorRows() { |
||||
|
return errorRows; |
||||
|
} |
||||
|
} |
||||
|
|
@ -0,0 +1,139 @@ |
|||||
|
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.entity.IcPartyUnitEntity; |
||||
|
import com.epmet.excel.IcPartyUnitImportExcel; |
||||
|
import com.epmet.service.impl.IcPartyUnitServiceImpl; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
import java.util.concurrent.atomic.AtomicBoolean; |
||||
|
|
||||
|
/** |
||||
|
* @Description |
||||
|
* @Author yzm |
||||
|
* @Date 2023/2/20 15:36 |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
public class IcPartyUnitExcelImportListener implements ReadListener<IcPartyUnitImportExcel> { |
||||
|
/** |
||||
|
* 最大条数阈值 |
||||
|
*/ |
||||
|
public static final int MAX_THRESHOLD = 200; |
||||
|
/** |
||||
|
* 当前操作用户 |
||||
|
*/ |
||||
|
private CustomerStaffInfoCacheResult staffInfo; |
||||
|
private String customerId; |
||||
|
/** |
||||
|
* 数据 |
||||
|
*/ |
||||
|
private List<IcPartyUnitEntity> datas = new ArrayList<>(); |
||||
|
|
||||
|
/** |
||||
|
* 错误项列表 |
||||
|
*/ |
||||
|
private List<IcPartyUnitImportExcel.ErrorRow> errorRows = new ArrayList<>(); |
||||
|
private IcPartyUnitServiceImpl icPartyUnitService; |
||||
|
//字典表数据
|
||||
|
private Map<String, String> partyUnitTypeMap; |
||||
|
|
||||
|
public IcPartyUnitExcelImportListener(String customerId, CustomerStaffInfoCacheResult staffInfo, IcPartyUnitServiceImpl icPartyUnitService,Map<String, String> partyUnitTypeMap) { |
||||
|
this.customerId=customerId; |
||||
|
this.staffInfo = staffInfo; |
||||
|
this.icPartyUnitService = icPartyUnitService; |
||||
|
this.partyUnitTypeMap=partyUnitTypeMap; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
@Override |
||||
|
public void invoke(IcPartyUnitImportExcel data, AnalysisContext context) { |
||||
|
try { |
||||
|
// 先校验数据
|
||||
|
ValidatorUtils.validateEntity(data); |
||||
|
AtomicBoolean bl = new AtomicBoolean(false); |
||||
|
StringBuffer errMsg = new StringBuffer(""); |
||||
|
//先对一下字段值填写是否正确做判断
|
||||
|
if (!partyUnitTypeMap.containsKey(data.getTypeName())) { |
||||
|
errMsg.append("‘返回方式’值填写错误;"); |
||||
|
bl.set(true); |
||||
|
} |
||||
|
//错误数据记录到错误文件
|
||||
|
if (bl.get()) { |
||||
|
IcPartyUnitImportExcel.ErrorRow errorRow = ConvertUtils.sourceToTarget(data,IcPartyUnitImportExcel.ErrorRow.class); |
||||
|
errorRow.setErrorInfo(errMsg.toString()); |
||||
|
errorRows.add(errorRow); |
||||
|
return; |
||||
|
} |
||||
|
// 判断名称是否已存在
|
||||
|
if(icPartyUnitService.checkUnitName(data.getUnitName(),staffInfo.getAgencyId(),null)){ |
||||
|
IcPartyUnitImportExcel.ErrorRow errorRow = ConvertUtils.sourceToTarget(data,IcPartyUnitImportExcel.ErrorRow.class); |
||||
|
errorRow.setErrorInfo("联建单位名称已存在"); |
||||
|
errorRows.add(errorRow); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
IcPartyUnitEntity e = ConvertUtils.sourceToTarget(data, IcPartyUnitEntity.class); |
||||
|
if (partyUnitTypeMap.containsKey(data.getTypeName())) { |
||||
|
e.setType(partyUnitTypeMap.get(data.getTypeName())); |
||||
|
} |
||||
|
e.setCustomerId(customerId); |
||||
|
e.setAgencyId(staffInfo.getAgencyId()); |
||||
|
e.setPids(staffInfo.getAgencyPIds()); |
||||
|
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)); |
||||
|
} |
||||
|
|
||||
|
IcPartyUnitImportExcel.ErrorRow errorRow = ConvertUtils.sourceToTarget(data,IcPartyUnitImportExcel.ErrorRow.class); |
||||
|
errorRow.setErrorInfo(errorMsg); |
||||
|
errorRows.add(errorRow); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void doAfterAllAnalysed(AnalysisContext analysisContext) { |
||||
|
// 最后几条达不到阈值,这里必须再调用一次
|
||||
|
execPersist(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 执行持久化 |
||||
|
*/ |
||||
|
private void execPersist() { |
||||
|
try { |
||||
|
if (datas != null && datas.size() > 0) { |
||||
|
icPartyUnitService.batchPersist(datas); |
||||
|
} |
||||
|
} finally { |
||||
|
datas.clear(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取错误行 |
||||
|
* @return |
||||
|
*/ |
||||
|
public List<IcPartyUnitImportExcel.ErrorRow> getErrorRows() { |
||||
|
return errorRows; |
||||
|
} |
||||
|
} |
||||
|
|
@ -0,0 +1 @@ |
|||||
|
alter table ic_party_unit MODIFY COLUMN `TYPE` varchar(32) NULL COMMENT '分类 【字典表】:02.20因烟台导入需求改为不必填'; |
Binary file not shown.
Loading…
Reference in new issue