UserService.java
5.14 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
package cn.fw.valhalla.rpc.erp;
import cn.fw.data.base.domain.common.Message;
import cn.fw.erp.sdk.api.UserRoleApi;
import cn.fw.erp.sdk.api.result.UserRoleDataRange;
import cn.fw.erp.sdk.api.result.UserRoleInfo;
import cn.fw.valhalla.rpc.AbsBaseRpcService;
import cn.fw.valhalla.rpc.erp.dto.PostUserDTO;
import cn.fw.valhalla.rpc.erp.dto.UserRoleDataRangeDTO;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
/**
* 用户服务
* <p>
* create at 2019-03-09
*
* @author kurisu
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class UserService extends AbsBaseRpcService {
/**
* 岗位用户服务
*/
private final UserRoleApi userRoleApi;
@Value("${spring.cache.custom.global-prefix}:rpc:erp")
@Getter
private String keyPrefix;
/**
* 获取某个流程角色的权限范围
*
* @param userId
* @param roleCode
* @return
*/
public List<UserRoleDataRangeDTO> getUserRoleDataRange(final Long userId, final String roleCode) {
if (Objects.isNull(userId) || Objects.isNull(roleCode)) {
return Collections.emptyList();
}
String key = generateKey(roleCode, userId);
try {
List<UserRoleDataRangeDTO> dtos = getListFromCache(key, UserRoleDataRangeDTO.class);
if (!CollectionUtils.isEmpty(dtos)) {
return dtos;
}
Message<List<UserRoleDataRange>> msg = userRoleApi.queryUserRoleDataRange(userId, roleCode);
log.info("userRoleApi.queryUserRoleDataRange: msg.code={}, msg.result={}, msg.data={}", msg.getCode(), msg.getResult(), msg.getData());
if (!msg.isSuccess() || CollectionUtils.isEmpty(msg.getData())) {
return Collections.emptyList();
}
List<UserRoleDataRange> data = msg.getData();
List<UserRoleDataRangeDTO> list = new ArrayList<>(data.size());
for (UserRoleDataRange datum : data) {
UserRoleDataRangeDTO dto = new UserRoleDataRangeDTO();
BeanUtils.copyProperties(datum, dto);
list.add(dto);
setListToCache(key, JSONObject.toJSONString(dto));
}
getQueue(key).expire(15, TimeUnit.SECONDS);
return list;
} catch (Exception e) {
e.printStackTrace();
}
return Collections.emptyList();
}
/**
* 获取门店下某岗位下员工
*
* @param shopId 门店
* @param roleCode 岗位码
* @return 用户集合
*/
public List<PostUserDTO> getUserByRole(final Long shopId, final String roleCode) {
if (Objects.isNull(shopId) || Objects.isNull(roleCode)) {
return Collections.emptyList();
}
String key = generateKey(shopId, roleCode);
List<PostUserDTO> dtos = getListFromCache(key, PostUserDTO.class);
if (!CollectionUtils.isEmpty(dtos)) {
return dtos;
}
try {
log.info("查询门店[{}]下的[{}]角色", shopId, roleCode);
Message<List<UserRoleInfo>> msg = userRoleApi.queryUserRoleInfos(roleCode, shopId);
log.info("userRoleApi.queryUserRoleInfos: msg.code={}, msg.result={}, msg.data={}", msg.getCode(), msg.getResult(), msg.getData());
if (!msg.isSuccess() || CollectionUtils.isEmpty(msg.getData())) {
return Collections.emptyList();
}
List<UserRoleInfo> data = msg.getData();
List<PostUserDTO> list = new ArrayList<>(data.size());
for (UserRoleInfo datum : data) {
PostUserDTO dto = new PostUserDTO();
BeanUtils.copyProperties(datum, dto);
list.add(dto);
setListToCache(key, JSONObject.toJSONString(dto));
}
getQueue(key).expire(10, TimeUnit.MINUTES);
return list;
} catch (Exception e) {
e.printStackTrace();
}
return Collections.emptyList();
}
private String generateKey(final Long shopId, final String roleCOde) {
Assert.notNull(shopId, "id cannot be null");
Assert.hasLength(roleCOde, "roleCOde cannot be null");
return String.format("%s:%s:%s:%s", getKeyPrefix(), "post", shopId, roleCOde);
}
private String generateKey(final String roleCode, final Long userId) {
Assert.notNull(userId, "id cannot be null");
Assert.hasLength(roleCode, "roleCOde cannot be null");
return String.format("%s:%s:%s:%s", getKeyPrefix(), "range", roleCode, userId);
}
private String generateKey(final Long userId) {
Assert.notNull(userId, "userId cannot be null");
return String.format("%s:%s:%s", getKeyPrefix(), "user", userId);
}
}