最新产品
This commit is contained in:
@ -0,0 +1,168 @@
|
||||
package com.yj.earth.business.controller;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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.common.service.SourceParamsValidator;
|
||||
import com.yj.earth.common.util.ApiResponse;
|
||||
import com.yj.earth.common.util.MapUtil;
|
||||
import com.yj.earth.dto.source.AddDirectoryDto;
|
||||
import com.yj.earth.dto.source.AddModelSourceDto;
|
||||
import com.yj.earth.dto.source.AddOtherSourceDto;
|
||||
import com.yj.earth.dto.source.UpdateSourceDto;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static com.yj.earth.common.constant.GlobalConstant.DIRECTORY;
|
||||
import static com.yj.earth.common.constant.GlobalConstant.SHOW;
|
||||
|
||||
@Slf4j
|
||||
@Tag(name = "树形结构管理")
|
||||
@RestController
|
||||
@RequestMapping("/source")
|
||||
public class SourceController {
|
||||
@Resource
|
||||
private SourceService sourceService;
|
||||
@Resource
|
||||
private UserService userService;
|
||||
@Resource
|
||||
private RoleSourceService roleSourceService;
|
||||
@Resource
|
||||
private SourceParamsValidator sourceParamsValidator;
|
||||
|
||||
@PostMapping("/addDirectory")
|
||||
@Operation(summary = "新增目录资源")
|
||||
public ApiResponse addDirectory(@RequestBody AddDirectoryDto addDirectoryDto) {
|
||||
// 校验是否通过
|
||||
String message = sourceService.checkIsPass(addDirectoryDto.getParentId(), addDirectoryDto.getSourceName());
|
||||
if (message != null) {
|
||||
return ApiResponse.failure(message);
|
||||
}
|
||||
// 通过之后保存资源
|
||||
Source source = new Source();
|
||||
BeanUtils.copyProperties(addDirectoryDto, source);
|
||||
source.setSourceType(DIRECTORY);
|
||||
source.setIsShow(SHOW);
|
||||
sourceService.save(source);
|
||||
// 添加资源到该用户的角色下
|
||||
roleSourceService.addRoleSource(userService.getById(StpUtil.getLoginIdAsString()).getRoleId(), source.getId());
|
||||
return ApiResponse.success(source);
|
||||
}
|
||||
|
||||
@Operation(summary = "新增模型资源")
|
||||
@PostMapping("/addModelSource")
|
||||
public ApiResponse addModelSource(@RequestBody AddModelSourceDto addModelSourceDto) {
|
||||
// 获取资源绝对路径
|
||||
String sourcePath = addModelSourceDto.getSourcePath();
|
||||
// 获取资源名称
|
||||
String sourceName = FileUtil.mainName(sourcePath);
|
||||
// 校验是否通过
|
||||
String message = sourceService.checkIsPass(addModelSourceDto.getParentId(), sourceName);
|
||||
if (message != null) {
|
||||
return ApiResponse.failure(message);
|
||||
}
|
||||
// 调用SDK加载资源
|
||||
String sourceId = sourceService.addAndGetSourceId(sourcePath);
|
||||
// 获取文件路径并处理详情
|
||||
String detail = sourceService.getDetail(sourcePath, sourceId);
|
||||
// 构建并保存资源对象
|
||||
Source source = new Source();
|
||||
source.setSourcePath(sourcePath);
|
||||
source.setSourceName(sourceName);
|
||||
source.setParentId(addModelSourceDto.getParentId());
|
||||
source.setTreeIndex(addModelSourceDto.getTreeIndex());
|
||||
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());
|
||||
return ApiResponse.success(source);
|
||||
}
|
||||
|
||||
|
||||
@Operation(summary = "新增其他资源")
|
||||
@PostMapping("/addOtherSource")
|
||||
public ApiResponse addOtherSource(@RequestBody AddOtherSourceDto addOtherSourceDto) throws JsonProcessingException {
|
||||
// 校验是否通过
|
||||
String message = sourceService.checkIsPass(addOtherSourceDto.getParentId(), addOtherSourceDto.getSourceName());
|
||||
if (message != null) {
|
||||
return ApiResponse.failure(message);
|
||||
}
|
||||
// 验证并转换参数
|
||||
Object validatedParams = sourceParamsValidator.validateAndConvert(
|
||||
addOtherSourceDto.getSourceType(),
|
||||
addOtherSourceDto.getParams()
|
||||
);
|
||||
System.out.println(validatedParams);
|
||||
Source source = new Source();
|
||||
BeanUtils.copyProperties(addOtherSourceDto, source);
|
||||
source.setIsShow(SHOW);
|
||||
source.setParams(MapUtil.objectToJson(validatedParams));
|
||||
sourceService.save(source);
|
||||
// 添加资源到该用户的角色下
|
||||
roleSourceService.addRoleSource(userService.getById(StpUtil.getLoginIdAsString()).getRoleId(), source.getId());
|
||||
return ApiResponse.success(source);
|
||||
}
|
||||
|
||||
|
||||
@Operation(summary = "更新资源信息及参数")
|
||||
@PostMapping("/update")
|
||||
public ApiResponse updateSource(@RequestBody UpdateSourceDto updateSourceDto) {
|
||||
// 查询资源
|
||||
Source source = sourceService.getById(updateSourceDto.getId());
|
||||
if (source == null) {
|
||||
return ApiResponse.failure("资源不存在");
|
||||
}
|
||||
|
||||
// 更新基本信息
|
||||
BeanUtils.copyProperties(updateSourceDto, source);
|
||||
|
||||
// 处理参数更新
|
||||
if (updateSourceDto.getParams() != null && !updateSourceDto.getParams().isEmpty()) {
|
||||
// 获取类型
|
||||
String sourceType = source.getSourceType();
|
||||
// 验证参数
|
||||
Object validatedParams = sourceParamsValidator.validateAndConvert(
|
||||
sourceType,
|
||||
updateSourceDto.getParams()
|
||||
);
|
||||
|
||||
// 获取原始数据的 Map 并合并新参数
|
||||
Map<String, Object> dataMap = MapUtil.jsonToMap(source.getParams());
|
||||
MapUtil.mergeMaps(dataMap, updateSourceDto.getParams());
|
||||
source.setParams(MapUtil.mapToString(dataMap));
|
||||
}
|
||||
|
||||
// 保存更新
|
||||
sourceService.updateById(source);
|
||||
return ApiResponse.success(source);
|
||||
}
|
||||
|
||||
@GetMapping("/type")
|
||||
@Operation(summary = "获取支持的资源类型")
|
||||
public ApiResponse getSupportedSourceTypes() {
|
||||
Set<String> supportedTypes = sourceParamsValidator.getSupportedSourceTypes();
|
||||
return ApiResponse.success(supportedTypes);
|
||||
}
|
||||
|
||||
|
||||
@Operation(summary = "获取资源列表")
|
||||
@GetMapping("/list")
|
||||
public ApiResponse list() {
|
||||
return ApiResponse.success(sourceService.getSourceListByUserId(StpUtil.getLoginIdAsString()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user