WeComUserDataJob.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.utils.HttpClientUtil;
  8. import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
  9. import org.apache.commons.lang3.ObjectUtils;
  10. import org.springframework.beans.BeanUtils;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.context.annotation.Bean;
  13. import org.springframework.stereotype.Component;
  14. import org.springframework.util.CollectionUtils;
  15. import java.io.IOException;
  16. import java.util.ArrayList;
  17. import java.util.Collections;
  18. import java.util.List;
  19. @Component
  20. public class WeComUserDataJob {
  21. String GET_USER_URL = "https://open.weibanzhushou.com/open-api/external_user/list";
  22. String UPDATE_USER_URL = "https://open.weibanzhushou.com/open-api/external_user/update/list";
  23. String ACCESS_TOKEN = "38969e430219e6b18f88792184a37b11";
  24. private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
  25. final int size = 100;
  26. @Autowired
  27. private UserMapper userMapper;
  28. public void insertAllUser() {
  29. Long endTime = System.currentTimeMillis() / 1000;
  30. int sum = 0;
  31. for (int i = 0; i < 1000; i++) {
  32. String res = null;
  33. try {
  34. //TODO accessToken 暂时写死 后续增加接口刷新和存储
  35. res = getUser(ACCESS_TOKEN, size, i * size, 1720540800L, endTime);
  36. //TODO 记录查询数据 info日志
  37. } catch (IOException e) {
  38. //TODO 记录异常日志 记录起止时间和页数 startTime endTime i
  39. e.printStackTrace();
  40. }
  41. if (ObjectUtils.isEmpty(res)) {
  42. continue;
  43. }
  44. JSONObject jsonObject = JSONObject.parseObject(res);
  45. Integer total = (Integer) jsonObject.get("total");
  46. JSONArray jsonArray = jsonObject.getJSONArray("external_user_list");
  47. for (int j = 0; j < jsonArray.size(); j++) {
  48. String id = (String) jsonArray.getJSONObject(j).get("id");
  49. jsonArray.getJSONObject(j).put("id", null);
  50. User user = jsonArray.getJSONObject(j).toJavaObject(User.class);
  51. user.setExternalUserId3rdParty(id);
  52. //TODO 记录对象数据 info日志
  53. try {
  54. int insert = userMapper.insert(user);
  55. if (insert <= 0) {
  56. //TODO 异常日志
  57. continue;
  58. }
  59. } catch (Exception e) {
  60. //TODO 打印异常日志
  61. }
  62. sum++;
  63. }
  64. if (jsonArray.size() < size) {
  65. if (total != null && total > sum) {
  66. //TODO 输出异常 插入数量不足
  67. System.out.println("插入数量不足");
  68. }
  69. break;
  70. }
  71. }
  72. }
  73. public String getUser(String accessToken, Integer limit, Integer offset, Long startTime, Long endTime) throws IOException {
  74. String url = GET_USER_URL
  75. + "?access_token=" + accessToken
  76. + "&limit=" + limit + "&offset=" + offset + "&start_time=" + startTime + "&end_time=" + endTime;
  77. return httpPoolClientDefault.get(url);
  78. }
  79. public void updateUser() {
  80. Long endTime = System.currentTimeMillis() / 1000;
  81. int sum = 0;
  82. for (int i = 0; i < 1000; i++) {
  83. String res = null;
  84. try {
  85. //TODO accessToken 暂时写死 后续增加接口刷新和存储
  86. res = getUpdateUser(ACCESS_TOKEN, size, i * size, 1726821389L, endTime);
  87. //TODO 记录查询数据 info日志
  88. } catch (IOException e) {
  89. //TODO 记录异常日志 记录起止时间和页数 startTime endTime i
  90. e.printStackTrace();
  91. }
  92. if (ObjectUtils.isEmpty(res)) {
  93. continue;
  94. }
  95. JSONObject jsonObject = JSONObject.parseObject(res);
  96. Integer total = (Integer) jsonObject.get("total");
  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. int insert = userMapper.insert(user);
  109. if (insert <= 0) {
  110. //TODO 异常日志
  111. continue;
  112. }
  113. } else {
  114. User oldUser = list.get(0);
  115. user.setId(oldUser.getId());
  116. int update = userMapper.updateByPrimaryKeySelective(user);
  117. if (update < 0) {
  118. //TODO 异常日志
  119. continue;
  120. }
  121. }
  122. sum++;
  123. if (jsonArray.size() < size) {
  124. if (total != null && total > sum) {
  125. //TODO 输出异常 插入数量不足
  126. System.out.println("插入数量不足");
  127. }
  128. break;
  129. }
  130. }
  131. }
  132. }
  133. public String getUpdateUser(String accessToken, Integer limit, Integer offset, Long startTime, Long endTime) throws IOException {
  134. String url = UPDATE_USER_URL
  135. + "?access_token=" + accessToken
  136. + "&limit=" + limit + "&offset=" + offset + "&start_update_time=" + startTime + "&end_update_time=" + endTime
  137. + "&source=external_user";
  138. return httpPoolClientDefault.get(url);
  139. }
  140. }