From 7473859cd3ee464fde053435f7c2da5aab71aa9b Mon Sep 17 00:00:00 2001
From: rain <1679827795@qq.com>
Date: Fri, 23 Jan 2026 10:02:14 +0800
Subject: [PATCH] 巡查任务报告及工单事件详情根据经纬度返回详细地址

---
 drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/service/impl/GdClueEventServiceImpl.java |    5 +
 drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/utils/GdPatrolReportWordUtil.java        |   66 ++++++++++++++++++++-
 drone-service/drone-gd/src/main/java/org/sxkj/gd/utils/GdGeoAddressUtil.java                        |   68 ++++++++++++++++++++++
 3 files changed, 135 insertions(+), 4 deletions(-)

diff --git a/drone-service/drone-gd/src/main/java/org/sxkj/gd/utils/GdGeoAddressUtil.java b/drone-service/drone-gd/src/main/java/org/sxkj/gd/utils/GdGeoAddressUtil.java
new file mode 100644
index 0000000..c1a10b1
--- /dev/null
+++ b/drone-service/drone-gd/src/main/java/org/sxkj/gd/utils/GdGeoAddressUtil.java
@@ -0,0 +1,68 @@
+/*
+ *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are met:
+ *
+ *  Redistributions of source code must retain the above copyright notice,
+ *  this list of conditions and the following disclaimer.
+ *  Redistributions in binary form must reproduce the above copyright
+ *  notice, this list of conditions and the following disclaimer in the
+ *  documentation and/or other materials provided with the distribution.
+ *  Neither the name of the dreamlu.net developer nor the names of its
+ *  contributors may be used to endorse or promote products derived from
+ *  this software without specific prior written permission.
+ *  Author: Chill 庄骞 (smallchill@163.com)
+ */
+package org.sxkj.gd.utils;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springblade.core.tool.utils.StringUtil;
+import org.springframework.web.client.RestTemplate;
+
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import org.springframework.web.util.UriUtils;
+
+public class GdGeoAddressUtil {
+	private static final Logger logger = LoggerFactory.getLogger(GdGeoAddressUtil.class);
+	private static final String TIAN_DI_TU_KEY = "9171789c872b8cfee7bba45c35a6b955";
+	private static final String TIAN_DI_TU_URL = "https://api.tianditu.gov.cn/geocoder";
+	private static final RestTemplate REST_TEMPLATE = new RestTemplate();
+	private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+
+	public static String getFormattedAddress(Double longitude, Double latitude) {
+		if (longitude == null || latitude == null) {
+			return null;
+		}
+		try {
+			String postStr = String.format("{'lon':%s,'lat':%s,'ver':1}", longitude, latitude);
+			String address = requestAddress(postStr);
+			return StringUtil.isBlank(address) ? null : address;
+		} catch (Exception e) {
+			logger.warn("天地图逆地理编码调用失败", e);
+			return null;
+		}
+	}
+
+	private static String requestAddress(String postStr) {
+		String encodedPostStr = UriUtils.encodeQueryParam(postStr, StandardCharsets.UTF_8);
+		String url = TIAN_DI_TU_URL + "?postStr=" + encodedPostStr + "&type=geocode&tk=" + TIAN_DI_TU_KEY;
+		URI uri = URI.create(url);
+		String response = REST_TEMPLATE.getForObject(uri, String.class);
+		if (StringUtil.isBlank(response)) {
+			return null;
+		}
+		try {
+			JsonNode root = OBJECT_MAPPER.readTree(response);
+			String address = root.path("result").path("formatted_address").asText();
+			return StringUtil.isBlank(address) ? null : address;
+		} catch (Exception e) {
+			logger.warn("解析天地图逆地理编码结果失败", e);
+			return null;
+		}
+	}
+}
diff --git a/drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/service/impl/GdClueEventServiceImpl.java b/drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/service/impl/GdClueEventServiceImpl.java
index f35b810..dbf21b8 100644
--- a/drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/service/impl/GdClueEventServiceImpl.java
+++ b/drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/service/impl/GdClueEventServiceImpl.java
@@ -32,6 +32,7 @@
 import org.sxkj.gd.workorder.vo.GdClueEventCountVO;
 import org.sxkj.gd.workorder.vo.GdClueEventListVO;
 import org.sxkj.gd.workorder.vo.GdClueEventVO;
+import org.sxkj.gd.utils.GdGeoAddressUtil;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springblade.core.mp.base.BaseServiceImpl;
@@ -79,6 +80,10 @@
 		if (detail == null) {
 			throw new RuntimeException("事件不存在");
 		}
+		String address = GdGeoAddressUtil.getFormattedAddress(detail.getLongitude(), detail.getLatitude());
+		if (StringUtil.isNotBlank(address)) {
+			detail.setEventLocation(address);
+		}
 		return detail;
 	}
 
