模型库、矢量文件
This commit is contained in:
@ -21,7 +21,7 @@ import java.nio.file.Paths;
|
||||
@RequestMapping("/auth")
|
||||
public class AuthController {
|
||||
|
||||
// 授权文件存储路径、项目根目录下的license目录
|
||||
// 授权文件存储路径、项目根目录下的 license 目录
|
||||
private static final String AUTH_FILE_PATH = "license/yjearth.lic";
|
||||
|
||||
@GetMapping("/info")
|
||||
|
||||
@ -0,0 +1,47 @@
|
||||
package com.yj.earth.business.controller;
|
||||
|
||||
import com.yj.earth.common.util.GdalUtil;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
@Tag(name = "矢量数据管理")
|
||||
@RestController
|
||||
@RequestMapping("/gdal")
|
||||
public class GdalController {
|
||||
|
||||
@Operation(summary = "导入矢量数据")
|
||||
@PostMapping("/import")
|
||||
public void importDataStreamGzip(@Parameter(description = "矢量文件路径", required = true) @RequestParam("path") String path, HttpServletResponse response) throws IOException {
|
||||
|
||||
// 解析矢量文件、得到JSON数据
|
||||
String jsonData = GdalUtil.readVectorToJson(path);
|
||||
|
||||
// 设置响应头
|
||||
String filename = URLEncoder.encode("data.gz", StandardCharsets.UTF_8);
|
||||
response.setContentType("application/gzip");
|
||||
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
|
||||
// 使用GZIP压缩并输出
|
||||
try (OutputStream outputStream = response.getOutputStream();
|
||||
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream)) {
|
||||
gzipOutputStream.write(jsonData.getBytes(StandardCharsets.UTF_8));
|
||||
gzipOutputStream.finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -10,7 +10,9 @@ import com.graphhopper.config.Profile;
|
||||
import com.graphhopper.util.shapes.GHPoint;
|
||||
import com.yj.earth.annotation.CheckAuth;
|
||||
import com.yj.earth.business.domain.FileInfo;
|
||||
import com.yj.earth.business.domain.WebSource;
|
||||
import com.yj.earth.business.service.FileInfoService;
|
||||
import com.yj.earth.business.service.WebSourceService;
|
||||
import com.yj.earth.common.config.GraphHopperProperties;
|
||||
import com.yj.earth.common.util.ApiResponse;
|
||||
import com.yj.earth.model.Point;
|
||||
@ -40,6 +42,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
@RestController
|
||||
@RequestMapping("/graphhopper")
|
||||
public class GraphHopperController {
|
||||
@Resource
|
||||
private WebSourceService webSourceService;
|
||||
@Resource
|
||||
private FileInfoService fileInfoService;
|
||||
@Resource
|
||||
@ -61,6 +65,15 @@ public class GraphHopperController {
|
||||
return ApiResponse.success(fileInfoService.list(queryWrapper));
|
||||
}
|
||||
|
||||
@Operation(summary = "获取地图列表(网页版)")
|
||||
@CheckAuth
|
||||
@GetMapping("/web/list")
|
||||
public ApiResponse webList() {
|
||||
LambdaQueryWrapper<WebSource> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(WebSource::getType, "pbf");
|
||||
return ApiResponse.success(webSourceService.list(queryWrapper));
|
||||
}
|
||||
|
||||
@Operation(summary = "加载地图数据")
|
||||
@PostMapping("/loadMap")
|
||||
@CheckAuth
|
||||
@ -120,6 +133,57 @@ public class GraphHopperController {
|
||||
return ApiResponse.success(null);
|
||||
}
|
||||
|
||||
@Operation(summary = "加载地图数据(网页版)")
|
||||
@PostMapping("/web/loadMap")
|
||||
@CheckAuth
|
||||
public ApiResponse webLoadMap(@Parameter(description = "文件路径") @RequestParam String path) {
|
||||
File osmFile = new File(path);
|
||||
if (!osmFile.exists()) {
|
||||
return ApiResponse.failure("地图文件不存在: " + path);
|
||||
}
|
||||
if (!osmFile.isFile() || !osmFile.getName().endsWith(".pbf")) {
|
||||
return ApiResponse.failure("仅支持有效的.pbf格式OSM文件");
|
||||
}
|
||||
// 防止并发加载
|
||||
if (isLoading.get()) {
|
||||
return ApiResponse.failure("地图正在加载中、请稍后查询状态");
|
||||
}
|
||||
|
||||
// 标记加载状态
|
||||
isLoading.set(true);
|
||||
isLoaded.set(false);
|
||||
|
||||
// 异步执行: 删除旧数据 → 创建新实例 → 加载新地图
|
||||
new Thread(() -> {
|
||||
GraphHopper newHopper = null;
|
||||
try {
|
||||
// 关键步骤1: 彻底删除旧地图数据目录
|
||||
deleteOldGraphDir();
|
||||
// 关键步骤2: 创建全新的GraphHopper实例
|
||||
newHopper = createNewGraphHopperInstance(path);
|
||||
// 关键步骤3: 加载新地图
|
||||
newHopper.importOrLoad();
|
||||
// 关键步骤4: 加载成功 → 替换当前实例 + 更新状态
|
||||
currentHopper = newHopper;
|
||||
isLoaded.set(true);
|
||||
log.info("地图加载成功");
|
||||
} catch (Exception e) {
|
||||
// 加载失败 → 清理新实例资源
|
||||
if (newHopper != null) {
|
||||
newHopper.close();
|
||||
}
|
||||
isLoaded.set(false);
|
||||
e.printStackTrace();
|
||||
log.error("地图加载失败: " + e.getMessage());
|
||||
|
||||
} finally {
|
||||
// 无论成功/失败、释放加载锁
|
||||
isLoading.set(false);
|
||||
}
|
||||
}).start();
|
||||
return ApiResponse.success(null);
|
||||
}
|
||||
|
||||
@Operation(summary = "路径规划")
|
||||
@PostMapping("/route")
|
||||
@CheckAuth
|
||||
|
||||
@ -265,6 +265,7 @@ public class IconLibraryController {
|
||||
icon.icon_name as iconName,
|
||||
icon.icon_type as iconType,
|
||||
icon.data,
|
||||
|
||||
icon.view,
|
||||
icon.created_at as createdAt,
|
||||
icon.updated_at as updatedAt,
|
||||
|
||||
@ -9,18 +9,24 @@ import com.yj.earth.business.domain.ModelType;
|
||||
import com.yj.earth.business.service.FileInfoService;
|
||||
import com.yj.earth.business.service.ModelLibraryService;
|
||||
import com.yj.earth.common.util.ApiResponse;
|
||||
import com.yj.earth.common.util.FileCommonUtil;
|
||||
import com.yj.earth.common.util.SQLiteUtil;
|
||||
import com.yj.earth.dto.militaryLibrary.DragMilitaryTypeDto;
|
||||
import com.yj.earth.dto.modelLibrary.AddModelTypeDto;
|
||||
import com.yj.earth.dto.modelLibrary.CreateModelLibraryDto;
|
||||
import com.yj.earth.dto.modelLibrary.DragModelTypeDto;
|
||||
import com.yj.earth.dto.modelLibrary.UpdateModelTypeNameDto;
|
||||
import com.yj.earth.vo.ModelDataVo;
|
||||
import com.yj.earth.vo.ModelTypeVo;
|
||||
import com.yj.earth.vo.ModelVo;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
@ -28,11 +34,11 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Tag(name = "模型库管理")
|
||||
@ -157,37 +163,6 @@ public class ModelLibraryController {
|
||||
return ApiResponse.success(modelTypeList());
|
||||
}
|
||||
|
||||
@Operation(summary = "拖动模型类型树")
|
||||
@PostMapping("/dragModelType")
|
||||
public ApiResponse dragMilitaryType(@RequestBody DragModelTypeDto dragModelTypeDto) throws SQLException {
|
||||
String militaryPath = getModelLibrary();
|
||||
if (militaryPath == null) {
|
||||
return ApiResponse.failure("请先创建或导入模型库");
|
||||
}
|
||||
|
||||
// 动态构建SQL和参数
|
||||
List<String> updateFields = new ArrayList<>();
|
||||
List<Object> params = new ArrayList<>();
|
||||
|
||||
// 判断 parentId 是否存在
|
||||
if (dragModelTypeDto.getParentId() != null) {
|
||||
updateFields.add("parent_id = ?");
|
||||
params.add(dragModelTypeDto.getParentId());
|
||||
}
|
||||
|
||||
// 判断 treeIndex 是否存在
|
||||
if (dragModelTypeDto.getTreeIndex() != null) {
|
||||
updateFields.add("tree_index = ?");
|
||||
params.add(dragModelTypeDto.getTreeIndex());
|
||||
}
|
||||
|
||||
// 构建完整 SQL
|
||||
String sql = "UPDATE model_type SET " + String.join(", ", updateFields) + " WHERE id = ?";
|
||||
params.add(dragModelTypeDto.getId());
|
||||
SQLiteUtil.executeUpdate(militaryPath, sql, params);
|
||||
return ApiResponse.success(null);
|
||||
}
|
||||
|
||||
@Operation(summary = "添加模型文件")
|
||||
@PostMapping("/addModelFile")
|
||||
public ApiResponse addModelFile(@RequestParam("files") MultipartFile[] files, @Parameter(description = "模型类型ID") @RequestParam("modelTypeId") String modelTypeId) throws IOException, SQLException {
|
||||
@ -212,16 +187,16 @@ public class ModelLibraryController {
|
||||
|
||||
// 构建插入SQL
|
||||
String sql = "INSERT INTO model " +
|
||||
"(id, model_type_id, model_name, model_type, data, created_at) " +
|
||||
"(id, model_type_id, model_name, model_type, model_data, created_at) " +
|
||||
"VALUES (?, ?, ?, ?, ?, ?)";
|
||||
|
||||
String url = fileInfoService.uploadWithPreview(file);
|
||||
List<Object> params = new ArrayList<>();
|
||||
params.add(UUID.fastUUID().toString(true));
|
||||
String modelId = UUID.fastUUID().toString(true);
|
||||
params.add(modelId);
|
||||
params.add(modelTypeId);
|
||||
params.add(fileNameWithoutSuffix);
|
||||
params.add(fileSuffix);
|
||||
params.add(url);
|
||||
params.add(file.getBytes());
|
||||
params.add(LocalDateTime.now());
|
||||
|
||||
// 执行插入操作
|
||||
@ -231,6 +206,72 @@ public class ModelLibraryController {
|
||||
return ApiResponse.success(null);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取模型数据")
|
||||
@GetMapping("/data/{type}/{modelId}/{fileSuffix}")
|
||||
public ResponseEntity<byte[]> modelData(
|
||||
@Parameter(description = "数据类型") @PathVariable("type") String type,
|
||||
@Parameter(description = "模型ID") @PathVariable("modelId") String modelId,
|
||||
@Parameter(description = "模型类型") @PathVariable(value = "fileSuffix") String fileSuffix) {
|
||||
try {
|
||||
// 获取最新的模型库
|
||||
String modelPath = getModelLibrary();
|
||||
if (modelPath == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
List<Object> params = new ArrayList<>();
|
||||
params.add(modelId);
|
||||
String sql = null;
|
||||
if (type.equals("model")) {
|
||||
sql = "SELECT model_name, model_data FROM model WHERE id = ?";
|
||||
} else {
|
||||
sql = "SELECT model_name, poster_data FROM model WHERE id = ?";
|
||||
}
|
||||
// 查询模型数据
|
||||
ModelDataVo modelDataVo = SQLiteUtil.queryForObject(
|
||||
modelPath,
|
||||
sql,
|
||||
params,
|
||||
ModelDataVo.class
|
||||
);
|
||||
|
||||
if (modelDataVo == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
// 构建原始文件名(使用从数据库查询的名称更合理)
|
||||
String originalFileName = modelDataVo.getModelName() + "." + fileSuffix;
|
||||
|
||||
// 对文件名进行URL编码处理、解决非ASCII字符问题
|
||||
String encodedFileName = URLEncoder.encode(originalFileName, StandardCharsets.UTF_8.name());
|
||||
|
||||
// 根据图片后缀获取对应的MediaType
|
||||
MediaType contentType = FileCommonUtil.getImageMediaType(fileSuffix);
|
||||
|
||||
// 设置响应头、使用 inline 确保可以预览
|
||||
if (type.equals("model")) {
|
||||
return ResponseEntity.ok()
|
||||
.contentType(contentType)
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION,
|
||||
"inline; filename=\"" + encodedFileName + "\"")
|
||||
.body(modelDataVo.getModelData());
|
||||
} else {
|
||||
return ResponseEntity.ok()
|
||||
.contentType(contentType)
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION,
|
||||
"inline; filename=\"" + encodedFileName + "\"")
|
||||
.body(modelDataVo.getPosterData());
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Operation(summary = "根据模型类型查看模型列表")
|
||||
@PostMapping("/modelList")
|
||||
public ApiResponse modelList(@Parameter(description = "模型类型ID") @RequestParam("modelTypeId") String modelTypeId) throws SQLException, IllegalAccessException, InstantiationException {
|
||||
@ -239,7 +280,18 @@ public class ModelLibraryController {
|
||||
if (modelPath == null) {
|
||||
return ApiResponse.failure("请先创建或导入模型库");
|
||||
}
|
||||
// 多表联查、查询所有的模型
|
||||
|
||||
// 获取分类ID及其所有子分类ID的列表
|
||||
List<String> typeIdList = getModelTypeIdsWithChildren(modelTypeId);
|
||||
if (typeIdList.isEmpty()) {
|
||||
return ApiResponse.success(null);
|
||||
}
|
||||
|
||||
String idsWithQuotes = typeIdList.stream()
|
||||
.map(id -> "'" + id + "'")
|
||||
.collect(Collectors.joining(","));
|
||||
|
||||
// 多表联查、查询所有指定分类及其子分类下的模型
|
||||
String sql = """
|
||||
SELECT
|
||||
model.id,
|
||||
@ -247,24 +299,34 @@ public class ModelLibraryController {
|
||||
model.model_name as modelName,
|
||||
model.model_type as modelType,
|
||||
model.poster_type as posterType,
|
||||
model.poster,
|
||||
model.data,
|
||||
model.view,
|
||||
model.created_at as createdAt,
|
||||
model.updated_at as updatedAt,
|
||||
model_type.name as modelTypeName from
|
||||
model JOIN model_type ON model.model_type_id = model_type.id
|
||||
WHERE model.model_type_id = ?
|
||||
""";
|
||||
model_type.name as modelTypeName
|
||||
FROM model
|
||||
JOIN model_type ON model.model_type_id = model_type.id
|
||||
WHERE model.model_type_id IN (?)
|
||||
""".replace("?", idsWithQuotes);
|
||||
|
||||
// 使用所有分类ID作为查询参数
|
||||
List<Object> params = new ArrayList<>();
|
||||
params.add(modelTypeId);
|
||||
List<ModelVo> modelVos = SQLiteUtil.queryForList(modelPath, sql, params, ModelVo.class);
|
||||
List<ModelVo> modelVos = SQLiteUtil.queryForList(modelPath, sql, null, ModelVo.class);
|
||||
// 循环遍历数据
|
||||
for (ModelVo modelVo : modelVos) {
|
||||
if (modelVo.getModelType() != null) {
|
||||
modelVo.setModelDataUrl("/modelLibrary/data/model/" + modelVo.getId() + "/" + modelVo.getModelType());
|
||||
}
|
||||
if (modelVo.getPosterType() != null) {
|
||||
modelVo.setPosterDataUrl("/modelLibrary/data/poster/" + modelVo.getId() + "/" + modelVo.getPosterType());
|
||||
}
|
||||
}
|
||||
return ApiResponse.success(modelVos);
|
||||
}
|
||||
|
||||
@Operation(summary = "更新模型信息")
|
||||
@PostMapping("/uploadModelInfo")
|
||||
public ApiResponse uploadModelInfo(@Parameter(description = "模型封面文件") @RequestParam(value = "file", required = false) MultipartFile file, @Parameter(description = "模型ID") @RequestParam("modelId") String modelId, @Parameter(description = "模型名称") @RequestParam(value = "modelName", required = false) String modelName) throws IOException, SQLException {
|
||||
public ApiResponse uploadModelInfo(@Parameter(description = "模型封面文件") @RequestParam(value = "file", required = false) MultipartFile file,
|
||||
@Parameter(description = "模型ID") @RequestParam("modelId") String modelId,
|
||||
@Parameter(description = "模型名称") @RequestParam(value = "modelName", required = false) String modelName) throws IOException, SQLException {
|
||||
|
||||
// 获取最新的模型库路径
|
||||
String modelPath = getModelLibrary();
|
||||
@ -279,10 +341,9 @@ public class ModelLibraryController {
|
||||
// 处理封面文件
|
||||
if (file != null && !file.isEmpty()) {
|
||||
String fileSuffix = FileUtil.extName(file.getOriginalFilename());
|
||||
String url = fileInfoService.uploadWithPreview(file);
|
||||
sql.append(", poster_type = ?, poster = ? ");
|
||||
sql.append(", poster_type = ?, poster_data = ? ");
|
||||
params.add(fileSuffix);
|
||||
params.add(url);
|
||||
params.add(file.getBytes());
|
||||
}
|
||||
|
||||
// 处理模型名称
|
||||
@ -344,7 +405,7 @@ public class ModelLibraryController {
|
||||
tree_index as treeIndex, created_at as createdAt,
|
||||
updated_at as updatedAt FROM model_type ORDER BY tree_index ASC
|
||||
""";
|
||||
// 查询所有模型类型
|
||||
// 查询所有模型的类型
|
||||
List<ModelType> modelTypes = SQLiteUtil.queryForList(modelPath, sql, null, ModelType.class);
|
||||
// 转换为树形结构
|
||||
return buildModelTypeTree(modelTypes);
|
||||
@ -354,11 +415,14 @@ public class ModelLibraryController {
|
||||
List<ModelTypeVo> treeNodes = modelTypes.stream()
|
||||
.map(modelType -> new ModelTypeVo(modelType))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 构建节点ID到节点的映射
|
||||
Map<String, ModelTypeVo> nodeMap = treeNodes.stream()
|
||||
.collect(Collectors.toMap(ModelTypeVo::getId, node -> node));
|
||||
|
||||
// 根节点列表
|
||||
List<ModelTypeVo> rootNodes = new ArrayList<>();
|
||||
|
||||
// 为每个节点添加子节点
|
||||
for (ModelTypeVo node : treeNodes) {
|
||||
String parentId = node.getParentId();
|
||||
@ -373,9 +437,29 @@ public class ModelLibraryController {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 排序根节点
|
||||
rootNodes.sort(Comparator.comparingInt(ModelTypeVo::getTreeIndex));
|
||||
|
||||
// 递归排序所有子节点
|
||||
sortChildren(rootNodes);
|
||||
|
||||
return rootNodes;
|
||||
}
|
||||
|
||||
// 递归排序所有子节点
|
||||
private void sortChildren(List<ModelTypeVo> nodes) {
|
||||
if (nodes == null || nodes.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
// 排序当前层级节点
|
||||
nodes.sort(Comparator.comparingInt(ModelTypeVo::getTreeIndex));
|
||||
// 递归排序下一层级
|
||||
for (ModelTypeVo node : nodes) {
|
||||
sortChildren(node.getChildren());
|
||||
}
|
||||
}
|
||||
|
||||
private String getModelLibrary() {
|
||||
// 获取启用的模型库
|
||||
LambdaQueryWrapper<ModelLibrary> queryWrapper = new LambdaQueryWrapper<>();
|
||||
@ -417,4 +501,69 @@ public class ModelLibraryController {
|
||||
modelLibraryService.save(newModel);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据模型分类ID、查询该分类ID及其所有子分类ID的列表(递归包含所有层级子分类)
|
||||
*/
|
||||
private List<String> getModelTypeIdsWithChildren(String modelTypeId) throws SQLException, IllegalAccessException, InstantiationException {
|
||||
// 结果列表初始化
|
||||
List<String> typeIdList = new ArrayList<>();
|
||||
|
||||
// 校验入参:分类ID不能为空
|
||||
if (StringUtils.isEmpty(modelTypeId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 获取当前启用的模型库路径
|
||||
String modelPath = getModelLibrary();
|
||||
if (modelPath == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 校验目标分类ID是否存在(避免无效ID导致递归无结果)
|
||||
if (!isModelTypeExist(modelPath, modelTypeId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 递归查询: 收集当前分类及所有子分类ID
|
||||
recursiveGetChildIds(modelPath, modelTypeId, typeIdList);
|
||||
|
||||
return typeIdList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归查询子分类ID、并将结果存入列表
|
||||
*/
|
||||
private void recursiveGetChildIds(String modelPath, String currentTypeId, List<String> resultList) throws SQLException, IllegalAccessException, InstantiationException {
|
||||
// 先将当前分类ID加入结果列表(确保包含自身)
|
||||
resultList.add(currentTypeId);
|
||||
|
||||
// 查询当前分类的「直接子分类」(按 tree_index 排序、保持树形结构原有顺序)
|
||||
String childSql = "SELECT id FROM model_type WHERE parent_id = ? ORDER BY tree_index ASC";
|
||||
List<Object> childParams = new ArrayList<>();
|
||||
childParams.add(currentTypeId);
|
||||
List<ModelType> childModelTypes = SQLiteUtil.queryForList(
|
||||
modelPath, childSql, childParams, ModelType.class
|
||||
);
|
||||
|
||||
// 若存在子分类、递归查询每个子分类的子分类
|
||||
if (childModelTypes != null && !childModelTypes.isEmpty()) {
|
||||
for (ModelType childType : childModelTypes) {
|
||||
recursiveGetChildIds(modelPath, childType.getId(), resultList);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验模型分类ID是否存在于模型库中
|
||||
*/
|
||||
private boolean isModelTypeExist(String modelPath, String typeId)
|
||||
throws SQLException, IllegalAccessException, InstantiationException {
|
||||
String checkSql = "SELECT id FROM model_type WHERE id = ?";
|
||||
List<Object> checkParams = new ArrayList<>();
|
||||
checkParams.add(typeId);
|
||||
List<ModelType> existTypes = SQLiteUtil.queryForList(modelPath, checkSql, checkParams, ModelType.class);
|
||||
// 若查询结果非空、说明分类存在
|
||||
return existTypes != null && !existTypes.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,120 @@
|
||||
package com.yj.earth.business.controller;
|
||||
|
||||
import com.yj.earth.common.util.ApiResponse;
|
||||
import com.yj.earth.dto.system.UpdateSystemServiceDto;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Tag(name = "系统服务管理")
|
||||
@RestController
|
||||
@RequestMapping("/systemService")
|
||||
public class SystemController {
|
||||
@Value("${server.host}")
|
||||
private String serverHost;
|
||||
@Value("${server.port}")
|
||||
private Integer serverPort;
|
||||
|
||||
// 配置文件路径(jar包所在目录的application.yml)
|
||||
private static final String CONFIG_FILE_PATH = "application.yml";
|
||||
|
||||
// 重启脚本路径(app同级目录)
|
||||
private static final String WINDOWS_REBOOT_SCRIPT = "../reboot.bat";
|
||||
private static final String LINUX_REBOOT_SCRIPT = "../reboot.sh";
|
||||
|
||||
@Operation(summary = "读取系统服务配置")
|
||||
@PostMapping("/info")
|
||||
public ApiResponse readSystemServiceConfig() {
|
||||
Map<String, Object> map = Map.of("serverHost", serverHost, "serverPort", serverPort);
|
||||
return ApiResponse.success(map);
|
||||
}
|
||||
|
||||
@Operation(summary = "修改系统服务配置并重启")
|
||||
@PostMapping("/update")
|
||||
public ApiResponse updateSystemService(@RequestBody UpdateSystemServiceDto updateSystemServiceDto) {
|
||||
try {
|
||||
// 读取并更新YAML配置
|
||||
Yaml yaml = new Yaml();
|
||||
File configFile = new File(CONFIG_FILE_PATH);
|
||||
if (!configFile.exists()) {
|
||||
return ApiResponse.failure("配置文件不存在");
|
||||
}
|
||||
// 解析YAML内容
|
||||
Map<String, Object> configMap;
|
||||
try (InputStream in = Files.newInputStream(Paths.get(CONFIG_FILE_PATH))) {
|
||||
configMap = yaml.load(in);
|
||||
if (configMap == null) {
|
||||
configMap = new HashMap<>();
|
||||
}
|
||||
}
|
||||
// 更新配置
|
||||
Map<String, Object> serverMap = (Map<String, Object>) configMap.getOrDefault("server", new HashMap<>());
|
||||
if (updateSystemServiceDto.getServerHost() != null && !updateSystemServiceDto.getServerHost().isEmpty()) {
|
||||
serverMap.put("host", updateSystemServiceDto.getServerHost());
|
||||
}
|
||||
if (updateSystemServiceDto.getServerPort() != null) {
|
||||
serverMap.put("port", updateSystemServiceDto.getServerPort());
|
||||
}
|
||||
configMap.put("server", serverMap);
|
||||
// 保存配置
|
||||
DumperOptions options = new DumperOptions();
|
||||
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||
options.setPrettyFlow(true);
|
||||
Yaml writeYaml = new Yaml(options);
|
||||
try (FileWriter writer = new FileWriter(configFile, StandardCharsets.UTF_8)) {
|
||||
writeYaml.dump(configMap, writer);
|
||||
}
|
||||
// 执行重启脚本
|
||||
executeRebootScript();
|
||||
return ApiResponse.success(null);
|
||||
|
||||
} catch (IOException e) {
|
||||
return ApiResponse.failure("更新配置失败:" + e.getMessage());
|
||||
} catch (Exception e) {
|
||||
return ApiResponse.failure("操作失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行重启脚本
|
||||
*/
|
||||
private void executeRebootScript() throws IOException, InterruptedException {
|
||||
String scriptPath;
|
||||
ProcessBuilder processBuilder;
|
||||
|
||||
// 判断操作系统类型、选择对应的脚本
|
||||
if (System.getProperty("os.name").toLowerCase().contains("win")) {
|
||||
scriptPath = WINDOWS_REBOOT_SCRIPT;
|
||||
// 检查Windows脚本是否存在
|
||||
if (!new File(scriptPath).exists()) {
|
||||
throw new FileNotFoundException("Windows重启脚本不存在: " + scriptPath);
|
||||
}
|
||||
processBuilder = new ProcessBuilder(scriptPath);
|
||||
} else {
|
||||
scriptPath = LINUX_REBOOT_SCRIPT;
|
||||
// 检查Linux脚本是否存在
|
||||
if (!new File(scriptPath).exists()) {
|
||||
throw new FileNotFoundException("Linux重启脚本不存在: " + scriptPath);
|
||||
}
|
||||
// 给脚本添加执行权限
|
||||
new ProcessBuilder("chmod", "+x", scriptPath).start().waitFor();
|
||||
processBuilder = new ProcessBuilder(scriptPath);
|
||||
}
|
||||
|
||||
// 执行脚本(在新进程中运行、不阻塞当前请求)
|
||||
processBuilder.start();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,123 @@
|
||||
package com.yj.earth.business.controller;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.lang.UUID;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.yj.earth.annotation.RoleAccess;
|
||||
import com.yj.earth.business.domain.WebSource;
|
||||
import com.yj.earth.business.service.SourceService;
|
||||
import com.yj.earth.business.service.WebSourceService;
|
||||
import com.yj.earth.common.util.ApiResponse;
|
||||
import com.yj.earth.dto.source.AddModelSourceDto;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Tag(name = "网页版独有接口")
|
||||
@RestController
|
||||
@RequestMapping("/webSource")
|
||||
public class WebSourceController {
|
||||
@Value("${sync.folder}")
|
||||
private String syncFolder;
|
||||
|
||||
@Resource
|
||||
private WebSourceService webSourceService;
|
||||
@Resource
|
||||
private SourceService sourceService;
|
||||
private static final List<String> SUPPORT_EXTENSIONS = Arrays.asList("clt", "mbtiles", "pak", "pbf");
|
||||
|
||||
@RoleAccess(roleNames = "管理员")
|
||||
@Operation(summary = "同步数据")
|
||||
@PostMapping("/sync")
|
||||
public ApiResponse sync() {
|
||||
try {
|
||||
Path syncPath = Paths.get(syncFolder);
|
||||
// 验证文件夹有效性
|
||||
if (!Files.exists(syncPath) || !Files.isDirectory(syncPath)) {
|
||||
return ApiResponse.failure("同步文件夹不存在或不是一个有效的目录");
|
||||
}
|
||||
// 循环处理所有支持的文件类型、减少重复代码
|
||||
for (String extension : SUPPORT_EXTENSIONS) {
|
||||
List<String> filePaths = getFilesByExtension(syncPath, extension);
|
||||
// 批量处理同类型文件
|
||||
filePaths.forEach(this::saveFileIfNotExists);
|
||||
}
|
||||
return ApiResponse.success(null);
|
||||
} catch (Exception e) {
|
||||
return ApiResponse.failure("同步数据失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 若文件路径不存在则保存到数据库
|
||||
*/
|
||||
private void saveFileIfNotExists(String filePath) {
|
||||
// 检查路径是否已存在
|
||||
LambdaQueryWrapper<WebSource> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(WebSource::getPath, filePath);
|
||||
if (webSourceService.count(queryWrapper) > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 构建并保存 WebSource 对象
|
||||
WebSource webSource = new WebSource();
|
||||
webSource.setName(FileUtil.getName(filePath));
|
||||
webSource.setType(FileUtil.extName(filePath));
|
||||
webSource.setPath(filePath);
|
||||
webSourceService.save(webSource);
|
||||
|
||||
if (!webSource.getType().equals("pbf")) {
|
||||
addModelSourceIfNotExists(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 若文件路径不存在则保存到数据库
|
||||
*/
|
||||
private void addModelSourceIfNotExists(String filePath) {
|
||||
AddModelSourceDto addModelSourceDto = new AddModelSourceDto();
|
||||
addModelSourceDto.setSourcePath(filePath);
|
||||
addModelSourceDto.setId(UUID.randomUUID().toString(true));
|
||||
sourceService.addModelSource(addModelSourceDto);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取所有文件")
|
||||
@GetMapping("/list")
|
||||
public ApiResponse list(@RequestParam(required = false) @Parameter(description = "文件类型") String type) {
|
||||
if (type != null) {
|
||||
LambdaQueryWrapper<WebSource> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(WebSource::getType, type);
|
||||
return ApiResponse.success(webSourceService.list(queryWrapper));
|
||||
}
|
||||
return ApiResponse.success(webSourceService.list());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定目录下特定扩展名的文件
|
||||
*/
|
||||
private List<String> getFilesByExtension(Path directory, String extension) throws IOException {
|
||||
try (Stream<Path> stream = Files.list(directory)) {
|
||||
return stream
|
||||
.filter(Files::isRegularFile)
|
||||
.filter(path -> {
|
||||
String fileName = path.getFileName().toString();
|
||||
return fileName.toLowerCase().endsWith("." + extension.toLowerCase());
|
||||
})
|
||||
.map(Path::toAbsolutePath)
|
||||
.map(Path::toString)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user