MemberRpcService.java
6.32 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
package cn.fw.shirasawa.rpc.member;
import cn.fw.data.base.domain.common.Message;
import cn.fw.member.sdk.api.FunctionApi;
import cn.fw.member.sdk.api.MemberApi;
import cn.fw.member.sdk.vo.BatchUserParam;
import cn.fw.member.sdk.vo.MobileLocation;
import cn.fw.member.sdk.vo.UserBaseInfoVO;
import cn.fw.shirasawa.common.utils.MobileUtilKt;
import cn.fw.shirasawa.rpc.AbsBaseRpcService;
import cn.fw.shirasawa.rpc.member.dto.MemberUserDTO;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import static org.apache.commons.lang3.Validate.isTrue;
/**
* 会员服务
*
* @author kurisu
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class MemberRpcService extends AbsBaseRpcService {
/**
* 会员服务
*/
private final MemberApi memberApi;
private final FunctionApi functionApi;
@Value("${spring.cache.custom.global-prefix}:rpc:member")
private String keyPrefix;
/**
* 根据会员ID获取会员信息
*
* @param userId 会员ID
* @return 会员信息
*/
public MemberUserDTO user(final Long userId) {
if (userId == null || userId <= 0) {
return null;
}
String key = String.format("%s:member-info:%s", getKeyPrefix(), userId);
MemberUserDTO memberUserDTO = getFromCache(key, MemberUserDTO.class);
if (Objects.nonNull(memberUserDTO)) {
return memberUserDTO;
}
try {
final Message<UserBaseInfoVO> msg = memberApi.queryUserByuserId(userId);
if (!msg.isSuccess()) {
log.warn("调用Member[根据会员ID[{}]获取会员信息]系统失败, 原因:{}", userId, msg.getResult());
return null;
}
final UserBaseInfoVO user = msg.getData();
if (user != null) {
final MemberUserDTO userDTO = new MemberUserDTO();
BeanUtils.copyProperties(user, userDTO);
setToCache(key, JSONObject.toJSONString(userDTO), 60 * 3);
return userDTO;
}
} catch (Exception e) {
log.error("调用Member[根据会员ID[{}]获取会员信息]系统失败", userId, e);
}
return null;
}
/**
* 根据会员ID集合获取会员列表
*
* @param userIds 会员ID集合
* @return 会员列表
*/
public List<MemberUserDTO> users(final List<Long> userIds) {
if (CollectionUtils.isEmpty(userIds)) {
return Collections.emptyList();
}
try {
final BatchUserParam param = new BatchUserParam();
param.setUserIdList(userIds);
final Message<List<UserBaseInfoVO>> msg = memberApi.batchUserByUserId(param);
if (!msg.isSuccess()) {
log.warn("调用Member[根据会员ID集合[{}]获取会员列表]系统失败, 原因:{}", userIds, msg.getResult());
return Collections.emptyList();
}
final List<UserBaseInfoVO> users = msg.getData();
if (!CollectionUtils.isEmpty(users)) {
final List<MemberUserDTO> members = new ArrayList<>();
users.forEach(item -> {
final MemberUserDTO userDTO = new MemberUserDTO();
BeanUtils.copyProperties(item, userDTO);
members.add(userDTO);
});
return members;
}
} catch (Exception e) {
log.error("调用Member[根据会员ID集合[{}]获取会员列表]系统失败", userIds, e);
}
return Collections.emptyList();
}
/**
* 根据手机号查询会员信息
*
* @param mobile 手机号
* @return 会员信息
*/
public MemberUserDTO queryByMobile(final String mobile) {
if (StringUtils.isBlank(mobile)) {
return null;
}
String key = String.format("%s:info:%s", getKeyPrefix(), mobile);
MemberUserDTO memberUserDTO = getFromCache(key, MemberUserDTO.class);
if (Objects.nonNull(memberUserDTO)) {
return memberUserDTO;
}
try {
final Message<UserBaseInfoVO> msg = memberApi.queryUserByMobile(mobile);
isTrue(msg.isSuccess(), String.format("调用Member系统失败: [根据会员手机号[%s]获取会员信息]", mobile));
final UserBaseInfoVO user = msg.getData();
if (Objects.isNull(user)) {
return null;
}
final MemberUserDTO userDTO = new MemberUserDTO();
BeanUtils.copyProperties(user, userDTO);
setToCache(key, JSONObject.toJSONString(userDTO), 60 * 3);
return userDTO;
} catch (Exception e) {
log.error("调用Member系统根据会员手机号[{}]获取会员信息失败,原因:{}", mobile, e);
}
return null;
}
/**
* 查询手机号归属地
*
* @param mobile
* @return
*/
@Cacheable(cacheNames = "mobile:attribution", key = "#mobile", unless = "#result.isEmpty()")
public String attribution(final String mobile) {
if (StringUtils.isBlank(mobile)) {
return "";
}
try {
String attribution = MobileUtilKt.attribution(mobile);
if (StringUtils.isNotBlank(attribution)) {
return attribution;
}
Message<MobileLocation> msg = functionApi.queryMobileLocation(mobile);
if (!msg.isSuccess()) {
return "";
}
MobileLocation data = msg.getData();
if (data != null) {
String province = data.getProvince();
String city = data.getCity();
return province.concat(" ").concat(city);
}
} catch (Exception ignored) {
}
return "";
}
@Override
public String getKeyPrefix() {
return this.keyPrefix;
}
}