liujie
2026-09-11 4f6f15b818704079ff2e4e4719b798f48d68a335
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
package com.tourguide.api.admin;
 
import com.tourguide.common.api.ApiResponse;
import com.tourguide.common.api.PageResponse;
import com.tourguide.modules.system.dto.PermissionModuleResponse;
import com.tourguide.modules.system.dto.RoleDetailResponse;
import com.tourguide.modules.system.dto.RoleResponse;
import com.tourguide.modules.system.dto.RoleSaveRequest;
import com.tourguide.modules.system.service.SystemRoleService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.util.List;
 
/**
 * 管理后台角色与菜单权限管理接口。
 * 维护角色资料及菜单授权关系,并保护内置超级管理员角色不被误删除。
 */
@RestController
@RequestMapping("/api/admin/roles")
public class AdminSystemRoleController {
    private final SystemRoleService roleService;
 
    public AdminSystemRoleController(SystemRoleService roleService) {
        this.roleService = roleService;
    }
 
    /**
     * 分页查询后台角色,支持按角色名称或编码关键字筛选。
     *
     * @param page 页码,从 1 开始
     * @param size 每页记录数
     * @param keyword 角色名称或角色编码关键字
     * @return 角色分页结果及关联账号数量
     */
    @GetMapping
    public ApiResponse<PageResponse<RoleResponse>> page(@RequestParam(defaultValue = "1") long page,
                                                        @RequestParam(defaultValue = "10") long size,
                                                        @RequestParam(required = false) String keyword) {
        return ApiResponse.success(roleService.page(page, size, keyword));
    }
 
    /**
     * 查询可分配给角色的菜单权限树,供新增和编辑角色页面选择。
     *
     * @return 按模块组织的菜单权限列表
     */
    @GetMapping("/permissions")
    public ApiResponse<List<PermissionModuleResponse>> permissions() {
        return ApiResponse.success(roleService.permissions());
    }
 
    /**
     * 查询指定角色资料及已授权菜单标识。
     *
     * @param id 角色主键
     * @return 角色完整详情
     */
    @GetMapping("/{id}")
    public ApiResponse<RoleDetailResponse> detail(@PathVariable Long id) {
        return ApiResponse.success(roleService.detail(id));
    }
 
    /**
     * 新增自定义角色并建立菜单授权关系,角色编码由系统自动生成。
     *
     * @param request 角色名称、备注和菜单主键集合
     * @return 新建后的角色详情
     */
    @PostMapping
    public ApiResponse<RoleDetailResponse> create(@Valid @RequestBody RoleSaveRequest request) {
        return ApiResponse.success(roleService.create(request));
    }
 
    /**
     * 更新指定角色资料并整体替换菜单授权关系。
     *
     * @param id 角色主键
     * @param request 最新角色名称、备注和菜单主键集合
     * @return 更新后的角色详情
     */
    @PutMapping("/{id}")
    public ApiResponse<RoleDetailResponse> update(@PathVariable Long id,
                                                  @Valid @RequestBody RoleSaveRequest request) {
        return ApiResponse.success(roleService.update(id, request));
    }
 
    /**
     * 删除未被后台账号使用的自定义角色及其菜单关系。
     * 内置 ADMIN 角色或仍有关联账号的角色不能删除。
     *
     * @param id 角色主键
     * @return 无业务数据的成功响应
     */
    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable Long id) {
        roleService.delete(id);
        return ApiResponse.success();
    }
}