forked from rongchao/epmet-cloud-rizhao
8 changed files with 390 additions and 8 deletions
@ -0,0 +1,120 @@ |
|||
package com.epmet.excel.handler; |
|||
|
|||
import com.alibaba.excel.context.AnalysisContext; |
|||
import com.alibaba.excel.read.listener.ReadListener; |
|||
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.entity.IcPropertyManagementEntity; |
|||
import com.epmet.excel.yt.IcPropertyManagementImportExcelData; |
|||
import com.epmet.service.impl.IcPropertyManagementServiceImpl; |
|||
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/8 14:23 |
|||
*/ |
|||
@Slf4j |
|||
public class IcPropertyManagementImportListener implements ReadListener<IcPropertyManagementImportExcelData> { |
|||
|
|||
// 最大条数阈值
|
|||
public static final int MAX_THRESHOLD = 200; |
|||
private String currentCustomerId; |
|||
private IcPropertyManagementServiceImpl propertyManagementService; |
|||
// 错误项列表
|
|||
private List<IcPropertyManagementImportExcelData.ErrorRow> errorRows = new ArrayList<>(); |
|||
// 要插入的数据
|
|||
private List<IcPropertyManagementEntity> insertDatas = new ArrayList<>(); |
|||
private List<IcPropertyManagementEntity> updateDatas = new ArrayList<>(); |
|||
|
|||
public IcPropertyManagementImportListener(String customerId, IcPropertyManagementServiceImpl propertyManagementService) { |
|||
this.currentCustomerId = customerId; |
|||
this.propertyManagementService = propertyManagementService; |
|||
} |
|||
|
|||
@Override |
|||
public void invoke(IcPropertyManagementImportExcelData data, AnalysisContext analysisContext) { |
|||
try { |
|||
// log.warn("有数据吗?"+JSON.toJSONString(data));
|
|||
// 不能为空先校验数据
|
|||
ValidatorUtils.validateEntity(data); |
|||
// 去除空格
|
|||
ObjectUtil.objectToTrim(data); |
|||
//物业名称唯一
|
|||
IcPropertyManagementEntity origin=propertyManagementService.getByName(currentCustomerId, data.getName()); |
|||
|
|||
IcPropertyManagementEntity propertyManagementEntity = ConvertUtils.sourceToTarget(data, IcPropertyManagementEntity.class); |
|||
propertyManagementEntity.setCustomerId(currentCustomerId); |
|||
if (null != origin) { |
|||
origin.setContactMobile(data.getContactMobile()); |
|||
origin.setContactName(data.getContactName()); |
|||
insertDatas.add(origin); |
|||
} else { |
|||
insertDatas.add(propertyManagementEntity); |
|||
} |
|||
|
|||
if (insertDatas.size() == MAX_THRESHOLD) { |
|||
execPersist(); |
|||
} |
|||
if (updateDatas.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)); |
|||
} |
|||
IcPropertyManagementImportExcelData.ErrorRow errorRow = ConvertUtils.sourceToTarget(data, IcPropertyManagementImportExcelData.ErrorRow.class); |
|||
errorRow.setErrorInfo(errorMsg); |
|||
errorRows.add(errorRow); |
|||
} |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void doAfterAllAnalysed(AnalysisContext analysisContext) { |
|||
// 最后几条达不到阈值,这里必须再调用一次
|
|||
execPersist(); |
|||
} |
|||
|
|||
/** |
|||
* 执行持久化 |
|||
*/ |
|||
private void execPersist() { |
|||
try { |
|||
if (CollectionUtils.isNotEmpty(insertDatas)) { |
|||
propertyManagementService.insertBatch(insertDatas); |
|||
} |
|||
|
|||
if (CollectionUtils.isNotEmpty(updateDatas)) { |
|||
propertyManagementService.updateBatchById(updateDatas); |
|||
} |
|||
} finally { |
|||
insertDatas.clear(); |
|||
updateDatas.clear(); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 获取错误行 |
|||
* |
|||
* @return |
|||
*/ |
|||
public List<IcPropertyManagementImportExcelData.ErrorRow> getErrorRows() { |
|||
return errorRows; |
|||
} |
|||
} |
|||
|
@ -0,0 +1,58 @@ |
|||
package com.epmet.excel.yt; |
|||
|
|||
import com.alibaba.excel.annotation.ExcelProperty; |
|||
import com.alibaba.excel.annotation.write.style.ColumnWidth; |
|||
import lombok.Data; |
|||
|
|||
/** |
|||
* @Description |
|||
* @Author yzm |
|||
* @Date 2023/5/8 14:24 |
|||
*/ |
|||
@Data |
|||
public class IcPropertyManagementImportExcelData { |
|||
/** |
|||
* 物业名称 |
|||
*/ |
|||
@ExcelProperty(value = "物业名称") |
|||
private String name; |
|||
|
|||
/** |
|||
* 烟台需求:物业联系人姓名 |
|||
*/ |
|||
@ExcelProperty(value = "物业联系人") |
|||
private String contactName; |
|||
|
|||
/** |
|||
* 烟台需求:物业联系人电话 |
|||
*/ |
|||
@ExcelProperty(value = "联系电话") |
|||
private String contactMobile; |
|||
|
|||
@Data |
|||
public static class ErrorRow { |
|||
/** |
|||
* 物业名称 |
|||
*/ |
|||
@ExcelProperty(value = "物业名称") |
|||
private String name; |
|||
|
|||
/** |
|||
* 烟台需求:物业联系人姓名 |
|||
*/ |
|||
@ExcelProperty(value = "物业联系人") |
|||
private String contactName; |
|||
|
|||
/** |
|||
* 烟台需求:物业联系人电话 |
|||
*/ |
|||
@ExcelProperty(value = "联系电话") |
|||
private String contactMobile; |
|||
|
|||
|
|||
@ColumnWidth(60) |
|||
@ExcelProperty("错误信息") |
|||
private String errorInfo; |
|||
} |
|||
} |
|||
|
Loading…
Reference in new issue