From 70b4182fb75db9a5acd234a0ab4c22fd8f93de92 Mon Sep 17 00:00:00 2001
From: linwei <872216696@qq.com>
Date: Tue, 14 Apr 2026 18:07:21 +0800
Subject: [PATCH] fix: 成果下载+成果列表+飞手查询所有

---
 drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/service/impl/GdTaskResultServiceImpl.java |   89 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 89 insertions(+), 0 deletions(-)

diff --git a/drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/service/impl/GdTaskResultServiceImpl.java b/drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/service/impl/GdTaskResultServiceImpl.java
index c88ccd1..31e7fc1 100644
--- a/drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/service/impl/GdTaskResultServiceImpl.java
+++ b/drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/service/impl/GdTaskResultServiceImpl.java
@@ -26,7 +26,16 @@
 import com.baomidou.mybatisplus.core.conditions.Wrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import org.springblade.core.mp.base.BaseServiceImpl;
+import org.springblade.core.tool.utils.Func;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
+import java.net.URL;
+import java.net.URLConnection;
+import java.nio.charset.StandardCharsets;
 import java.util.List;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
 
 /**
  * 成果表 服务实现类
@@ -104,4 +113,84 @@
 		return result;
 	}
 
+	@Override
+	public void downloadResultFiles(String ids, HttpServletResponse response) {
+		// 根据ID列表查询成果记录
+		List<Long> idList = Func.toLongList(ids);
+		List<GdTaskResultEntity> resultList = listByIds(idList);
+
+		if (resultList == null || resultList.isEmpty()) {
+			throw new RuntimeException("未找到成果数据");
+		}
+
+		// 设置响应头
+		response.setContentType("application/zip");
+		response.setHeader("Content-Disposition", "attachment; filename=result_files.zip");
+
+		try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) {
+			for (GdTaskResultEntity result : resultList) {
+				String resultUrl = result.getResultUrl();
+				if (resultUrl == null || resultUrl.isEmpty()) {
+					continue;
+				}
+
+				// 处理转义字符
+				resultUrl = unescapeUrl(resultUrl);
+
+				try {
+					// 从URL下载文件
+					URL url = new URL(resultUrl);
+					URLConnection connection = url.openConnection();
+					connection.setConnectTimeout(5000);
+					connection.setReadTimeout(10000);
+
+					// 从URL中提取文件名
+					String fileName = extractFileName(resultUrl, result.getId());
+
+					// 添加到ZIP
+					ZipEntry zipEntry = new ZipEntry(fileName);
+					zos.putNextEntry(zipEntry);
+
+					try (InputStream is = connection.getInputStream()) {
+						byte[] buffer = new byte[4096];
+						int len;
+						while ((len = is.read(buffer)) > 0) {
+							zos.write(buffer, 0, len);
+						}
+					}
+
+					zos.closeEntry();
+				} catch (Exception e) {
+					// 单个文件下载失败,继续处理其他文件
+					// 可以记录日志
+				}
+			}
+		} catch (IOException e) {
+			throw new RuntimeException("下载文件失败", e);
+		}
+	}
+
+	/**
+	 * 从URL中提取文件名
+	 */
+	private String extractFileName(String url, Long resultId) {
+		if (url == null || url.isEmpty()) {
+			return resultId + "_unknown";
+		}
+
+		// 从URL中提取最后一部分作为文件名
+		int lastSlash = url.lastIndexOf('/');
+		if (lastSlash != -1 && lastSlash < url.length() - 1) {
+			String fileName = url.substring(lastSlash + 1);
+			// 处理可能的查询参数
+			int questionMark = fileName.indexOf('?');
+			if (questionMark != -1) {
+				fileName = fileName.substring(0, questionMark);
+			}
+			return resultId + "_" + fileName;
+		}
+
+		return resultId + "_file";
+	}
+
 }

--
Gitblit v1.9.3