rk
3 天以前 5df732ffe16b3f162422c2db61a78458e28c7f8d
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
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
238
239
240
package com.doumee.service.aware;
 
import com.doumee.core.aware.DataPermissionMapping;
import com.doumee.core.aware.DefaultDataPermissionAware;
import com.doumee.core.constants.DataPermissionConstants;
import com.doumee.dao.system.model.SystemPosition;
import com.doumee.dao.system.model.SystemPositionUser;
import com.doumee.dao.system.vo.SystemPositionListVO;
import com.doumee.service.system.SystemPositionService;
import com.doumee.service.system.SystemPositionUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
/**
 * 岗位数据权限控制
 * @author Eva.Caesar Liu
 * @date 2023/03/21 14:49
 */
@Component
public class PositionDataPermissionAware extends DefaultDataPermissionAware<SystemPositionListVO> {
 
    @Autowired
    private SystemPositionService systemPositionService;
 
    @Autowired
    private SystemPositionUserService systemPositionUserService;
 
    @Override
    public DataPermissionConstants.Module module() {
        return DataPermissionConstants.Module.POSITION;
    }
 
    @Override
    public List<SystemPositionListVO> defaultData(Integer userId) {
        return this.onlyUser(userId);
    }
 
    /**
     * 全部数据
     *
     * @return List<SystemPositionListVO>
     */
    @DataPermissionMapping(value = DataPermissionConstants.Type.ALL, priority = 1)
    public List<SystemPositionListVO> all() {
        return this.getRootList(systemPositionService.findList());
    }
 
    /**
     * 自定义
     *
     * @param customData 自定义数据ID集
     * @return List<SystemPositionListVO>
     */
    @DataPermissionMapping(value = DataPermissionConstants.Type.POSITION_CUSTOM, priority = 2, injectCustomData = true)
    public List<SystemPositionListVO> custom(String customData) {
        if (StringUtils.isBlank(customData)) {
            return Collections.emptyList();
        }
        List<Integer> ids = new ArrayList<>();
        String[] stringIds = customData.split(",");
        for(String stringId : stringIds) {
            ids.add(Integer.valueOf(stringId));
        }
        return this.getRootList(toSystemPositionListVOs(systemPositionService.findByIds(ids)));
    }
 
    /**
     * 用户所属岗位及其子孙岗位
     *
     * @param userId 用户ID
     * @return List<SystemPositionListVO>
     */
    @DataPermissionMapping(value = DataPermissionConstants.Type.POSITION_CHILDREN, priority = 3, injectUser = true)
    public List<SystemPositionListVO> children(Integer userId) {
        return this.getRootList(getUserChildren(userId));
    }
 
    /**
     * 用户所属岗位及其子岗位
     *
     * @param userId 用户ID
     * @return List<SystemPositionListVO>
     */
    @DataPermissionMapping(value = DataPermissionConstants.Type.POSITION_CHILD, priority = 4, injectUser = true)
    public List<SystemPositionListVO> child(Integer userId) {
        List<SystemPositionListVO> children = this.getRootList(getUserChildren(userId));
        for (SystemPositionListVO root : children) {
            if (CollectionUtils.isEmpty(root.getChildren())) {
                continue;
            }
            for (SystemPositionListVO child : root.getChildren()) {
                if (CollectionUtils.isEmpty(child.getChildren())) {
                    continue;
                }
                child.setHasChildren(Boolean.TRUE);
                child.setChildren(null);
            }
        }
        return children;
    }
 
    /**
     * 仅用户所属岗位
     *
     * @param userId 用户ID
     * @return List<SystemPositionListVO>
     */
    @DataPermissionMapping(value = DataPermissionConstants.Type.POSITION, priority = 5, injectUser = true)
    public List<SystemPositionListVO> onlyUser(Integer userId) {
        List<SystemPositionListVO> userPositions = this.getUserPositions(userId);
        if (CollectionUtils.isEmpty(userPositions)) {
            return Collections.emptyList();
        }
        return this.getRootList(userPositions);
    }
 
