diff --git a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/controller/IcSocietyOrgController.java b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/controller/IcSocietyOrgController.java index 8c30990fc9..a5123e155a 100644 --- a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/controller/IcSocietyOrgController.java +++ b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/controller/IcSocietyOrgController.java @@ -268,7 +268,6 @@ public class IcSocietyOrgController implements ResultDataResolver { @PostMapping("importV2") public Result importExcelV2(@LoginUser TokenDto tokenDto, @RequestPart("file") MultipartFile file) { String userId = EpmetRequestHolder.getHeader(AppClientConstant.USER_ID); - // 1.暂存文件 String originalFilename = file.getOriginalFilename(); String extName = originalFilename.substring(originalFilename.lastIndexOf(".")); diff --git a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/dao/IcSocietyOrgDao.java b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/dao/IcSocietyOrgDao.java index 0e78731571..5580421547 100644 --- a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/dao/IcSocietyOrgDao.java +++ b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/dao/IcSocietyOrgDao.java @@ -59,4 +59,8 @@ public interface IcSocietyOrgDao extends BaseDao { * @Description 查询当前组织下的社会组织数据 **/ List getByAgencyId(@Param("agencyId") String agencyId); + + List selectListForUniqueName(@Param("agencyId") String agencyId, + @Param("societyName") String societyName, + @Param("id") String id); } diff --git a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/IcSocietyOrgImportExcelDTO.java b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/IcSocietyOrgImportExcelDTO.java index 284f6d024d..bc14e774a0 100644 --- a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/IcSocietyOrgImportExcelDTO.java +++ b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/IcSocietyOrgImportExcelDTO.java @@ -16,22 +16,21 @@ import javax.validation.constraints.NotBlank; public class IcSocietyOrgImportExcelDTO { @ExcelProperty(value = "组织名称") - @NotBlank(message = "不能为空") - @Length(max=50,message = "不能超过50个字") + @NotBlank(message = "组织名称不能为空") + @Length(max=50,message = "组织名称不能超过50个字") private String societyName; @ExcelProperty(value = "服务内容") - @NotBlank(message = "不能为空") - @Length(max=1000,message = "不能超过1000个字") + @NotBlank(message = "服务内容不能为空") + @Length(max=1000,message = "服务内容不能超过1000个字") private String serviceMatters; @ExcelProperty(value = "服务电话") - @NotBlank(message = "不能为空") - @Length(max=11,message = "不能超过11个字") + @NotBlank(message = "服务电话不能为空") private String mobile; @ExcelProperty(value = "服务时间") - @NotBlank(message = "不能为空") + @NotBlank(message = "服务时间不能为空") private String serviceTimeStr; @ExcelProperty(value = "管理员姓名") diff --git a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/handler/IcSocietyOrgExcelImportListener.java b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/handler/IcSocietyOrgExcelImportListener.java new file mode 100644 index 0000000000..54b23d8fd2 --- /dev/null +++ b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/handler/IcSocietyOrgExcelImportListener.java @@ -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 { + // 最大条数阈值 + public static final int MAX_THRESHOLD = 2; + private String currentCustomerId; + /** + * 当前操作用户 + */ + private CustomerStaffInfoCacheResult staffInfo; + private IcSocietyOrgServiceImpl icSocietyOrgService; + private List errorRows = new ArrayList<>(); + /** + * 要插入的数据 + */ + private List insertDatas = new ArrayList<>(); + /** + * 根据组织名称更新的数据 + */ + private List updateDatas = new ArrayList<>(); + private Map staffMap=new HashMap<>(); + public IcSocietyOrgExcelImportListener(String customerId, CustomerStaffInfoCacheResult staffInfo, IcSocietyOrgServiceImpl icSocietyOrgService, Map 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 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 getErrorRows() { + return errorRows; + } +} + diff --git a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/service/impl/IcSocietyOrgServiceImpl.java b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/service/impl/IcSocietyOrgServiceImpl.java index 332db0bae7..be5ccf565b 100644 --- a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/service/impl/IcSocietyOrgServiceImpl.java +++ b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/service/impl/IcSocietyOrgServiceImpl.java @@ -41,6 +41,7 @@ import com.epmet.dto.result.demand.OptionDTO; import com.epmet.entity.IcSocietyOrgEntity; import com.epmet.excel.IcSocietyOrgExcel; import com.epmet.excel.IcSocietyOrgImportExcelDTO; +import com.epmet.excel.handler.IcSocietyOrgExcelImportListener; import com.epmet.feign.EpmetCommonServiceOpenFeignClient; import com.epmet.feign.EpmetUserOpenFeignClient; import com.epmet.feign.GovOrgOpenFeignClient; @@ -164,14 +165,23 @@ public class IcSocietyOrgServiceImpl extends BaseServiceImpl pointMap = icUserDemandRecService.getServicePoint(formDTO.getCustomerId(), UserDemandConstant.SOCIAL_ORG); //2.查询被绑定管理员信息 UserIdsFormDTO dto = new UserIdsFormDTO(); - List staffIdList = result.getList().stream().map(SocietyOrgListResultDTO::getAdminStaffId).collect(Collectors.toList()); - staffIdList = staffIdList.stream().distinct().collect(Collectors.toList()); - dto.setUserIds(staffIdList); - Result> listResult = epmetUserOpenFeignClient.getStaffInfoList(dto); - if (!listResult.success()) { - throw new RenException("获取工作人员基本信息失败......"); + List staffIdList = result.getList().stream().filter(u->StringUtils.isNotBlank(u.getAdminStaffId())).map(SocietyOrgListResultDTO::getAdminStaffId).collect(Collectors.toList()); + if(!CollectionUtils.isEmpty(staffIdList)){ + staffIdList = staffIdList.stream().distinct().collect(Collectors.toList()); + dto.setUserIds(staffIdList); + Result> listResult = epmetUserOpenFeignClient.getStaffInfoList(dto); + if (!listResult.success()) { + throw new RenException("获取工作人员基本信息失败......"); + } + result.getList().stream().filter(f->StringUtils.isNotBlank(f.getAdminStaffId())).forEach(r -> listResult.getData().stream().filter(u -> r.getAdminStaffId().equals(u.getStaffId())).forEach(u -> r.setAdminStaffName(u.getStaffName()))); + /* result.getList().forEach(dto1->{ + listResult.getData().forEach(staff->{ + if(dto1.getAdminStaffId().equals(staff.getStaffId())){ + dto1.setAdminStaffName(staff.getStaffName()); + } + }); + });*/ } - result.getList().forEach(r -> listResult.getData().stream().filter(u -> r.getAdminStaffId().equals(u.getStaffId())).forEach(u -> r.setAdminStaffName(u.getStaffName()))); //3.查询被绑定管理员信息 OrgInfoFormDTO org = new OrgInfoFormDTO(); @@ -340,14 +350,28 @@ public class IcSocietyOrgServiceImpl extends BaseServiceImpl> staffIdsRes=govOrgOpenFeignClient.getAgencyStaffs(agencyIdFormDTO); + if (!staffIdsRes.success() || CollectionUtils.isEmpty(staffIdsRes.getData())) { + String msg = "查询当前组织下工作人员列表异常"; + throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), msg, msg); + } + Result currentAgencyStaffs=epmetUserOpenFeignClient.getCustomerStaffList(staffIdsRes.getData()); + if (!currentAgencyStaffs.success() || null == currentAgencyStaffs.getData() || CollectionUtils.isEmpty(currentAgencyStaffs.getData().getStaffList())) { + String msg = "查询当前组织下工作人员信息异常"; + throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), msg, msg); + } + List staffList=currentAgencyStaffs.getData().getStaffList(); + Map staffMap=staffList.stream().collect(Collectors.toMap(StaffListResultDTO::getStaffId, StaffListResultDTO::getStaffName)); - // todo - /*IcEnterpriseExcelImportListener listener = new IcEnterpriseExcelImportListener(customerId,staffInfo, this); - EasyExcel.read(filePath.toFile(), IcSocietyOrgImportExcelDTO.class, listener).headRowNumber(1).sheet(0).doRead();*/ + IcSocietyOrgExcelImportListener listener = new IcSocietyOrgExcelImportListener(customerId,staffInfo, this,staffMap); + EasyExcel.read(filePath.toFile(), IcSocietyOrgImportExcelDTO.class, listener).headRowNumber(1).sheet(0).doRead(); Path errorDescFile = null; String errorDesFileUrl = null; - List errorRows = null;// todo listener.getErrorRows(); + List errorRows = listener.getErrorRows(); boolean failed = errorRows.size() > 0; if (failed) { @@ -420,4 +444,8 @@ public class IcSocietyOrgServiceImpl extends BaseServiceImpl selectForUniqueName(String agencyId, String societyName, String id) { + return baseDao.selectListForUniqueName(agencyId,societyName,id); + } } diff --git a/epmet-module/epmet-heart/epmet-heart-server/src/main/resources/mapper/IcSocietyOrgDao.xml b/epmet-module/epmet-heart/epmet-heart-server/src/main/resources/mapper/IcSocietyOrgDao.xml index 4939225de6..9fc03ab87d 100644 --- a/epmet-module/epmet-heart/epmet-heart-server/src/main/resources/mapper/IcSocietyOrgDao.xml +++ b/epmet-module/epmet-heart/epmet-heart-server/src/main/resources/mapper/IcSocietyOrgDao.xml @@ -92,4 +92,13 @@ AND agency_id = #{agencyId} + diff --git a/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/CustomerStaffServiceImpl.java b/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/CustomerStaffServiceImpl.java index 3f9abd5e70..ecfcade3fd 100644 --- a/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/CustomerStaffServiceImpl.java +++ b/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/CustomerStaffServiceImpl.java @@ -617,25 +617,23 @@ public class CustomerStaffServiceImpl extends BaseServiceImpl staffInfoList = new ArrayList<>(); userIds.forEach(staffId -> { CustomerStaffInfoCacheResult staffInfo = CustomerStaffRedis.getStaffInfo(formDTO.getCustomerId(), staffId); - if (staffInfo == null) { - log.error("getStaffInfoList fail customerId:{}, staffId:{} not exist in db", formDTO.getCustomerId(), staffId); - return; + if (null != staffInfo) { + StaffSinGridResultDTO resultDTO = new StaffSinGridResultDTO(); + resultDTO.setStaffId(staffId); + resultDTO.setStaffName(staffInfo.getRealName()); + resultDTO.setHeadPhoto(staffInfo.getHeadPhoto()); + resultDTO.setGender(staffInfo.getGender()); + + List roleInfoList = new ArrayList<>(); + staffInfo.getRoleMap().forEach((key, value) -> { + RoleResultDTO dto = new RoleResultDTO(); + dto.setRoleKey(key); + dto.setRoleName(value); + roleInfoList.add(dto); + }); + resultDTO.setRoleList(roleInfoList); + staffInfoList.add(resultDTO); } - StaffSinGridResultDTO resultDTO = new StaffSinGridResultDTO(); - resultDTO.setStaffId(staffId); - resultDTO.setStaffName(staffInfo.getRealName()); - resultDTO.setHeadPhoto(staffInfo.getHeadPhoto()); - resultDTO.setGender(staffInfo.getGender()); - - List roleInfoList = new ArrayList<>(); - staffInfo.getRoleMap().forEach((key, value) -> { - RoleResultDTO dto = new RoleResultDTO(); - dto.setRoleKey(key); - dto.setRoleName(value); - roleInfoList.add(dto); - }); - resultDTO.setRoleList(roleInfoList); - staffInfoList.add(resultDTO); }); /*List staffInfoList = customerStaffDao.getStaffInfoList(userIds);