cloudroam
2025-01-09 eeb3b46afe7da500bce62daad35ded0886df3b88
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.mzl.flower.service.impl.map;
 
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpUtil;
import com.google.gson.Gson;
import com.mzl.flower.config.TengxunMapProperties;
import com.mzl.flower.dto.map.gaode.GaodeDistrict;
import com.mzl.flower.dto.map.tengxun.TengxunDistrict;
import com.mzl.flower.dto.map.tengxun.TencentMapResponse;
import com.mzl.flower.entity.district.DistrictDO;
import com.mzl.flower.entity.district.DistrictTengxunDO;
import com.mzl.flower.service.BaseService;
import com.mzl.flower.service.district.DistrictTengxunService;
import com.mzl.flower.service.map.MapTengxunService;
import com.mzl.flower.utils.ConverterUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
 
@Slf4j
@Service
public class MapTengxunServiceImpl extends BaseService implements MapTengxunService {
 
    @Autowired
    private TengxunMapProperties tengxunMapProperties;
 
    @Autowired
    private DistrictTengxunService districtTengxunService;
 
    @Override
    public void getAdministrativeDivision() {
        String administrativeDivisionsUrl = tengxunMapProperties.getAdministrativeDivisionsUrl();
        //可以单独传入http参数,这样参数会自动做URL编码,拼接在URL中
        HashMap<String, Object> paramMap = new HashMap<>();
 
        String result= HttpUtil.get(administrativeDivisionsUrl, paramMap);
        System.out.println(result);
        // 使用 Gson 解析 JSON 字符串
        Gson gson = new Gson();
        TencentMapResponse response = gson.fromJson(result, TencentMapResponse.class);
 
        if(response.getStatus()==0){
 
 
            List<TengxunDistrict> allDistricts = response.getResult();
            // 将腾讯地图返回的数据格式转换成标准的三层结构
//            List<TengxunDistrict> formatterDistrict =normalizeTreeToThreeLevels(allDistricts);
//
//            List<TengxunDistrict> allTengxunDistricts = new ArrayList<>();
//            // 递归遍历所有的节点,然后把所有节点加入到 allDistricts 列表中
//            for (TengxunDistrict tengxunDistrict : formatterDistrict){
//                addDistrictToList(tengxunDistrict, null, allTengxunDistricts);  // 从根节点开始,父节点 ID 为 null
//            }
 
//            List<TengxunDistrict> formatterDistrict =normalizeTreeToThreeLevels(allDistricts);
 
            List<TengxunDistrict> allTengxunDistricts = new ArrayList<>();
            // 递归遍历所有的节点,然后把所有节点加入到 allDistricts 列表中
            for (TengxunDistrict tengxunDistrict : allDistricts){
                addDistrictToList(tengxunDistrict, null, allTengxunDistricts);  // 从根节点开始,父节点 ID 为 null
            }
 
            List<DistrictTengxunDO> districtTengxunDOList= ConverterUtil.transList(allTengxunDistricts, DistrictTengxunDO.class);
 
            districtTengxunService.saveRemoteDistricts(districtTengxunDOList);
 
 
//            for (TengxunDistrict tengxunDistrict : allTengxunDistricts) {
//                printDistrictInfo(tengxunDistrict);
//            }
 
        }
 
    }
 
    public static List<TengxunDistrict> normalizeTreeToThreeLevels(List<TengxunDistrict> tree) {
        List<TengxunDistrict> result = new ArrayList<>();
        for (TengxunDistrict node : tree) {
            int depth = getTreeDepth(node);
            if(depth<3){
                List<TengxunDistrict> childrenList=new ArrayList<>();
                childrenList.add(node);
 
                TengxunDistrict tengxunDistrict= new TengxunDistrict();
                BeanUtils.copyProperties(node, tengxunDistrict);
                tengxunDistrict.setDistricts(childrenList);
                // 虚拟的id、code
                tengxunDistrict.setId(tengxunDistrict.getId()+"virtual");
                // 虚拟的层级
                tengxunDistrict.setLevel(-1);
                result.add(tengxunDistrict);
                continue;
            }else{
                result.add(node);
            }
 
        }
        return result;
    }
 
    /**
     * 遍历树的深度
     * @param node
     * @return
     */
    private static int getTreeDepth(TengxunDistrict node) {
        if (node == null || node.getDistricts() == null || node.getDistricts().isEmpty()) {
            return 1;
        }
        int maxDepth = 0;
        for (TengxunDistrict child : node.getDistricts()) {
            maxDepth = Math.max(maxDepth, getTreeDepth(child));
        }
        return maxDepth + 1;
    }
 
    // 打印区划信息的辅助方法
    public static void printDistrictInfo(TengxunDistrict tengxunDistrict) {
        System.out.println("ID: " + tengxunDistrict.getId() + ", 父节点ID: " + tengxunDistrict.getParentId());
        System.out.println("名称: " + tengxunDistrict.getName());
        System.out.println("全称: " + tengxunDistrict.getFullname());
        System.out.println("级别: " + tengxunDistrict.getLevel());
        System.out.println("拼音: " + tengxunDistrict.getPinyin());
        System.out.println("经纬度: " + tengxunDistrict.getTengxunLocation());
        System.out.println("--------------------------");
    }
 
 
    // 递归遍历区划节点,并将它们添加到 allDistricts 列表
    public static void addDistrictToList(TengxunDistrict tengxunDistrict, String parentId, List<TengxunDistrict> allTengxunDistricts) {
        // 将腾讯地图返回的id作为code存放到code中
        tengxunDistrict.setCode(tengxunDistrict.getId());
        // 如果name 是空的话,则将fullname赋值给name
        if(StringUtils.isBlank(tengxunDistrict.getName())){
            tengxunDistrict.setName(tengxunDistrict.getFullname());
        }
 
//        tengxunDistrict.setId(IdUtil.simpleUUID());
        // 设置父节点 ID
        tengxunDistrict.setParentId(parentId);
 
        // 将当前区划添加到结果列表
        allTengxunDistricts.add(tengxunDistrict);
 
        // 递归遍历子区划
        if (tengxunDistrict.getDistricts()!= null) {
            for (TengxunDistrict child : tengxunDistrict.getDistricts()) {
                addDistrictToList(child, tengxunDistrict.getId(), allTengxunDistricts);  // 将当前节点的 ID 作为子节点的父节点 ID
            }
        }
    }
 
 
}