feat: 任务列表优化

This commit is contained in:
2025-10-27 17:25:32 +08:00
parent c4fc11fd34
commit cce34d8f06
11 changed files with 937 additions and 59 deletions
@@ -8,6 +8,7 @@ import com.central.order.constants.EditTaskStatus;
import com.central.order.enums.OrderStatus;
import com.central.order.feign.FileCenterService;
import com.central.order.model.*;
import com.central.order.model.params.FileDownloadRequest;
import com.central.order.service.*;
import com.central.order.util.DateUtils;
import io.swagger.v3.oas.annotations.Operation;
@@ -22,6 +23,15 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@@ -29,6 +39,8 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* Order Files information
@@ -570,4 +582,133 @@ public class OrderFileController {
}
}
/**
* 代理下载单个文件 - 解决前端跨域问题
*/
@Operation(summary = "下载单个文件")
@PostMapping("/download")
public byte[] downloadFile(@RequestBody FileDownloadRequest request) throws IOException {
String fileUrl = request.getUrl();
log.info("开始代理下载文件: {}", fileUrl);
if (fileUrl == null || fileUrl.trim().isEmpty()) {
throw new IllegalArgumentException("文件URL不能为空");
}
try {
// 从外部 CDN 下载文件
URL url = new URL(fileUrl);
URLConnection connection = url.openConnection();
// 设置超时
connection.setConnectTimeout(10000);
connection.setReadTimeout(30000);
// 获取文件内容
byte[] buffer = new byte[4096];
int bytesRead;
java.io.ByteArrayOutputStream byteArrayOutputStream = new java.io.ByteArrayOutputStream();
try (InputStream input = connection.getInputStream()) {
while ((bytesRead = input.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
}
}
log.info("文件下载成功: {}", extractFileNameFromUrl(fileUrl));
return byteArrayOutputStream.toByteArray();
} catch (Exception e) {
log.error("文件下载失败: fileUrl={}", fileUrl, e);
throw new RuntimeException("文件下载失败: " + e.getMessage(), e);
}
}
/**
* 批量下载所有文件 - 打包为 ZIP
*/
@Operation(summary = "批量下载订单所有资源文件")
@PostMapping("/download/batch")
public byte[] batchDownloadFiles(@RequestBody FileDownloadRequest request) {
String orderNo = request.getOrderNo();
log.info("开始批量下载订单文件: {}", orderNo);
if (orderNo == null || orderNo.trim().isEmpty()) {
throw new IllegalArgumentException("订单编号不能为空");
}
try {
// 查询订单所有文件
List<OrderFile> orderFiles = orderFileService.getOrderFileByOrderNo(orderNo);
if (CollectionUtils.isEmpty(orderFiles)) {
throw new IllegalArgumentException("订单暂无资源文件");
}
// 创建 ZIP 字节流
java.io.ByteArrayOutputStream byteArrayOutputStream = new java.io.ByteArrayOutputStream();
try (ZipOutputStream zipOut = new ZipOutputStream(byteArrayOutputStream)) {
for (OrderFile orderFile : orderFiles) {
String viewUrl = fileCenterService.getFileViewUrl(orderFile.getFileId());
if (viewUrl == null || viewUrl.isEmpty()) {
continue;
}
try {
// 下载单个文件
URL fileUrl = new URL(viewUrl);
URLConnection connection = fileUrl.openConnection();
connection.setConnectTimeout(10000);
connection.setReadTimeout(30000);
String fileName = extractFileNameFromUrl(viewUrl);
// 创建 ZIP 条目
ZipEntry zipEntry = new ZipEntry(fileName);
zipOut.putNextEntry(zipEntry);
// 写入文件内容到 ZIP
try (InputStream fileInput = connection.getInputStream()) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = fileInput.read(buffer)) != -1) {
zipOut.write(buffer, 0, bytesRead);
}
}
zipOut.closeEntry();
log.info("已添加文件到ZIP: {}", fileName);
} catch (Exception e) {
log.warn("添加文件到ZIP失败: {}", viewUrl, e);
// 继续处理其他文件
}
}
}
log.info("批量下载完成: {}", orderNo);
return byteArrayOutputStream.toByteArray();
} catch (Exception e) {
log.error("批量下载文件失败: orderNo={}", orderNo, e);
throw new RuntimeException("批量下载失败: " + e.getMessage(), e);
}
}
/**
* 从 URL 中提取文件名
*/
private String extractFileNameFromUrl(String url) {
if (url == null || url.isEmpty()) {
return "file_" + System.currentTimeMillis();
}
try {
// 移除查询参数
String urlWithoutParams = url.split("\\?")[0];
// 获取最后一个 /之后的部分
String[] parts = urlWithoutParams.split("/");
return parts.length > 0 ? parts[parts.length - 1] : "file_" + System.currentTimeMillis();
} catch (Exception e) {
log.warn("提取文件名失败: {}", url, e);
return "file_" + System.currentTimeMillis();
}
}
}
@@ -0,0 +1,10 @@
package com.central.order.model.params;
import lombok.Data;
@Data
public class FileDownloadRequest {
private String url;
private String orderNo;
}
@@ -87,6 +87,29 @@ public class OrderServiceImpl extends SuperServiceImpl<OrderMapper, Order> imple
@Override
public PageResult<Order> findList(Map<String, Object> params){
log.info("查询订单列表,参数: {}", params);
// 获取租户ID,支持三级回退机制
String anchor = MapUtils.getString(params, "tenantId");
try {
if (anchor == null || anchor.isEmpty()) {
anchor = com.central.common.context.TenantQueryOverrideContext.getTenantId();
if (anchor == null || anchor.isEmpty()) {
anchor = com.central.common.context.LoginUserContextHolder.getCurrentTenantId();
}
}
} catch (Exception e) {
log.error("获取当前登录用户租户失败", e);
}
// 获取租户及其所有子孙租户ID列表
List<String> tenantIds = userCenterService.getSonTenantIds(anchor);
// 根据租户ID列表查询该租户所属的景区
List<Long> tourismIds = tourismMapper.findTourismIdsByTenantIds(tenantIds);
// 将景区ID列表放入查询参数,用于查询订单
params.put("tourismIds", tourismIds);
Page<Order> page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"));
List<Order> list = baseMapper.findList(page, params);
log.info("查询结果数量: {}", list.size());