Blame view

src/main/java/com/huaheng/pc/system/role/service/RoleServiceImpl.java 10.5 KB
tangying authored
1
2
package com.huaheng.pc.system.role.service;
游杰 authored
3
import java.util.*;
游杰 authored
4
import java.util.stream.Collectors;
pengcheng authored
5
游杰 authored
6
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
周鸿 authored
7
import com.huaheng.common.utils.Wrappers;
游杰 authored
8
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
pengcheng authored
9
import com.huaheng.common.exception.service.ServiceException;
游杰 authored
10
11
12
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.system.menu.domain.Menu;
import com.huaheng.pc.system.menu.service.IMenuService;
tangying authored
13
14
15
16
17
18
19
20
21
22
23
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huaheng.common.constant.UserConstants;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.pc.system.role.domain.Role;
import com.huaheng.pc.system.role.domain.RoleMenu;
import com.huaheng.pc.system.role.mapper.RoleMapper;
import com.huaheng.pc.system.role.mapper.RoleMenuMapper;
import com.huaheng.pc.system.user.mapper.UserRoleMapper;
pengcheng authored
24
import org.springframework.transaction.annotation.Transactional;
tangying authored
25
游杰 authored
26
27
import javax.annotation.Resource;
tangying authored
28
29
30
31
32
33
/**
 * 角色 业务层处理
 * 
 * @author huaheng
 */
@Service
游杰 authored
34
public class RoleServiceImpl implements IRoleService {
tangying authored
35
游杰 authored
36
    @Resource
tangying authored
37
38
39
40
41
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;
游杰 authored
42
    @Resource
tangying authored
43
    private UserRoleMapper userRoleMapper;
游杰 authored
44
45
46
47
48
    @Resource
    private IRoleMenuService roleMenuService;
    @Resource
    private IMenuService menuService;
tangying authored
49
50
51
52
53
54
55
56
57
58

    /**
     * 根据条件分页查询角色数据
     * 
     * @param role 角色信息
     * @return 角色数据集合信息
     */
    @Override
    public List<Role> selectRoleList(Role role)
    {
周鸿 authored
59
        /*if(StringUtils.isEmpty(role.getWarehouseCode())) {
游杰 authored
60
            role.setWarehouseCode(ShiroUtils.getWarehouseCode());
周鸿 authored
61
        }*/
tangying authored
62
63
64
65
66
67
68
69
70
71
72
73
        return roleMapper.selectRoleList(role);
    }

    /**
     * 根据用户ID查询角色代码
     * 
     * @param userId 用户ID
     * @return 权限列表
     */
    @Override
    public Set<String> selectRoleCodes(Integer userId)
    {
74
        List<Role> perms = roleMapper.selectRolesByUserId(userId, ShiroUtils.getWarehouseCode());
tangying authored
75
76
77
        Set<String> permsSet = new HashSet<>();
        for (Role perm : perms)
        {
78
            if (StringUtils.isNotNull(perm))
tangying authored
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
            {
                permsSet.addAll(Arrays.asList(perm.getRoleCode().trim().split(",")));
            }
        }
        return permsSet;
    }

    /**
     * 根据用户ID查询角色
     * 
     * @param userId 用户ID
     * @return 角色列表
     */
    @Override
    public List<Role> selectRolesByUserId(Integer userId)
    {
周鸿 authored
95
96
        List<Role> userRoles = roleMapper.selectRolesByUserIdV1(userId);
        List<Role> roles = roleMapper.selectRolesAllV1();
tangying authored
97
98
99
100
        for (Role role : roles)
        {
            for (Role userRole : userRoles)
            {
101
                if (userRole!=null&&role.getId().intValue() == userRole.getId().intValue())
tangying authored
102
                {
游杰 authored
103
                    role.setFlag(true);
tangying authored
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
                    break;
                }
            }
        }
        return roles;
    }

    /**
     * 查询所有角色
     * 
     * @return 角色列表
     */
    @Override
    public List<Role> selectRoleAll()
    {
周鸿 authored
119
        return roleMapper.selectRolesAllV1();
tangying authored
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
    }

