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 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 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); } }