add
yj
2024-12-05 b9900893177c78fc559223521fe839aa21000017
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
package com.iotechn.unimall.admin.api.role;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iotechn.unimall.data.domain.AdminDO;
import com.iotechn.unimall.data.domain.RoleDO;
import com.iotechn.unimall.data.domain.RolePermissionDO;
import com.iotechn.unimall.data.dto.RoleSetPermissionDTO;
import com.iotechn.unimall.data.enums.RoleStatusType;
import com.iotechn.unimall.data.exception.ExceptionDefinition;
import com.iotechn.unimall.data.mapper.AdminMapper;
import com.iotechn.unimall.data.mapper.RoleMapper;
import com.iotechn.unimall.data.mapper.RolePermissionMapper;
import com.dobbinsoft.fw.core.exception.AdminServiceException;
import com.dobbinsoft.fw.core.exception.ServiceException;
import com.dobbinsoft.fw.support.model.Page;
import com.dobbinsoft.fw.support.model.PermissionPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * Created by rize on 2019/4/11.
 */
@Service
public class RoleServiceImpl implements RoleService {
 
    @Autowired
    private RoleMapper roleMapper;
 
    @Autowired
    private AdminMapper adminMapper;
 
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
 
    private List<PermissionPoint> permDTOs;
 
    private Set<String> allPermPoint = new HashSet<>();
 
    public void setPerms(List<PermissionPoint> permDTOs) {
        this.permDTOs = permDTOs;
        this.getPermPoints(permDTOs);
    }
 
    private void getPermPoints(List<PermissionPoint> permDTOs) {
        for (PermissionPoint permissionPoint : permDTOs) {
            String api = permissionPoint.getApi();
            if (ObjectUtils.isEmpty(api) && !ObjectUtils.isEmpty(permissionPoint.getChildren())) {
                this.getPermPoints(permissionPoint.getChildren());
            } else {
                allPermPoint.add(permissionPoint.getId());
            }
        }
    }
 
    @Override
    public Page<RoleDO> list(String name, Integer page, Integer limit, Long adminId) throws ServiceException {
        QueryWrapper<RoleDO> wrapper = new QueryWrapper<RoleDO>();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        return roleMapper.selectPage(Page.div(page,limit,RoleDO.class),wrapper);
    }
 
    @Override
    public RoleDO create(RoleDO roleDO, Long adminId) throws ServiceException {
        Date now = new Date();
        roleDO.setStatus(RoleStatusType.ACTIVE.getCode());
        roleDO.setGmtUpdate(now);
        roleDO.setGmtCreate(now);
        if (roleMapper.insert(roleDO) > 0) {
            return roleDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long roleId, Long adminId) throws ServiceException {
        // 列举出所有管理员,并校验是否存在关联此角色的管理员
        List<List<Long>> roleIds = adminMapper.selectList(new QueryWrapper<AdminDO>().select("role_ids")).stream().map(item -> JSONObject.parseArray(item.getRoleIds(), Long.class)).collect(Collectors.toList());
        for (List<Long> list : roleIds) {
            for (Long id : list) {
                if (roleId.longValue() == id.longValue()) {
                    throw new AdminServiceException(ExceptionDefinition.ADMIN_ROLE_UNION_ADMIN);
                }
            }
        }
        if (roleMapper.deleteById(roleId) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }
 
    @Override
    public RoleDO update(RoleDO roleDO, Long adminId) throws ServiceException {
        if (roleMapper.updateById(roleDO) > 0) {
            return roleDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String permissionSet(RoleSetPermissionDTO roleSetPermissionDTO, Long adminId) throws ServiceException {
        Long roleId = roleSetPermissionDTO.getRoleId();
        if (roleId == null) {
            throw new AdminServiceException(ExceptionDefinition.PARAM_CHECK_FAILED);
        }
        rolePermissionMapper.delete(new QueryWrapper<RolePermissionDO>().eq("role_id", roleId));
        //构建插入
        List<String> permissions = roleSetPermissionDTO.getPermissions();
        if (!CollectionUtils.isEmpty(permissions)) {
            Date now = new Date();
            for (String permission : permissions) {
                RolePermissionDO rolePermissionDO = new RolePermissionDO();
                rolePermissionDO.setRoleId(roleId);
                rolePermissionDO.setPermission(permission);
                rolePermissionDO.setGmtCreate(now);
                rolePermissionDO.setGmtUpdate(now);
                rolePermissionMapper.insert(rolePermissionDO);
            }
        }
        return "ok";
    }
 
    @Override
    public Map<String,Object> permissionList(Long roleId, Long adminId) throws ServiceException {
        List<RolePermissionDO> rolePermissionDOList =
                rolePermissionMapper.selectList(
                        new QueryWrapper<RolePermissionDO>()
                                .eq("role_id", roleId));
 
        Map<String,Object> map = new HashMap<>();
        Set<String> permissionPoint = new HashSet<>();
        for (RolePermissionDO permissionDO : rolePermissionDOList) {
            if ("*".equals(permissionDO.getPermission())) {
                //若为超级管理员,则直接push所有权限进去
                permissionPoint.addAll(allPermPoint);
                break;
            } else {
                permissionPoint.add(permissionDO.getPermission());
            }
        }
        map.put("systemPermissions", permDTOs);
        map.put("assignedPermissions", permissionPoint);
        return map;
    }
 
    @Override
    public List<Map<String, Object>> options(Long adminId) throws ServiceException {
        List<RoleDO> roleDOS = roleMapper.selectList(new QueryWrapper<>());
        List<Map<String,Object>> list = new LinkedList<>();
        roleDOS.forEach(item -> {
            Map<String,Object> map = new HashMap<>();
            map.put("value", item.getId());
            map.put("label", item.getName());
            list.add(map);
        });
        return list;
    }
 
}