rentaiming
2024-07-30 5667d14c708f9244fe2419707f98ff472ca23480
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package com.ruoyi.system.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.api.domain.SysRole;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.SysMenus;
import com.ruoyi.system.domain.SysRoleMenu;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.domain.dto.RoleAddDto;
import com.ruoyi.system.domain.dto.RoleQuery;
import com.ruoyi.system.domain.dto.RoleUpdateDto;
import com.ruoyi.system.domain.vo.RoleInfoVo;
import com.ruoyi.system.mapper.SysMenuMapper;
import com.ruoyi.system.mapper.SysRoleMenuMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserRoleService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
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;
 
/**
 * 角色信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色模块")
public class SysRoleController extends BaseController
{
    @Autowired
    private ISysRoleService roleService;
 
    @Autowired
    private ISysUserService userService;
 
    @Autowired
    private ISysDeptService deptService;
 
    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;
 
    @Autowired
    private ISysUserRoleService sysUserRoleService;
 
 
    @Resource
    private SysMenuMapper menuMapper;
 
    // @ApiOperation("获取所有角色信息根据公司id")
    @GetMapping("/list")
    @ApiOperation("获取不分页角色列表")
    public AjaxResult list()
    {
        SysRole role = new SysRole();
        List<SysRole> list = roleService.selectRoleList(role);
        return AjaxResult.success(list);
    }
 
 
    @ApiOperation("获取分页角色列表")
    @PostMapping("/listPage")
    public AjaxResult listPage(@Validated @RequestBody RoleQuery query)
    {
        PageInfo<SysRole> pageInfo = new PageInfo<>(query.getPageCurr(), query.getPageSize());
 
        PageInfo<SysRole> page = roleService.page(pageInfo,
                new LambdaQueryWrapper<SysRole>().ne(SysRole::getRoleId, 20)
                        .like(StringUtils.hasLength(query.getRoleName()), SysRole::getRoleName,
                                query.getRoleName()).eq(SysRole::getDelFlag, "0"));
        return AjaxResult.success(page);
    }
 
 
    // @ApiOperation("角色启用停用")
    @GetMapping("/roleStart")
    public AjaxResult roleStart(Long roleId)
    {
        SysRole role = roleService.selectRoleById(roleId);
        if(role.getStatus().equals("1")){
            role.setStatus("0");
        }else {
            role.setStatus("1");
        }
        return AjaxResult.success(roleService.updateRole(role));
    }
 
 
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @ApiOperation("添加角色")
    @PostMapping("/roleAdd")
    public AjaxResult roleAdd(@Validated @RequestBody RoleAddDto dto)
    {
        SysRole role = new SysRole();
        role.setRoleName(dto.getRoleName());
        long count = roleService.count(Wrappers.lambdaQuery(SysRole.class)
                .eq(SysRole::getRoleName, dto.getRoleName()));
        if(count>0){
            return AjaxResult.error("角色已存在,请重新输入");
        }
        List<Long> menuIds1 = dto.getMenuIds();
        if(CollectionUtils.isEmpty(menuIds1)){
            return AjaxResult.error("菜单id不能为空");
        }
        role.setMenuIds(dto.getMenuIds().toArray(new Long[0]));
        // 添加角色
        role.setCreateBy(SecurityUtils.getUsername());
        role.setCreateTime(new Date());
        roleService.insertRole(role);
//        ArrayList<SysRoleMenu> sysRoleMenus = new ArrayList<>();
//        List<Long> menuIds = dto.getMenuIds();
//        for (Long menuId : menuIds) {
//            SysRoleMenu sysRoleMenu = new SysRoleMenu();
//            sysRoleMenu.setMenuId(menuId);
//            sysRoleMenu.setRoleId(role.getRoleId());
//            sysRoleMenus.add(sysRoleMenu);
//        }
//        sysRoleMenuMapper.batchRoleMenu(sysRoleMenus);
        return AjaxResult.success();
    }
 
 
    @ApiOperation("角色详情")
    @GetMapping("/roleInfo/{id}")
    public AjaxResult roleInfo(
            @ApiParam(value = "角色id", name = "id", required = true) @PathVariable("id") Long id)
    {
        SysRole role = roleService.selectRoleById(id);
        RoleInfoVo roleInfoVo = new RoleInfoVo();
        roleInfoVo.setRoleId(role.getRoleId());
        roleInfoVo.setRoleName(role.getRoleName());
        // 获取当前角色的菜单id
        List<Long> menusId = sysRoleMenuMapper.selectList(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, id)).stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
        if(menusId.size()==0){
            return AjaxResult.success(new ArrayList<>());
        }
        //获取当前的权限菜单
        List<SysMenus> all = menuMapper.getAllInIds(menusId);
        // 第三级
        List<SysMenus> s3 = all.stream().filter(e -> e.getMenuType().equals("F")).collect(Collectors.toList());
        // 第二级
        List<SysMenus> s2 = all.stream().filter(e -> e.getMenuType().equals("C")).collect(Collectors.toList());
        // 第一级
        List<SysMenus> s1 = all.stream().filter(e -> e.getMenuType().equals("M")).collect(Collectors.toList());
 
        for (SysMenus menus : s2) {
            List<SysMenus> collect = s3.stream().filter(e -> e.getParentId().equals(menus.getMenuId())).collect(Collectors.toList());
            menus.setChildren(collect);
        }
 
        for (SysMenus menus : s1) {
            List<SysMenus> collect = s2.stream().filter(e -> e.getParentId().equals(menus.getMenuId())).collect(Collectors.toList());
            menus.setChildren(collect);
        }
 
        roleInfoVo.setMenus(menusId);
        return AjaxResult.success(roleInfoVo);
    }
 
 
    @ApiOperation("用户获取权限菜单")
    @GetMapping("/roleInfoFromUserId")
    public AjaxResult roleInfoFromUserId( @RequestParam Long userId)
    {
        SysUserRole one = sysUserRoleService.getOne(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, userId));
        if (Objects.isNull(one)) {
            return AjaxResult.success();
        }
        Long id =one.getRoleId();
        // 获取当前角色的菜单id
        List<Long> menusId = sysRoleMenuMapper.selectList(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, id)).stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
        if(menusId.size()==0){
            return AjaxResult.success(new ArrayList<>());
        }
        //获取当前的权限菜单
        List<SysMenus> allUser = menuMapper.getAllInIds(menusId);
        // 查询所有权限菜单
        List<SysMenus> all = menuMapper.getAll();
        // 创建一个Map来存储all集合中的menuId
        Map<Long, Boolean> menuIdExistMap = allUser.stream()
                .collect(Collectors.toMap(SysMenus::getMenuId, aMenu -> true,
                        (oldValue, newValue) -> oldValue));
 
        // 遍历allUser集合,根据menuIdExistMap来设置isHave值
        for (SysMenus allMenu : all) {
            allMenu.setIsHave(menuIdExistMap.containsKey(allMenu.getMenuId()) ? 1 : 2);
        }
 
        // 第三级
        List<SysMenus> s3 = all.stream().filter(e -> e.getMenuType().equals("F")).collect(Collectors.toList());
        // 第二级
        List<SysMenus> s2 = all.stream().filter(e -> e.getMenuType().equals("C")).collect(Collectors.toList());
        // 第一级
        List<SysMenus> s1 = all.stream().filter(e -> e.getMenuType().equals("M")).collect(Collectors.toList());
 
        for (SysMenus menus : s2) {
            List<SysMenus> collect = s3.stream().filter(e -> e.getParentId().equals(menus.getMenuId())).collect(Collectors.toList());
            menus.setChildren(collect);
        }
 
        for (SysMenus menus : s1) {
            List<SysMenus> collect = s2.stream().filter(e -> e.getParentId().equals(menus.getMenuId())).collect(Collectors.toList());
            menus.setChildren(collect);
        }
 
        return AjaxResult.success(s1);
    }
 
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @ApiOperation("编辑角色")
    @PostMapping("/roleUpdate")
    public AjaxResult roleUpdate(@Validated @RequestBody RoleUpdateDto dto)
    {
        SysRole role = new SysRole();
        role.setRoleName(dto.getRoleName());
        SysRole one = roleService.getOne(new LambdaQueryWrapper<SysRole>().eq(SysRole::getRoleId, dto.getRoleId()));
        List<SysRole> sysRoles = roleService.isExitUpdate(dto.getRoleName(), dto.getRoleId());
        if(sysRoles.size()>0){
            return AjaxResult.error("角色已存在,请重新输入");
        }
        // 编辑角色
        role.setUpdateBy(SecurityUtils.getUsername());
        role.setUpdateTime(new Date());
        role.setRoleId(dto.getRoleId());
        roleService.updateRole(role);
        ArrayList<SysRoleMenu> sysRoleMenus = new ArrayList<>();
        List<Long> menuIds = dto.getMenuIds();
        // 移除原来的权限菜单
        if(menuIds.contains(1061L)){
            sysRoleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>()
                    .eq(SysRoleMenu::getRoleId,dto.getRoleId()));
        }else {
            sysRoleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>()
                    .eq(SysRoleMenu::getRoleId,dto.getRoleId())
                    .ne(SysRoleMenu::getMenuId,1061L)
                    .ne(SysRoleMenu::getMenuId,1062L)
                    .ne(SysRoleMenu::getMenuId,1065L)
                    .ne(SysRoleMenu::getMenuId,1073L)
                    .ne(SysRoleMenu::getMenuId,1161L)
                    .ne(SysRoleMenu::getMenuId,1203L)
            );
        }
        for (Long menuId : menuIds) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setRoleId(role.getRoleId());
            sysRoleMenus.add(sysRoleMenu);
        }
        sysRoleMenuMapper.batchRoleMenu(sysRoleMenus);
        return AjaxResult.success();
    }
 
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @ApiOperation("删除角色")
    @DeleteMapping("/del/{id}")
    public AjaxResult removeRole(
            @ApiParam(name = "id", value = "角色ID", required = true) @PathVariable Long id) {
        roleService.removeRole(id);
        return AjaxResult.success();
    }
    @Log(title = "角色管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:role:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysRole role)
    {
        List<SysRole> list = roleService.selectRoleList(role);
        ExcelUtil<SysRole> util = new ExcelUtil<SysRole>(SysRole.class);
        util.exportExcel(response, list, "角色数据");
    }
 
    /**
     * 根据角色编号获取详细信息
     */
    @RequiresPermissions("system:role:query")
    @GetMapping(value = "/{roleId}")
    public AjaxResult getInfo(@PathVariable Long roleId)
    {
        roleService.checkRoleDataScope(roleId);
        return success(roleService.selectRoleById(roleId));
    }
 
    /**
     * 新增角色
     */
    @RequiresPermissions("system:role:add")
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysRole role)
    {
        if (!roleService.checkRoleNameUnique(role))
        {
            return error("新增角色'" + role.getRoleName() + "'失败,角色名称已存在");
        }
        else if (!roleService.checkRoleKeyUnique(role))
        {
            return error("新增角色'" + role.getRoleName() + "'失败,角色权限已存在");
        }
        role.setCreateBy(SecurityUtils.getUsername());
        return toAjax(roleService.insertRole(role));
 
    }
 
    /**
     * 修改保存角色
     */
    @RequiresPermissions("system:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysRole role)
    {
        roleService.checkRoleAllowed(role);
        roleService.checkRoleDataScope(role.getRoleId());
        if (!roleService.checkRoleNameUnique(role))
        {
            return error("修改角色'" + role.getRoleName() + "'失败,角色名称已存在");
        }
        else if (!roleService.checkRoleKeyUnique(role))
        {
            return error("修改角色'" + role.getRoleName() + "'失败,角色权限已存在");
        }
        role.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(roleService.updateRole(role));
    }
 
    /**
     * 修改保存数据权限
     */
    @RequiresPermissions("system:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping("/dataScope")
    public AjaxResult dataScope(@RequestBody SysRole role)
    {
        roleService.checkRoleAllowed(role);
        roleService.checkRoleDataScope(role.getRoleId());
        return toAjax(roleService.authDataScope(role));
    }
 
    /**
     * 状态修改
     */
    @RequiresPermissions("system:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysRole role)
    {
        roleService.checkRoleAllowed(role);
        roleService.checkRoleDataScope(role.getRoleId());
        role.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(roleService.updateRoleStatus(role));
    }
 
    /**
     * 删除角色
     */
    @RequiresPermissions("system:role:remove")
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roleIds}")
    public AjaxResult remove(@PathVariable Long[] roleIds)
    {
        return toAjax(roleService.deleteRoleByIds(roleIds));
    }
 
 
 
    /**
     * 停用角色
     */
    @RequiresPermissions("system:role:stop")
    @Log(title = "角色管理", businessType = BusinessType.STOP)
    @PutMapping("/stop")
    public AjaxResult stop(@RequestBody Long roleId)
    {
        SysRole sysRole = roleService.selectRoleById(roleId);
        if(sysRole.getStatus().equals("0")){
            sysRole.setStatus("1");
        }else {
            sysRole.setStatus("0");
        }
        return toAjax(roleService.updateRole(sysRole));
    }
 
    /**
     * 获取角色选择框列表
     */
    @RequiresPermissions("system:role:query")
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        return success(roleService.selectRoleAll());
    }
    /**
     * 查询已分配用户角色列表
     */
    @RequiresPermissions("system:role:list")
    @GetMapping("/authUser/allocatedList")
    public TableDataInfo allocatedList(SysUser user)
    {
        startPage();
        List<SysUser> list = userService.selectAllocatedList(user);
        return getDataTable(list);
    }
 
    /**
     * 查询未分配用户角色列表
     */
    @RequiresPermissions("system:role:list")
    @GetMapping("/authUser/unallocatedList")
    public TableDataInfo unallocatedList(SysUser user)
    {
        startPage();
        List<SysUser> list = userService.selectUnallocatedList(user);
        return getDataTable(list);
    }
 
    /**
     * 取消授权用户
     */
    @RequiresPermissions("system:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/cancel")
    public AjaxResult cancelAuthUser(@RequestBody SysUserRole userRole)
    {
        return toAjax(roleService.deleteAuthUser(userRole));
    }
 
    /**
     * 批量取消授权用户
     */
    @RequiresPermissions("system:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/cancelAll")
    public AjaxResult cancelAuthUserAll(Long roleId, Long[] userIds)
    {
        return toAjax(roleService.deleteAuthUsers(roleId, userIds));
    }
 
    /**
     * 批量选择用户授权
     */
    @RequiresPermissions("system:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/selectAll")
    public AjaxResult selectAuthUserAll(Long roleId, Long[] userIds)
    {
        roleService.checkRoleDataScope(roleId);
        return toAjax(roleService.insertAuthUsers(roleId, userIds));
    }
 
    /**
     * 获取对应角色部门树列表
     */
    @RequiresPermissions("system:role:query")
    @GetMapping(value = "/deptTree/{roleId}")
    public AjaxResult deptTree(@PathVariable("roleId") Long roleId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", deptService.selectDeptListByRoleId(roleId));
        ajax.put("depts", deptService.selectDeptTreeList(new SysDept()));
        return ajax;
    }
}