44323
2024-04-23 16b704d18a875d1fb63827aaa507790ba2bef5be
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
package com.stylefeng.guns.modular.code.controller;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stylefeng.guns.core.cache.CacheKit;
import com.stylefeng.guns.core.common.annotion.BussinessLog;
import com.stylefeng.guns.core.common.constant.Const;
import com.stylefeng.guns.core.common.constant.cache.Cache;
import com.stylefeng.guns.core.common.constant.dictmap.RoleDict;
import com.stylefeng.guns.core.common.constant.factory.ConstantFactory;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.mutidatasource.annotion.DataSource;
import com.stylefeng.guns.core.node.ZTreeNode;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dto.DeptQuery;
import com.stylefeng.guns.modular.system.dto.TreeBean;
import com.stylefeng.guns.modular.system.model.Dept;
import com.stylefeng.guns.modular.system.model.Role;
import com.stylefeng.guns.modular.system.model.User;
import com.stylefeng.guns.modular.system.service.IMenuService;
import com.stylefeng.guns.modular.system.service.IRoleService;
import com.stylefeng.guns.modular.system.service.IUserService;
import com.stylefeng.guns.modular.system.util.ListToTreeUtil;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
 
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
@Controller
@RequestMapping("/base/role")
public class SysRoleController {
 
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IMenuService menuService;
    @Autowired
    private IUserService userService;
 
    @GetMapping(value = "/list")
    @ApiOperation(value = "列表", tags = {"后台-角色管理"})
    @ResponseBody
    public ResultUtil<PageInfo<Map>> list(DeptQuery req) {
//        PageHelper.startPage(req.getPageNum(),req.getPageSize());
        List<Map<String, Object>> roles = this.roleService.selectRoles(req.getDeptName());
        PageInfo<Map> info=new PageInfo<>(roles);
        return ResultUtil.success(info);
    }
 
    @PostMapping(value = "/add")
    @BussinessLog(value = "添加角色", key = "name", dict = RoleDict.class)
    @ApiOperation(value = "添加角色", tags = {"后台-角色管理"})
    @ResponseBody
    public ResultUtil add(@Valid Role role, @RequestParam("ids") String ids,BindingResult result) {
        if (result.hasErrors()) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        role.setId(null);
        role.setDeptid(0);
        role.setPid(0);
        role.setInsertTime(new Date());
        role.setCreateBy(Objects.requireNonNull(ShiroKit.getUser()).id);
        this.roleService.insert(role);
        this.roleService.setAuthority(role.getId(), ids);
        return ResultUtil.success("添加成功");
    }
 
    @RequestMapping(value = "/pre/edit/{roleId}")
    public Role preEdit(@PathVariable Integer roleId) {
        if (ToolUtil.isEmpty(roleId)) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        Role role = this.roleService.selectById(roleId);
        return role;
    }
 
 
    @PostMapping(value = "/edit")
    @BussinessLog(value = "修改角色", key = "name", dict = RoleDict.class)
    @ApiOperation(value = "修改角色", tags = {"后台-角色管理"})
    @ResponseBody
    public ResultUtil edit(@RequestParam("id") Integer roleId, @RequestParam("name") String name,@RequestParam("ids") String ids) {
 
        Role role = roleService.selectById(roleId);
        role.setDeptid(0);
        role.setName(name);
        role.setPid(0);
        this.roleService.updateById(role);
        this.roleService.setAuthority(role.getId(), ids);
        return ResultUtil.success("修改成功");
    }
 
    @PostMapping(value = "/delete")
    @BussinessLog(value = "删除角色", key = "name", dict = RoleDict.class)
    @ApiOperation(value = "删除角色", tags = {"后台-角色管理"})
    @ResponseBody
    public ResultUtil delete( String ids) {
        String[] split = ids.split(",");
        for (String id : split) {
            List<User> roleid = userService.selectList(new EntityWrapper<User>().eq("roleid", Integer.valueOf(id)));
            if (!roleid.isEmpty()){
                return ResultUtil.error("当前角色已绑定用户,无法删除");
            }
 
 
            if (ToolUtil.isEmpty(id)) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
 
        //不能删除超级管理员角色
        if (id.equals(Const.ADMIN_ROLE_ID)) {
            throw new GunsException(BizExceptionEnum.CANT_DELETE_ADMIN);
        }
 
        //缓存被删除的角色名称
        LogObjectHolder.me().set(ConstantFactory.me().getSingleRoleName(Integer.valueOf(id)));
        Role role = roleService.selectById(id);
        this.roleService.delRoleById(Integer.valueOf(id));
        //删除缓存
        CacheKit.removeAll(Cache.CONSTANT);
        }
 
        return ResultUtil.success("删除成功");
    }
 
 
 
 
    @DataSource(name = "dataSourceGuns")
    @GetMapping (value = "/menuTreeListByRoleId/{roleId}")
    @ApiOperation(value = "角色分配权限获取列表", tags = {"后台-角色管理"})
    @ResponseBody
    public  List<TreeBean> menuTreeListByRoleId(@PathVariable Integer roleId) {
        List<Long> menuIds = this.menuService.getMenuIdsByRoleId(roleId);
        if (ToolUtil.isEmpty(menuIds)) {
            List<ZTreeNode> roleTreeList = this.menuService.menuTreeList();
            List<TreeBean> root = ListToTreeUtil.toTree(roleTreeList, "root");
            return root;
        }else {
            for (Long menuId : menuIds) {
                System.err.println("菜单id:"+menuId);
            }
            List<ZTreeNode> roleTreeListByUserId = this.menuService.menuTreeListByMenuIds(menuIds);
            List<TreeBean> root = ListToTreeUtil.toTree(roleTreeListByUserId, "root");
            return root;
        }
    }
    @DataSource(name = "dataSourceGuns")
    @RequestMapping("/setAuthority")
    @BussinessLog(value = "配置权限操作", key = "roleId,ids", dict = RoleDict.class)
    @ResponseBody
    public ResultUtil setAuthority(@RequestParam("roleId") Integer roleId, @RequestParam("ids") String ids) {
        if (ToolUtil.isOneEmpty(roleId)) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        this.roleService.setAuthority(roleId, ids);
        Role role = roleService.selectById(roleId);
        return ResultUtil.success("分配成功");
    }
    private List<ZTreeNode> getParent(List<ZTreeNode> roleTreeListByUserId,List<ZTreeNode> parent){
        List<ZTreeNode> result = new ArrayList<>();
        for (ZTreeNode zTreeNode : parent) {
 
            List<ZTreeNode> children = roleTreeListByUserId.stream().filter(e -> e.getpId().equals(zTreeNode.getId())).collect(Collectors.toList());
            if(CollectionUtils.isEmpty(children)){
                zTreeNode.setChildren(children);
            }else {
                getParent(roleTreeListByUserId,children);
            }
            ZTreeNode zTreeNode1 = new ZTreeNode();
            BeanUtils.copyProperties(zTreeNode,zTreeNode1);
            result.add(zTreeNode1);
        }
        return result;
    }
 
 
 
 
 
 
    @GetMapping(value = "/roleTreeList")
    @ApiOperation(value = "获取角色下拉框", tags = {"后台-角色管理"})
    @ResponseBody
    public List<Role> roleTreeList() {
//        List<ZTreeNode> roleTreeList = this.roleService.roleTreeList();
//        roleTreeList.add(ZTreeNode.createParent());
        return roleService.selectList(null);
//        return roleTreeList;
    }
}