From b9903ead016b8b1aa68eb04b48fca3b53fdab0d3 Mon Sep 17 00:00:00 2001
From: cloudroam <cloudroam>
Date: 星期一, 30 十二月 2024 10:42:43 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master-v4' into master-v4
---
src/main/java/com/mzl/flower/service/impl/map/MapTengxunServiceImpl.java | 152 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 152 insertions(+), 0 deletions(-)
diff --git a/src/main/java/com/mzl/flower/service/impl/map/MapTengxunServiceImpl.java b/src/main/java/com/mzl/flower/service/impl/map/MapTengxunServiceImpl.java
new file mode 100644
index 0000000..fc0b4f5
--- /dev/null
+++ b/src/main/java/com/mzl/flower/service/impl/map/MapTengxunServiceImpl.java
@@ -0,0 +1,152 @@
+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<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
+ }
+ }
+ }
+
+
+}
--
Gitblit v1.9.3