package com.cm.population.utils; import com.cm.common.plugin.pojo.dtos.dataarea.DataAreaDTO; import com.cm.population.pojo.dtos.areatree.AreaZtreeDTO; import com.cm.population.service.populationinfo.IPopulationInfoService; import com.cm.population.service.populationinfo.impl.PopulationInfoServiceImpl; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentSkipListMap; @Component public class AreaUtils { @Autowired private IPopulationInfoService PopulationInfoService; private static IPopulationInfoService PopulationInfoStaticService; private static final Map> aree = new ConcurrentSkipListMap>(); private static final Map area0 = new ConcurrentSkipListMap();//省 private static final Map area1 = new ConcurrentSkipListMap();//市 private static final Map area2 = new ConcurrentSkipListMap();//线 private static final Map area3 = new ConcurrentSkipListMap();//街道 private static final Map area4 = new ConcurrentSkipListMap();//社区 @PostConstruct public void init() { PopulationInfoStaticService = PopulationInfoService; initArea0(); initArea1(); initArea2(); initArea3(); initArea4(); aree.put(0, area0); aree.put(1, area1); aree.put(2, area2); aree.put(3, area3); aree.put(4, area4); } public List getList(String id) { id = "0".equals(id) ? "" : id; List list = new ArrayList(); Integer level = -1; Integer subCount = 0; // 确定级别 if (!StringUtils.isEmpty(id) ) { AreaZtreeDTO areaDTO = PopulationInfoStaticService.getAreaByAreaId(id); level = Integer.valueOf(areaDTO.getAreaLevel()); subCount = areaDTO.getSubCount(); } Map dataMap = aree.get(level + 1); if (dataMap != null) { // 找出所有的子类 for(Map.Entry m : dataMap.entrySet()) { AreaZtreeDTO ztreeDTO = m.getValue(); String pId = ztreeDTO.getpId(); String title = ztreeDTO.getTitle(); if(!StringUtils.isEmpty(id) && pId.equals(id)) { if (StringUtils.isEmpty(title)) { ArrayList names = new ArrayList<>(); getParent(level, names, id); String newName = StringUtils.join(names, "/") + "/" + ztreeDTO.getName(); ztreeDTO.setTitle(newName); } list.add(ztreeDTO); if (list.size() == subCount) { break; } } if(StringUtils.isEmpty(id)) { ztreeDTO.setTitle(ztreeDTO.getName()); list.add(ztreeDTO); } } } return list; } // 递归拼接全名称 public void getParent(Integer level, ArrayList names, String pId) { Map dataMap = aree.get(level); if (level > -1 && dataMap != null) { level--; for(Map.Entry m : dataMap.entrySet()) { String id = m.getValue().getId(); if(pId.equals(id)) { names.add(0, m.getValue().getName()); getParent(level, names, m.getValue().getpId()); break; } } } } private static void initArea0() { List dto = PopulationInfoStaticService.getAreaListByLevel("0", -1); for(AreaZtreeDTO areaDTO : dto) { if (areaDTO.getSubCount() > 0 ) { areaDTO.setIsParent(true); } area0.put(areaDTO.getId(), areaDTO); } } private static void initArea1() { List dto = PopulationInfoStaticService.getAreaListByLevel("1", -1); for(AreaZtreeDTO areaDTO : dto) { if (areaDTO.getSubCount() > 0 ) { areaDTO.setIsParent(true); } area1.put(areaDTO.getId(), areaDTO); } } private static void initArea2() { List dto = PopulationInfoStaticService.getAreaListByLevel("2", -1); for(AreaZtreeDTO areaDTO : dto) { if (areaDTO.getSubCount() > 0 ) { areaDTO.setIsParent(true); } area2.put(areaDTO.getId(), areaDTO); } } private static void initArea3() { for(int i = 0 ; i < 9 ; i++ ) { List dto = PopulationInfoStaticService.getAreaListByLevel("3", i); for(AreaZtreeDTO areaDTO : dto) { if (areaDTO.getSubCount() > 0 ) { areaDTO.setIsParent(true); } area3.put(areaDTO.getId(), areaDTO); } } } private static void initArea4() { for(int i = 0 ; i < 67 ; i++ ) { List dto = PopulationInfoStaticService.getAreaListByLevel("4", i); for(AreaZtreeDTO areaDTO : dto) { if (areaDTO.getSubCount() > 0 ) { areaDTO.setIsParent(true); } area4.put(areaDTO.getId(), areaDTO); } } } }