forked from rongchao/epmet-cloud-rizhao
				
			
				 3 changed files with 358 additions and 2 deletions
			
			
		@ -0,0 +1,316 @@ | 
				
			|||
 | 
				
			|||
package com.epmet.model; | 
				
			|||
 | 
				
			|||
import com.alibaba.excel.context.AnalysisContext; | 
				
			|||
import com.alibaba.excel.event.AnalysisEventListener; | 
				
			|||
import com.epmet.commons.tools.constant.NumConstant; | 
				
			|||
import com.epmet.commons.tools.exception.EpmetException; | 
				
			|||
import com.epmet.commons.tools.redis.common.CustomerOrgRedis; | 
				
			|||
import com.epmet.commons.tools.redis.common.bean.AgencyInfoCache; | 
				
			|||
import com.epmet.commons.tools.redis.common.bean.GridInfoCache; | 
				
			|||
import com.epmet.commons.tools.utils.ConvertUtils; | 
				
			|||
import com.epmet.constant.CustomerGridConstant; | 
				
			|||
import com.epmet.dao.IcBuildingDao; | 
				
			|||
import com.epmet.dto.ImportGeneralDTO; | 
				
			|||
import com.epmet.dto.form.ImportInfoFormDTO; | 
				
			|||
import com.epmet.dto.result.ImportResultDTO; | 
				
			|||
import com.epmet.enums.BuildingTypeEnums; | 
				
			|||
import com.epmet.redis.IcHouseRedis; | 
				
			|||
import com.epmet.service.IcNeighborHoodService; | 
				
			|||
import org.apache.commons.lang3.StringUtils; | 
				
			|||
import org.springframework.util.CollectionUtils; | 
				
			|||
 | 
				
			|||
import java.util.ArrayList; | 
				
			|||
import java.util.Arrays; | 
				
			|||
import java.util.List; | 
				
			|||
import java.util.Map; | 
				
			|||
import java.util.stream.Collectors; | 
				
			|||
 | 
				
			|||
/** | 
				
			|||
 * @Author zxc | 
				
			|||
 * @DateTime 2022/2/15 10:06 上午 | 
				
			|||
 * @DESC | 
				
			|||
 */ | 
				
			|||
public class ImportBuildingInfoListener extends AnalysisEventListener<BuildingInfoModel> { | 
				
			|||
 | 
				
			|||
    List<Integer> nums = new ArrayList<>(); | 
				
			|||
    Integer num = NumConstant.ZERO; | 
				
			|||
 | 
				
			|||
    List<ImportGeneralDTO> needDisposeList = new ArrayList<>(); | 
				
			|||
    List<ImportGeneralDTO> needInsertList = new ArrayList<>(); | 
				
			|||
 | 
				
			|||
    String gridName = null; | 
				
			|||
    String agencyName = null; | 
				
			|||
 | 
				
			|||
    Map<String, Object> gridInfos = null; | 
				
			|||
    List<ImportGeneralDTO> gridInfoDTOs = null; | 
				
			|||
    Map<String, Object> neighborHoodInfos = null; | 
				
			|||
    List<ImportGeneralDTO> neighborHoodInfoDTOs = null; | 
				
			|||
    ImportGeneralDTO neighborHoodGeneralDTO = null; | 
				
			|||
    Map<String, Object> buildingInfos = null; | 
				
			|||
    List<ImportGeneralDTO> buildingInfoDTOs = null; | 
				
			|||
    ImportGeneralDTO buildingGeneralDTO = null; | 
				
			|||
 | 
				
			|||
    private ImportInfoFormDTO formDTO; | 
				
			|||
    private IcHouseRedis icHouseRedis; | 
				
			|||
    private IcBuildingDao icBuildingDao; | 
				
			|||
    private IcNeighborHoodService neighborHoodService; | 
				
			|||
 | 
				
