123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- package com.tzld.piaoquan.wecom.job;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.tzld.piaoquan.wecom.dao.mapper.UserMapper;
- import com.tzld.piaoquan.wecom.model.po.User;
- import com.tzld.piaoquan.wecom.model.po.UserExample;
- import com.tzld.piaoquan.wecom.service.AccessTokenService;
- import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
- import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
- import org.apache.commons.lang3.ObjectUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import org.springframework.util.CollectionUtils;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- @Component
- public class WeComUserDataJob {
- String GET_USER_URL = "https://open.weibanzhushou.com/open-api/external_user/list";
- String UPDATE_USER_URL = "https://open.weibanzhushou.com/open-api/external_user/update/list";
- private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
- final int size = 100;
- @Autowired
- private UserMapper userMapper;
- @Autowired
- private AccessTokenService accessTokenService;
- public void insertAllUser() {
- try {
- Long endTime = System.currentTimeMillis() / 1000;
- Long startTime = 1720540800L;
- Integer total = getUserTotal(startTime, endTime);
- if (total == null || total == 0) {
- return;
- }
- int page = total / size + 1;
- int sum = 0;
- for (int i = 0; i < page; i++) {
- String res = getUser(size, i * size, 1720540800L, endTime);
- //TODO 记录查询数据 info日志
- if (ObjectUtils.isEmpty(res)) {
- continue;
- }
- JSONObject jsonObject = JSONObject.parseObject(res);
- JSONArray jsonArray = jsonObject.getJSONArray("external_user_list");
- List<User> userList = new ArrayList<>();
- for (int j = 0; j < jsonArray.size(); j++) {
- String id = (String) jsonArray.getJSONObject(j).get("id");
- jsonArray.getJSONObject(j).put("id", null);
- User user = jsonArray.getJSONObject(j).toJavaObject(User.class);
- user.setExternalUserId3rdParty(id);
- userList.add(user);
- sum++;
- }
- userMapper.insertList(userList);
- if (jsonArray.size() < size) {
- if (total > sum) {
- //TODO 输出异常 插入数量不足
- System.out.println("插入数量不足");
- }
- break;
- }
- }
- } catch (Exception e) {
- //TODO 记录异常日志
- e.printStackTrace();
- }
- }
- public Integer getUserTotal(Long startTime, Long endTime) throws IOException {
- String res = getUser(1, 0, startTime, endTime);
- JSONObject jsonObject = JSONObject.parseObject(res);
- return jsonObject.getInteger("total");
- }
- public String getUser(Integer limit, Integer offset, Long startTime, Long endTime) throws IOException {
- String accessToken = accessTokenService.getAccessToken();
- String url = GET_USER_URL
- + "?access_token=" + accessToken
- + "&limit=" + limit + "&offset=" + offset + "&start_time=" + startTime + "&end_time=" + endTime;
- return httpPoolClientDefault.get(url);
- }
- public void updateUser() {
- try {
- Long endTime = System.currentTimeMillis() / 1000;
- Long startTime = endTime - 60 * 60;
- Integer total = getUpdateUserTotal(startTime, endTime);
- if (total == null || total == 0) {
- return;
- }
- int page = total / size + 1;
- int sum = 0;
- for (int i = 0; i < page; i++) {
- String res = getUpdateUser(size, i * size, 1726821389L, endTime);
- //TODO 记录查询数据 info日志
- if (ObjectUtils.isEmpty(res)) {
- continue;
- }
- JSONObject jsonObject = JSONObject.parseObject(res);
- JSONArray jsonArray = jsonObject.getJSONArray("external_user_list");
- for (int j = 0; j < jsonArray.size(); j++) {
- String id = (String) jsonArray.getJSONObject(j).get("id");
- jsonArray.getJSONObject(j).put("id", null);
- User user = jsonArray.getJSONObject(j).toJavaObject(User.class);
- user.setExternalUserId3rdParty(id);
- UserExample example = new UserExample();
- example.createCriteria().andExternalUserId3rdPartyEqualTo(user.getExternalUserId3rdParty());
- List<User> list = userMapper.selectByExample(example);
- if (CollectionUtils.isEmpty(list)) {
- //没有用户,走插入逻辑
- userMapper.insert(user);
- } else {
- User oldUser = list.get(0);
- user.setId(oldUser.getId());
- userMapper.updateByPrimaryKeySelective(user);
- }
- sum++;
- if (jsonArray.size() < size) {
- if (total > sum) {
- //TODO 输出异常 插入数量不足
- System.out.println("插入数量不足");
- }
- break;
- }
- }
- }
- } catch (IOException e) {
- //TODO 记录异常日志
- e.printStackTrace();
- }
- }
- public Integer getUpdateUserTotal(Long startTime, Long endTime) throws IOException {
- String res = getUpdateUser(1, 0, startTime, endTime);
- JSONObject jsonObject = JSONObject.parseObject(res);
- return jsonObject.getInteger("total");
- }
- public String getUpdateUser(Integer limit, Integer offset, Long startTime, Long endTime) throws IOException {
- String accessToken = accessTokenService.getAccessToken();
- String url = UPDATE_USER_URL
- + "?access_token=" + accessToken
- + "&limit=" + limit + "&offset=" + offset + "&start_update_time=" + startTime + "&end_update_time=" + endTime
- + "&source=external_user";
- return httpPoolClientDefault.get(url);
- }
- }
|