btxtgxq-system-population/src/main/java/com/cm/population/utils/AreaUtils.java
2024-01-08 15:41:58 +08:00

161 lines
6.0 KiB
Java

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<Integer, Map<String, AreaZtreeDTO>> aree = new ConcurrentSkipListMap<Integer, Map<String, AreaZtreeDTO>>();
private static final Map<String, AreaZtreeDTO> area0 = new ConcurrentSkipListMap<String, AreaZtreeDTO>();//省
private static final Map<String, AreaZtreeDTO> area1 = new ConcurrentSkipListMap<String, AreaZtreeDTO>();//市
private static final Map<String, AreaZtreeDTO> area2 = new ConcurrentSkipListMap<String, AreaZtreeDTO>();//线
private static final Map<String, AreaZtreeDTO> area3 = new ConcurrentSkipListMap<String, AreaZtreeDTO>();//街道
private static final Map<String, AreaZtreeDTO> area4 = new ConcurrentSkipListMap<String, AreaZtreeDTO>();//社区
@PostConstruct
public void init() {
new Thread(() -> {
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);
}).start();
}
public List<AreaZtreeDTO> getList(String id) {
id = "0".equals(id) ? "" : id;
List<AreaZtreeDTO> list = new ArrayList<AreaZtreeDTO>();
Integer level = -1;
Integer subCount = 0;
// 确定级别
if (!StringUtils.isEmpty(id)) {
AreaZtreeDTO areaDTO = PopulationInfoStaticService.getAreaByAreaId(id);
level = Integer.valueOf(areaDTO.getAreaLevel());
subCount = areaDTO.getSubCount();
}
Map<String, AreaZtreeDTO> dataMap = aree.get(level + 1);
if (dataMap != null) {
// 找出所有的子类
for (Map.Entry<String, AreaZtreeDTO> 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<String> 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<String> names, String pId) {
Map<String, AreaZtreeDTO> dataMap = aree.get(level);
if (level > -1 && dataMap != null) {
level--;
for (Map.Entry<String, AreaZtreeDTO> 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<AreaZtreeDTO> 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<AreaZtreeDTO> 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<AreaZtreeDTO> 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<AreaZtreeDTO> 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<AreaZtreeDTO> dto = PopulationInfoStaticService.getAreaListByLevel("4", i);
for (AreaZtreeDTO areaDTO : dto) {
if (areaDTO.getSubCount() > 0) {
areaDTO.setIsParent(true);
}
area4.put(areaDTO.getId(), areaDTO);
}
}
}
}