    /**
     * 通过角色ID查询角色
     * 
     * @param id 角色ID
     * @return 角色对象信息
     */
    @Override
    public Role selectRoleById(Integer id)
    {
        return roleMapper.selectRoleById(id);
    }

    /**
     * 通过角色ID删除角色
     * 
     * @param id 角色ID
     * @return 结果
     */
    @Override
    public boolean deleteRoleById(Integer id)
    {
143
        Integer result = roleMapper.deleteRoleById(ShiroUtils.getWarehouseCode(), id);
tangying authored
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
        return  result > 0 ? true : false;
    }

    /**
     * 批量删除角色信息
     * 
     * @param ids 需要删除的数据ID
     * @throws Exception
     */
    @Override
    public int deleteRoleByIds(String ids) throws Exception
    {
        Integer[] roleIds = Convert.toIntArray(ids);
        for (Integer id : roleIds)
        {
            Role role = selectRoleById(id);
            if (countUserRoleByRoleId(id) > 0)
            {
                throw new Exception(String.format("%1$s已分配,不能删除", role.getRoleName()));
            }
        }
165
        return roleMapper.deleteRoleByIds(ShiroUtils.getWarehouseCode(), roleIds);
tangying authored
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
    }

    /**
     * 新增保存角色信息
     * 
     * @param role 角色信息
     * @return 结果
     */
    @Override
    public int insertRole(Role role)
    {
        role.setWarehouseCode(ShiroUtils.getWarehouseCode());
        role.setCreateBy(ShiroUtils.getLoginName());
        // 新增角色信息
        roleMapper.insertRole(role);
        insertRoleMenu(role);
        ShiroUtils.clearCachedAuthorizationInfo();
        return 1;
    }

    /**
     * 修改保存角色信息
     * 
     * @param role 角色信息
     * @return 结果
     */
    @Override
    public int updateRole(Role role)
    {
        role.setWarehouseCode(ShiroUtils.getWarehouseCode());
        role.setUpdateBy(ShiroUtils.getLoginName());
        // 修改角色信息
        roleMapper.updateRole(role);
        // 删除角色与菜单关联
        roleMenuMapper.deleteRoleMenuByRoleId(role.getId());
        insertRoleMenu(role);
        ShiroUtils.clearCachedAuthorizationInfo();
        return  1;
    }

    /**
     * 新增角色菜单信息
     * 
     * @param role 角色对象
     */
    public void insertRoleMenu(Role role)
    {
        // 新增用户与角色管理
        List<RoleMenu> list = new ArrayList<RoleMenu>();
        for (Integer menuId : role.getMenuIds())
        {
            RoleMenu rm = new RoleMenu();
            rm.setRoleId(role.getId());
            rm.setMenuId(menuId);
            list.add(rm);
        }
        if (list.size() > 0)
        {
            roleMenuMapper.batchRoleMenu(list);
        }
    }

    /**
     * 校验角色名称是否唯一
     * 
     * @param role 角色信息
     * @return 结果
     */
    @Override
    public String checkRoleNameUnique(Role role)
    {
        Integer id = StringUtils.isNull(role.getId()) ? -1 : role.getId();
238
        Role info = roleMapper.checkRoleNameUnique(ShiroUtils.getWarehouseCode(), role.getRoleName());
tangying authored
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
        if (StringUtils.isNotNull(info) && info.getId().intValue() != id.intValue())
        {
            return UserConstants.ROLE_NAME_NOT_UNIQUE;
        }
        return UserConstants.ROLE_NAME_UNIQUE;
    }

    /**
     * 校验角色权限是否唯一
     * 
     * @param role 角色信息
     * @return 结果
     */
    @Override
    public String checkroleCodeUnique(Role role)
    {
        Integer id = StringUtils.isNull(role.getId()) ? -1 : role.getId();
256
        Role info = roleMapper.checkroleCodeUnique(ShiroUtils.getWarehouseCode(), role.getRoleCode());
tangying authored
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
        if (StringUtils.isNotNull(info) && info.getId().intValue() != id.intValue())
        {
            return UserConstants.ROLE_KEY_NOT_UNIQUE;
        }
        return UserConstants.ROLE_KEY_UNIQUE;
    }

