刘光辉
6 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
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
package jnpf.dmsPermission;
 
import jnpf.dmsEntity.permission.model.DmsSubjectSet;
import jnpf.permission.OrganizeApi;
import jnpf.permission.PositionApi;
import jnpf.permission.entity.OrganizeEntity;
import jnpf.permission.entity.PositionEntity;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
@Component
@RequiredArgsConstructor
public class DmsSubjectHierarchyResolver {
    private static final String ROOT_PARENT_ID = "-1";
    private static final String LEGACY_ROOT_PARENT_ID = "0";
 
    private final OrganizeApi organizeApi;
    private final PositionApi positionApi;
 
    public void resolve(Collection<DmsSubjectSet> subjects) {
        resolve(subjects, false);
    }
 
    public void resolveStrict(Collection<DmsSubjectSet> subjects) {
        resolve(subjects, true);
    }
 
    private void resolve(Collection<DmsSubjectSet> subjects, boolean strict) {
        if (subjects == null || subjects.isEmpty()) {
            return;
        }
        resolvePositions(subjects, strict);
        resolveOrganizations(subjects, strict);
    }
 
    private void resolveOrganizations(Collection<DmsSubjectSet> subjects, boolean strict) {
        Set<String> directIds = collectDirectIds(subjects, true);
        if (directIds.isEmpty()) {
            return;
        }
        for (DmsSubjectSet subject : subjects) {
            if (subject != null) {
                subject.getAncestorOrgIds().addAll(subject.getDirectOrgIds());
            }
        }
 
        Map<String, OrganizeEntity> entitiesById = new LinkedHashMap<>();
        List<OrganizeEntity> entities = organizeApi.getListByIds(new ArrayList<>(directIds));
        if (entities != null) {
            for (OrganizeEntity entity : entities) {
                String id = entity == null ? null : normalizeId(entity.getId());
                if (id != null && directIds.contains(id)) {
                    entitiesById.putIfAbsent(id, entity);
                }
            }
        }
        rejectMissing(directIds, entitiesById.keySet(), "直属组织", strict);
 
        for (DmsSubjectSet subject : subjects) {
            if (subject == null) {
                continue;
            }
            for (String directId : subject.getDirectOrgIds()) {
                OrganizeEntity entity = entitiesById.get(directId);
                if (entity == null) {
                    continue;
                }
                List<String> hierarchy = parseHierarchy(entity.getOrganizeIdTree(), directId);
                subject.getAncestorOrgIds().addAll(hierarchy);
                addImmediateParent(subject.getImmediateParentOrgIds(), subject.getAncestorOrgIds(),
                        entity.getParentId(), hierarchy, directId);
            }
        }
    }
 
    private void resolvePositions(Collection<DmsSubjectSet> subjects, boolean strict) {
        Set<String> directIds = collectDirectIds(subjects, false);
        if (directIds.isEmpty()) {
            return;
        }
        for (DmsSubjectSet subject : subjects) {
            if (subject != null) {
                subject.getAncestorPositionIds().addAll(subject.getPositionIds());
            }
        }
 
        Map<String, PositionEntity> entitiesById = new LinkedHashMap<>();
        List<PositionEntity> entities = positionApi.getListByIds(new ArrayList<>(directIds));
        if (entities != null) {
            for (PositionEntity entity : entities) {
                String id = entity == null ? null : normalizeId(entity.getId());
                if (id != null && directIds.contains(id)) {
                    entitiesById.putIfAbsent(id, entity);
                }
            }
        }
        rejectMissing(directIds, entitiesById.keySet(), "直属岗位", strict);
 
        for (DmsSubjectSet subject : subjects) {
            if (subject == null) {
                continue;
            }
            for (String directId : subject.getPositionIds()) {
                PositionEntity entity = entitiesById.get(directId);
                if (entity == null) {
                    continue;
                }
                String organizationId = normalizeId(entity.getOrganizeId());
                if (organizationId != null) {
                    subject.getDirectOrgIds().add(organizationId);
                }
                List<String> hierarchy = parseHierarchy(entity.getPositionIdTree(), directId);
                subject.getAncestorPositionIds().addAll(hierarchy);
                addImmediateParent(subject.getImmediateParentPositionIds(),
                        subject.getAncestorPositionIds(), entity.getParentId(), hierarchy, directId);
            }
        }
    }
 
    private Set<String> collectDirectIds(Collection<DmsSubjectSet> subjects, boolean organization) {
        Set<String> directIds = new LinkedHashSet<>();
        for (DmsSubjectSet subject : subjects) {
            if (subject == null) {
                continue;
            }
            Set<String> source = organization ? subject.getDirectOrgIds() : subject.getPositionIds();
            Set<String> normalized = new LinkedHashSet<>();
            if (source != null) {
                for (String id : source) {
                    String value = normalizeId(id);
                    if (value != null) {
                        normalized.add(value);
                    }
                }
                source.clear();
                source.addAll(normalized);
            }
            directIds.addAll(normalized);
        }
        return directIds;
    }
 
    private List<String> parseHierarchy(String idTree, String directId) {
        Set<String> hierarchy = new LinkedHashSet<>();
        if (idTree != null) {
            for (String id : idTree.split(",")) {
                String value = normalizeId(id);
                if (value != null) {
                    hierarchy.add(value);
                }
            }
        }
        hierarchy.add(directId);
        return new ArrayList<>(hierarchy);
    }
 
    private void addImmediateParent(Set<String> parentIds, Set<String> ancestorIds,
                                    String entityParentId, List<String> hierarchy, String directId) {
        String parentId = normalizeId(entityParentId);
        if (parentId == null) {
            int directIndex = hierarchy.indexOf(directId);
            if (directIndex > 0) {
                parentId = hierarchy.get(directIndex - 1);
            }
        }
        if (parentId != null && !parentId.equals(directId)) {
            parentIds.add(parentId);
            ancestorIds.add(parentId);
        }
    }
 
    private void rejectMissing(Set<String> requestedIds, Set<String> foundIds, String subjectType,
                               boolean strict) {
        Set<String> missingIds = new LinkedHashSet<>(requestedIds);
        missingIds.removeAll(foundIds);
        if (strict && !missingIds.isEmpty()) {
            throw DmsPermissionException.of(DmsPermissionError.PRINCIPAL_NOT_FOUND,
                    subjectType + "主体不存在或主体服务不可用: " + missingIds);
        }
    }
 
    private String normalizeId(String id) {
        if (id == null) {
            return null;
        }
        String value = id.trim();
        return value.isEmpty() || ROOT_PARENT_ID.equals(value) || LEGACY_ROOT_PARENT_ID.equals(value)
                ? null : value;
    }
}