diff --git a/drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/utils/GdPatrolReportWordUtil.java b/drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/utils/GdPatrolReportWordUtil.java
index 62aecde..e42b7b2 100644
--- a/drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/utils/GdPatrolReportWordUtil.java
+++ b/drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/utils/GdPatrolReportWordUtil.java
@@ -38,6 +38,7 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springblade.core.tool.utils.StringUtil;
+import org.sxkj.gd.utils.GdGeoAddressUtil;
 import org.sxkj.gd.workorder.entity.GdPatrolTaskEntity;
 import org.sxkj.gd.workorder.entity.GdTaskResultEntity;
 
@@ -101,9 +102,13 @@
             CompletableFuture<Map<String, byte[]>> qrCodeFuture = CompletableFuture.supplyAsync(
                 () -> preloadQrCodes(results), RESOURCE_EXECUTOR
             );
-            CompletableFuture.allOf(imageFuture, qrCodeFuture).join();
+            CompletableFuture<Map<String, String>> addressFuture = CompletableFuture.supplyAsync(
+                () -> preloadAddresses(results), RESOURCE_EXECUTOR
+            );
+            CompletableFuture.allOf(imageFuture, qrCodeFuture, addressFuture).join();
             Map<String, byte[]> imageCache = imageFuture.get();
             Map<String, byte[]> qrCodeCache = qrCodeFuture.get();
+            Map<String, String> addressCache = addressFuture.get();
 
             createTitle(document, title);
             createSectionHeader(document, "一、任务详情");
@@ -111,7 +116,7 @@
 
             int resultCount = results.size();
             createSectionHeader(document, "二、巡查结果(" + resultCount + "件)");
-            addResultDetails(document, results, imageCache, qrCodeCache);
+            addResultDetails(document, results, imageCache, qrCodeCache, addressCache);
 
             document.write(fos);
             return reportFile;
@@ -163,7 +168,8 @@
     private static void addResultDetails(XWPFDocument document,
                                          List<GdTaskResultEntity> results,
                                          Map<String, byte[]> imageCache,
-                                         Map<String, byte[]> qrCodeCache)
+                                         Map<String, byte[]> qrCodeCache,
+                                         Map<String, String> addressCache)
         throws IOException, InvalidFormatException {
         if (results == null || results.isEmpty()) {
             addDetailItem(document, "/");
@@ -209,7 +215,8 @@
             currentRow++;
 
             setTableCellWithBoldAndCenter(resultTable, currentRow, 1, "成果地址");
-            setTableCellWithSpacing(resultTable, currentRow, 2, "/");
+            String address = getAddressFromCache(result, addressCache);
+            setTableCellWithSpacing(resultTable, currentRow, 2, formatString(address));
             currentRow++;
 
             setTableCellWithBoldAndCenter(resultTable, currentRow, 1, "成果定位");
@@ -554,6 +561,39 @@
         return qrCodeCache;
     }
 
+    private static Map<String, String> preloadAddresses(List<GdTaskResultEntity> results) {
+        if (results == null || results.isEmpty()) {
+            return Collections.emptyMap();
+        }
+        List<GdTaskResultEntity> needAddressResults = results.stream()
+            .filter(result -> result.getLongitude() != null && result.getLatitude() != null)
+            .collect(Collectors.toList());
+        if (needAddressResults.isEmpty()) {
+            return Collections.emptyMap();
+        }
+
+        Map<String, String> addressCache = new ConcurrentHashMap<>();
+        List<CompletableFuture<Void>> futures = needAddressResults.stream()
+            .map(result -> CompletableFuture.runAsync(() -> {
+                String key = buildCoordinateKey(result.getLongitude(), result.getLatitude());
+                if (StringUtil.isBlank(key) || addressCache.containsKey(key)) {
+                    return;
+                }
+                String address = GdGeoAddressUtil.getFormattedAddress(result.getLongitude(), result.getLatitude());
+                if (!StringUtil.isBlank(address)) {
+                    addressCache.put(key, address);
+                }
+            }, RESOURCE_EXECUTOR))
+            .collect(Collectors.toList());
+
+        try {
+            CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
+        } catch (Exception e) {
+            logger.error("等待成果地址获取完成时发生异常", e);
+        }
+        return addressCache;
+    }
+
     private static byte[] downloadImage(String urlString) throws IOException {
         URL url = new URL(urlString);
         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
@@ -628,6 +668,24 @@
         return String.format(Locale.CHINA, "%.6f", coordinate);
     }
 
+    private static String getAddressFromCache(GdTaskResultEntity result, Map<String, String> addressCache) {
+        if (result == null || addressCache == null || addressCache.isEmpty()) {
+            return null;
+        }
+        String key = buildCoordinateKey(result.getLongitude(), result.getLatitude());
+        if (StringUtil.isBlank(key)) {
+            return null;
+        }
+        return addressCache.get(key);
+    }
+
+    private static String buildCoordinateKey(Double longitude, Double latitude) {
+        if (longitude == null || latitude == null) {
+            return "";
+        }
+        return formatCoordinateValue(longitude) + "," + formatCoordinateValue(latitude);
+    }
+
     private static int findImageFormat(String url) {
         String cleanUrl = url;
         int queryIndex = url.indexOf("?");

--
Gitblit v1.9.3