Pu Zhibing
2025-02-19 81e2eb4dd2e27da3c4cc447d6aeb9150a5ff7464
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
package com.ruoyi.sange.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.sange.domain.SystemRole;
import com.ruoyi.sange.domain.SystemRoleMenu;
import com.ruoyi.sange.service.ISystemRoleMenuService;
import com.ruoyi.sange.service.ISystemRoleService;
import com.ruoyi.sange.warpper.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @author zhibing.pu
 * @Date 2025/2/19 16:41
 */
@Api
@RestController
@RequestMapping("/api/systemRole")
public class SystemRoleController extends BaseController {
    
    @Resource
    private ISystemRoleService systemRoleService;
    
    @Resource
    private ISystemRoleMenuService systemRoleMenuService;
    
    
    
    @GetMapping("/list")
    @ApiOperation(value = "获取列表数据", tags = {"管理后台-角色管理"})
    public TableDataInfo<SystemRoleListVo> list(SystemRoleList systemRoleList){
        startPage();
        List<SystemRoleListVo> list = systemRoleService.list(systemRoleList.getName());
        return getDataTable(list);
    }
    
    
    
    @PostMapping("/add")
    @ApiOperation(value = "添加角色", tags = {"管理后台-角色管理"})
    public R add(@RequestBody AddSystemRole addSystemRole){
        int count = systemRoleService.count(new LambdaQueryWrapper<SystemRole>().eq(SystemRole::getDel, 0).eq(SystemRole::getName, addSystemRole.getName()));
        if(0 < count){
            return R.fail("该角色名称已存在");
        }
        SystemRole systemRole = new SystemRole();
        systemRole.setName(addSystemRole.getName());
        systemRole.setDel(0);
        systemRole.setCreateTime(LocalDateTime.now());
        systemRoleService.save(systemRole);
        //添加角色菜单权限
        List<Integer> systemMenuIds = addSystemRole.getSystemMenuIds();
        for (Integer systemMenuId : systemMenuIds) {
            SystemRoleMenu systemRoleMenu = new SystemRoleMenu();
            systemRoleMenu.setSystemRoleId(systemRole.getId());
            systemRoleMenu.setSystemMenuId(systemMenuId);
            systemRoleMenuService.save(systemRoleMenu);
        }
        return R.ok();
    }
    
    @PutMapping("/edit")
    @ApiOperation(value = "编辑角色", tags = {"管理后台-角色管理"})
    public R edit(@RequestBody EditSystemRole editSystemRole){
        int count = systemRoleService.count(new LambdaQueryWrapper<SystemRole>().eq(SystemRole::getDel, 0)
                .eq(SystemRole::getName, editSystemRole.getName()).ne(SystemRole::getId, editSystemRole.getId()));
        if(0 < count){
            return R.fail("该角色名称已存在");
        }
        SystemRole systemRole = systemRoleService.getById(editSystemRole.getId());
        if(null == systemRole){
            return R.fail("无效的角色数据");
        }
        systemRole.setName(editSystemRole.getName());
        systemRoleService.updateById(systemRole);
        //添加角色菜单权限
        systemRoleMenuService.remove(new LambdaQueryWrapper<SystemRoleMenu>().eq(SystemRoleMenu::getSystemRoleId, editSystemRole.getId()));
        List<Integer> systemMenuIds = editSystemRole.getSystemMenuIds();
        for (Integer systemMenuId : systemMenuIds) {
            SystemRoleMenu systemRoleMenu = new SystemRoleMenu();
            systemRoleMenu.setSystemRoleId(systemRole.getId());
            systemRoleMenu.setSystemMenuId(systemMenuId);
            systemRoleMenuService.save(systemRoleMenu);
        }
        return R.ok();
    }
    
    
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除角色", tags = {"管理后台-角色管理"})
    public R delete(@PathVariable("id") Integer id){
        SystemRole systemRole = systemRoleService.getById(id);
        systemRole.setDel(1);
        systemRoleService.updateById(systemRole);
        //删除角色菜单权限
        systemRoleMenuService.remove(new LambdaQueryWrapper<SystemRoleMenu>().eq(SystemRoleMenu::getSystemRoleId, id));
        return R.ok();
    }
    
    
    
    @GetMapping("/getSystemRoleInfo/{id}")
    @ApiOperation(value = "获取角色详情", tags = {"管理后台-角色管理"})
    public R<SystemRoleInfo> getSystemRoleInfo(@PathVariable("id") Integer id){
        SystemRole systemRole = systemRoleService.getById(id);
        if(null == systemRole){
            return R.fail("无效的角色数据");
        }
        
        List<SystemRoleMenu> list = systemRoleMenuService.list(new LambdaQueryWrapper<SystemRoleMenu>().eq(SystemRoleMenu::getSystemRoleId, id));
        SystemRoleInfo systemRoleInfo = new SystemRoleInfo();
        systemRoleInfo.setId(systemRole.getId());
        systemRoleInfo.setName(systemRoleInfo.getName());
        systemRoleInfo.setSystemMenuIds(list.stream().map(SystemRoleMenu::getSystemMenuId).collect(Collectors.toList()));
        return R.ok(systemRoleInfo);
    }
    
}