liujie
2026-09-11 4d15e6b553c448e2e07527e9c06aa4b5f9b099cf
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
package com.ruoyi.system.service.impl;
 
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysRoleMenu;
import com.ruoyi.system.dto.SysRoleManageAddRequest;
import com.ruoyi.system.dto.SysRoleManageDeleteRequest;
import com.ruoyi.system.dto.SysRoleManageDetailRequest;
import com.ruoyi.system.dto.SysRoleManageEditRequest;
import com.ruoyi.system.dto.SysRoleManagePageRequest;
import com.ruoyi.system.mapper.SysMenuMapper;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.mapper.SysRoleMenuMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysRoleManageService;
import com.ruoyi.system.vo.SysMenuPermissionVO;
import com.ruoyi.system.vo.SysRoleManageDetailVO;
import com.ruoyi.system.vo.SysRoleManageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
/**
 * 角色管理服务实现
 *
 * @author ruoyi
 */
@Service
public class SysRoleManageServiceImpl implements ISysRoleManageService
{
    @Autowired
    private SysRoleMapper roleMapper;
 
    @Autowired
    private SysMenuMapper menuMapper;
 
    @Autowired
    private SysRoleMenuMapper roleMenuMapper;
 
    @Autowired
    private SysUserRoleMapper userRoleMapper;
 
    @Override
    public PageInfo<SysRoleManageVO> pageList(SysRoleManagePageRequest request)
    {
        SysRoleManagePageRequest query = request == null ? new SysRoleManagePageRequest() : request;
        query.setRoleName(StringUtils.trim(query.getRoleName()));
        PageInfo<SysRoleManageVO> pageInfo = new PageInfo<SysRoleManageVO>(query.getPageNum(), query.getPageSize());
        List<SysRoleManageVO> list = roleMapper.selectRoleManagePage(query, pageInfo);
        pageInfo.setRecords(list);
        return pageInfo;
    }
 
    @Override
    public SysRoleManageDetailVO detail(SysRoleManageDetailRequest request)
    {
        Long roleId = getRequiredRoleId(request == null ? null : request.getRoleId());
        SysRoleManageVO role = roleMapper.selectRoleManageById(roleId);
        if (role == null)
        {
            throw new ServiceException("角色不存在");
        }
        SysRoleManageDetailVO detail = new SysRoleManageDetailVO();
        BeanUtils.copyProperties(role, detail);
        detail.setMenuIds(menuMapper.selectMenuListByRoleId(roleId, false));
        return detail;
    }
 
    @Override
    @Transactional
    public int add(SysRoleManageAddRequest request, String username)
    {
        if (request == null)
        {
            throw new ServiceException("角色内容不能为空");
        }
        String roleName = getRequiredRoleName(request.getRoleName());
        ensureRoleNameUnique(null, roleName);
 
        SysRole role = new SysRole();
        role.setRoleName(roleName);
        role.setRemark(StringUtils.trim(request.getRemark()));
        role.setStatus(0);
        role.setCreateBy(username);
        roleMapper.insertRole(role);
        saveRoleMenus(role.getRoleId(), request.getMenuIds());
        return 1;
    }
 
    @Override
    @Transactional
    public int edit(SysRoleManageEditRequest request, String username)
    {
        if (request == null)
        {
            throw new ServiceException("角色内容不能为空");
        }
        Long roleId = getRequiredRoleId(request.getRoleId());
        if (SysRole.isAdmin(roleId))
        {
            throw new ServiceException("不允许操作超级管理员角色");
        }
        SysRole role = roleMapper.selectRoleById(roleId);
        if (role == null || Integer.valueOf(2).equals(role.getDelFlag()))
        {
            throw new ServiceException("角色不存在");
        }
        String roleName = getRequiredRoleName(request.getRoleName());
        ensureRoleNameUnique(roleId, roleName);
 
        SysRole update = new SysRole();
        update.setRoleId(roleId);
        update.setRoleName(roleName);
        update.setRemark(StringUtils.trim(request.getRemark()));
        update.setUpdateBy(username);
        roleMapper.updateRole(update);
        roleMenuMapper.deleteRoleMenuByRoleId(roleId);
        saveRoleMenus(roleId, request.getMenuIds());
        return 1;
    }
 
