郑永安
2023-06-19 f65443d8abeaedc9d102324565e8368e7c9d90c8
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
package com.gk.firework.Controller;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gk.firework.Domain.DistrictInfo;
import com.gk.firework.Domain.RoleInfo;
import com.gk.firework.Domain.RolePermissionsInfo;
import com.gk.firework.Domain.UserInfo;
import com.gk.firework.Domain.Utils.*;
import com.gk.firework.Domain.Vo.Menu;
import com.gk.firework.Service.*;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.management.relation.Role;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;
 
@Api(tags = "角色管理数据接口")
@RestController
public class RoleController {
 
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;
    @Autowired
    UserRolesService userRolesService;
    @Autowired
    RolePermissionsService rolePermissionsService;
    @Autowired
    ExcelExportService excelExportService;
 
    @GetMapping("/role")
    @ApiOperation(value = "获取角色数据",response = Msg.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "角色名"),
    })
    public Msg getRoleInfo(String name){
        Msg msg = new Msg();
        msg.setCode("200");
        msg.setMessage("success");
        List<RoleInfo> roleInfos = roleService.selectList(name);
        msg.setResult(roleInfos);
        return msg;
    }
 
    @GetMapping("/rolepermissions")
    @ApiOperation(value = "获取角色菜单数据",response = Msg.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId",value = "角色id"),
    })
    public Msg getRolePermissions(String roleId){
        Msg msg = new Msg();
        msg.setCode("200");
        msg.setMessage("success");
        List<Menu> roleInfos = rolePermissionsService.selectMenuList(roleId);
        msg.setResult(roleInfos);
        return msg;
    }
 
    @PostMapping("/addrole")
    @ApiOperation(value = "添加角色数据",response = Msg.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "角色名",required = true),
            @ApiImplicitParam(name = "operator",value = "操作人"),
            @ApiImplicitParam(name = "btnaccess",value = "按钮权限"),
    })
    public Msg addRoleInfo(@RequestBody JSONObject jsonObject){
        Msg msg = new Msg();
        msg.setCode("200");
        msg.setMessage("success");
        String name = jsonObject.getString("name");
        String operator = jsonObject.getString("operator");
        String btnaccess = jsonObject.getString("btnaccess");
        RoleInfo roleInfo = new RoleInfo();
        roleInfo.setName(name);
        RoleInfo roleInfoExist = roleService.selectRoleByName(roleInfo);
        if (roleInfoExist != null){
            msg.setCode("500");
            msg.setMessage("角色名重复");
            return msg;
        }else {
            roleInfo.setBtnaccess(btnaccess);
            roleInfo.setCreatedby(operator);
            roleInfo.setCreateddate(new Date());
            roleInfo.setLastmodifiedby(operator);
            roleInfo.setLastmodifieddate(new Date());
            roleInfo.setIsdel((byte)0);
            roleService.save(roleInfo);
            return msg;
        }
    }
 
    @PostMapping("/putrole")
    @ApiOperation(value = "修改角色数据",response = Msg.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "角色id",required = true),
            @ApiImplicitParam(name = "name",value = "角色名",required = true),
            @ApiImplicitParam(name = "btnaccess",value = "按钮权限"),
            @ApiImplicitParam(name = "operator",value = "操作人"),
    })
    public Msg putRoleInfo(@RequestBody JSONObject jsonObject){
        Msg msg = new Msg();
        msg.setCode("200");
        msg.setMessage("success");
        String name = jsonObject.getString("name");
        String operator = jsonObject.getString("operator");
        String btnaccess = jsonObject.getString("btnaccess");
        Long id = jsonObject.getLong("id");
        List<RoleInfo> roleInfoExist = roleService.selectExistRole(id,name);
        if (roleInfoExist.size() > 0){
            msg.setCode("500");
            msg.setMessage("角色名重复");
            return msg;
        }else {
            RoleInfo roleInfo = new RoleInfo();
            roleInfo.setId(id);
            roleInfo.setName(name);
            roleInfo.setBtnaccess(btnaccess);
            roleInfo.setLastmodifiedby(operator);
            roleInfo.setLastmodifieddate(new Date());
            roleService.updateById(roleInfo);
            return msg;
        }
    }
 
    @PostMapping("/rolepermissions")
    @ApiOperation(value = "获取角色菜单数据",response = Msg.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "角色id"),
    })
    public Msg putRolePermissions(@ApiParam(value = "permissionIds,id")
                                      @RequestBody JSONObject jsonObject){
        Msg msg = new Msg();
        msg.setCode("200");
        msg.setMessage("success");
        JSONArray permissionId = jsonObject.getJSONArray("permissionIds");
        Long roleid = jsonObject.getLong("id");
        rolePermissionsService.delRolePermissionByRoleId(roleid);
        for (int i = 0; i < permissionId.size(); i++) {
            RolePermissionsInfo rolePermissionsInfo = new RolePermissionsInfo();
            rolePermissionsInfo.setRoleid(roleid);
            rolePermissionsInfo.setPermissionid(Long.parseLong(permissionId.get(i).toString()));
            rolePermissionsService.save(rolePermissionsInfo);
        }
        return msg;
    }
 
    @PostMapping("/delrole")
    @ApiOperation(value = "删除角色数据", response = Msg.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "id",value = "id",required = true),
            @ApiImplicitParam(paramType="body",name = "operator",value = "更新人"),
 
    })
    public Msg delRoleInfo(@ApiParam(value = "id,operator")
                             @RequestBody JSONObject jsonObject){
        Msg msg = new Msg();
        msg.setCode("200");
        msg.setMessage("success");
        Long id = jsonObject.getLong("id");
        if (id == 1 || id == 2 || id == 3){
            msg.setCode("999");
            msg.setMessage("内置角色不允许删除");
            return msg;
        }
        RoleInfo roleInfo = new RoleInfo();
        roleInfo.setId(jsonObject.getLong("id"));
        roleInfo.setLastmodifiedby(jsonObject.getString("operator"));
        roleInfo.setLastmodifieddate(new Date());
        roleInfo.setIsdel((byte)1);
        roleService.updateById(roleInfo);
        return msg;
    }
 
}