UserServiceHelper.java
5.05 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
/**
*
*/
package com.yoho.unions.helper;
import com.alibaba.fastjson.JSONObject;
import com.netflix.config.DynamicLongProperty;
import com.netflix.config.DynamicPropertyFactory;
import com.yoho.core.common.utils.JsonUtil;
import com.yoho.core.rest.client.ServiceCaller;
import com.yoho.core.rest.client.hystrix.AsyncFuture;
import com.yoho.error.ServiceError;
import com.yoho.error.exception.ServiceException;
import com.yoho.service.model.order.request.OrdersStatusStatisticsRequest;
import com.yoho.service.model.order.response.CountBO;
import com.yoho.service.model.response.UserInfoRspBO;
import com.yoho.service.model.union.request.UnionUicUserReqBO;
import com.yoho.unions.common.redis.RedisValueCache;
import com.yoho.unions.vo.ApiResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* @author ping.huang
* 2016年12月29日
*/
@Component
public class UserServiceHelper {
static Logger log = LoggerFactory.getLogger(UserServiceHelper.class);
@Resource
ServiceCaller serviceCaller;
@Resource
RedisValueCache redisValueCache;
@Resource
ClientSecretHelper clientSecretHelper;
@Value("${gateway.url}")
private String gatewayUrl;
private static final String USERINFO_KEY = "yh:union:share:userinfo:";
/**
* 根据uid,查询订单数
*
* @param uid
* @return
* @throws ServiceException
*/
public CountBO getOrderCountByUid(int uid) throws ServiceException {
log.info("start with getOrderCountByUid. uid is {}", uid);
if (uid == 0) {
log.warn("getOrderCountByUid error with uid={}", uid);
return null;
}
OrdersStatusStatisticsRequest request = new OrdersStatusStatisticsRequest();
request.setUid(uid);
CountBO result = serviceCaller.call("order.getOrdersCountByUidAndStatus", request, CountBO.class);
log.info("getOrderCountByUid success with response={}. uid is {}", result, uid);
return result;
}
public ApiResponse getProfileByUid(int uid) throws ServiceException {
log.info("start with getProfileByUid. uid is {}", uid);
if (uid == 0) {
log.warn("getProfileByUid error with uid={}", uid);
return null;
}
Map<String, String> map = new HashMap<String, String>();
map.put("method", "app.passport.profile");
map.put("uid", String.valueOf(uid));
String param = clientSecretHelper.createClientSecret(map);
// 调用gateway的获取用户信息
AsyncFuture<ApiResponse> response = serviceCaller.get("user.getProfileByUid", gatewayUrl + "?" + param, null, ApiResponse.class, null);
ApiResponse apiResponse = response.get();
if (apiResponse.getCode() != 200) {
log.warn("call app.passport.profile error e={}", apiResponse.getMessage());
return new ApiResponse();
}
log.info("getProfileByUid success with response={}. uid is {}", apiResponse, uid);
return apiResponse;
}
public UserInfoRspBO getUserInfoFromUic(int uid) throws ServiceException {
if (1 > uid) {
log.warn("getUserInfoFromUic: param uid is null.");
throw new ServiceException(ServiceError.UID_IS_NULL);
}
UserInfoRspBO userInfo = redisValueCache.get(USERINFO_KEY, UserInfoRspBO.class);
if (userInfo != null) {
log.info("getUserInfoFromUic: get userInfo from cache: uid is {}", uid);
return userInfo;
}
try {
UnionUicUserReqBO uicUserReqBO = new UnionUicUserReqBO();
uicUserReqBO.setYohoUid(uid);
uicUserReqBO.setChannel(1);
userInfo = serviceCaller.call("uic.getUserInfoByYohoUid", uicUserReqBO, UserInfoRspBO.class);
if (userInfo == null) {
return null;
}
redisValueCache.set(USERINFO_KEY+uid,userInfo,180, TimeUnit.SECONDS);
log.info("getUserInfoFromUic: end call uic.getUserInfo, uid is {}, userInfo is {}", uid, userInfo);
} catch (Exception e) {
log.warn("get user info from uic failed, uid is {}", uid);
return new UserInfoRspBO();
}
return userInfo;
}
public List<UserInfoRspBO> getUserInfoListFromUic(Set<Integer> uidSet) throws ServiceException {
if (CollectionUtils.isEmpty(uidSet)) {
log.warn("getUserInfoFromUic: param uids is null.");
throw new ServiceException(ServiceError.UID_IS_NULL);
}
String uids=StringUtils.join(uidSet, ',');
List<UserInfoRspBO> result = new ArrayList<>();
try {
UnionUicUserReqBO uicUserReqBO = new UnionUicUserReqBO();
uicUserReqBO.setUids(uids);
uicUserReqBO.setChannel(1);
List<JSONObject> userInfo = serviceCaller.asyncCall("uic.getUserInfoListByYohoUid", uicUserReqBO, List.class,new ArrayList()).get(100);
log.info("getUserInfoFromUic: end call uic.getUserInfo, uids is {}, userInfo is {}", uids, userInfo);
if (CollectionUtils.isEmpty(userInfo)) {
return result;
}
userInfo.forEach(u->{
result.add(JsonUtil.jsonToObject(u.toJSONString(), UserInfoRspBO.class));
});
return result;
} catch (Exception e) {
log.warn("get user info from uic failed, uids is {}", uids);
return result;
}
}
}