ny
昨天 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
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
package jnpf.permission.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jnpf.base.Pagination;
import jnpf.base.UserInfo;
import jnpf.base.service.SuperServiceImpl;
import jnpf.constant.CodeConst;
import jnpf.constant.PermissionConst;
import jnpf.permission.entity.AuthorizeEntity;
import jnpf.permission.entity.RoleEntity;
import jnpf.permission.entity.RoleRelationEntity;
import jnpf.permission.mapper.AuthorizeMapper;
import jnpf.permission.mapper.RoleMapper;
import jnpf.permission.mapper.RoleRelationMapper;
import jnpf.permission.model.position.PosConModel;
import jnpf.permission.model.role.RolePagination;
import jnpf.permission.service.CodeNumService;
import jnpf.permission.service.RoleService;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 系统角色
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2019年9月26日 上午9:18
 */
@Service
public class RoleServiceImpl extends SuperServiceImpl<RoleMapper, RoleEntity> implements RoleService {
    @Autowired
    private CodeNumService codeNumService;
    @Autowired
    private AuthorizeMapper authorizeMapper;
    @Autowired
    private RoleRelationMapper roleRelationMapper;
 
    @Override
    public List<RoleEntity> getList(RolePagination pagination) {
        return this.baseMapper.getList(pagination);
    }
 
    @Override
    public Boolean isExistByFullName(String fullName, String id, String type) {
        return this.baseMapper.isExistByFullName(fullName, id, type);
    }
 
    @Override
    public Boolean isExistByEnCode(String enCode, String id) {
        return this.baseMapper.isExistByEnCode(enCode, id);
    }
 
    /**
     * 设置编码
     *
     * @param entity
     */
    private void setEnCode(RoleEntity entity) {
        if (StringUtil.isEmpty(entity.getEnCode())) {
            String codeType = CodeConst.YHJS;
            if (PermissionConst.ORGANIZE.equals(entity.getType())) {
                codeType = CodeConst.ZZJS;
            } else if (PermissionConst.POSITION.equals(entity.getType())) {
                codeType = CodeConst.GWJS;
            }
            final String codeTypeP = codeType;
            entity.setEnCode(codeNumService.getCodeFunction(() -> codeNumService.getCodeOnce(codeTypeP), code -> isExistByEnCode(code, null)));
        }
    }
 
    @Override
    public void create(RoleEntity entity) {
        setEnCode(entity);
        this.baseMapper.create(entity);
    }
 
    @Override
    public Boolean update(String id, RoleEntity entity) {
        setEnCode(entity);
        if (Objects.equals(entity.getIsCondition(), 0)) {
            entity.setConditionJson("");
        }
        return this.baseMapper.update(id, entity);
    }
 
    @Override
    public RoleEntity getInfo(String id) {
        return this.baseMapper.getInfo(id);
    }
 
    @Override
    @Transactional
    public void delete(RoleEntity entity) {
        if (entity != null) {
            this.removeById(entity.getId());
            QueryWrapper<AuthorizeEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(AuthorizeEntity::getObjectId, entity.getId());
            authorizeMapper.deleteByIds(authorizeMapper.selectList(queryWrapper));
            QueryWrapper<RoleRelationEntity> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(RoleRelationEntity::getRoleId, entity.getId());
            roleRelationMapper.deleteByIds(roleRelationMapper.selectList(wrapper));
        }
    }
 
    @Override
    public RoleEntity getByEnCode(String enCode) {
        return this.baseMapper.getByEnCode(enCode);
    }
 
    @Override
    public List<RoleEntity> getList(boolean filterEnabledMark, String type, Integer isSystem) {
        return this.baseMapper.getList(filterEnabledMark, type, isSystem);
    }
 
    @Override
    public List<RoleEntity> getListByIds(List<String> id, String keyword, boolean filterEnabledMark) {
        return this.baseMapper.getListByIds(id, keyword, filterEnabledMark);
    }
 
    @Override
    public List<RoleEntity> getListByIds(List<String> idList) {
        return this.baseMapper.getListByIds(idList);
    }
 
    @Override
    public List<RoleEntity> getListByIds(Pagination pagination, List<String> idList) {
        return this.baseMapper.getListByIds(pagination, idList);
    }
 
    @Override
    public Map<String, Object> getRoleMap() {
        return this.baseMapper.getRoleMap();
    }
 
    @Override
    public Map<String, Object> getRoleNameAndIdMap() {
        return this.baseMapper.getRoleNameAndIdMap();
    }
 
    @Override
    public Map<String, Object> getRoleNameAndIdMap(boolean enabledMark) {
        return this.baseMapper.getRoleNameAndIdMap(enabledMark);
    }
 
    @Override
    public RoleEntity getInfoByFullName(String fullName) {
        return this.baseMapper.getInfoByFullName(fullName);
    }
 
    @Override
    public List<RoleEntity> getCurRolesByOrgId() {
        UserInfo user = UserProvider.getUser();
        //同组织下所有角色
        List<RoleRelationEntity> roleRelations = new ArrayList<>();
        roleRelations.addAll(roleRelationMapper.getListByObjectId(user.getOrganizeId(), PermissionConst.ORGANIZE));
        roleRelations.addAll(roleRelationMapper.getListByObjectId(user.getPositionId(), PermissionConst.POSITION));
        roleRelations.addAll(roleRelationMapper.getListByObjectId(user.getId(), PermissionConst.USER));
        if (CollectionUtil.isEmpty(roleRelations)) {
            return Collections.EMPTY_LIST;
        }
        List<String> roleIds = roleRelations.stream().map(RoleRelationEntity::getRoleId).collect(Collectors.toList());
        return this.baseMapper.getListByIds(roleIds, null, true);
    }
 
    @Override
    public List<RoleEntity> getList(List<String> idList, Pagination pagination, boolean filterEnabledMark) {
        return this.baseMapper.getList(idList, pagination, filterEnabledMark);
    }
 
    @Override
    public Map<String, Integer> roleUserCount() {
        Map<String, Integer> map = new HashMap<>();
        List<RoleEntity> list = this.baseMapper.getList(true, PermissionConst.USER, null);
        List<RoleRelationEntity> listByRoleId = roleRelationMapper.getListByRoleId("", PermissionConst.USER);
        Map<String, List<RoleRelationEntity>> roleGroup = listByRoleId.stream().collect(Collectors.groupingBy(RoleRelationEntity::getRoleId));
        for (RoleEntity role : list) {
            map.put(role.getFullName(), CollectionUtil.isEmpty(roleGroup.get(role.getId())) ? 0 : roleGroup.get(role.getId()).size());
        }
        return map;
    }
 
    @Override
    public List<RoleEntity> getUserRoles(String userId) {
        List<String> roleIds = roleRelationMapper.getListByObjectId(userId, PermissionConst.USER).stream().map(RoleRelationEntity::getRoleId).collect(Collectors.toList());
        return this.getListByIds(roleIds);
    }
 
    @Override
    public void linkUpdate(String id, PosConModel posConModel) {
        this.baseMapper.linkUpdate(id, posConModel);
    }
 
}