UserApiService.java
7.59 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
package cn.com.glsx.admin.modules.service;
import cn.com.glsx.admin.modules.model.param.BasicSearch;
import cn.com.glsx.usercenter.api.dto.request.FindUserRequest;
import cn.com.glsx.usercenter.api.dto.response.User;
import cn.com.glsx.usercenter.api.feign.ApiUserService;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.core.web.R;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* 用户服务
*/
@Slf4j
@Service
public class UserApiService {
@Autowired
private cn.com.glsx.usercenter.api.feign.UserService userService;
/**
* 不需要登录得开放用户服务
*/
@Autowired
private ApiUserService apiUserService;
/**
* 根据用户id查找用户信息
*
* @param userId
* @return
*/
public User getUserById(Long userId) {
R<User> userR = userService.getById(userId);
if (userR == null) {
return null;
}
User user = userR.getData(User.class);
return user;
}
/**
* 根据用户id查找上级用户信息
*
* @param userId
* @return
*/
public User getSuperiorUserById(Long userId) {
R<User> userR = userService.getSuperiorUserById(userId);
if (userR == null) {
return null;
}
User user = userR.getData(User.class);
return user;
}
/**
* 根据用户id查找上级用户信息
*
* @param userIds
* @return
*/
public Map<Long, User> getSuperiorUserMapByIds(Collection<Long> userIds) {
Map<Long, User> userMap = Maps.newHashMap();
R<List<User>> listR = userService.getSuperiorUserByIds(userIds);
if (listR == null) {
return userMap;
}
List<User> userList = listR.getDataList(new TypeToken<List<User>>() {
}.getType());
if (CollectionUtils.isNotEmpty(userList)) {
userMap = userList.stream().collect(Collectors.toMap(User::getMid, Function.identity()));
}
return userMap;
}
/**
* 根据用户名称查找用户id
*
* @param username
* @return
*/
public Long getUserIdByUsername(String username) {
if (StringUtils.isNullOrEmpty(username)) {
return null;
}
R<List<User>> userR = apiUserService.findByUsername(username);
if (userR == null) {
return null;
}
Long userId = null;
List<User> userList = userR.getDataList(new TypeToken<List<User>>() {
}.getType());
if (CollectionUtils.isNotEmpty(userList) && userList.size() == 1) {
userId = userList.get(0).getId();
}
return userId;
}
/**
* 根据用户id获取用户列表
*
* @param userIds
* @return
*/
public List<User> findByUser(Set<Long> userIds) {
R<List<User>> userR = userService.findByUser(new FindUserRequest().setUserIds(userIds));
if (userR == null || !userR.isSuccess()) {
return Lists.newArrayList();
}
List<User> userList = userR.getDataList(new TypeToken<List<User>>() {
}.getType());
return userList;
}
// @Cacheable(value = "user", key = "#userId")
// public User getUser(Long userId) {
// List<User> userList = this.findAllUser();
// Map<Long, User> userMap = userList.stream().collect(Collectors.toMap(User::getId, t -> t));
// return userMap.get(userId);
// }
/**
* 根据组织id获取用户列表
*
* @param orgIds
* @return
*/
public List<User> findAllByOrg(List<Long> orgIds) {
R<List<User>> byOrg = userService.findAllByOrg(new FindUserRequest().setOrgIds(orgIds));
if (byOrg == null || !byOrg.isSuccess()) {
return Lists.newArrayList();
}
List<User> userList = byOrg.getDataList(new TypeToken<List<User>>() {
}.getType());
return userList;
}
/**
* 根据组织id获取用户列表-用户权限内
*
* @param orgIds
* @return
*/
public List<User> findByOrg(List<Long> orgIds) {
R<List<User>> byOrg = userService.findByOrg(new FindUserRequest().setOrgIds(orgIds));
if (byOrg == null || !byOrg.isSuccess()) {
return Lists.newArrayList();
}
List<User> userList = byOrg.getDataList(new TypeToken<List<User>>() {
}.getType());
return userList;
}
/**
* 查找所有用户信息
*
* @return
*/
public List<User> findAllUser() {
R<List<User>> listR = userService.findAll();
if (listR == null || !listR.isSuccess()) {
return Lists.newArrayList();
}
List<User> userList = listR.getDataList(new TypeToken<List<User>>() {
}.getType());
return userList;
}
/**
* 封装用户信息 key:用户账号 value:用户对象
*
* @return
*/
public Map<String, User> findUserMapByAccount() {
Map<String, User> userMap = Maps.newHashMap();
List<User> userList = this.findAllUser();
if (userList == null) {
return userMap;
}
//要去重,避免以后出现不同租户下允许相同账号名的需求
userMap = userList.stream().collect(Collectors.toMap(User::getAccount, Function.identity(), (o, n) -> n));
return userMap;
}
/**
* 封装用户信息 key:用户id value:用户对象
*
* @return
*/
public Map<Long, User> findUserMapByUserId() {
Map<Long, User> userMap = Maps.newHashMap();
List<User> userList = this.findAllUser();
if (userList == null) {
return userMap;
}
userMap = userList.stream().collect(Collectors.toMap(User::getId, Function.identity()));
return userMap;
}
public void setCheckedOrgUsers(BasicSearch search) {
if (CollectionUtils.isEmpty(search.getCheckedOrgIds())) {
return;
}
if ("u".equals(search.getOrgType())) {
search.setCheckedUserIdsStr(StringUtils.join(search.getCheckedOrgIds().toArray(), ","));
} else {
List<User> userList = this.findAllByOrg(Lists.newArrayList(search.getCheckedOrgIds()));
if (CollectionUtils.isEmpty(userList)) {
search.setCheckedUserIdsStr("0");
} else {
search.setCheckedUserIdsStr(StringUtils.join(userList.stream().map(User::getId).toArray(), ","));
}
log.info("组织机构选中节点用户:{}", search.getCheckedUserIdsStr());
}
}
public Map<String, User> getUserListByUserIds(Set<Long> userIds) {
Map<String, User> userMapper = new HashedMap();
if (CollectionUtils.isEmpty(userIds)) {
return userMapper;
}
for (Long userId:userIds) {
if (userId==null) {
continue;
}
User user = getUserById(userId);
if (user==null) {
log.info("getUserListByUserIds userId{} empty.", userId);
continue;
}
userMapper.put(user.getUsername(), user);
}
return userMapper;
}
}