forked from rongchao/epmet-cloud-rizhao
8 changed files with 408 additions and 8 deletions
@ -0,0 +1,110 @@ |
|||
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.constant.StrConstant; |
|||
import com.epmet.commons.tools.exception.EpmetException; |
|||
import com.epmet.commons.tools.exception.ExceptionUtils; |
|||
import com.epmet.commons.tools.utils.ConvertUtils; |
|||
import com.epmet.commons.tools.validator.ValidatorUtils; |
|||
import com.epmet.entity.CustomerAgencyEntity; |
|||
import com.epmet.entity.CustomerDepartmentEntity; |
|||
import com.epmet.excel.yt.DeptImportExcelDTO; |
|||
import com.epmet.service.CustomerDepartmentService; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.apache.commons.collections4.CollectionUtils; |
|||
import org.apache.commons.lang3.StringUtils; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
/** |
|||
* @Description |
|||
* @Author yzm |
|||
* @Date 2023/4/26 16:11 |
|||
*/ |
|||
@Slf4j |
|||
public class DeptExcelImportListener implements ReadListener<DeptImportExcelDTO> { |
|||
//最大条数阈值
|
|||
public static final int MAX_THRESHOLD = 200; |
|||
private String customerId; |
|||
private String staffId; |
|||
private CustomerAgencyEntity agencyEntity; |
|||
/** |
|||
* 当前操作用户 |
|||
*/ |
|||
//要插入的数据
|
|||
private List<CustomerDepartmentEntity> insertDatas = new ArrayList<>(); |
|||
//错误项列表
|
|||
private List<DeptImportExcelDTO.ErrorRow> errorRows = new ArrayList<>(); |
|||
private CustomerDepartmentService departmentService; |
|||
public DeptExcelImportListener(String loginUserCustomerId, String loginUserId, CustomerAgencyEntity agencyEntity, CustomerDepartmentService departmentService) { |
|||
this.customerId = loginUserCustomerId; |
|||
this.staffId = loginUserId; |
|||
this.agencyEntity = agencyEntity; |
|||
this.departmentService = departmentService; |
|||
} |
|||
|
|||
@Override |
|||
public void invoke(DeptImportExcelDTO data, AnalysisContext analysisContext) { |
|||
try { |
|||
ValidatorUtils.validateEntity(data); |
|||
//当前组织下,是否存在该部门
|
|||
departmentService.checkUnqiueName(agencyEntity.getId(),data.getDepartmentName(),null); |
|||
CustomerDepartmentEntity entity = ConvertUtils.sourceToTarget(data, CustomerDepartmentEntity.class); |
|||
entity.setCustomerId(customerId); |
|||
entity.setAgencyId(agencyEntity.getId()); |
|||
entity.setCreatedBy(staffId); |
|||
entity.setUpdatedBy(staffId); |
|||
entity.setTotalUser(NumConstant.ZERO); |
|||
entity.setAreaCode(StringUtils.isNotBlank(agencyEntity.getAreaCode())?agencyEntity.getAreaCode(): StrConstant.EPMETY_STR); |
|||
insertDatas.add(entity); |
|||
if (insertDatas.size() == MAX_THRESHOLD) { |
|||
execPersist(); |
|||
} |
|||
} catch (Exception e) { |
|||
String errorMsg = null; |
|||
if(e instanceof EpmetException){ |
|||
errorMsg=((EpmetException) e).getMsg(); |
|||
}else { |
|||
errorMsg = "未知错误"; |
|||
log.error("【部门导入】出错:{}", ExceptionUtils.getErrorStackTrace(e)); |
|||
} |
|||
DeptImportExcelDTO.ErrorRow errorRow = ConvertUtils.sourceToTarget(data,DeptImportExcelDTO.ErrorRow.class); |
|||
errorRow.setErrorInfo(errorMsg); |
|||
errorRows.add(errorRow); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void doAfterAllAnalysed(AnalysisContext context) { |
|||
// 最后几条达不到阈值,这里必须再调用一次
|
|||
execPersist(); |
|||
} |
|||
|
|||
/** |
|||
* 执行持久化 |
|||
*/ |
|||
private void execPersist() { |
|||
try { |
|||
if (CollectionUtils.isNotEmpty(insertDatas)) { |
|||
departmentService.insertBatch(insertDatas); |
|||
} |
|||
|
|||
} finally { |
|||
insertDatas.clear(); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 获取错误行 |
|||
* |
|||
* @return |
|||
*/ |
|||
public List<DeptImportExcelDTO.ErrorRow> getErrorRows() { |
|||
return errorRows; |
|||
} |
|||
|
|||
} |
|||
|
@ -0,0 +1,72 @@ |
|||
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/4/26 15:59 |
|||
*/ |
|||
@Data |
|||
public class DeptImportExcelDTO { |
|||
/** |
|||
* 部门名称 |
|||
*/ |
|||
@NotBlank(message = "部门名称不能为空") |
|||
@Length(max = 50, message = "部门名称不能超过50个字") |
|||
@ExcelProperty(value = "*部门名称") |
|||
private String departmentName; |
|||
|
|||
/** |
|||
* 组织编码 |
|||
*/ |
|||
@ExcelProperty(value = "组织编码") |
|||
private String code; |
|||
/** |
|||
* 负责人 |
|||
*/ |
|||
@ExcelProperty(value = "联系人") |
|||
private String contacts; |
|||
/** |
|||
* 联系电话 |
|||
*/ |
|||
@ExcelProperty(value = "联系电话") |
|||
private String mobile; |
|||
|
|||
@Data |
|||
public static class ErrorRow { |
|||
@ColumnWidth(50) |
|||
@ExcelProperty(value = "*部门名称") |
|||
private String departmentName; |
|||
|
|||
/** |
|||
* 组织编码 |
|||
*/ |
|||
@ColumnWidth(20) |
|||
@ExcelProperty(value = "组织编码") |
|||
private String code; |
|||
/** |
|||
* 负责人 |
|||
*/ |
|||
@ColumnWidth(20) |
|||
@ExcelProperty(value = "联系人") |
|||
private String contacts; |
|||
|
|||
/** |
|||
* 联系电话 |
|||
*/ |
|||
@ColumnWidth(20) |
|||
@ExcelProperty(value = "联系电话") |
|||
private String mobile; |
|||
|
|||
@ColumnWidth(50) |
|||
@ExcelProperty("错误信息") |
|||
private String errorInfo; |
|||
} |
|||
} |
|||
|
Binary file not shown.
Loading…
Reference in new issue