			|||
    public ImportBuildingInfoListener(ImportInfoFormDTO formDTO, IcHouseRedis icHouseRedis,IcBuildingDao icBuildingDao,IcNeighborHoodService neighborHoodService){ | 
				
			|||
        this.formDTO = formDTO; | 
				
			|||
        this.icHouseRedis = icHouseRedis; | 
				
			|||
        this.icBuildingDao = icBuildingDao; | 
				
			|||
        this.neighborHoodService = neighborHoodService; | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    @Override | 
				
			|||
    public void invoke(BuildingInfoModel data, AnalysisContext context) { | 
				
			|||
        if (null == data){ | 
				
			|||
            return; | 
				
			|||
        } | 
				
			|||
        num = num + NumConstant.ONE; | 
				
			|||
        ImportGeneralDTO dto = ConvertUtils.sourceToTarget(data, ImportGeneralDTO.class); | 
				
			|||
        dto.setNum(num); | 
				
			|||
        dto.setCustomerId(formDTO.getCustomerId()); | 
				
			|||
        dto.setType(BuildingTypeEnums.getKeyByValue(dto.getType())); | 
				
			|||
        if (formDTO.getOrgType().equals(CustomerGridConstant.NEIGHBORHOOD)){ | 
				
			|||
            disposeNeighborHoodBuilding(dto); | 
				
			|||
        }else if (formDTO.getOrgType().equals(CustomerGridConstant.GRID)){ | 
				
			|||
            disposeGridBuilding(dto); | 
				
			|||
        }else if (formDTO.getOrgType().equals(CustomerGridConstant.AGENCY)){ | 
				
			|||
            disposeAgencyBuilding(dto); | 
				
			|||
        } | 
				
			|||
        // 数据筛选完毕,当num每满100条,处理一次
 | 
				
			|||
        if (num%NumConstant.ONE_HUNDRED == NumConstant.ZERO){ | 
				
			|||
            finalDispose(); | 
				
			|||
        } | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    public void finalDispose(){ | 
				
			|||
        Map<String, Long> groupByAllName = needDisposeList.stream().collect(Collectors.groupingBy( | 
				
			|||
                n -> n.getAgencyName() + "_" + n.getGridName() + "_" + | 
				
			|||
                        n.getNeighborHoodName() + "_" + n.getBuildingName(), Collectors.counting())); | 
				
			|||
        groupByAllName.forEach((k,v) -> { | 
				
			|||
            if (Integer.valueOf(v.toString()).compareTo(1) > 0){ | 
				
			|||
                for (ImportGeneralDTO r : needDisposeList) { | 
				
			|||
                    if (k.equals(r.getAgencyName() + "_" + r.getGridName() + "_" + | 
				
			|||
                            r.getNeighborHoodName() + "_" + r.getBuildingName())){ | 
				
			|||
                        // 集合里重复的
 | 
				
			|||
                        r.setExistStatus(true); | 
				
			|||
                        nums.add(r.getNum()); | 
				
			|||
                    } | 
				
			|||
                } | 
				
			|||
            } | 
				
			|||
        }); | 
				
			|||
        Map<Boolean, List<ImportGeneralDTO>> groupByStatus = needDisposeList.stream().collect(Collectors.groupingBy(ImportGeneralDTO::getExistStatus)); | 
				
			|||
        List<ImportGeneralDTO> importGeneralDTOS = groupByStatus.get(false); | 
				
			|||
        if (!CollectionUtils.isEmpty(importGeneralDTOS)){ | 
				
			|||
            List<ImportGeneralDTO> importInfo = neighborHoodService.getImportInfo(formDTO, importGeneralDTOS); | 
				
			|||
            Map<Boolean, List<ImportGeneralDTO>> groupByBuildingExistStatus = importInfo.stream().collect(Collectors.groupingBy(ImportGeneralDTO::getBuildingExistStatus)); | 
				
			|||
            List<ImportGeneralDTO> existList = groupByBuildingExistStatus.get(true); | 
				
			|||
            if (!CollectionUtils.isEmpty(existList)){ | 
				
			|||
                existList.forEach(e -> { | 
				
			|||
                    nums.add(e.getNum()); | 
				
			|||
                }); | 
				
			|||
            } | 
				
			|||
        } | 
				
			|||
        // 清除
 | 
				
			|||
        needDisposeList = new ArrayList<>(); | 
				
			|||
        needInsertList = new ArrayList<>(); | 
				
			|||
        gridName = null; | 
				
			|||
        agencyName = null; | 
				
			|||
        gridInfos = null; | 
				
			|||
        gridInfoDTOs = null; | 
				
			|||
        neighborHoodInfos = null; | 
				
			|||
        neighborHoodInfoDTOs = null; | 
				
			|||
        neighborHoodGeneralDTO = null; | 
				
			|||
        buildingInfos = null; | 
				
			|||
        buildingInfoDTOs = null; | 
				
			|||
        buildingGeneralDTO = null; | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    public void fillData(ImportGeneralDTO dto, String orgType){ | 
				
			|||
        if (orgType.equals(CustomerGridConstant.GRID) || orgType.equals(CustomerGridConstant.AGENCY)){ | 
				
			|||
            List<String> gridIds = new ArrayList<>(); | 
				
			|||
            if (orgType.equals(CustomerGridConstant.GRID)){ | 
				
			|||
                gridIds = Arrays.asList(formDTO.getOrgId()); | 
				
			|||
            }else if (orgType.equals(CustomerGridConstant.AGENCY)){ | 
				
			|||
                gridIds = gridInfoDTOs.stream().map(m -> m.getGridId()).collect(Collectors.toList()); | 
				
			|||
            } | 
				
			|||
            neighborHoodInfos = null == neighborHoodInfos ? getNeighborHoodInfos(gridIds) : neighborHoodInfos; | 
				
			|||
            Object cacheNeighBorHood = icHouseRedis.getTemporaryCacheNeighBorHood(formDTO.getCustomerId(), formDTO.getUserId(), formDTO.getOrgId() + "_" + dto.getNeighborHoodName()); | 
				
			|||
            // 赋值小区ID
 | 
				
			|||
            dto.setNeighborHoodId(null == cacheNeighBorHood ? "" : cacheNeighBorHood.toString()); | 
				
			|||
        } | 
				
			|||
        if (StringUtils.isNotBlank(dto.getNeighborHoodId())){ | 
				
			|||
            //小区ID不为空赋值楼栋ID
 | 
				
			|||
            buildingInfos = null == buildingInfos ? getBuildingInfos(dto.getNeighborHoodId()) : buildingInfos; | 
				
			|||
            Object cacheBuilding = icHouseRedis.getTemporaryCacheBuilding(formDTO.getCustomerId(), formDTO.getUserId(), dto.getNeighborHoodId() + "_" + dto.getBuildingName()); | 
				
			|||
            dto.setBuildingId(null == cacheBuilding ? "" : cacheBuilding.toString()); | 
				
			|||
            if (StringUtils.isNotBlank(dto.getBuildingId())){ | 
				
			|||
                // 说明数据库已存在此楼栋,不需添加
 | 
				
			|||
                nums.add(num); | 
				
			|||
            }else { | 
				
			|||
                needDisposeList.add(dto); | 
				
			|||
            } | 
				
			|||
        }else { | 
				
			|||
            needDisposeList.add(dto); | 
				
			|||
        } | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * @Description  左侧树点击小区时调用 | 
				
			|||
     * @param dto | 
				
			|||
     * @author zxc | 
				
			|||
     * @date 2022/2/15 10:41 上午 | 
				
			|||
     */ | 
				
			|||
    public void disposeNeighborHoodBuilding(ImportGeneralDTO dto){ | 
				
			|||
        neighborHoodGeneralDTO = null == neighborHoodGeneralDTO ? icBuildingDao.selectNeighborHoodById(formDTO.getOrgId()) : neighborHoodGeneralDTO; | 
				
			|||
        //排除不是本小区的
 | 
				
			|||
        if (!dto.getNeighborHoodName().equals(neighborHoodGeneralDTO.getNeighborHoodName())){ | 
				
			|||
            nums.add(num); | 
				
			|||
        }else { | 
				
			|||
            GridInfoCache gridInfo = CustomerOrgRedis.getGridInfo(neighborHoodGeneralDTO.getGridId()); | 
				
			|||
            if (null == gridInfo){ | 
				
			|||
                throw new EpmetException("查询网格失败..."); | 
				
			|||
            } | 
				
			|||
            // 1.排除网格名不一样但小区名一样 2.排除组织不一样,网格一样,小区一样
 | 
				
			|||
            if ((!gridInfo.getGridName().equals(dto.getGridName()) && neighborHoodGeneralDTO.getNeighborHoodName().equals(dto.getNeighborHoodName())) || | 
				
			|||
                    (!gridInfo.getAgencyName().equals(dto.getAgencyName()) && gridInfo.getGridName().equals(dto.getGridName()) && neighborHoodGeneralDTO.getNeighborHoodName().equals(dto.getNeighborHoodName()))){ | 
				
			|||
                nums.add(num); | 
				
			|||
                return; | 
				
			|||
            } | 
				
			|||
            dto.setNeighborHoodId(formDTO.getOrgId()); | 
				
			|||
            dto.setGridId(neighborHoodGeneralDTO.getGridId()); | 
				
			|||
            dto.setAgencyId(neighborHoodGeneralDTO.getAgencyId()); | 
				
			|||
            AgencyInfoCache agencyInfo = CustomerOrgRedis.getAgencyInfo(dto.getAgencyId()); | 
				
			|||
            if (null == agencyInfo){ | 
				
			|||
                throw new EpmetException("查询组织信息失败..."); | 
				
			|||
            } | 
				
			|||
            dto.setPid(agencyInfo.getPid()); | 
				
			|||
            dto.setPids(agencyInfo.getPids()); | 
				
			|||
            // 填充各种ID
 | 
				
			|||
            fillData(dto,CustomerGridConstant.NEIGHBORHOOD); | 
				
			|||
        } | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * @Description  左侧树点击网格时调用 | 
				
			|||
     * @param dto | 
				
			|||
     * @author zxc | 
				
			|||
     * @date 2022/2/15 10:41 上午 | 
				
			|||
     */ | 
				
			|||
    public void disposeGridBuilding(ImportGeneralDTO dto){ | 
				
			|||
        gridName = null == gridName ? icBuildingDao.selectGridNameById(formDTO.getOrgId()) : gridName; | 
				
			|||
        GridInfoCache gridInfo = CustomerOrgRedis.getGridInfo(formDTO.getOrgId()); | 
				
			|||
        if (null == gridInfo){ | 
				
			|||
            throw new EpmetException("查询网格失败..."); | 
				
			|||
        } | 
				
			|||
        //排除不是本网格的 gridName不一样排除,gridName一样但是agencyName不一样也得排除
 | 
				
			|||
        if (!dto.getGridName().equals(gridName) || (!dto.getAgencyName().equals(gridInfo.getAgencyName()) && dto.getGridName().equals(gridName))){ | 
				
			|||
            nums.add(num); | 
				
			|||
        }else { | 
				
			|||
            //
 | 
				
			|||
            dto.setGridId(formDTO.getOrgId()); | 
				
			|||
            dto.setAgencyId(gridInfo.getPid()); | 
				
			|||
            AgencyInfoCache agencyInfo = CustomerOrgRedis.getAgencyInfo(dto.getAgencyId()); | 
				
			|||
            if (null == agencyInfo){ | 
				
			|||
                throw new EpmetException("查询组织信息失败..."); | 
				
			|||
            } | 
				
			|||
            dto.setPid(agencyInfo.getPid()); | 
				
			|||
            dto.setPids(agencyInfo.getPids()); | 
				
			|||
            // 填充各种ID
 | 
				
			|||
            fillData(dto, CustomerGridConstant.GRID); | 
				
			|||
        } | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * @Description  左侧树点击组织时调用 | 
				
			|||
     * @param dto | 
				
			|||
     * @author zxc | 
				
			|||
     * @date 2022/2/15 10:41 上午 | 
				
			|||
     */ | 
				
			|||
    public void disposeAgencyBuilding(ImportGeneralDTO dto){ | 
				
			|||
        agencyName = null == agencyName ? icBuildingDao.selectAgencyNameById(formDTO.getOrgId()) : agencyName; | 
				
			|||
        //排除不是本组织的
 | 
				
			|||
        if (!dto.getAgencyName().equals(agencyName)){ | 
				
			|||
            nums.add(num); | 
				
			|||
        }else { | 
				
			|||
            // 根据组织查询出所有网格,甩出不是本组织下的网格
 | 
				
			|||
            gridInfos = null == gridInfos ? getGridInfos(formDTO.getOrgId()) : gridInfos; | 
				
			|||
            if (null == gridInfos){ | 
				
			|||
                // 组织下确实不存在网格
 | 
				
			|||
                nums.add(num); | 
				
			|||
                return; | 
				
			|||
            } | 
				
			|||
            // 根据网格名对比,没有找到的就把行号加入到未执行成功队列中
 | 
				
			|||
            Object cacheGridName = icHouseRedis.getTemporaryCacheGrid(formDTO.getCustomerId(), formDTO.getUserId(), dto.getGridName()); | 
				
			|||
            if (null == cacheGridName){ | 
				
			|||
                nums.add(num); | 
				
			|||
                return; | 
				
			|||
            } | 
				
			|||
            dto.setGridId(cacheGridName.toString()); | 
				
			|||
            dto.setAgencyId(formDTO.getOrgId()); | 
				
			|||
            AgencyInfoCache agencyInfo = CustomerOrgRedis.getAgencyInfo(dto.getAgencyId()); | 
				
			|||
            if (null == agencyInfo){ | 
				
			|||
                throw new EpmetException("查询组织信息失败..."); | 
				
			|||
            } | 
				
			|||
            dto.setPid(agencyInfo.getPid()); | 
				
			|||
            dto.setPids(agencyInfo.getPids()); | 
				
			|||
            // 填充各种ID
 | 
				
			|||
            fillData(dto,CustomerGridConstant.AGENCY); | 
				
			|||
        } | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * @Description  获取网格信息 | 
				
			|||
     * @param orgId | 
				
			|||
     * @author zxc | 
				
			|||
     * @date 2022/2/14 9:57 上午 | 
				
			|||
     */ | 
				
			|||
    public Map<String,Object> getGridInfos(String orgId){ | 
				
			|||
        gridInfoDTOs = icBuildingDao.selectAllGridByOrgId(orgId); | 
				
			|||
        gridInfos = gridInfoDTOs.stream().collect(Collectors.toMap(n -> n.getGridName(), n -> n.getGridId())); | 
				
			|||
        icHouseRedis.setTemporaryCacheGrid(formDTO.getCustomerId(), gridInfos, formDTO.getUserId()); | 
				
			|||
        return gridInfos; | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * @Description  获取网格下的小区 | 
				
			|||
     * @param gridIds | 
				
			|||
     * @author zxc | 
				
			|||
     * @date 2022/2/14 10:16 上午 | 
				
			|||
     */ | 
				
			|||
    public Map<String,Object> getNeighborHoodInfos(List<String> gridIds){ | 
				
			|||
        neighborHoodInfoDTOs = icBuildingDao.selectAllNeighborHoodByGridIds(gridIds); | 
				
			|||
        neighborHoodInfos = neighborHoodInfoDTOs.stream().collect(Collectors.toMap(n -> n.getGridId() + "_" + n.getNeighborHoodName(), n -> n.getNeighborHoodId())); | 
				
			|||
        icHouseRedis.setTemporaryCacheNeighBorHood(formDTO.getCustomerId(), neighborHoodInfos, formDTO.getUserId()); | 
				
			|||
        return neighborHoodInfos; | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * @Description  获取小区下的楼栋 | 
				
			|||
     * @param neighborHoodId | 
				
			|||
     * @author zxc | 
				
			|||
     * @date 2022/2/14 1:32 下午 | 
				
			|||
     */ | 
				
			|||
    public Map<String,Object> getBuildingInfos(String neighborHoodId){ | 
				
			|||
        buildingInfoDTOs = icBuildingDao.selectAllBuildingByNeighborHoodId(neighborHoodId); | 
				
			|||
        buildingInfos = buildingInfoDTOs.stream().collect(Collectors.toMap(n -> n.getNeighborHoodId() + "_" + n.getBuildingName(), n -> n.getBuildingId())); | 
				
			|||
        icHouseRedis.setTemporaryCacheBuilding(formDTO.getCustomerId(), buildingInfos, formDTO.getUserId()); | 
				
			|||
        return buildingInfos; | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    @Override | 
				
			|||
    public void doAfterAllAnalysed(AnalysisContext context) { | 
				
			|||
        finalDispose(); | 
				
			|||
        // 删除缓存
 | 
				
			|||
        icHouseRedis.delTemporaryCacheGrids(formDTO.getCustomerId(), formDTO.getUserId()); | 
				
			|||
        icHouseRedis.delTemporaryCacheNeighBorHood(formDTO.getCustomerId(), formDTO.getUserId()); | 
				
			|||
        icHouseRedis.delTemporaryCacheBuilding(formDTO.getCustomerId(), formDTO.getUserId()); | 
				
			|||
        icHouseRedis.delTemporaryCacheBuildingUnit(formDTO.getCustomerId(), formDTO.getUserId()); | 
				
			|||
        icHouseRedis.delTemporaryCacheHouses(formDTO.getCustomerId(), formDTO.getUserId()); | 
				
			|||
        // 放结果
 | 
				
			|||
        icHouseRedis.setImportResultDTO(formDTO.getCustomerId(), formDTO.getUserId(), new ImportResultDTO(nums,num)); | 
				
			|||
    } | 
				
			|||
} | 
				
			|||
					Loading…
					
					
				
		Reference in new issue