ErpApiServiceHelper.java
3.97 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 com.yoho.unions.common.utils;
import com.netflix.config.DynamicPropertyFactory;
import com.yoho.service.model.union.request.UserInfoBO;
import com.yoho.tools.common.beans.Response;
import com.yoho.unions.common.enums.CodeEnum;
import com.yoho.unions.common.enums.ErpApiEnum;
import com.yoho.unions.common.model.ERPProfileRequestBO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Component
public class ErpApiServiceHelper {
static Logger log = LoggerFactory.getLogger(ErpApiServiceHelper.class);
@Resource
private RestTemplate restTemplate;
private static String ERP_DOMAIN = DynamicPropertyFactory.getInstance().getStringProperty("erp.domain", "").get();
/**
* 根据pid,查询用户
* @param pid
* @return
*/
@SuppressWarnings("unchecked")
public UserInfoBO getUserByPid(String pid) {
log.info("getUserByPid with pid={}", pid);
ERPProfileRequestBO profile = new ERPProfileRequestBO();
profile.setPid(Integer.parseInt(pid));
Response<Map<String, Object>> resp = restTemplate.postForObject(ERP_DOMAIN + ErpApiEnum.GET_USER_BY_PID.getUrl(), profile, Response.class);
if (resp == null || resp.getCode() != CodeEnum.SUCCESS.getCode()) {
log.warn("getUserByPid error with pid={}", pid);
return null;
}
UserInfoBO user = toSysUserResponse(resp.getData());
return user;
}
/**
* 查询用户列表
* @param request
* @return
*/
@SuppressWarnings("unchecked")
public List<UserInfoBO> getUsersByPids(List<Integer> pids) {
log.info("getUsers with pids={}", pids == null ? 0 : pids.size());
if (CollectionUtils.isEmpty(pids)) {
log.warn("getUsersByPids error. pids is empty");
return null;
}
ERPProfileRequestBO profile = new ERPProfileRequestBO();
profile.setpIds(pids);
profile.setPageNo(1);
profile.setPageSize(pids.size());
Response<Map<String, Object>> resp = restTemplate.postForObject(ERP_DOMAIN + ErpApiEnum.GET_USER_LIST.getUrl(), profile, Response.class);
if (resp == null || resp.getCode() != CodeEnum.SUCCESS.getCode()) {
log.warn("getUsers error with pids={}", pids.size());
return null;
}
Map<String, Object> map = resp.getData();
if (MapUtils.isEmpty(map)) {
log.warn("getUsersList error. result is empty. with pids={}", pids.size());
return null;
}
List<Map<String, Object>> records = (List<Map<String, Object>>) map.get("records");
if (CollectionUtils.isEmpty(records)) {
return null;
}
List<UserInfoBO> list = new ArrayList<>();
for (Map<String, Object> m : records) {
list.add(toSysUserResponse(m));
}
return list;
}
/**
* 对象转换
*
* @param map
* @return
*/
private UserInfoBO toSysUserResponse(Map<String, Object> map) {
if (MapUtils.isEmpty(map)) {
return null;
}
UserInfoBO bo = new UserInfoBO();
bo.setCreate_time(MapUtils.getString(map, "create_time"));
bo.setEmail(MapUtils.getString(map, "email"));
bo.setAccount(MapUtils.getString(map, "account"));
bo.setPhone(MapUtils.getString(map, "phone"));
bo.setPid(MapUtils.getInteger(map, "pid"));
bo.setRole_id(MapUtils.getString(map, "role_id"));
bo.setStatus(MapUtils.getString(map, "status"));
bo.setTruename(MapUtils.getString(map, "truename"));
bo.setAuth_site(MapUtils.getString(map, "auth_site"));
bo.setCreate_date(MapUtils.getString(map, "create_date"));
bo.setCreate_id(MapUtils.getString(map, "create_id"));
bo.setDept_id(MapUtils.getString(map, "dept_id"));
bo.setExpires(MapUtils.getString(map, "expires"));
bo.setIdentity(MapUtils.getString(map, "identity"));
bo.setLogin_time(MapUtils.getString(map, "login_time"));
bo.setLogout_time(MapUtils.getString(map, "logout_time"));
bo.setStaff_code(MapUtils.getString(map, "staff_code"));
return bo;
}
}