    @Override
    @Transactional
    public int delete(SysRoleManageDeleteRequest request)
    {
        List<Long> roleIds = request == null ? null : request.getRoleIds();
        if (CollectionUtils.isEmpty(roleIds))
        {
            throw new ServiceException("请选择角色");
        }
        for (Long roleId : roleIds)
        {
            Long requiredRoleId = getRequiredRoleId(roleId);
            if (SysRole.isAdmin(requiredRoleId))
            {
                throw new ServiceException("超级管理员不能删除");
            }
            SysRole role = roleMapper.selectRoleById(requiredRoleId);
            if (role == null || Integer.valueOf(2).equals(role.getDelFlag()))
            {
                throw new ServiceException("角色不存在");
            }
            if (userRoleMapper.countUserRoleByRoleId(requiredRoleId) > 0)
            {
                throw new ServiceException("仅未绑定账号情况下可删除权限组");
            }
        }
        roleMenuMapper.deleteRoleMenu(roleIds);
        return roleMapper.deleteRoleByIds(roleIds);
    }
 
    @Override
    public List<SysMenuPermissionVO> permissionTree()
    {
        List<SysMenu> menus = menuMapper.selectMenuList(new SysMenu());
        Map<Long, SysMenuPermissionVO> menuMap = new LinkedHashMap<Long, SysMenuPermissionVO>();
        List<SysMenuPermissionVO> roots = new ArrayList<SysMenuPermissionVO>();
        for (SysMenu menu : menus)
        {
            if ("1".equals(menu.getStatus()))
            {
                continue;
            }
            SysMenuPermissionVO item = new SysMenuPermissionVO();
            item.setMenuId(menu.getMenuId());
            item.setParentId(menu.getParentId());
            item.setMenuName(menu.getMenuName());
            item.setMenuType(menu.getMenuType());
            item.setPerms(menu.getPerms());
            item.setOrderNum(menu.getOrderNum());
            item.setChildren(new ArrayList<SysMenuPermissionVO>());
            menuMap.put(item.getMenuId(), item);
        }
        for (SysMenuPermissionVO item : menuMap.values())
        {
            SysMenuPermissionVO parent = menuMap.get(item.getParentId());
            if (parent == null)
            {
                roots.add(item);
            }
            else
            {
                parent.getChildren().add(item);
            }
        }
        return roots;
    }
 
    private void saveRoleMenus(Long roleId, List<Long> menuIds)
    {
        Set<Long> normalizedMenuIds = new LinkedHashSet<Long>();
        if (!CollectionUtils.isEmpty(menuIds))
        {
            normalizedMenuIds.addAll(menuIds);
        }
        if (normalizedMenuIds.isEmpty())
        {
            return;
        }
 
        Set<Long> existingMenuIds = new LinkedHashSet<Long>();
        for (SysMenu menu : menuMapper.selectMenuList(new SysMenu()))
        {
            existingMenuIds.add(menu.getMenuId());
        }
 
        List<SysRoleMenu> roleMenus = new ArrayList<SysRoleMenu>();
        for (Long menuId : normalizedMenuIds)
        {
            if (menuId == null || !existingMenuIds.contains(menuId))
            {
                continue;
            }
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            roleMenus.add(roleMenu);
        }
        if (!roleMenus.isEmpty())
        {
            roleMenuMapper.batchRoleMenu(roleMenus);
        }
    }
 
    private void ensureRoleNameUnique(Long roleId, String roleName)
    {
        Long currentRoleId = roleId == null ? -1L : roleId;
        SysRole exists = roleMapper.checkRoleNameUnique(roleName);
        if (exists != null && !currentRoleId.equals(exists.getRoleId()))
        {
            throw new ServiceException("角色名称已存在");
        }
    }
 
    private Long getRequiredRoleId(Long roleId)
    {
        if (roleId == null || roleId <= 0)
        {
            throw new ServiceException("角色ID不能为空");
        }
        return roleId;
    }
 
    private String getRequiredRoleName(String roleName)
    {
        String value = StringUtils.trim(roleName);
        if (StringUtils.isEmpty(value))
        {
            throw new ServiceException("角色名称不能为空");
        }
        if (value.length() > 30)
        {
            throw new ServiceException("角色名称不能超过30个字符");
        }
        return value;
    }
}