潘志宝
2024-12-16 df99e46312fdd5ee830f1451e478f6658e09f9ed
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.system.controller.admin.permission;
H 2
3 import com.iailab.framework.apilog.core.annotation.ApiAccessLog;
4 import com.iailab.framework.common.enums.CommonStatusEnum;
5 import com.iailab.framework.common.pojo.CommonResult;
6 import com.iailab.framework.common.pojo.PageParam;
7 import com.iailab.framework.common.pojo.PageResult;
8 import com.iailab.framework.common.util.object.BeanUtils;
9 import com.iailab.framework.excel.core.util.ExcelUtils;
10 import com.iailab.module.system.controller.admin.permission.vo.role.*;
11 import com.iailab.module.system.dal.dataobject.permission.RoleDO;
12 import com.iailab.module.system.service.permission.RoleService;
13 import io.swagger.v3.oas.annotations.Operation;
14 import io.swagger.v3.oas.annotations.Parameter;
15 import io.swagger.v3.oas.annotations.tags.Tag;
16 import org.springframework.security.access.prepost.PreAuthorize;
17 import org.springframework.validation.annotation.Validated;
18 import org.springframework.web.bind.annotation.*;
19
20 import javax.annotation.Resource;
21 import javax.servlet.http.HttpServletResponse;
22 import javax.validation.Valid;
23 import java.io.IOException;
24 import java.util.Comparator;
25 import java.util.List;
26
27 import static com.iailab.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
28 import static com.iailab.framework.common.pojo.CommonResult.success;
29 import static java.util.Collections.singleton;
30
31 @Tag(name = "管理后台 - 角色")
32 @RestController
33 @RequestMapping("/system/role")
34 @Validated
35 public class RoleController {
36
37     @Resource
38     private RoleService roleService;
39
40     @PostMapping("/create")
41     @Operation(summary = "创建角色")
42     @PreAuthorize("@ss.hasPermission('system:role:create')")
43     public CommonResult<Long> createRole(@Valid @RequestBody RoleSaveReqVO createReqVO) {
44         return success(roleService.createRole(createReqVO, null));
45     }
46
47     @PutMapping("/update")
48     @Operation(summary = "修改角色")
49     @PreAuthorize("@ss.hasPermission('system:role:update')")
50     public CommonResult<Boolean> updateRole(@Valid @RequestBody RoleSaveReqVO updateReqVO) {
51         roleService.updateRole(updateReqVO);
52         return success(true);
53     }
54
55     @DeleteMapping("/delete")
56     @Operation(summary = "删除角色")
57     @Parameter(name = "id", description = "角色编号", required = true, example = "1024")
58     @PreAuthorize("@ss.hasPermission('system:role:delete')")
59     public CommonResult<Boolean> deleteRole(@RequestParam("id") Long id) {
60         roleService.deleteRole(id);
61         return success(true);
62     }
63
64     @GetMapping("/get")
65     @Operation(summary = "获得角色信息")
66     @PreAuthorize("@ss.hasPermission('system:role:query')")
67     public CommonResult<RoleRespVO> getRole(@RequestParam("id") Long id) {
68         RoleDO role = roleService.getRole(id);
69         return success(BeanUtils.toBean(role, RoleRespVO.class));
70     }
71
72     @GetMapping("/page")
73     @Operation(summary = "获得角色分页")
74     @PreAuthorize("@ss.hasPermission('system:role:query')")
75     public CommonResult<PageResult<RoleRespVO>> getRolePage(RolePageReqVO pageReqVO) {
76         PageResult<RoleDO> pageResult = roleService.getRolePage(pageReqVO);
77         return success(BeanUtils.toBean(pageResult, RoleRespVO.class));
78     }
79
80     @GetMapping({"/list-all-simple", "/simple-list"})
81     @Operation(summary = "获取角色精简信息列表", description = "只包含被开启的角色,主要用于前端的下拉选项")
82     public CommonResult<List<RoleRespVO>> getSimpleRoleList() {
83         List<RoleDO> list = roleService.getRoleListByStatus(singleton(CommonStatusEnum.ENABLE.getStatus()));
84         list.sort(Comparator.comparing(RoleDO::getSort));
85         return success(BeanUtils.toBean(list, RoleRespVO.class));
86     }
87
88     @GetMapping("/export-excel")
89     @Operation(summary = "导出角色 Excel")
90     @ApiAccessLog(operateType = EXPORT)
91     @PreAuthorize("@ss.hasPermission('system:role:export')")
92     public void export(HttpServletResponse response, @Validated RolePageReqVO exportReqVO) throws IOException {
93         exportReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
94         List<RoleDO> list = roleService.getRolePage(exportReqVO).getList();
95         // 输出
96         ExcelUtils.write(response, "角色数据.xls", "数据", RoleRespVO.class,
97                 BeanUtils.toBean(list, RoleRespVO.class));
98     }
99
100 }