20 changed files with 755 additions and 136 deletions
			
			
		@ -0,0 +1,42 @@ | 
				
			|||
package com.epmet.excel; | 
				
			|||
 | 
				
			|||
import com.alibaba.excel.annotation.ExcelProperty; | 
				
			|||
import com.alibaba.excel.annotation.write.style.ColumnWidth; | 
				
			|||
import lombok.Data; | 
				
			|||
 | 
				
			|||
/** | 
				
			|||
 * @Description | 
				
			|||
 * @Author yzm | 
				
			|||
 * @Date 2022/8/24 8:41 | 
				
			|||
 */ | 
				
			|||
@Data | 
				
			|||
public class IcSocietyOrgExportExcel { | 
				
			|||
    @ExcelProperty(value = "所属组织") | 
				
			|||
    @ColumnWidth(30) | 
				
			|||
    private String agencyName; | 
				
			|||
 | 
				
			|||
    @ExcelProperty(value = "组织名称") | 
				
			|||
    @ColumnWidth(30) | 
				
			|||
    private String societyName; | 
				
			|||
 | 
				
			|||
    @ColumnWidth(50) | 
				
			|||
    @ExcelProperty(value = "服务内容") | 
				
			|||
    private String serviceMatters; | 
				
			|||
 | 
				
			|||
    @ColumnWidth(20) | 
				
			|||
    @ExcelProperty(value = "服务电话") | 
				
			|||
    private String mobile; | 
				
			|||
 | 
				
			|||
    @ColumnWidth(20) | 
				
			|||
    @ExcelProperty(value = "服务时间") | 
				
			|||
    private String serviceTimeStr; | 
				
			|||
 | 
				
			|||
    @ColumnWidth(20) | 
				
			|||
    @ExcelProperty(value = "管理员姓名") | 
				
			|||
    private String adminStaffName; | 
				
			|||
 | 
				
			|||
    /*@ColumnWidth(30) | 
				
			|||
    @ExcelProperty(value = "地址") | 
				
			|||
    private String address;*/ | 
				
			|||
} | 
				
			|||
 | 
				
			|||
@ -0,0 +1,74 @@ | 
				
			|||
package com.epmet.excel; | 
				
			|||
 | 
				
			|||
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 2022/8/24 10:46 | 
				
			|||
 */ | 
				
			|||
@Data | 
				
			|||
public class IcSocietyOrgImportExcelDTO { | 
				
			|||
 | 
				
			|||
    @ExcelProperty(value = "组织名称") | 
				
			|||
    @NotBlank(message = "组织名称不能为空") | 
				
			|||
    @Length(max=50,message = "组织名称不能超过50个字") | 
				
			|||
    private String societyName; | 
				
			|||
 | 
				
			|||
    @ExcelProperty(value = "服务内容") | 
				
			|||
    @NotBlank(message = "服务内容不能为空") | 
				
			|||
    @Length(max=1000,message = "服务内容不能超过1000个字") | 
				
			|||
    private String serviceMatters; | 
				
			|||
 | 
				
			|||
    @ExcelProperty(value = "服务电话") | 
				
			|||
    @NotBlank(message = "服务电话不能为空") | 
				
			|||
    private String mobile; | 
				
			|||
 | 
				
			|||
    @ExcelProperty(value = "服务时间") | 
				
			|||
    @NotBlank(message = "服务时间不能为空") | 
				
			|||
    private String serviceTimeStr; | 
				
			|||
 | 
				
			|||
    @ExcelProperty(value = "管理员姓名") | 
				
			|||
    private String adminStaffName; | 
				
			|||
 | 
				
			|||
    @ExcelProperty(value = "地址") | 
				
			|||
    private String address; | 
				
			|||
 | 
				
			|||
    @Data | 
				
			|||
    public static class ErrorRow { | 
				
			|||
 | 
				
			|||
        @ColumnWidth(40) | 
				
			|||
        @ExcelProperty(value = "组织名称") | 
				
			|||
        private String societyName; | 
				
			|||
 | 
				
			|||
        @ColumnWidth(50) | 
				
			|||
        @ExcelProperty(value = "服务内容") | 
				
			|||
        private String serviceMatters; | 
				
			|||
 | 
				
			|||
        @ColumnWidth(20) | 
				
			|||
        @ExcelProperty(value = "服务电话") | 
				
			|||
        private String mobile; | 
				
			|||
 | 
				
			|||
        @ColumnWidth(25) | 
				
			|||
        @ExcelProperty(value = "服务时间") | 
				
			|||
        private String serviceTimeStr; | 
				
			|||
 | 
				
			|||
        @ColumnWidth(20) | 
				
			|||
        @ExcelProperty(value = "管理员姓名") | 
				
			|||
        private String adminStaffName; | 
				
			|||
 | 
				
			|||
        @ColumnWidth(50) | 
				
			|||
        @ExcelProperty(value = "地址") | 
				
			|||
        private String address; | 
				
			|||
 | 
				
			|||
        @ColumnWidth(60) | 
				
			|||
        @ExcelProperty("错误信息") | 
				
			|||
        private String errorInfo; | 
				
			|||
    } | 
				
			|||
} | 
				
			|||
 | 
				
			|||
@ -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; | 
				
			|||
    } | 
				
			|||
} | 
				
			|||
 | 
				
			|||
								
									Binary file not shown.
								
							
						
					
					Loading…
					
					
				
		Reference in new issue