无关风月
2024-12-09 2053b8fe0e98d4b4449bc756a93ced78f42277c4
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package com.jilongda.manage.authority.controller;
 
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jilongda.manage.authority.model.*;
import com.jilongda.common.basic.ApiResult;
import com.jilongda.common.basic.PageInfo;
import com.jilongda.common.log.OperationLog;
import com.jilongda.manage.authority.dto.SecRoleDTO;
import com.jilongda.manage.authority.dto.SecRolesDTO;
import com.jilongda.manage.authority.dto.SecUserQueryDTO;
import com.jilongda.manage.authority.mapper.SecRoleResourceMapper;
import com.jilongda.manage.authority.mapper.SecUserRoleMapper;
import com.jilongda.manage.authority.service.SecResourcesService;
import com.jilongda.manage.authority.service.SecRoleService;
import com.jilongda.manage.authority.service.SecUserService;
import com.jilongda.manage.authority.vo.SecResourceVO;
import com.jilongda.manage.authority.vo.SecUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2022-06-08
 */
@Api(tags = "角色")
@RestController
@RequestMapping("/sec-role")
public class SecRoleController {
 
    private final SecRoleService secRoleService;
    private final SecResourcesService secResourcesService;
    private final SecUserRoleMapper secUserRoleMapper;
    private final SecRoleResourceMapper secRoleResourceMapper;
    private final SecUserService secUserService;
 
    @Autowired
    public SecRoleController(SecRoleService secRoleService, SecResourcesService secResourcesService, SecUserRoleMapper secUserRoleMapper, SecRoleResourceMapper secRoleResourceMapper, SecUserService secUserService) {
        this.secRoleService = secRoleService;
        this.secResourcesService = secResourcesService;
        this.secUserRoleMapper = secUserRoleMapper;
        this.secRoleResourceMapper = secRoleResourceMapper;
        this.secUserService = secUserService;
    }
 
 
    @ApiOperation(value = "获取角色权限(包含菜单及功能)", notes = "获取角色权限(包含菜单及功能),传递角色id即可")
    @GetMapping(value = "resource/{rid}")
    public ApiResult<Map<String, Object>> roleResource(@PathVariable Long rid) {
        Map<String, Object> map = new HashMap<>(2);
        SecRole secRole = secRoleService.getById(rid);
        map.put("roleInfo", secRole);
        List<SecResourceVO> sysResourceVos = secRoleService.selectRoleResourcesByRid(rid);
//        List<SecResourceVO> sysResourceVos = secRoleService.getRecursion(rid);
        map.put("resourceInfo", sysResourceVos);
        return ApiResult.success(map);
    }
 
