liujie
2023-06-12 c8638bb17163cc95e9063c358eb92cada1474102
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
package com.stylefeng.guns.modular.system.controller;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.Tip;
import com.stylefeng.guns.core.cache.CacheKit;
import com.stylefeng.guns.core.common.constant.Const;
import com.stylefeng.guns.core.common.constant.cache.Cache;
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.node.ZTreeNode;
import com.stylefeng.guns.core.util.Convert;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.service.IRoleService;
import com.stylefeng.guns.modular.system.service.ITUserService;
import com.stylefeng.guns.modular.system.service.IUserService;
import com.stylefeng.guns.modular.system.service.SysOperationService;
import com.stylefeng.guns.modular.system.utils.tips.SuccessTip;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
 
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * 角色控制器
 *
 * @author fengshuonan
 * @Date 2017年2月12日21:59:14
 */
@Controller
@Api(tags = "角色")
@RequestMapping("/api/role")
public class RoleController extends BaseController {
 
 
    @Autowired
    private IUserService userService;
 
    @Autowired
    private IRoleService roleService;
 
    @Autowired
    private SysOperationService sysOperation;
 
    @Autowired
    private ITUserService itUserService;
 
 
 
    /**
     * 获取角色列表
     */
    @ApiOperation(value = "获取角色列表",notes="获取角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."),
            @ApiImplicitParam(name = "roleName", value = "名称", required = false, dataType = "String"),
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String roleName) {
        List<Map<String, Object>> roles = this.roleService.selectRoles(super.getPara("roleName"));
        return new SuccessTip(roles);
    }
 
    /**
     * 角色新增
     */
    @ApiOperation(value = "角色新增",notes="角色新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."),
    })
    @PostMapping(value = "/add")
    @ResponseBody
    public Tip add(@Valid Role role) {
        role.setId(null);
        role.setCreateTime(new Date());
        this.roleService.insert(role);
        return SUCCESS_TIP;
    }
 
    /**
     * 角色修改
     */
    @ApiOperation(value = "角色修改",notes="角色修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."),
    })
    @PostMapping(value = "/update")
    @ResponseBody
    public Tip edit(@Valid Role role) {
        this.roleService.updateById(role);
        //删除缓存
        CacheKit.removeAll(Cache.CONSTANT);
        return SUCCESS_TIP;
    }
 
    /**
     * 删除角色
     */
    @ApiOperation(value = "删除角色",notes="删除角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."),
            @ApiImplicitParam(name = "roleId", value = "角色Id", required = true, dataType = "int"),
    })
    @DeleteMapping(value = "/delete")
    @ResponseBody
    public Tip remove(@RequestParam Integer roleId) {
        if (ToolUtil.isEmpty(roleId)) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
 
        //不能删除超级管理员角色
        if (roleId.equals(Const.ADMIN_ROLE_ID)) {
            throw new GunsException(BizExceptionEnum.CANT_DELETE_ADMIN);
        }
 
        //缓存被删除的角色名称
        LogObjectHolder.me().set(ConstantFactory.me().getSingleRoleName(roleId));
 
        this.roleService.delRoleById(roleId);
 
        //删除缓存
        CacheKit.removeAll(Cache.CONSTANT);
        return SUCCESS_TIP;
    }
 
    /**
     * 查看角色
     */
    @RequestMapping(value = "/view/{roleId}")
    @ResponseBody
    public Tip view(@PathVariable Integer roleId) {
        if (ToolUtil.isEmpty(roleId)) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        this.roleService.selectById(roleId);
        return SUCCESS_TIP;
    }
 
    /**
     * 配置权限
     */
    @ApiOperation(value = "配置权限",notes="配置权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."),
            @ApiImplicitParam(name = "roleId", value = "角色Id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "ids", value = "菜单权限ids(逗号拼接)", required = true, dataType = "int"),
    })
    @PostMapping("/setAuthority")
    @ResponseBody
    public Tip setAuthority(@RequestParam("roleId") Integer roleId, @RequestParam("ids") String ids) {
        if (ToolUtil.isOneEmpty(roleId)) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        this.roleService.setAuthority(roleId, ids);
        return SUCCESS_TIP;
    }
    /**
     * 配置权限
     */
    @ApiOperation(value = "配置权限-version1",notes="配置权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."),
    })
    @PostMapping("/setAuthority1")
    @ResponseBody
    public Tip setAuthority1(@RequestBody List<Relation> relations)  {
        if (ToolUtil.isOneEmpty(relations)) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        this.roleService.setAuthorityOne(relations);
        return SUCCESS_TIP;
    }
 
    /**
     * 根据菜单id获取所拥有的权限按钮
     */
    @ApiOperation(value = "根据菜单id获取所拥有的权限按钮",notes="根据菜单id获取所拥有的权限按钮")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."),
            @ApiImplicitParam(name = "menuId", value = "菜单Id", required = true, dataType = "int"),
    })
    @GetMapping("/getButton")
    @ResponseBody
    public Object getButton(@RequestParam Integer menuId)  {
        List<SysOperation> sysOperations = sysOperation.selectList(new EntityWrapper<SysOperation>().eq("menu_id", menuId));
        return new SuccessTip(sysOperations);
    }
    /**
     * 配置权限
     */
    @ApiOperation(value = "根据所有的权限按钮",notes="根据所有的权限按钮")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."),
    })
    @GetMapping("/getAllButton")
    @ResponseBody
    public Object getAllButton()  {
        List<SysOperation> sysOperations = sysOperation.selectList(null);
        return new SuccessTip(sysOperations);
    }
 
    @ApiOperation(value = "根据用户id获取权限信息",notes="根据用户id获取权限信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."),
            @ApiImplicitParam(name = "userId", value = "用户Id", required = true, dataType = "int"),
    })
    @GetMapping("/getAuthority")
    @ResponseBody
    public Object getAuthority(Integer userId) {
        TUser user = itUserService.selectById(userId);
        if(user==null){
            throw new GunsException(BizExceptionEnum.NO_THIS_USER);
        }
        // 当前拥有的
        List<Relation> relations = roleService.selectAuthority(String.valueOf(userId));
        List<Menu> menuList = roleService.getRelation1(relations.stream().map(Relation::getMenuid).collect(Collectors.toList()));
        List<Menu> parent = menuList.stream().filter(role -> role.getPcode().equals("0")).collect(Collectors.toList());
        getChildren(parent,menuList);
        return new SuccessTip(parent);
    }
 
    private void getChildren(List<Menu> parentList,List<Menu> menuList) {
        for (Menu parent : parentList) {
            List<Menu> children = menuList.stream().filter(menu -> menu.getPcode().equals(String.valueOf(parent.getId())) && menu.getLevels().equals(2)).collect(Collectors.toList());
            parent.setList(children);
            if(!CollectionUtils.isEmpty(children)){
                getChildren(children,menuList);
            }else {
                List<Menu> child = menuList.stream().filter(menu -> menu.getPcode().equals(String.valueOf(parent.getId())) && menu.getLevels().equals(3)).collect(Collectors.toList());
                parent.setChildren(child);
                continue;
            }
        }
    }
 
    @ApiOperation(value = "根据角色id获取权限信息",notes="根据角色id获取权限信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."),
            @ApiImplicitParam(name = "roleId", value = "用户Id", required = true, dataType = "int"),
    })
    @GetMapping("/getAuthorityFromRole")
    @ResponseBody
    public Object getAuthorityFromRole(String roleId) {
        List<Relation> relations = roleService.selectAuthority(roleId);
        List<Menu> menuList = roleService.getRelation1(relations.stream().map(Relation::getMenuid).collect(Collectors.toList()));
        List<Menu> parent = menuList.stream().filter(role -> role.getPcode().equals("0")).collect(Collectors.toList());
        getChildren(parent,menuList);
        return new SuccessTip(parent);
    }
 
    /**
     * 获取角色列表
     */
    @ApiOperation(value = "获取所有权限菜单",notes="获取所有权限菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."),
    })
    @GetMapping("/getRelation")
    @ResponseBody
    public List<Menu> getRelation() {
        return roleService.getRelation();
    }
 
    /**
     * 获取角色列表
     */
    @ResponseBody
    public List<ZTreeNode> roleTreeListByUserId(@PathVariable Integer userId) {
        User theUser = this.userService.selectById(userId);
        String roleid = theUser.getRoleid();
        if (ToolUtil.isEmpty(roleid)) {
            List<ZTreeNode> roleTreeList = this.roleService.roleTreeList();
            return roleTreeList;
        } else {
            String[] strArray = Convert.toStrArray(",", roleid);
            List<ZTreeNode> roleTreeListByUserId = this.roleService.roleTreeListByRoleId(strArray);
            return roleTreeListByUserId;
        }
    }
 
}