7 changed files with 396 additions and 7 deletions
@ -0,0 +1,75 @@ |
|||
package com.epmet.excel.data; |
|||
|
|||
import com.alibaba.excel.annotation.ExcelProperty; |
|||
import com.alibaba.excel.annotation.write.style.ColumnWidth; |
|||
import lombok.Data; |
|||
import org.hibernate.validator.constraints.Length; |
|||
|
|||
import javax.validation.constraints.NotBlank; |
|||
import javax.validation.constraints.NotNull; |
|||
import java.util.Date; |
|||
|
|||
|
|||
/** |
|||
* 行程上报excel数据 |
|||
*/ |
|||
@Data |
|||
public class IcTripReportExcelData { |
|||
@NotBlank(message = "姓名为必填项") |
|||
@ExcelProperty("姓名") |
|||
private String name; |
|||
|
|||
@NotBlank(message = "身份证号为必填项") |
|||
@ExcelProperty("身份证号") |
|||
private String idCard; |
|||
|
|||
@NotBlank(message = "手机号为必填项") |
|||
@ExcelProperty("手机号") |
|||
private String mobile; |
|||
|
|||
@NotBlank(message = "现居地为必填项") |
|||
@ExcelProperty("现居地(格式:省-市-区-街道-社区)") |
|||
private String presentAddress; |
|||
|
|||
@NotBlank(message = "详细地址为必填项") |
|||
@ExcelProperty("详细地址") |
|||
private String detailAddress; |
|||
|
|||
@NotBlank(message = "来自地区为必填项") |
|||
@ExcelProperty("来自地区(格式:省-市-区-街道-社区)") |
|||
private String sourceAddress; |
|||
|
|||
@NotNull(message = "来到本地时间为必填项") |
|||
@ExcelProperty("来到本地时间(格式:2022-01-01)") |
|||
private Date arriveDate; |
|||
|
|||
@ExcelProperty("离开本地时间(格式:2022-01-01)") |
|||
private Date leaveDate; |
|||
|
|||
/** |
|||
* 备注信息 |
|||
*/ |
|||
@Length(max = 500,message = "备注不能超过500字") |
|||
@ExcelProperty("备注(500字以内)") |
|||
private String remark; |
|||
|
|||
@Data |
|||
public static class ErrorRow { |
|||
|
|||
@ExcelProperty("姓名") |
|||
@ColumnWidth(20) |
|||
private String name; |
|||
|
|||
@ColumnWidth(20) |
|||
@ExcelProperty("身份证号") |
|||
private String idCard; |
|||
|
|||
@ExcelProperty("手机号") |
|||
@ColumnWidth(20) |
|||
private String mobile; |
|||
|
|||
@ColumnWidth(60) |
|||
@ExcelProperty("错误信息") |
|||
private String errorInfo; |
|||
} |
|||
} |
@ -0,0 +1,113 @@ |
|||
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.data.IcTripReportExcelData; |
|||
import com.epmet.service.impl.IcTripReportRecordServiceImpl; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
|
|||
/** |
|||
* 行程上报excel导入监听器 |
|||
*/ |
|||
@Slf4j |
|||
public class IcTripReportExcelImportListener implements ReadListener<IcTripReportExcelData> { |
|||
|
|||
|
|||
/** |
|||
* 最大条数阈值 |
|||
*/ |
|||
public static final int MAX_THRESHOLD = 2; |
|||
/** |
|||
* 当前操作用户 |
|||
*/ |
|||
private CustomerStaffInfoCacheResult staffInfo; |
|||
|
|||
/** |
|||
* 数据 |
|||
*/ |
|||
private List<IcTripReportRecordEntity> datas = new ArrayList<>(); |
|||
|
|||
/** |
|||
* 错误项列表 |
|||
*/ |
|||
private List<IcTripReportExcelData.ErrorRow> errorRows = new ArrayList<>(); |
|||
|
|||
private IcTripReportRecordServiceImpl tripReportRecordService; |
|||
|
|||
public IcTripReportExcelImportListener(CustomerStaffInfoCacheResult staffInfo, IcTripReportRecordServiceImpl tripReportRecordService) { |
|||
this.staffInfo = staffInfo; |
|||
this.tripReportRecordService = tripReportRecordService; |
|||
} |
|||
|
|||
@Override |
|||
public void invoke(IcTripReportExcelData data, AnalysisContext context) { |
|||
|
|||
try { |
|||
// 先校验数据
|
|||
ValidatorUtils.validateEntity(data); |
|||
|
|||
IcTripReportRecordEntity tripReportRecordEntity = ConvertUtils.sourceToTarget(data, IcTripReportRecordEntity.class); |
|||
tripReportRecordEntity.setAgencyId(staffInfo.getAgencyId()); |
|||
tripReportRecordEntity.setPids(staffInfo.getAgencyPIds()); |
|||
tripReportRecordEntity.setUserType(IcResiUserConstant.USER_TYPE_IMPORT); |
|||
datas.add(tripReportRecordEntity); |
|||
|
|||
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)); |
|||
} |
|||
|
|||
IcTripReportExcelData.ErrorRow errorRow = new IcTripReportExcelData.ErrorRow(); |
|||
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<IcTripReportExcelData.ErrorRow> getErrorRows() { |
|||
return errorRows; |
|||
} |
|||
} |
Loading…
Reference in new issue