package cn.iocoder.yudao.module.archive.controller.admin.category;
|
|
import cn.hutool.core.collection.CollUtil;
|
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
import cn.iocoder.yudao.module.archive.controller.admin.category.vo.ArchiveCategoryListReqVO;
|
import cn.iocoder.yudao.module.archive.controller.admin.category.vo.ArchiveCategoryRespVO;
|
import cn.iocoder.yudao.module.archive.controller.admin.category.vo.ArchiveCategorySaveReqVO;
|
import cn.iocoder.yudao.module.archive.dal.dataobject.category.ArchiveCategoryDO;
|
import cn.iocoder.yudao.module.archive.service.category.ArchiveCategoryService;
|
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
import jakarta.annotation.Resource;
|
import jakarta.validation.Valid;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.validation.annotation.Validated;
|
import org.springframework.web.bind.annotation.*;
|
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.LinkedHashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
|
@Tag(name = "管理后台 - 档案分类")
|
@RestController
|
@RequestMapping("/archive/category")
|
@Validated
|
public class ArchiveCategoryController {
|
|
/** 顶级分类的父分类编号 */
|
private static final Long ROOT_PARENT_ID = 0L;
|
|
@Resource
|
private ArchiveCategoryService categoryService;
|
|
@PostMapping("/create")
|
@Operation(summary = "创建档案分类")
|
@PreAuthorize("@ss.hasPermission('archive:category:create')")
|
public CommonResult<Long> createCategory(@Valid @RequestBody ArchiveCategorySaveReqVO createReqVO) {
|
return success(categoryService.createCategory(createReqVO));
|
}
|
|
@PutMapping("/update")
|
@Operation(summary = "更新档案分类")
|
@PreAuthorize("@ss.hasPermission('archive:category:update')")
|
public CommonResult<Boolean> updateCategory(@Valid @RequestBody ArchiveCategorySaveReqVO updateReqVO) {
|
categoryService.updateCategory(updateReqVO);
|
return success(true);
|
}
|
|
@DeleteMapping("/delete")
|
@Operation(summary = "删除档案分类")
|
@Parameter(name = "id", description = "编号", required = true)
|
@PreAuthorize("@ss.hasPermission('archive:category:delete')")
|
public CommonResult<Boolean> deleteCategory(@RequestParam("id") Long id) {
|
categoryService.deleteCategory(id);
|
return success(true);
|
}
|
|
@GetMapping("/get")
|
@Operation(summary = "获得档案分类")
|
@Parameter(name = "id", description = "编号", required = true, example = "1024")
|
@PreAuthorize("@ss.hasPermission('archive:category:query')")
|
public CommonResult<ArchiveCategoryRespVO> getCategory(@RequestParam("id") Long id) {
|
ArchiveCategoryDO category = categoryService.getCategory(id);
|
return success(BeanUtils.toBean(category, ArchiveCategoryRespVO.class));
|
}
|
|
@GetMapping("/list")
|
@Operation(summary = "获得档案分类列表(不分页)")
|
@PreAuthorize("@ss.hasPermission('archive:category:query')")
|
public CommonResult<List<ArchiveCategoryRespVO>> getCategoryList(@Valid ArchiveCategoryListReqVO reqVO) {
|
return success(BeanUtils.toBean(categoryService.getCategoryList(reqVO), ArchiveCategoryRespVO.class));
|
}
|
|
@GetMapping("/tree")
|
@Operation(summary = "获得档案分类树(供左树右表与档案分类下拉使用)")
|
@PreAuthorize("@ss.hasPermission('archive:category:query')")
|
public CommonResult<List<ArchiveCategoryRespVO>> getCategoryTree() {
|
return success(buildTree(BeanUtils.toBean(categoryService.getCategoryList(), ArchiveCategoryRespVO.class)));
|
}
|
|
// ==================== 内部方法 ====================
|
|
private List<ArchiveCategoryRespVO> buildTree(List<ArchiveCategoryRespVO> categoryList) {
|
if (CollUtil.isEmpty(categoryList)) {
|
return Collections.emptyList();
|
}
|
// 1. 建立「编号 -> 节点」索引,并初始化 children
|
Map<Long, ArchiveCategoryRespVO> nodeMap = new LinkedHashMap<>();
|
for (ArchiveCategoryRespVO category : categoryList) {
|
category.setChildren(new ArrayList<>());
|
nodeMap.put(category.getId(), category);
|
}
|
// 2. 挂载子节点;父节点不存在时视为顶级,避免分类被隐藏
|
List<ArchiveCategoryRespVO> roots = new ArrayList<>();
|
for (ArchiveCategoryRespVO category : categoryList) {
|
ArchiveCategoryRespVO parent = nodeMap.get(category.getParentId());
|
if (parent == null || ROOT_PARENT_ID.equals(category.getParentId())) {
|
roots.add(category);
|
} else {
|
parent.getChildren().add(category);
|
}
|
}
|
return roots;
|
}
|
|
}
|