forked from luyan/epmet-cloud-lingshan
10 changed files with 516 additions and 17 deletions
@ -0,0 +1,117 @@ |
|||
package com.epmet.excel.handler; |
|||
|
|||
import com.alibaba.excel.context.AnalysisContext; |
|||
import com.alibaba.excel.read.listener.ReadListener; |
|||
import com.epmet.commons.tools.constant.NumConstant; |
|||
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.utils.ObjectUtil; |
|||
import com.epmet.commons.tools.validator.ValidatorUtils; |
|||
import com.epmet.dto.result.CommunityBuildingManagerDTO; |
|||
import com.epmet.excel.yt.CommunityBuildingManagerImportExcelData; |
|||
import com.epmet.service.impl.CommunityBuildingManagerServiceImpl; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.apache.commons.collections4.CollectionUtils; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
/** |
|||
* @Description |
|||
* @Author yzm |
|||
* @Date 2023/5/9 16:26 |
|||
*/ |
|||
@Slf4j |
|||
public class CommunityBuildingManagerImportListener implements ReadListener<CommunityBuildingManagerImportExcelData> { |
|||
|
|||
// 最大条数阈值
|
|||
public static final int MAX_THRESHOLD = 200; |
|||
private String customerId; |
|||
private String staffId; |
|||
private String agencyId; |
|||
private String districtId; |
|||
private String streetId; |
|||
private String rediPrex; |
|||
private CommunityBuildingManagerServiceImpl communityBuildingManagerService; |
|||
// 错误项列表
|
|||
private List<CommunityBuildingManagerImportExcelData.ErrorRow> errorRows = new ArrayList<>(); |
|||
// 要插入的数据
|
|||
private List<CommunityBuildingManagerDTO> excelDataList = new ArrayList<>(); |
|||
|
|||
public CommunityBuildingManagerImportListener(String customerId, String staffId, String agencyId,String districtId,String streetId,String rediPrex,CommunityBuildingManagerServiceImpl communityBuildingManagerService) { |
|||
this.customerId = customerId; |
|||
this.staffId = staffId; |
|||
this.agencyId=agencyId; |
|||
this.districtId=districtId; |
|||
this.streetId=streetId; |
|||
this.rediPrex=rediPrex; |
|||
this.communityBuildingManagerService = communityBuildingManagerService; |
|||
} |
|||
|
|||
|
|||
@Override |
|||
public void invoke(CommunityBuildingManagerImportExcelData data, AnalysisContext analysisContext) { |
|||
try { |
|||
// log.warn("有数据吗?"+JSON.toJSONString(data));
|
|||
// 不能为空先校验数据
|
|||
ValidatorUtils.validateEntity(data); |
|||
// 去除空格
|
|||
ObjectUtil.objectToTrim(data); |
|||
|
|||
CommunityBuildingManagerDTO communityBuildingManagerDTO = ConvertUtils.sourceToTarget(data, CommunityBuildingManagerDTO.class); |
|||
communityBuildingManagerDTO.setCustomerId(customerId); |
|||
communityBuildingManagerDTO.setCommunityId(agencyId); |
|||
communityBuildingManagerDTO.setType("单元长".equals(data.getTypeName()) ? NumConstant.ONE_STR : NumConstant.ZERO_STR); |
|||
excelDataList.add(communityBuildingManagerDTO); |
|||
if (excelDataList.size() == MAX_THRESHOLD) { |
|||
execPersist(); |
|||
} |
|||
} catch (Exception e) { |
|||
String errorMsg = null; |
|||
if (e instanceof ValidateException) { |
|||
errorMsg = ((ValidateException) e).getMsg(); |
|||
} else if (e instanceof EpmetException) { |
|||
errorMsg = ((EpmetException) e).getInternalMsg(); |
|||
} else { |
|||
errorMsg = "未知错误"; |
|||
log.error("【楼长单元长ic_property_management导入】出错:{}", ExceptionUtils.getErrorStackTrace(e)); |
|||
} |
|||
CommunityBuildingManagerImportExcelData.ErrorRow errorRow = ConvertUtils.sourceToTarget(data, CommunityBuildingManagerImportExcelData.ErrorRow.class); |
|||
errorRow.setErrorInfo(errorMsg); |
|||
errorRows.add(errorRow); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void doAfterAllAnalysed(AnalysisContext analysisContext) { |
|||
// 最后几条达不到阈值,这里必须再调用一次
|
|||
execPersist(); |
|||
} |
|||
|
|||
/** |
|||
* 执行持久化 |
|||
*/ |
|||
private void execPersist() { |
|||
try { |
|||
if (CollectionUtils.isNotEmpty(excelDataList)) { |
|||
communityBuildingManagerService.handleImportExcelData(customerId,staffId,agencyId, districtId, streetId,rediPrex,excelDataList,this); |
|||
} |
|||
} finally { |
|||
excelDataList.clear(); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 获取错误行 |
|||
* |
|||
* @return |
|||
*/ |
|||
public List<CommunityBuildingManagerImportExcelData.ErrorRow> getErrorRows() { |
|||
return errorRows; |
|||
} |
|||
|
|||
|
|||
} |
|||
|
@ -0,0 +1,116 @@ |
|||
package com.epmet.excel.yt; |
|||
|
|||
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; |
|||
|
|||
/** |
|||
* @Description |
|||
* @Author yzm |
|||
* @Date 2023/5/9 16:13 |
|||
*/ |
|||
@Data |
|||
public class CommunityBuildingManagerImportExcelData { |
|||
|
|||
/** |
|||
* 姓名 |
|||
*/ |
|||
@ExcelProperty(value = "*姓名") |
|||
@Length(max = 50, message = "姓名最多输入50字") |
|||
@NotBlank(message = "姓名不能为空") |
|||
private String name; |
|||
|
|||
/** |
|||
* 联系电话 |
|||
*/ |
|||
@ExcelProperty(value = "*联系电话") |
|||
@Length(max = 50, message = "联系电话最多输入50字") |
|||
@NotBlank(message = "联系电话不能为空") |
|||
private String phone; |
|||
|
|||
/** |
|||
* 身份证号 |
|||
*/ |
|||
@ExcelProperty(value = "*身份证号") |
|||
@Length(max = 50, message = "身份证号最多输入50字") |
|||
@NotBlank(message = "身份证号不能为空") |
|||
private String idCard; |
|||
|
|||
/** |
|||
* 类型:0楼长;1单元长 |
|||
*/ |
|||
@ExcelProperty(value = "*类型") |
|||
@NotBlank(message = "类型不能为空") |
|||
private String typeName; |
|||
|
|||
@ExcelProperty(value = "*所属网格") |
|||
@NotBlank(message = "所属网格不能为空") |
|||
private String gridName; |
|||
|
|||
@ExcelProperty(value = "*所属小区") |
|||
@NotBlank(message = "所属小区不能为空") |
|||
private String viliageName; |
|||
|
|||
@ExcelProperty(value = "*楼栋") |
|||
@NotBlank(message = "楼栋不能为空") |
|||
private String buildingName; |
|||
|
|||
@ExcelProperty(value = "单元") |
|||
private String unitName; |
|||
|
|||
@Data |
|||
public static class ErrorRow { |
|||
/** |
|||
* 姓名 |
|||
*/ |
|||
@ExcelProperty(value = "*姓名") |
|||
@ColumnWidth(20) |
|||
private String name; |
|||
|
|||
/** |
|||
* 联系电话 |
|||
*/ |
|||
@ExcelProperty(value = "*联系电话") |
|||
@ColumnWidth(20) |
|||
private String phone; |
|||
|
|||
/** |
|||
* 身份证号 |
|||
*/ |
|||
@ExcelProperty(value = "*身份证号") |
|||
@ColumnWidth(25) |
|||
private String idCard; |
|||
|
|||
/** |
|||
* 类型:0楼长;1单元长 |
|||
*/ |
|||
@ExcelProperty(value = "*类型") |
|||
@ColumnWidth(15) |
|||
private String typeName; |
|||
|
|||
@ExcelProperty(value = "*所属网格") |
|||
@ColumnWidth(30) |
|||
private String gridName; |
|||
|
|||
@ExcelProperty(value = "*所属小区") |
|||
@ColumnWidth(30) |
|||
private String viliageName; |
|||
|
|||
@ExcelProperty(value = "*楼栋") |
|||
@ColumnWidth(20) |
|||
private String buildingName; |
|||
|
|||
@ExcelProperty(value = "单元") |
|||
@ColumnWidth(20) |
|||
private String unitName; |
|||
|
|||
@ColumnWidth(60) |
|||
@ExcelProperty("错误信息") |
|||
private String errorInfo; |
|||
} |
|||
|
|||
} |
|||
|
Loading…
Reference in new issue