    /**
     * 获取根岗位
     */
    private List<SystemPositionListVO> getRootList(List<SystemPositionListVO> positions) {
        List<SystemPositionListVO> rootPositions = new ArrayList<>();
        // 添加根岗位
        for (SystemPositionListVO currentPosition : positions) {
            boolean hasParent = false;
            for (SystemPositionListVO position: positions) {
                if (position.getId().equals(currentPosition.getParentId())) {
                    hasParent = true;
                    break;
                }
            }
            if (!hasParent) {
                rootPositions.add(currentPosition);
            }
        }
        // 移除根岗位
        for (SystemPositionListVO rootPosition : rootPositions) {
            positions.removeIf(position -> position.getId().equals(rootPosition.getId()));
        }
        // 填充子岗位
        for (SystemPositionListVO child : rootPositions) {
            this.fillChildren(child, positions);
        }
        return rootPositions;
    }
 
    /**
     * 获取用户岗位及其子孙岗位
     */
    private List<SystemPositionListVO> getUserChildren(Integer userId) {
        List<SystemPositionListVO> userPositions = this.getUserPositions(userId);
        if (CollectionUtils.isEmpty(userPositions)) {
            return Collections.emptyList();
        }
        // 查询用户所在岗位以下岗位
        List<SystemPositionListVO> positionListVos = new ArrayList<>();
        for (SystemPositionListVO userPosition : userPositions) {
            List<SystemPosition> underPositions = systemPositionService.findByIds(systemPositionService.findChildren(userPosition.getId()));
            for (SystemPosition underPosition : underPositions) {
                if(positionListVos.stream().anyMatch(item -> item.getId().equals(underPosition.getId()))) {
                    continue;
                }
                SystemPositionListVO vo = new SystemPositionListVO();
                BeanUtils.copyProperties(underPosition, vo);
                positionListVos.add(vo);
            }
            if(positionListVos.stream().noneMatch(item -> item.getId().equals(userPosition.getId()))) {
                positionListVos.add(userPosition);
            }
        }
        return positionListVos;
    }
 
    /**
     * 获取用户岗位
     */
    private List<SystemPositionListVO> getUserPositions(Integer userId) {
        SystemPositionUser queryDto = new SystemPositionUser();
        queryDto.setUserId(userId);
        queryDto.setDeleted(Boolean.FALSE);
        List<SystemPositionUser> positionUsers = systemPositionUserService.findList(queryDto);
        if (CollectionUtils.isEmpty(positionUsers)) {
            return null;
        }
        List<Integer> ids = new ArrayList<>();
        for (SystemPositionUser positionUser : positionUsers) {
            ids.add(positionUser.getPositionId());
        }
        return toSystemPositionListVOs(systemPositionService.findByIds(ids));
    }
 
    /**
     * 转为ListVO对象集合
     */
    private List<SystemPositionListVO> toSystemPositionListVOs (List<SystemPosition> systemPositions) {
        List<SystemPositionListVO> positionListVOs = new ArrayList<>();
        for (SystemPosition systemPosition : systemPositions) {
            SystemPositionListVO positionListVO = new SystemPositionListVO();
            BeanUtils.copyProperties(systemPosition, positionListVO);
            positionListVOs.add(positionListVO);
        }
        return positionListVOs;
    }
 
    /**
     * 填充子岗位
     */
    private void fillChildren(SystemPositionListVO parent, List<SystemPositionListVO> positions) {
        if (positions.size() == 0) {
            return;
        }
        if (parent.getChildren() == null) {
            parent.setChildren(new ArrayList<>());
        }
        List<Integer> handledIds = new ArrayList<>();
        for (SystemPositionListVO position : positions) {
            if (parent.getId().equals(position.getParentId())) {
                SystemPositionListVO child = new SystemPositionListVO();
                BeanUtils.copyProperties(position, child, "children");
                child.setChildren(new ArrayList<>());
                parent.getChildren().add(child);
                handledIds.add(position.getId());
            }
        }
        positions.removeIf(menu -> handledIds.contains(menu.getId()));
        parent.setHasChildren(Boolean.TRUE);
        if (parent.getChildren().size() > 0) {
            parent.setHasChildren(Boolean.FALSE);
            for (SystemPositionListVO child : parent.getChildren()) {
                this.fillChildren(child, positions);
            }
        }
    }
}