    @ApiOperation(value = "获取角色权限---无层级", notes = "获取角色权限无层级,传递角色id即可")
    @GetMapping(value = "resource/nolevel/{rid}")
    public ApiResult<List<SecResources>> roleResourceNolevel(@PathVariable Long rid) {
        List<SecRoleResource> secRoleResources = secRoleResourceMapper.selectList(Wrappers.lambdaQuery(SecRoleResource.class).eq(SecRoleResource::getRoleId, rid));
        List<SecResources> secResources = new ArrayList<>();
        if (!CollectionUtils.isEmpty(secRoleResources)) {
            List<Long> resIds = secRoleResources.stream().map(SecRoleResource::getResourceId).collect(Collectors.toList());
            secResources = secResourcesService.list(Wrappers.lambdaQuery(SecResources.class).in(SecResources::getId, resIds));
        }
        return ApiResult.success(secResources);
    }
 
 
    @ApiOperation(value = "通过角色查找人员")
    @PostMapping("getUserByRole")
    public ApiResult<PageInfo<SecUserVO>> getUserByRole(@RequestBody SecUserQueryDTO dto) {
        PageInfo<SecUserVO> secUserVOS = secRoleService.getUserByRole(dto);
        return ApiResult.success(secUserVOS);
    }
 
 
    @ApiOperation("获取系统角色列表-含分页")
    @PostMapping("getSecRoleList")
    public ApiResult<PageInfo<SecRole>> getSecRoleList(@RequestBody SecRolesDTO dto) {
        PageInfo<SecRole> secRolePage = new PageInfo<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<SecRole> qw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getRolename())) {
            qw.like(SecRole::getRolename, dto.getRolename());
        }
        if (Objects.nonNull(dto.getRoleState())) {
            qw.eq(SecRole::getRoleState, dto.getRoleState());
        }
        PageInfo<SecRole> secRoleIPage = secRoleService.page(secRolePage, qw);
        List<SecRole> records = secRoleIPage.getRecords();
        records.stream().forEach(record -> {
//            List<SecUserRole> secUserRoles = secUserRoleMapper.selectList(Wrappers.lambdaQuery(SecUserRole.class).eq(SecUserRole::getRoleId, record.getId()));
            ApiResult<List<SecResources>> listApiResult = this.roleResourceNolevel(record.getId());
            List<SecResources> data = listApiResult.getData();
            record.setSecResourceVOS(data);
            List<SecUser> secUsers = secUserService.getUserByRoleId(record.getId());
            record.setSecUsers(secUsers);
        });
        return ApiResult.success(secRoleIPage);
    }
 
    @ApiOperation("获取权限角色字典")
    @GetMapping("getSecRoles")
    public ApiResult<List<SecRole>> getSecRole() {
        List<SecRole> secRoles = secRoleService.list();
        return ApiResult.success(secRoles);
    }
 
    @OperationLog(operType = "新增或修改", operDesc = "新增或修改系统角色", operModul = "角色")
    @ApiOperation("新增或修改系统角色")
    @PostMapping("addOrUpdate")
    @Transactional(rollbackFor = Exception.class)
    public ApiResult<String> addOrUpdateSecRole(@Validated @RequestBody SecRoleDTO dto) {
        if (org.springframework.util.CollectionUtils.isEmpty(dto.getResourceIds())) {
            return ApiResult.failed("请为角色分配资源");
        }
        SecRole secRole = secRoleService.getOne(Wrappers.lambdaQuery(SecRole.class).eq(SecRole::getRolename, dto.getRolename()));
        if (Objects.nonNull(dto.getId())) {
            // 系统管理员不可修改
//            SecRole byId = secRoleService.getById(dto.getId());
//            Assert.isTrue(!"系统管理员".equals(byId.getRolename()),"系统管理员无法修改");
            if (Objects.isNull(secRole) || (Objects.nonNull(secRole) && dto.getId().equals(secRole.getId()))) {
                secRoleService.updateById(dto);
                // 删除之前的所有功能列表,重新添加
                secRoleResourceMapper.delete(Wrappers.lambdaQuery(SecRoleResource.class).eq(SecRoleResource::getRoleId, dto.getId()));
            } else {
                return ApiResult.failed("角色名已存在");
            }
        } else {
            // 判断名称是否存在
            if (Objects.nonNull(secRole)) {
                return ApiResult.failed("角色名已存在");
            }
            secRoleService.save(dto);
        }
        // 向角色添加所拥有的资源
        SecRoleResource secRoleResource = new SecRoleResource();
        for (Long resourceId : dto.getResourceIds()) {
            secRoleResource.setResourceId(resourceId);
            secRoleResource.setRoleId(dto.getId());
            secRoleResourceMapper.insert(secRoleResource);
        }
        return ApiResult.success();
    }
 
    @OperationLog(operType = "启用禁用", operDesc = "系统角色启用禁用", operModul = "角色")
    @ApiOperation("系统角色启用禁用  state(0,1) 非0 则为禁用")
    @PutMapping("state/{id}/{state}")
    public ApiResult<String> state(@PathVariable("id") Long id, @PathVariable("state") Boolean state) {
        SecRole role = new SecRole();
        role.setId(id);
        role.setRoleState(state);
        secRoleService.updateById(role);
        return ApiResult.success();
    }
 
    @OperationLog(operType = "删除", operDesc = "删除角色", operModul = "角色")
    @ApiOperation("删除角色")
    @DeleteMapping("delRole/{roleId}")
    public ApiResult<String> delRole(@PathVariable Long roleId) {
        SecRole secRole = secRoleService.getById(roleId);
        Assert.isTrue(!"超级管理员".equals(secRole.getRolename()), "超级管理员角色无法删除");
        Long count = secUserRoleMapper.selectCount(Wrappers.lambdaQuery(SecUserRole.class).eq(SecUserRole::getRoleId, roleId));
        Assert.isTrue(count==0, "该角色已关联账号,无法删除!");
        secRoleService.removeById(roleId);
        return ApiResult.success();
    }
 
    @OperationLog(operType = "批量删除", operDesc = "删除角色", operModul = "角色")
    @ApiOperation("批量删除角色")
    @DeleteMapping("delRoleByIds")
    public ApiResult<String> delRoleByIds(@RequestBody List<Long> roleIds) {
        List<SecRole> list = secRoleService.list(Wrappers.lambdaQuery(SecRole.class)
                .in(SecRole::getId,roleIds));
        list.forEach(secRole -> {
            Assert.isTrue(!"超级管理员".equals(secRole.getRolename()), "超级管理员角色无法删除");
        });
        Long count = secUserRoleMapper.selectCount(Wrappers.lambdaQuery(SecUserRole.class)
                .in(SecUserRole::getRoleId, roleIds));
        Assert.isTrue(count==0, "该角色已关联账号,无法删除!");
        secRoleService.remove(Wrappers.lambdaQuery(SecRole.class)
                .in(SecRole::getId,roleIds));
        return ApiResult.success();
    }
 
}