UsersSiteJob.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
package com.cnlive.shenhe.job;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cnlive.shenhe.entity.ShySites;
import com.cnlive.shenhe.entity.ShyUsers;
import com.cnlive.shenhe.mapper.ShySitesMapper;
import com.cnlive.shenhe.mapper.ShyUsersMapper;
import com.cnlive.shenhe.utils.CommonConst;
import com.cnlive.shenhe.utils.CommonUtils;
import com.cnlive.shenhe.utils.HttpUtil;
import com.cnlive.shenhe.utils.OpenUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Auther: 周伟鹏
* @Date: 2018/11/30 11:31
* @Description:同步用户和站点信息
*/
@Component
public class UsersSiteJob {
private static Logger logger = LoggerFactory.getLogger(UsersSiteJob.class);
@Value("${sync_users_api}")
private String syncUserApi;
@Value("${sync_users_app_key}")
private String app_key;
@Autowired
ShyUsersMapper shyUsersMapper;
@Autowired
ShySitesMapper shySitesMapper;
@Scheduled(fixedRate = 30 * 60 * 1000)
public void syncUsers() {
try {
String tm = "0";
Example usersExample = new Example(ShyUsers.class);
usersExample.setOrderByClause("tm desc");
List<ShyUsers> usersList = shyUsersMapper.selectByExample(usersExample);
if (!usersList.isEmpty()) {
Long tmStr = usersList.get(0).getTm();
if (CommonUtils.isNotNull(tmStr)) tm = tmStr + "";
}
HashMap<String, String> dataMap = new HashMap<>();
dataMap.put("tm", tm);
dataMap.put("timestamp", new Date().getTime() / 1000 + "");
String httpUrl = OpenUtil.buildURL(syncUserApi, dataMap, app_key);
logger.info("httpUrl===" + httpUrl);
Map<String, String> resultMap = HttpUtil.init().post(httpUrl);
JSONObject result = JSON.parseObject(resultMap.get("result"));
String errorCode = result.getString("errorCode");
if ("0".equals(errorCode)) {
JSONArray data = result.getJSONArray("data");
for (int i = 0; i < data.size(); i++) {
//用户信息入库
JSONObject user = data.getJSONObject(i);
Integer id = user.getInteger("sid");
String email = user.getString("email");
Integer spid = user.getInteger("new_sp_id");
String userLogo = user.getString("userLogo");
boolean master = user.getBoolean("master");
Long utm = user.getLong("tm");
String username = user.getString("contacts");
boolean status = user.getIntValue("status") == CommonConst.USER_STATUS_USE;
if (CommonUtils.isEmpty(username)) {
username = Integer.toString(id);
}
String mobile = user.getString("mobile");
String company_brief = user.getString("sname");
String company = user.getString("company");
ShyUsers shyUsers = new ShyUsers();
shyUsers.setEmail(email);
shyUsers.setId(id);
shyUsers.setSp_id(spid);
shyUsers.setMaster(master);
shyUsers.setState(status);
shyUsers.setMobile(mobile);
shyUsers.setUsername(username);
shyUsers.setCompany_brief(company_brief);
shyUsers.setCompany_name(company);
shyUsers.setUser_logo(userLogo);
shyUsers.setTm(utm);
ShyUsers old_shyUsers = shyUsersMapper.selectByPrimaryKey(id);
if (CommonUtils.isNull(old_shyUsers)) {
shyUsersMapper.insertSelective(shyUsers);
} else {
shyUsersMapper.updateByPrimaryKeySelective(shyUsers);
}
//站点信息入库
ShySites shySites = new ShySites();
shySites.setSpid(spid);
List<ShySites> shySitesList = shySitesMapper.select(shySites);
if(shySitesList.isEmpty()){
shySites.setCompany(company);
shySites.setName(company_brief);
shySitesMapper.insertSelective(shySites);
}
}
} else {
logger.error("请求用户云失败,errorCode:{},errorMessage{}", errorCode, result.getString("errorMessage"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}