WeComUserDataJob.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package com.tzld.piaoquan.wecom.job;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.tzld.piaoquan.wecom.dao.mapper.UserMapper;
  5. import com.tzld.piaoquan.wecom.model.po.User;
  6. import com.tzld.piaoquan.wecom.model.po.UserExample;
  7. import com.tzld.piaoquan.wecom.service.AccessTokenService;
  8. import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
  9. import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
  10. import org.apache.commons.lang3.ObjectUtils;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Component;
  13. import org.springframework.util.CollectionUtils;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. @Component
  18. public class WeComUserDataJob {
  19. String GET_USER_URL = "https://open.weibanzhushou.com/open-api/external_user/list";
  20. String UPDATE_USER_URL = "https://open.weibanzhushou.com/open-api/external_user/update/list";
  21. private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
  22. final int size = 100;
  23. @Autowired
  24. private UserMapper userMapper;
  25. @Autowired
  26. private AccessTokenService accessTokenService;
  27. public void insertAllUser() {
  28. try {
  29. Long endTime = System.currentTimeMillis() / 1000;
  30. Long startTime = 1720540800L;
  31. Integer total = getUserTotal(startTime, endTime);
  32. if (total == null || total == 0) {
  33. return;
  34. }
  35. int page = total / size + 1;
  36. int sum = 0;
  37. for (int i = 0; i < page; i++) {
  38. String res = getUser(size, i * size, 1720540800L, endTime);
  39. //TODO 记录查询数据 info日志
  40. if (ObjectUtils.isEmpty(res)) {
  41. continue;
  42. }
  43. JSONObject jsonObject = JSONObject.parseObject(res);
  44. JSONArray jsonArray = jsonObject.getJSONArray("external_user_list");
  45. List<User> userList = new ArrayList<>();
  46. for (int j = 0; j < jsonArray.size(); j++) {
  47. String id = (String) jsonArray.getJSONObject(j).get("id");
  48. jsonArray.getJSONObject(j).put("id", null);
  49. User user = jsonArray.getJSONObject(j).toJavaObject(User.class);
  50. user.setExternalUserId3rdParty(id);
  51. userList.add(user);
  52. sum++;
  53. }
  54. userMapper.insertList(userList);
  55. if (jsonArray.size() < size) {
  56. if (total > sum) {
  57. //TODO 输出异常 插入数量不足
  58. System.out.println("插入数量不足");
  59. }
  60. break;
  61. }
  62. }
  63. } catch (Exception e) {
  64. //TODO 记录异常日志
  65. e.printStackTrace();
  66. }
  67. }
  68. public Integer getUserTotal(Long startTime, Long endTime) throws IOException {
  69. String res = getUser(1, 0, startTime, endTime);
  70. JSONObject jsonObject = JSONObject.parseObject(res);
  71. return jsonObject.getInteger("total");
  72. }
  73. public String getUser(Integer limit, Integer offset, Long startTime, Long endTime) throws IOException {
  74. String accessToken = accessTokenService.getAccessToken();
  75. String url = GET_USER_URL
  76. + "?access_token=" + accessToken
  77. + "&limit=" + limit + "&offset=" + offset + "&start_time=" + startTime + "&end_time=" + endTime;
  78. return httpPoolClientDefault.get(url);
  79. }
  80. public void updateUser() {
  81. try {
  82. Long endTime = System.currentTimeMillis() / 1000;
  83. Long startTime = endTime - 60 * 60;
  84. Integer total = getUpdateUserTotal(startTime, endTime);
  85. if (total == null || total == 0) {
  86. return;
  87. }
  88. int page = total / size + 1;
  89. int sum = 0;
  90. for (int i = 0; i < page; i++) {
  91. String res = getUpdateUser(size, i * size, 1726821389L, endTime);
  92. //TODO 记录查询数据 info日志
  93. if (ObjectUtils.isEmpty(res)) {
  94. continue;
  95. }
  96. JSONObject jsonObject = JSONObject.parseObject(res);
  97. JSONArray jsonArray = jsonObject.getJSONArray("external_user_list");
  98. for (int j = 0; j < jsonArray.size(); j++) {
  99. String id = (String) jsonArray.getJSONObject(j).get("id");
  100. jsonArray.getJSONObject(j).put("id", null);
  101. User user = jsonArray.getJSONObject(j).toJavaObject(User.class);
  102. user.setExternalUserId3rdParty(id);
  103. UserExample example = new UserExample();
  104. example.createCriteria().andExternalUserId3rdPartyEqualTo(user.getExternalUserId3rdParty());
  105. List<User> list = userMapper.selectByExample(example);
  106. if (CollectionUtils.isEmpty(list)) {
  107. //没有用户,走插入逻辑
  108. userMapper.insert(user);
  109. } else {
  110. User oldUser = list.get(0);
  111. user.setId(oldUser.getId());
  112. userMapper.updateByPrimaryKeySelective(user);
  113. }
  114. sum++;
  115. if (jsonArray.size() < size) {
  116. if (total > sum) {
  117. //TODO 输出异常 插入数量不足
  118. System.out.println("插入数量不足");
  119. }
  120. break;
  121. }
  122. }
  123. }
  124. } catch (IOException e) {
  125. //TODO 记录异常日志
  126. e.printStackTrace();
  127. }
  128. }
  129. public Integer getUpdateUserTotal(Long startTime, Long endTime) throws IOException {
  130. String res = getUpdateUser(1, 0, startTime, endTime);
  131. JSONObject jsonObject = JSONObject.parseObject(res);
  132. return jsonObject.getInteger("total");
  133. }
  134. public String getUpdateUser(Integer limit, Integer offset, Long startTime, Long endTime) throws IOException {
  135. String accessToken = accessTokenService.getAccessToken();
  136. String url = UPDATE_USER_URL
  137. + "?access_token=" + accessToken
  138. + "&limit=" + limit + "&offset=" + offset + "&start_update_time=" + startTime + "&end_update_time=" + endTime
  139. + "&source=external_user";
  140. return httpPoolClientDefault.get(url);
  141. }
  142. }