刘光辉
16 小时以前 34981c30a78e8bbd7791131059a9210f9928b62c
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
package jnpf.permission.service.impl;
 
import jnpf.base.service.SuperServiceImpl;
import jnpf.constant.CodeConst;
import jnpf.constant.PermissionConst;
import jnpf.emnus.SysParamEnum;
import jnpf.permission.entity.*;
import jnpf.permission.mapper.*;
import jnpf.permission.model.permissiongroup.PaginationPermissionGroup;
import jnpf.permission.service.CodeNumService;
import jnpf.permission.service.PermissionGroupService;
import jnpf.util.RandomUtil;
import jnpf.util.StringUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.util.*;
import java.util.stream.Collectors;
 
@Service
@RequiredArgsConstructor
public class PermissionGroupServiceImpl extends SuperServiceImpl<PermissionGroupMapper, PermissionGroupEntity> implements PermissionGroupService {
 
    private final  CodeNumService codeNumService;
    private final  AuthorizeMapper authorizeMapper;
    private final  PositionMapper positionMapper;
    private final  OrganizeMapper organizeMapper;
    private final  RoleRelationMapper roleRelationMapper;
    private final  UserRelationMapper userRelationMapper;
 
    @Override
    public List<PermissionGroupEntity> list(PaginationPermissionGroup pagination) {
        return this.baseMapper.list(pagination);
    }
 
    @Override
    public List<PermissionGroupEntity> list(boolean filterEnabledMark, List<String> ids) {
        return this.baseMapper.list(filterEnabledMark, ids);
    }
 
    @Override
    public PermissionGroupEntity info(String id) {
        return this.baseMapper.info(id);
    }
 
    @Override
    public boolean create(PermissionGroupEntity entity) {
        if (StringUtil.isEmpty(entity.getEnCode())) {
            entity.setEnCode(codeNumService.getCodeFunction(() -> codeNumService.getCodeOnce(CodeConst.QXJH), code -> this.isExistByEnCode(code, null)));
        }
        return this.baseMapper.create(entity);
    }
 
    @Override
    public boolean update(String id, PermissionGroupEntity entity) {
        if (StringUtil.isEmpty(entity.getEnCode())) {
            entity.setEnCode(codeNumService.getCodeFunction(() -> codeNumService.getCodeOnce(CodeConst.QXJH), code -> this.isExistByEnCode(code, null)));
        }
        return this.baseMapper.update(id, entity);
    }
 
    @Override
    public boolean delete(PermissionGroupEntity entity) {
        return this.baseMapper.delete(entity);
    }
 
    @Override
    public boolean isExistByFullName(String id, PermissionGroupEntity entity) {
        return this.baseMapper.isExistByFullName(id, entity);
    }
 
    @Override
    public boolean isExistByEnCode(String enCode, String id) {
        return this.baseMapper.isExistByEnCode(enCode, id);
    }
 
    @Override
    public List<PermissionGroupEntity> list(List<String> ids) {
        return this.baseMapper.list(ids);
    }
 
    @Override
    public PermissionGroupEntity permissionMember(String id) {
        return this.baseMapper.permissionMember(id);
    }
 
    @Override
    public List<PermissionGroupEntity> getPermissionGroupByObjectId(String objectId, String objectType) {
        List<PermissionGroupEntity> permissionGroupEntities = this.list(true, null).stream()
                .filter(t -> StringUtil.isNotEmpty(t.getPermissionMember())).collect(Collectors.toList());
        String id = objectId + "--" + objectType;
        return permissionGroupEntities.stream()
                .filter(entity -> entity.getPermissionMember().contains(id)).collect(Collectors.toList());
    }
 
    @Override
    public List<String> setAuthByIds(String id, List<String> ids) {
        List<AuthorizeEntity> hasAuthList = authorizeMapper.getListByObjectId(Arrays.asList(id));
        Set<String> orgIds = new HashSet<>();
        Set<String> posIds = new HashSet<>();
        Set<String> roleIds = new HashSet<>();
        Set<String> objectIds = new HashSet<>();
        for (String s : ids) {
            if (s.contains("--")) {
                String[] split = s.split("--");
                String thisId = split[0];
                String thisType = split[1];
                if (SysParamEnum.ORG.getCode().equals(thisType)) {
                    orgIds.add(thisId);
                }
                if (SysParamEnum.POS.getCode().equals(thisType)) {
                    posIds.add(thisId);
                }
                if (SysParamEnum.ROLE.getCode().equals(thisType)) {
                    roleIds.add(thisId);
                }
            }
        }
        List<PositionEntity> posList = positionMapper.getListByIds(new ArrayList<>(posIds));
        for (PositionEntity item : posList) {
            posIds.addAll(Arrays.asList(item.getPositionIdTree().split(",")));
            orgIds.add(item.getOrganizeId());
        }
 
        List<OrganizeEntity> orgList = organizeMapper.getListByIds(new ArrayList<>(orgIds));
        for (OrganizeEntity item : orgList) {
            orgIds.addAll(Arrays.asList(item.getOrganizeIdTree().split(",")));
        }
        objectIds.addAll(posIds);
        objectIds.addAll(orgIds);
        objectIds.addAll(roleIds);
        addAuthList(posIds, hasAuthList, PermissionConst.POSITION);
        addAuthList(orgIds, hasAuthList, PermissionConst.ORGANIZE);
        addAuthList(roleIds, hasAuthList, PermissionConst.ROLE);
 
        Set<String> userIds = new HashSet<>();
        List<String> roleUser = roleRelationMapper.getListByRoleId(new ArrayList<>(roleIds), null).stream()
                .map(RoleRelationEntity::getObjectId).collect(Collectors.toList());
        userIds.addAll(roleUser);
        List<String> orgUser = userRelationMapper.getListByObjectIdAll(new ArrayList<>(objectIds)).stream()
                .map(UserRelationEntity::getUserId).collect(Collectors.toList());
        userIds.addAll(orgUser);
        return new ArrayList<>(userIds);
    }
 
    /**
     * 添加权限
     *
     * @param roleIds
     * @param hasAuthList
     * @param role
     */
    private void addAuthList(Set<String> roleIds, List<AuthorizeEntity> hasAuthList, String role) {
        for (String objectId : roleIds) {
            List<AuthorizeEntity> thisAuth = authorizeMapper.getListByObjectId(objectId, null);
            Set<String> list2Keys = thisAuth.stream()
                    .map(item -> item.getItemId() + "|" + item.getItemType())
                    .collect(Collectors.toSet());
            hasAuthList.stream().forEach(t -> {
                if (!list2Keys.contains(t.getItemId() + "|" + t.getItemType())) {
                    AuthorizeEntity authorizeEntity = new AuthorizeEntity();
                    authorizeEntity.setId(RandomUtil.uuId());
                    authorizeEntity.setObjectId(objectId);
                    authorizeEntity.setObjectType(role);
                    authorizeEntity.setItemType(t.getItemType());
                    authorizeEntity.setItemId(t.getItemId());
                    authorizeMapper.insert(authorizeEntity);
                }
            });
        }
    }
}