shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
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
package com.iplatform.base.controller;
 
import com.iplatform.base.SystemController;
import com.iplatform.base.event.RoleSecurityChangeEvent;
import com.iplatform.base.pojo.role.RoleAuthParam;
import com.iplatform.base.pojo.role.RoleParam;
import com.iplatform.base.pojo.role.RoleUserParam;
import com.iplatform.base.service.RoleServiceImpl;
import com.iplatform.base.util.role.SystemRole;
import com.iplatform.core.BeanContextAware;
import com.iplatform.model.po.S_role;
import com.iplatform.model.po.S_user_core;
import com.walker.db.page.GenericPager;
import com.walker.infrastructure.utils.NumberGenerator;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.web.ResponseValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
@RequestMapping("/system/role")
public class RoleController extends SystemController {
 
    private RoleServiceImpl roleService;
 
    @Autowired
    public RoleController(RoleServiceImpl roleService){
        this.roleService = roleService;
    }
 
    /**
     * 批量添加授权用户。
     * @param roleId 角色ID
     * @param userIds 用户ID集合
     * @return
     * @date 2022-12-21
     */
    @RequestMapping("/select/authUser/all")
    public ResponseValue batchAuthUserList(Long roleId, Long[] userIds){
        if(userIds == null || userIds.length == 0 || roleId == null){
            return ResponseValue.error("参数错误");
        }
        logger.debug(roleId + ", " + userIds);
        this.roleService.execInsertRoleUserList(roleId, userIds);
        return ResponseValue.success();
    }
 
    /**
     * 取消已授权用户,从角色里移除。
     * @param roleUserParam
     * @return
     * @date 2022-12-21
     */
    @RequestMapping("/select/authUser/cancel")
    public ResponseValue cancelAuthUser(@RequestBody RoleUserParam roleUserParam){
        if(roleUserParam == null || roleUserParam.getRoleId() == null || roleUserParam.getUserId() == null){
            return ResponseValue.error("参数错误");
        }
        this.roleService.execDeleteRoleUser(roleUserParam.getRoleId(), roleUserParam.getUserId());
        return ResponseValue.success();
    }
 
    @RequestMapping("/select/authUser/unallocatedList")
    public ResponseValue unallocatedList(RoleAuthParam roleAuthParam){
        if(roleAuthParam == null || roleAuthParam.getRoleId() == null){
            return ResponseValue.error();
        }
        // 2023-01-28 这个由于url地址设置不合理,没能匹配: /list 所以暂时不能删除
        this.preparePageSearch();
 
        S_role s_role = this.roleService.get(new S_role(roleAuthParam.getRoleId()));
        GenericPager<S_user_core> pager = this.roleService.queryUnAllocatedUserList(roleAuthParam.getRoleId(), s_role.getOrg_id(), roleAuthParam.getUserName());
//        return this.acquireTablePage(pager.getDatas(), pager.getTotalRows());
        return ResponseValue.success(pager);
    }
 
    /**
     * 展示给定角色,已关联用户列表。
     * @param roleAuthParam
     * @return
     * @date 2022-12-21
     */
    @RequestMapping("/select/authUser/allocatedList")
    public ResponseValue allocatedList(RoleAuthParam roleAuthParam){
        if(roleAuthParam == null || roleAuthParam.getRoleId() == null){
            return ResponseValue.error();
        }
        // 2023-01-28 这个由于url地址设置不合理,没能匹配: /list 所以暂时不能删除
        this.preparePageSearch();
 
        GenericPager<S_user_core> pager = this.roleService.queryAllocatedUserList(roleAuthParam.getRoleId(), roleAuthParam.getUserName());
//        return this.acquireTablePage(pager.getDatas(), pager.getTotalRows());
        return ResponseValue.success(pager);
    }
 
    @RequestMapping("/edit")
    public ResponseValue saveEdit(@RequestBody SystemRole systemRole){
        if(systemRole == null || StringUtils.isEmpty(systemRole.getRole_name()) || systemRole.getOrg_id() == null
            || systemRole.getRole_id() == null){
            return ResponseValue.error("角色参数错误");
        }
        long orgId = systemRole.getOrg_id();
        S_role existRole = this.roleService.queryRoleByName(orgId, systemRole.getRole_name());
        if(existRole != null && existRole.getRole_id().longValue() != systemRole.getRole_id().longValue()){
            return ResponseValue.error("角色名称已经存在");
        }
 
        this.roleService.execUpdateRole(systemRole.$clone(), systemRole.getMenuIds());
 
        // 发送事件通知,让security重新加载权限数据
        BeanContextAware.publishEvent(new RoleSecurityChangeEvent(systemRole.getRole_id()));
 
        return ResponseValue.success();
    }
 
    @RequestMapping("/view/{roleId}")
    public ResponseValue view(@PathVariable Long roleId){
        S_role s_role = this.roleService.get(new S_role(roleId));
        if(s_role == null){
            return ResponseValue.error("角色不存在");
        }
        return ResponseValue.success(s_role);
    }
 
    @RequestMapping("/remove/{roleId}")
    public ResponseValue removeRole(@PathVariable Long roleId){
        if(roleId == null || roleId.longValue() <= 0){
            return ResponseValue.error("参数错误");
        }
        int roleUserCount = this.roleService.queryRoleUserSize(roleId);
        if(roleUserCount > 0){
            return ResponseValue.error("角色已分配用户,无法删除");
        }
        this.roleService.execDeleteRole(roleId);
        return ResponseValue.success();
    }
 
    @RequestMapping("/add")
    public ResponseValue saveAdd(@RequestBody SystemRole systemRole){
        if(systemRole == null || StringUtils.isEmpty(systemRole.getRole_name()) || systemRole.getOrg_id() == null){
            return ResponseValue.error("角色参数错误");
        }
        long orgId = systemRole.getOrg_id();
        if(orgId <= 0){
            return ResponseValue.error("角色所属机构必须提供");
        }
        S_role existRole = this.roleService.queryRoleByName(orgId, systemRole.getRole_name());
        if(existRole != null){
            return ResponseValue.error("角色名称已经存在");
        }
        systemRole.setRole_id(NumberGenerator.getSequenceNumber());
        this.roleService.execInsertRole(systemRole.$clone(), systemRole.getMenuIds());
        return ResponseValue.success();
    }
 
    @PostMapping("/select/changeStatus")
    public ResponseValue changeStatus(@RequestBody S_role s_role){
        if(s_role == null || s_role.getRole_id() == null){
            return ResponseValue.error("参数错误");
        }
        this.roleService.execUpdateStatus(s_role.getRole_id(), s_role.getStatus());
        return ResponseValue.success();
    }
 
    @GetMapping("/list")
    public ResponseValue pageList(RoleParam roleParam){
        if(roleParam == null || roleParam.getOrgId() <= 0){
            return ResponseValue.error("无法查询角色:没有条件");
        }
//        this.preparePageSearch();
 
        long orgId = 0;
        if(!this.isSupervisor()){
            // 普通用户,只能看到自己顶级机构用户
            orgId = this.getCurrentUser().getOrg_id();
        } else {
            orgId = roleParam.getOrgId();
        }
        GenericPager<S_role> pager = this.roleService.queryPageRoleList(orgId, roleParam.getStatus(), roleParam.getRoleName());
//        return this.acquireTablePage(pager.getDatas(), pager.getTotalRows());
        return ResponseValue.success(pager);
    }
 
}