7 changed files with 231 additions and 37 deletions
			
			
		| @ -0,0 +1,157 @@ | |||
| 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.dto.result.CustomerStaffInfoCacheResult; | |||
| import com.epmet.commons.tools.exception.EpmetErrorCode; | |||
| 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.IcSocietyOrgEntity; | |||
| import com.epmet.excel.IcSocietyOrgImportExcelDTO; | |||
| import com.epmet.service.impl.IcSocietyOrgServiceImpl; | |||
| import lombok.extern.slf4j.Slf4j; | |||
| import org.apache.commons.collections4.CollectionUtils; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| 
 | |||
| import java.util.ArrayList; | |||
| import java.util.HashMap; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| 
 | |||
| /** | |||
|  * @Description | |||
|  * @Author yzm | |||
|  * @Date 2022/8/24 11:42 | |||
|  */ | |||
| @Slf4j | |||
| public class IcSocietyOrgExcelImportListener implements ReadListener<IcSocietyOrgImportExcelDTO> { | |||
|     // 最大条数阈值
 | |||
|     public static final int MAX_THRESHOLD = 2; | |||
|     private String currentCustomerId; | |||
|     /** | |||
|      * 当前操作用户 | |||
|      */ | |||
|     private CustomerStaffInfoCacheResult staffInfo; | |||
|     private IcSocietyOrgServiceImpl icSocietyOrgService; | |||
|     private List<IcSocietyOrgImportExcelDTO.ErrorRow> errorRows = new ArrayList<>(); | |||
|     /** | |||
|      * 要插入的数据 | |||
|      */ | |||
|     private List<IcSocietyOrgEntity> insertDatas = new ArrayList<>(); | |||
|     /** | |||
|      * 根据组织名称更新的数据 | |||
|      */ | |||
|     private List<IcSocietyOrgEntity> updateDatas = new ArrayList<>(); | |||
|     private Map<String,String> staffMap=new HashMap<>(); | |||
|     public IcSocietyOrgExcelImportListener(String customerId, CustomerStaffInfoCacheResult staffInfo, IcSocietyOrgServiceImpl icSocietyOrgService, Map<String,String> staffMap) { | |||
|         this.currentCustomerId = customerId; | |||
|         this.staffInfo = staffInfo; | |||
|         this.icSocietyOrgService = icSocietyOrgService; | |||
|         this.staffMap=staffMap; | |||
|     } | |||
| 
 | |||
|     @Override | |||
|     public void invoke(IcSocietyOrgImportExcelDTO data, AnalysisContext analysisContext) { | |||
|         try { | |||
|             // log.warn("有数据吗?"+JSON.toJSONString(data));
 | |||
|             // 不能为空先校验数据
 | |||
|             ValidatorUtils.validateEntity(data); | |||
|             // 去除空格
 | |||
|             ObjectUtil.objectToTrim(data); | |||
|             IcSocietyOrgEntity icSocietyOrgEntity = ConvertUtils.sourceToTarget(data, IcSocietyOrgEntity.class); | |||
|             icSocietyOrgEntity.setCustomerId(currentCustomerId); | |||
|             icSocietyOrgEntity.setAgencyId(staffInfo.getAgencyId()); | |||
|             icSocietyOrgEntity.setPids(staffInfo.getAgencyPIds()); | |||
|             if(StringUtils.isNotBlank(data.getAdminStaffName())){ | |||
|                 String adminStaffId = null; | |||
|                 for(String key:staffMap.keySet()){ | |||
|                     if (data.getAdminStaffName().equals(staffMap.get(key))) { | |||
|                         adminStaffId=key; | |||
|                         break; | |||
|                     } | |||
|                 } | |||
|                 if (StringUtils.isBlank(adminStaffId)) { | |||
|                     String msg = String.format("当前组织下没有【%s】", data.getAdminStaffName()); | |||
|                     throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), msg, msg); | |||
|                 } | |||
|                 icSocietyOrgEntity.setAdminStaffId(adminStaffId); | |||
|             } | |||
|             //网格id+场所名称
 | |||
|             List<IcSocietyOrgEntity> originList = icSocietyOrgService.selectForUniqueName(icSocietyOrgEntity.getAgencyId(), icSocietyOrgEntity.getSocietyName(), null); | |||
|             if(CollectionUtils.isEmpty(originList)){ | |||
|                 insertDatas.add(icSocietyOrgEntity); | |||
|             }else{ | |||
|                 IcSocietyOrgEntity origin=originList.get(NumConstant.ZERO); | |||
|                 icSocietyOrgEntity.setId(origin.getId()); | |||
|                 updateDatas.add(icSocietyOrgEntity); | |||
|             } | |||
| 
 | |||
| 
 | |||
|             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).getMsg(); | |||
|             }else { | |||
|                 errorMsg = "未知错误"; | |||
|                 log.error("【社会组织导入】出错:{}", ExceptionUtils.getErrorStackTrace(e)); | |||
|             } | |||
|             IcSocietyOrgImportExcelDTO.ErrorRow errorRow = new IcSocietyOrgImportExcelDTO.ErrorRow(); | |||
|             errorRow.setSocietyName(data.getSocietyName()); | |||
|             errorRow.setServiceMatters(data.getServiceMatters()); | |||
|             errorRow.setMobile(data.getMobile()); | |||
|             errorRow.setServiceTimeStr(data.getServiceTimeStr()); | |||
|             errorRow.setAdminStaffName(data.getAdminStaffName()); | |||
|             errorRow.setAddress(data.getAddress()); | |||
|             errorRow.setErrorInfo(errorMsg); | |||
|             errorRows.add(errorRow); | |||
|         } | |||
|     } | |||
| 
 | |||
|     @Override | |||
|     public void doAfterAllAnalysed(AnalysisContext analysisContext) { | |||
|         log.info("☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆"); | |||
|         // 最后几条达不到阈值,这里必须再调用一次
 | |||
|         execPersist(); | |||
|     } | |||
| 
 | |||
|     /** | |||
|      * 执行持久化 | |||
|      */ | |||
|     private void execPersist() { | |||
|         try { | |||
|             if (CollectionUtils.isNotEmpty(insertDatas)) { | |||
|                 icSocietyOrgService.insertBatch(insertDatas); | |||
|             } | |||
| 
 | |||
|             if (CollectionUtils.isNotEmpty(updateDatas)) { | |||
|                 icSocietyOrgService.updateBatchById(updateDatas); | |||
|             } | |||
|         } finally { | |||
|             insertDatas.clear(); | |||
|             updateDatas.clear(); | |||
|         } | |||
|     } | |||
| 
 | |||
|     /** | |||
|      * 获取错误行 | |||
|      * | |||
|      * @return | |||
|      */ | |||
|     public List<IcSocietyOrgImportExcelDTO.ErrorRow> getErrorRows() { | |||
|         return errorRows; | |||
|     } | |||
| } | |||
| 
 | |||
					Loading…
					
					
				
		Reference in new issue