luo
2023-12-09 cb5ddf074d60b72e35ee4bafaec95530f53d1839
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
package com.stylefeng.guns.modular.code.controller;
 
import com.stylefeng.guns.core.base.tips.Tip;
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.model.Role;
import com.stylefeng.guns.modular.system.service.IMenuService;
import com.stylefeng.guns.modular.system.service.IRoleService;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import com.stylefeng.guns.modular.system.warpper.RoleWarpper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
 
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/base/role")
public class SysRoleController {
 
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IMenuService menuService;
 
    @GetMapping(value = "/list")
    @ApiOperation(value = "列表", tags = {"后台-角色管理"})
    @ResponseBody
    public Object list(@RequestParam(required = false) String roleName) {
        List<Map<String, Object>> roles = this.roleService.selectRoles(roleName);
        return roles;
    }
 
 
    @PostMapping(value = "/add")
    @BussinessLog(value = "添加角色", key = "name", dict = RoleDict.class)
    @ApiOperation(value = "添加角色", tags = {"后台-角色管理"})
    @ResponseBody
    public ResultUtil add(@Valid Role role, BindingResult result) {
        if (result.hasErrors()) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        role.setId(null);
        role.setDeptid(0);
        role.setPid(0);
        role.setInsertTime(new Date());
        this.roleService.insert(role);
        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(@Valid Role role, BindingResult result) {
        if (result.hasErrors()) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        role.setDeptid(0);
        role.setPid(0);
        this.roleService.updateById(role);
        return ResultUtil.success("修改成功");
    }
 
 
    @PostMapping(value = "/delete/{id}")
    @BussinessLog(value = "删除角色", key = "name", dict = RoleDict.class)
    @ApiOperation(value = "删除角色", tags = {"后台-角色管理"})
    @ResponseBody
    public ResultUtil delete(@PathVariable Integer id) {
        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(id));
        Role role = roleService.selectById(id);
        this.roleService.delRoleById(id);
        //删除缓存
        CacheKit.removeAll(Cache.CONSTANT);
        return ResultUtil.success("删除成功");
    }
 
 
 
 
    @DataSource(name = "dataSourceGuns")
    @GetMapping (value = "/menuTreeListByRoleId/{roleId}")
    @ApiOperation(value = "角色分配权限获取列表", tags = {"后台-角色管理"})
    @ResponseBody
    public List<ZTreeNode> menuTreeListByRoleId(@PathVariable Integer roleId) {
 
        List<Long> menuIds = this.menuService.getMenuIdsByRoleId(roleId);
        if (ToolUtil.isEmpty(menuIds)) {
            List<ZTreeNode> roleTreeList = this.menuService.menuTreeList();
            return roleTreeList;
        }else {
            List<ZTreeNode> roleTreeListByUserId = this.menuService.menuTreeListByMenuIds(menuIds);
            return roleTreeListByUserId;
        }
    }
 
    @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("分配成功");
    }
 
 
 
    @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;
    }
}