云
2026-09-29 f87ce86307056f63f79312178037cc8714eb2b13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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;
    }
 
}