资源相关

This commit is contained in:
ZZX9599
2025-09-16 11:41:45 +08:00
parent eda0bc0999
commit 89df7e6c0e
22 changed files with 1011 additions and 64 deletions

View File

@ -2,26 +2,40 @@ package com.yj.earth.business.controller;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yj.earth.annotation.CheckAuth;
import com.yj.earth.business.domain.FileInfo;
import com.yj.earth.business.domain.Role;
import com.yj.earth.business.domain.Source;
import com.yj.earth.business.service.RoleSourceService;
import com.yj.earth.business.service.SourceService;
import com.yj.earth.business.service.UserService;
import com.yj.earth.business.service.*;
import com.yj.earth.common.service.SourceDataGenerator;
import com.yj.earth.common.service.SourceParamsValidator;
import com.yj.earth.common.util.ApiResponse;
import com.yj.earth.common.util.MapUtil;
import com.yj.earth.dto.source.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import static com.yj.earth.common.constant.GlobalConstant.DIRECTORY;
@ -39,10 +53,19 @@ public class SourceController {
@Resource
private RoleSourceService roleSourceService;
@Resource
private RoleService roleService;
@Resource
private SourceParamsValidator sourceParamsValidator;
@Resource
private SourceDataGenerator sourceDataGenerator;
@Resource
private FileInfoService fileInfoService;
@Resource
private FileInfoController fileInfoControllerl;
@Value("${file.upload.path}")
private String uploadPath;
@PostMapping("/addDirectory")
@Operation(summary = "新增目录资源")
@ -86,10 +109,10 @@ public class SourceController {
source.setSourceName(sourceName);
source.setParentId(addModelSourceDto.getParentId());
source.setTreeIndex(addModelSourceDto.getTreeIndex());
source.setParams(addModelSourceDto.getParams());
source.setDetail(detail);
source.setSourceType(MapUtil.getString(MapUtil.jsonToMap(detail), "fileType"));
source.setIsShow(SHOW);
source.setParams(addModelSourceDto.getParams());
sourceService.save(source);
// 添加资源到该用户的角色下
roleSourceService.addRoleSource(userService.getById(StpUtil.getLoginIdAsString()).getRoleId(), source.getId());
@ -196,6 +219,30 @@ public class SourceController {
return ApiResponse.success(null);
}
@PostMapping("/uploadLocationImage")
@Operation(summary = "新增定位图片")
public ApiResponse uploadLocationImage(@RequestParam("ids") @Parameter(description = "上传定位图片ID列表") List<String> ids,
@RequestParam(value = "parentId", required = false) @Parameter(description = "父节点ID") String parentId,
@RequestParam(value = "treeIndex", required = false) @Parameter(description = "树状索引") Integer treeIndex,
@RequestParam("files") @Parameter(description = "带有定位的图片文件", required = true) MultipartFile[] files) {
for (int i = 0; i < files.length; i++) {
String detail = fileInfoControllerl.handleLocationImageUpload(files[i]);
// 构建并保存资源对象
Source source = new Source();
source.setId(ids.get(i));
source.setSourceName(files[i].getOriginalFilename());
source.setParentId(parentId);
source.setSourceType("locationImage");
source.setTreeIndex(treeIndex);
source.setDetail(detail);
source.setIsShow(SHOW);
sourceService.save(source);
// 添加资源到该用户的角色下
roleSourceService.addRoleSource(userService.getById(StpUtil.getLoginIdAsString()).getRoleId(), source.getId());
}
return ApiResponse.success(null);
}
@GetMapping("/type")
@Operation(summary = "获取已有类型")
public ApiResponse getSupportedSourceTypes() {
@ -208,4 +255,61 @@ public class SourceController {
public String getExampleData(String sourceType) throws JsonProcessingException {
return sourceDataGenerator.generateDefaultJson(sourceType);
}
@Operation(summary = "设置默认数据")
@GetMapping("/default")
public ApiResponse getDefaultData() {
log.info("开始初始化默认资源数据");
String userId = StpUtil.getLoginIdAsString();
String roleId = userService.getById(userId).getRoleId();
// 创建一级目录
createSourceIfNotExists("倾斜模型", "directory", null, 0, 1, roleId);
createSourceIfNotExists("人工模型", "directory", null, 0, 1, roleId);
createSourceIfNotExists("卫星底图", "directory", null, 0, 1, roleId);
createSourceIfNotExists("地形", "directory", null, 0, 1, roleId);
// 创建"在线图源"目录及其子资源
Source onlineSource = createSourceIfNotExists("在线图源", "directory", null, 0, 1, roleId);
if (onlineSource != null) {
String onlineSourceId = onlineSource.getId();
createSourceIfNotExists("卫星图", "arcgisWximagery", onlineSourceId, 0, 1, roleId);
createSourceIfNotExists("暗黑地图", "arcgisBlueImagery", onlineSourceId, 0, 1, roleId);
createSourceIfNotExists("路网图", "gdlwImagery", onlineSourceId, 0, 1, roleId);
createSourceIfNotExists("矢量图", "gdslImagery", onlineSourceId, 0, 1, roleId);
}
log.info("默认资源数据初始化完成");
return ApiResponse.success(null);
}
/**
* 检查资源是否存在、不存在则创建并关联角色资源
*/
private Source createSourceIfNotExists(String sourceName, String sourceType, String parentId, int treeIndex, int isShow, String roleId) {
// 检查资源是否已存在通过名称和父ID组合判断唯一性
Source existingSource = sourceService.getOne(new LambdaQueryWrapper<Source>()
.eq(Source::getSourceName, sourceName)
.eq(parentId != null, Source::getParentId, parentId)
.isNull(parentId == null, Source::getParentId));
if (existingSource != null) {
return existingSource;
}
// 不存在则创建新资源
try {
Source newSource = new Source();
newSource.setId(cn.hutool.core.lang.UUID.fastUUID().toString(true));
newSource.setSourceName(sourceName);
newSource.setSourceType(sourceType);
newSource.setParentId(parentId);
newSource.setTreeIndex(treeIndex);
newSource.setIsShow(isShow);
sourceService.save(newSource);
// 关联角色资源
roleSourceService.addRoleSource(roleId, newSource.getId());
return newSource;
} catch (Exception e) {
return null;
}
}
}