MemberRpcService.java
7.24 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
package cn.fw.valhalla.rpc.member;
import cn.fw.common.util.MobileUtil;
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.member.sdk.vo.UserRegistryVO;
import cn.fw.valhalla.common.utils.ThreadPoolUtil;
import cn.fw.valhalla.rpc.member.dto.MemberUserDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.concurrent.CompletableFuture;
import static org.apache.commons.lang3.Validate.isTrue;
import static org.apache.commons.lang3.Validate.notNull;
/**
* 会员服务
*
* @author kurisu
*/
@Slf4j
@Service
public class MemberRpcService {
/**
* 会员服务
*/
private final MemberApi memberApi;
private final FunctionApi functionApi;
/**
* 默认构造器
*/
@Autowired
public MemberRpcService(final MemberApi memberApi,
final FunctionApi functionApi) {
this.memberApi = memberApi;
this.functionApi = functionApi;
}
/**
* 根据会员ID获取会员信息
*
* @param userId 会员ID
* @return 会员信息
*/
public MemberUserDTO user(final Long userId) {
if (userId == null) {
return null;
}
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);
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;
}
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);
return userDTO;
} catch (Exception e) {
log.error("调用Member系统根据会员手机号[{}]获取会员信息失败,原因:{}", mobile, e);
}
return null;
}
/**
* 注册会员
*
* @param memberUser 注册信息
* @return 会员
*/
public MemberUserDTO register(final MemberUserDTO memberUser) {
notNull(memberUser, "会员注册信息不能为空");
UserRegistryVO vo = new UserRegistryVO();
BeanUtils.copyProperties(memberUser, vo);
vo.setNickName(StringUtils.isNotBlank(memberUser.getNickName()) ? memberUser.getNickName() : memberUser.getRealName());
final Message<UserBaseInfoVO> msg = memberApi.register(vo);
isTrue(msg.isSuccess(), String.format("调用Member[注册会员[%s]]系统失败", vo));
final UserBaseInfoVO user = msg.getData();
notNull(user, String.format("调用Member[注册会员[%s]]系统失败,返回会员信息为空", vo));
final MemberUserDTO userDTO = new MemberUserDTO();
BeanUtils.copyProperties(user, userDTO);
return userDTO;
}
/**
* 查询手机号归属地
*
* @param mobile
* @return
*/
@Cacheable(cacheNames = "mobile:attribution", key = "#mobile", unless = "#result.isEmpty()")
public String attribution(final String mobile) {
if (StringUtils.isBlank(mobile)) {
return "";
}
try {
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
MobileUtil.Result result = MobileUtil.attribution(mobile);
String att = "";
if (Objects.nonNull(result)) {
att = result.getProvince().concat(" ").concat(result.getCity());
}
return att;
}, ThreadPoolUtil.getInstance().getExecutor());
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
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);
}
return "";
}, ThreadPoolUtil.getInstance().getExecutor());
return future1.applyToEither(future2, String::trim).join();
} catch (Exception e) {
log.warn("查询手机号归属地失败:", e);
}
return "";
}
}