EhrRpcService.java
4.13 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
package cn.fw.valhalla.rpc.ehr;
import cn.fw.data.base.domain.common.Message;
import cn.fw.ehr.sdk.api.StaffApi;
import cn.fw.ehr.sdk.api.result.StaffInfo;
import cn.fw.valhalla.common.utils.StringUtils;
import cn.fw.valhalla.rpc.AbsBaseRpcService;
import cn.fw.valhalla.rpc.ehr.dto.StaffInfoDTO;
import cn.fw.valhalla.rpc.erp.dto.UserInfoDTO;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.util.Objects;
/**
* @author : kurisu
* @className : EhrRpcService
* @description : 人事
* @date: 2021-01-19 17:34
*/
@Slf4j
@Service
public class EhrRpcService extends AbsBaseRpcService {
private final StaffApi staffApi;
@Value("${spring.cache.custom.global-prefix}:rpc:ehr")
private String keyPrefix;
@Autowired
public EhrRpcService(final StaffApi staffApi) {
this.staffApi = staffApi;
}
@Nullable
public StaffInfoDTO queryStaffInfo(final Long userId) {
if (userId == null) {
return null;
}
final String key = generateKey(userId);
StaffInfoDTO dto = getFromCache(key, StaffInfoDTO.class);
if (Objects.nonNull(dto)) {
return dto;
}
try {
final Message<StaffInfo> msg = staffApi.getStaffInfoById(userId);
log.info("StaffApi.queryStaffInfo: msg.code={}, msg.result={}, msg.data={}", msg.getCode(), msg.getResult(), msg.getData());
if (msg.isSuccess() && !Objects.isNull(msg.getData())) {
final StaffInfoDTO staffInfoDTO = new StaffInfoDTO();
BeanUtils.copyProperties(msg.getData(), staffInfoDTO);
setToCache(key, JSONObject.toJSONString(staffInfoDTO));
return staffInfoDTO;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 查询人员信息
* @param userId
* @return
*/
@Nullable
public UserInfoDTO user(final Long userId) {
StaffInfoDTO staffInfoDTO = queryStaffInfo(userId);
if (Objects.isNull(staffInfoDTO)) {
return null;
}
UserInfoDTO userInfoDTO = new UserInfoDTO();
userInfoDTO.setId(staffInfoDTO.getId());
userInfoDTO.setUserName(staffInfoDTO.getName());
userInfoDTO.setHeadImg(staffInfoDTO.getAvatar());
userInfoDTO.setMobile(staffInfoDTO.getMobile());
return userInfoDTO;
}
@Nullable
public StaffInfoDTO queryStaffInfoByMobile(final String mobile) {
if (StringUtils.isEmpty(mobile)) {
return null;
}
final String key = generateKey(mobile);
StaffInfoDTO dto = getFromCache(key, StaffInfoDTO.class);
if (Objects.nonNull(dto)) {
return dto;
}
try {
final Message<StaffInfo> msg = staffApi.getStaffInfoByMobile(mobile);
log.info("StaffApi.queryStaffInfoByMobile: msg.code={}, msg.result={}, msg.data={}", msg.getCode(), msg.getResult(), msg.getData());
if (msg.isSuccess() && !Objects.isNull(msg.getData())) {
final StaffInfoDTO staffInfoDTO = new StaffInfoDTO();
BeanUtils.copyProperties(msg.getData(), staffInfoDTO);
setToCache(key, JSONObject.toJSONString(staffInfoDTO), 60 * 60);
return staffInfoDTO;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected String getKeyPrefix() {
return this.keyPrefix;
}
private String generateKey(final Long userId) {
Assert.notNull(userId, "userId cannot be null");
return String.format("%s:user:%s", getKeyPrefix(), userId);
}
private String generateKey(final String mobile) {
Assert.notNull(mobile, "mobile cannot be null");
return String.format("%s:mobile:%s", getKeyPrefix(), mobile);
}
}