彭雪彬
2025-07-14 c1d20b425b10e8ba59f102dd1ab413055883eed0
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
package com.oying.modules.system.service.impl;
 
import cn.hutool.core.collection.CollUtil;
import com.oying.modules.system.domain.Role;
import com.oying.modules.system.domain.User;
import lombok.RequiredArgsConstructor;
import com.oying.modules.system.service.DataService;
import com.oying.modules.system.service.RoleService;
import com.oying.utils.CacheKey;
import com.oying.utils.RedisUtils;
import com.oying.utils.enums.DataScopeEnum;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.concurrent.TimeUnit;
 
/**
 * @author Z
 * @description 数据权限服务实现
 * @date 2020-05-07
 **/
@Service
@RequiredArgsConstructor
public class DataServiceImpl implements DataService {
 
    private final RedisUtils redisUtils;
    private final RoleService roleService;
 
    /**
     * 用户角色和用户机构改变时需清理缓存
     * @param user /
     * @return /
     */
    @Override
    public List<Long> getDataIds(User user) {
        String key = CacheKey.DATA_USER + user.getId();
        List<Long> ids = redisUtils.getList(key, Long.class);
        if (CollUtil.isEmpty(ids)) {
            Set<Long> dataIds = new HashSet<>();
            // 查询用户角色
            List<Role> roleList = roleService.findByUsersId(user.getId());
            // 获取对应的机构ID
            for (Role role : roleList) {
                DataScopeEnum dataScopeEnum = DataScopeEnum.find(role.getDataScope());
                switch (Objects.requireNonNull(dataScopeEnum)) {
                    case THIS_LEVEL:
                        break;
                    case CUSTOMIZE:
                        dataIds.addAll(getCustomize(dataIds, role));
                        break;
                    default:
                        return new ArrayList<>();
                }
            }
            ids = new ArrayList<>(dataIds);
            redisUtils.set(key, ids, 1, TimeUnit.DAYS);
        }
        return ids;
    }
 
    /**
     * 获取自定义的数据权限
     * @param dataIds 机构ID
     * @param role 角色
     * @return 数据权限ID
     */
    public Set<Long> getCustomize(Set<Long> dataIds, Role role){
        System.out.println(role);
        return dataIds;
    }
}