    /**
     * 通过角色ID查询角色使用数量
     * 
     * @param id 角色ID
     * @return 结果
     */
    @Override
    public int countUserRoleByRoleId(Integer id)
    {
        return userRoleMapper.countUserRoleByRoleId(id);
    }
276
277
278

    //复制角色
    @Override
pengcheng authored
279
    @Transactional
280
    public Boolean roleCopy(String code, String newCode) {
pengcheng authored
281
282
283
284
285
        int i = 0;
        i = roleMapper.roleCopy(code,newCode);
        if(i < 1){
            throw new ServiceException("复制角色表失败");
        }
286
287
288
289
//        if(i < 1){
//            throw new ServiceException("复制角色菜单关系表失败");
//        }
        i = roleMapper.roleUserCopy(newCode,ShiroUtils.getUserId());
pengcheng authored
290
291
        if(i < 1){
            throw new ServiceException("复制用户角色关系表失败");
292
        }
pengcheng authored
293
        return true;
294
295
    }
游杰 authored
296
297
298
299
300
301
302
    @Override
    @Transactional
    public Boolean roleMenuCopy(String code, String newCode) {
        Role role = new Role();
        role.setWarehouseCode(code);
        List<Role> roles =  selectRoleList(role);
        List<Integer> roleIds = roles.stream().map(Role::getId).distinct().collect(Collectors.toList());
303
        LambdaQueryWrapper<RoleMenu> roleMenuLambdaQueryWrapper = Wrappers.lambdaQueryNoWarehouse();
游杰 authored
304
305
        roleMenuLambdaQueryWrapper.in(RoleMenu::getRoleId, roleIds);
        List<RoleMenu> roleMenuList = roleMenuService.list(roleMenuLambdaQueryWrapper);
游杰 authored
306
        List<RoleMenu> removeRoleMenuList = new ArrayList<>();
游杰 authored
307
308
309
310
311
312
313
314
        for(RoleMenu roleMenu : roleMenuList) {
            for(Role roleChild : roles) {
                if(roleMenu.getRoleId().equals(roleChild.getId())) {
                    Role role1 = new Role();
                    role1.setRoleName(roleChild.getRoleName());
                    role1.setRoleCode(roleChild.getRoleCode());
                    role1.setWarehouseCode(newCode);
                    Menu menu = menuService.selectMenuById(roleMenu.getMenuId());
游杰 authored
315
316
317
318
319
320
321
322
323
324
                    if(menu != null) {
                        menu.setWarehouseCode(newCode);
                        Menu menu2 = menuService.selectMenu(menu);
                        Role role2 = selectRoleList(role1).get(0);
                        roleMenu.setRoleId(role2.getId());
                        roleMenu.setMenuId(menu2.getId());
                        roleMenu.setId(null);
                    } else {
                        removeRoleMenuList.add(roleMenu);
                    }
游杰 authored
325
326
327
                }
            }
        }
游杰 authored
328
329
        roleMenuList.removeAll(removeRoleMenuList);
        roleMenuList = removeDupliByMorePro(roleMenuList);
游杰 authored
330
331
332
333
334
335
336
337
338
        try {
            int length =  roleMenuMapper.batchRoleMenu(roleMenuList);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
339
340
341
342
343
344
    @Override
    public List<Role> selectRolesByUserId(int userId,String warehouseCode) {
        return roleMapper.selectRolesByUserId(userId,warehouseCode);
    }

    private List<RoleMenu> removeDupliByMorePro(List<RoleMenu> persons){
游杰 authored
345
346
347
348
349
350
351
352
        List<RoleMenu> personList = persons.stream().collect(Collectors
                .collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> {
                    // 根据useId和userName进行去重
                    return o.getRoleId() + "," + o.getMenuId();
                }))), ArrayList::new));
        return  personList;
    }
tangying authored
353
}