123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- 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.utils.HttpClientUtil;
- import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
- import org.apache.commons.lang3.ObjectUtils;
- import org.springframework.beans.BeanUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Bean;
- import org.springframework.stereotype.Component;
- import org.springframework.util.CollectionUtils;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Collections;
- 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";
- String ACCESS_TOKEN = "38969e430219e6b18f88792184a37b11";
- private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
- final int size = 100;
- @Autowired
- private UserMapper userMapper;
- public void insertAllUser() {
- Long endTime = System.currentTimeMillis() / 1000;
- int sum = 0;
- for (int i = 0; i < 1000; i++) {
- String res = null;
- try {
- //TODO accessToken 暂时写死 后续增加接口刷新和存储
- res = getUser(ACCESS_TOKEN, size, i * size, 1720540800L, endTime);
- //TODO 记录查询数据 info日志
- } catch (IOException e) {
- //TODO 记录异常日志 记录起止时间和页数 startTime endTime i
- e.printStackTrace();
- }
- if (ObjectUtils.isEmpty(res)) {
- continue;
- }
- JSONObject jsonObject = JSONObject.parseObject(res);
- Integer total = (Integer) jsonObject.get("total");
- 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);
- //TODO 记录对象数据 info日志
- try {
- int insert = userMapper.insert(user);
- if (insert <= 0) {
- //TODO 异常日志
- continue;
- }
- } catch (Exception e) {
- //TODO 打印异常日志
- }
- sum++;
- }
- if (jsonArray.size() < size) {
- if (total != null && total > sum) {
- //TODO 输出异常 插入数量不足
- System.out.println("插入数量不足");
- }
- break;
- }
- }
- }
- public String getUser(String accessToken, Integer limit, Integer offset, Long startTime, Long endTime) throws IOException {
- 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() {
- Long endTime = System.currentTimeMillis() / 1000;
- int sum = 0;
- for (int i = 0; i < 1000; i++) {
- String res = null;
- try {
- //TODO accessToken 暂时写死 后续增加接口刷新和存储
- res = getUpdateUser(ACCESS_TOKEN, size, i * size, 1726821389L, endTime);
- //TODO 记录查询数据 info日志
- } catch (IOException e) {
- //TODO 记录异常日志 记录起止时间和页数 startTime endTime i
- e.printStackTrace();
- }
- if (ObjectUtils.isEmpty(res)) {
- continue;
- }
- JSONObject jsonObject = JSONObject.parseObject(res);
- Integer total = (Integer) jsonObject.get("total");
- 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)) {
- //没有用户,走插入逻辑
- int insert = userMapper.insert(user);
- if (insert <= 0) {
- //TODO 异常日志
- continue;
- }
- } else {
- User oldUser = list.get(0);
- user.setId(oldUser.getId());
- int update = userMapper.updateByPrimaryKeySelective(user);
- if (update < 0) {
- //TODO 异常日志
- continue;
- }
- }
- sum++;
- if (jsonArray.size() < size) {
- if (total != null && total > sum) {
- //TODO 输出异常 插入数量不足
- System.out.println("插入数量不足");
- }
- break;
- }
- }
- }
- }
- public String getUpdateUser(String accessToken, Integer limit, Integer offset, Long startTime, Long endTime) throws IOException {
- 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);
- }
- }
|