资源相关
This commit is contained in:
		| @ -2,19 +2,19 @@ package com.yj.earth.business.controller; | ||||
|  | ||||
| import cn.dev33.satoken.stp.StpUtil; | ||||
| import cn.hutool.core.io.FileUtil; | ||||
| import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | ||||
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | ||||
| 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.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.AddDirectoryDto; | ||||
| import com.yj.earth.dto.source.AddModelSourceDto; | ||||
| import com.yj.earth.dto.source.AddOtherSourceDto; | ||||
| import com.yj.earth.dto.source.UpdateSourceDto; | ||||
| import com.yj.earth.dto.source.*; | ||||
| import io.swagger.v3.oas.annotations.Operation; | ||||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||||
| import lombok.extern.slf4j.Slf4j; | ||||
| @ -22,8 +22,7 @@ import org.springframework.beans.BeanUtils; | ||||
| import org.springframework.web.bind.annotation.*; | ||||
|  | ||||
| import javax.annotation.Resource; | ||||
| import java.util.Map; | ||||
| import java.util.Set; | ||||
| import java.util.*; | ||||
|  | ||||
| import static com.yj.earth.common.constant.GlobalConstant.DIRECTORY; | ||||
| import static com.yj.earth.common.constant.GlobalConstant.SHOW; | ||||
| @ -42,6 +41,9 @@ public class SourceController { | ||||
|     @Resource | ||||
|     private SourceParamsValidator sourceParamsValidator; | ||||
|  | ||||
|     @Resource | ||||
|     private SourceDataGenerator sourceDataGenerator; | ||||
|  | ||||
|     @PostMapping("/addDirectory") | ||||
|     @Operation(summary = "新增目录资源") | ||||
|     public ApiResponse addDirectory(@RequestBody AddDirectoryDto addDirectoryDto) { | ||||
| @ -79,6 +81,7 @@ public class SourceController { | ||||
|         String detail = sourceService.getDetail(sourcePath, sourceId); | ||||
|         // 构建并保存资源对象 | ||||
|         Source source = new Source(); | ||||
|         source.setId(addModelSourceDto.getId()); | ||||
|         source.setSourcePath(sourcePath); | ||||
|         source.setSourceName(sourceName); | ||||
|         source.setParentId(addModelSourceDto.getParentId()); | ||||
| @ -107,7 +110,6 @@ public class SourceController { | ||||
|                 addOtherSourceDto.getSourceType(), | ||||
|                 addOtherSourceDto.getParams() | ||||
|         ); | ||||
|         System.out.println(validatedParams); | ||||
|         Source source = new Source(); | ||||
|         BeanUtils.copyProperties(addOtherSourceDto, source); | ||||
|         source.setIsShow(SHOW); | ||||
| @ -119,7 +121,7 @@ public class SourceController { | ||||
|     } | ||||
|  | ||||
|  | ||||
|     @Operation(summary = "更新资源信息及参数") | ||||
|     @Operation(summary = "更新资源信息") | ||||
|     @PostMapping("/update") | ||||
|     public ApiResponse updateSource(@RequestBody UpdateSourceDto updateSourceDto) { | ||||
|         // 查询资源 | ||||
| @ -127,10 +129,8 @@ public class SourceController { | ||||
|         if (source == null) { | ||||
|             return ApiResponse.failure("资源不存在"); | ||||
|         } | ||||
|  | ||||
|         // 更新基本信息 | ||||
|         BeanUtils.copyProperties(updateSourceDto, source); | ||||
|  | ||||
|         // 处理参数更新 | ||||
|         if (updateSourceDto.getParams() != null && !updateSourceDto.getParams().isEmpty()) { | ||||
|             // 获取类型 | ||||
| @ -140,29 +140,72 @@ public class SourceController { | ||||
|                     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())); | ||||
|     } | ||||
|  | ||||
|     @Operation(summary = "删除资源数据") | ||||
|     @PostMapping("/delete") | ||||
|     public ApiResponse delete(@RequestBody DeleteSourceDto deleteSourceDto) { | ||||
|         if (deleteSourceDto.getIds() == null || deleteSourceDto.getIds().isEmpty()) { | ||||
|             return ApiResponse.failure("资源ID列表不能为空"); | ||||
|         } | ||||
|  | ||||
|         // 收集所有需要删除的资源ID(包括子资源) | ||||
|         Set<String> allIds = new HashSet<>(); | ||||
|  | ||||
|         for (String id : deleteSourceDto.getIds()) { | ||||
|             // 获取资源 | ||||
|             Source source = sourceService.getById(id); | ||||
|             if (source == null) { | ||||
|                 return ApiResponse.failure("资源ID为" + id + "的资源不存在"); | ||||
|             } | ||||
|  | ||||
|             // 查询ID等于自身或者父级ID等于自身的资源 | ||||
|             LambdaQueryWrapper<Source> queryWrapper = new LambdaQueryWrapper<>(); | ||||
|             queryWrapper.eq(Source::getId, source.getId()) | ||||
|                     .or() | ||||
|                     .eq(Source::getParentId, source.getId()); | ||||
|             List<Source> list = sourceService.list(queryWrapper); | ||||
|  | ||||
|             // 筛选ID列表并添加到总集合中 | ||||
|             list.stream().map(Source::getId).forEach(allIds::add); | ||||
|         } | ||||
|  | ||||
|         // 删除这些资源 | ||||
|         sourceService.removeByIds(allIds); | ||||
|  | ||||
|         // 删除这些资源下的所有角色资源关系 | ||||
|         roleSourceService.deleteRoleSource( | ||||
|                 userService.getById(StpUtil.getLoginIdAsString()).getRoleId(), | ||||
|                 new ArrayList<>(allIds) | ||||
|         ); | ||||
|  | ||||
|         return ApiResponse.success(null); | ||||
|     } | ||||
|  | ||||
|     @GetMapping("/type") | ||||
|     @Operation(summary = "获取已有类型") | ||||
|     public ApiResponse getSupportedSourceTypes() { | ||||
|         Set<String> supportedTypes = sourceParamsValidator.getSupportedSourceTypes(); | ||||
|         return ApiResponse.success(supportedTypes); | ||||
|     } | ||||
|  | ||||
|     @Operation(summary = "获取示例数据") | ||||
|     @GetMapping("/paramsExample") | ||||
|     public String getExampleData(String sourceType) throws JsonProcessingException { | ||||
|         return sourceDataGenerator.generateDefaultJson(sourceType); | ||||
|     } | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 ZZX9599
					ZZX9599