1
zyy
2025-11-04 f02b01f524ee3f48f460890881d8411dc5e7413a
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
package com.yami.trading.admin.facade;
 
import cn.hutool.core.collection.CollectionUtil;
import com.yami.trading.bean.model.User;
import com.yami.trading.common.exception.BusinessException;
import com.yami.trading.common.util.RedisUtil;
import com.yami.trading.security.common.util.SecurityUtils;
import com.yami.trading.service.user.UserRecomService;
import com.yami.trading.service.user.UserService;
import com.yami.trading.sys.model.SysRole;
import com.yami.trading.sys.service.SysRoleService;
import com.yami.trading.sys.service.SysUserService;
import lombok.extern.flogger.Flogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@Slf4j
@Component
public class PermissionFacade {
    @Autowired
    private UserRecomService userRecomService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    SysRoleService sysRoleService;
    @Autowired
    UserService userService;
 
    public boolean checkAgent() {
 
        List<Long> roleIds = sysRoleService.listRoleIdByUserId(SecurityUtils.getSysUser().getUserId());
        Map<Long, SysRole> sysRoleMap = sysRoleService.list().stream().collect(Collectors.toMap(SysRole::getRoleId, SysRole -> SysRole));
        List<String> roleNames = new ArrayList<>();
        boolean isAgent = false;
        for (Long id : roleIds) {
            if (sysRoleMap.containsKey(id)) {
                if (sysRoleMap.get(id).getRoleName().equals("代理商")) {
                    isAgent = true;
                }
            }
        }
        return isAgent;
    }
 
    public  List<String> getCahceOwnerUserIds(){
        String userName = SecurityUtils.getSysUser().getUsername();
        if (userName.equals("admin")) {
            return null;
        }
        String key="agent:user:username:"+userName;
        List<String> userIds=    RedisUtil.get(key);
        if (userIds!=null){
           userIds=   getOwnerUserIds();
           RedisUtil.set(key,userIds,60);
        }
        return userIds;
    }
 
    /**
     * 可以看到的前端用户的数据,如果没有,传入-,null表示所有所的权限
     *
     * @return
     */
    public List<String> getOwnerUserIds() {
        String userName = SecurityUtils.getSysUser().getUsername();
        if (userName.equals("admin")) {
            return null;
        }
        if (checkAgent()) {
            User user = userService.findByUserName(userName);
 
            if (user == null) {
                throw new BusinessException("账号异常");
            }
            List<String> checked_list = userRecomService.
                    findChildren(user.getUserId());
            if (checked_list.size() > 0) {
                return checked_list;
            } else {
                checked_list.add(userName);
                return checked_list;
            }
        } else {
            return null;
        }
//        if (CollectionUtil.isNotEmpty(roleNames) && roleNames.contains(Constants.SECURITY_ROLE_AGENT)) {
//            List<String> children = this.userRecomService.findChildren(userId.toString());
//            if (CollectionUtil.isEmpty(children)) {
//                children = Lists.newArrayList("-");
//            }
//            return children;
//        } else {
//            return null;
//        }
    }
 
 
    /**
     * 可以看到的前端用户的数据,如果没有,传入-,null表示所有所的权限
     *
     * @return
     */
    public List<String> getAdminOwnerUserIds() {
        String userName = SecurityUtils.getSysUser().getUsername();
        if (userName.equals("admin")) {
            return null;
        }
        if (checkAgent()) {
            User user = userService.findByUserName(userName);
 
            if (user == null) {
                throw new BusinessException("账号异常");
            }
            List<String> checked_list = userRecomService.
                    findChildren(user.getUserId());
            //log.info("getAdminOwnerUserIds => "+checked_list);
            for (int i = 0 ; checked_list!=null && i < checked_list.size() ; i++) {
                String id = checked_list.get(i);
                User child = userService.findByUserId(id);
//                log.info("getAdminOwnerUserIds id=> "+id);
//                log.info("getAdminOwnerUserIds child=> "+child);
                User virtuallyUser = userService.findByUserName(child.getUserId());
                if(virtuallyUser!=null){
                    //log.info("getAdminOwnerUserIds virtuallyUser=> "+virtuallyUser);
                    checked_list.add(virtuallyUser.getUserId());
                }
            }
 
 
            if (checked_list.size() > 0) {
                return checked_list;
            } else {
                checked_list.add(userName);
                return checked_list;
            }
        } else {
            return null;
        }
//        if (CollectionUtil.isNotEmpty(roleNames) && roleNames.contains(Constants.SECURITY_ROLE_AGENT)) {
//            List<String> children = this.userRecomService.findChildren(userId.toString());
//            if (CollectionUtil.isEmpty(children)) {
//                children = Lists.newArrayList("-");
//            }
//            return children;
//        } else {
//            return null;
//        }
    }
 
 
}