package com.tzld.piaoquan.wecom.job; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.tzld.piaoquan.wecom.common.constant.TimeConstant; import com.tzld.piaoquan.wecom.dao.mapper.StaffMapper; import com.tzld.piaoquan.wecom.dao.mapper.StaffWithUserMapper; import com.tzld.piaoquan.wecom.dao.mapper.UserMapper; import com.tzld.piaoquan.wecom.model.bo.XxlJobParam; import com.tzld.piaoquan.wecom.model.po.*; import com.tzld.piaoquan.wecom.service.AccessTokenService; import com.tzld.piaoquan.wecom.utils.HttpClientUtil; import com.tzld.piaoquan.wecom.utils.HttpPoolClient; import com.tzld.piaoquan.wecom.utils.page.Page; import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.handler.annotation.XxlJob; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; 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; import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.*; @Slf4j @Component public class WeComUserDataJob { 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; @Autowired private StaffMapper staffMapper; @Autowired private StaffWithUserMapper staffWithUserMapper; @Autowired private WeComUserDataJob1 weComUserDataJob1; @XxlJob("insertStaffWithUserJob") public ReturnT insertStaffWithUser(String param) { XxlJobParam xxlJobParam = new XxlJobParam(); if (StringUtils.isNotEmpty(param)) { xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class); } if (xxlJobParam.getStartTime() == null) { xxlJobParam.setStartTime(1720540800L); } if (xxlJobParam.getEndTime() == null) { xxlJobParam.setEndTime(System.currentTimeMillis() / 1000); } StaffExample example = new StaffExample(); StaffExample.Criteria criteria = example.createCriteria(); if (xxlJobParam.getStaffId() != null) { criteria.andIdEqualTo(xxlJobParam.getStaffId()); } List staffList = staffMapper.selectByExample(example); for (Staff staff : staffList) { insertAllUser(xxlJobParam.getStartTime(), xxlJobParam.getEndTime(), staff); } return ReturnT.SUCCESS; } //初始化用户使用此任务 private void insertAllUser(long startTime, long endTime, Staff staff) { try { Integer total = getUserTotal(startTime, endTime, staff.getStaffExtId()); 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, startTime, endTime, staff.getStaffExtId()); log.info("insertAllUser size={}, i={}, staffExtId = {}, startTime={}, endTime={}, res={}", size, i, staff.getStaffExtId(), startTime, endTime, res); if (ObjectUtils.isEmpty(res)) { continue; } JSONObject jsonObject = JSONObject.parseObject(res); JSONArray jsonArray = jsonObject.getJSONArray("external_user_list"); List insertStaffWithUserList = new ArrayList<>(); for (int j = 0; j < jsonArray.size(); j++) { String externalUserId3rdParty = (String) jsonArray.getJSONObject(j).get("id"); Long userId = userMapper.selectIdByExternalUserId3rdParty(externalUserId3rdParty); if (userId == null) { jsonArray.getJSONObject(j).put("id", null); User user = jsonArray.getJSONObject(j).toJavaObject(User.class); user.setExternalUserId3rdParty(externalUserId3rdParty); userMapper.insert(user); userId = user.getId(); } if (userId == null) { System.out.println("插入数据异常:" + jsonArray.getJSONObject(j)); continue; } StaffWithUserExample staffWithUserExample = new StaffWithUserExample(); staffWithUserExample.createCriteria().andStaffIdEqualTo(staff.getId()).andUserIdEqualTo(userId); List staffWithUserList = staffWithUserMapper.selectByExample(staffWithUserExample); if (CollectionUtils.isEmpty(staffWithUserList)) { StaffWithUser staffWithUser = new StaffWithUser(); staffWithUser.setUserId(userId); staffWithUser.setStaffId(staff.getId()); insertStaffWithUserList.add(staffWithUser); } sum++; } if (!CollectionUtils.isEmpty(insertStaffWithUserList)) { staffWithUserMapper.insertList(insertStaffWithUserList); } if (jsonArray.size() < size) { break; } } if (total > sum) { log.error("insertAllUser插入数量不足 total = {}, sum={}", total, sum); } } catch (Exception e) { log.error("insertAllUser error", e); } } private Integer getUserTotal(Long startTime, Long endTime, String staffId) throws IOException { String res = getUser(1, 0, startTime, endTime, staffId); JSONObject jsonObject = JSONObject.parseObject(res); return jsonObject.getInteger("total"); } private String getUser(Integer limit, Integer offset, Long startTime, Long endTime, String staffExtId) throws IOException { String accessToken = accessTokenService.getAccessToken(); String url = GET_USER_URL + "?access_token=" + accessToken + "&limit=" + limit + "&offset=" + offset + "&start_time=" + startTime + "&end_time=" + endTime; if (StringUtils.isNotEmpty(staffExtId)) { url = url + "&staff_id=" + staffExtId; } return httpPoolClientDefault.get(url); } @XxlJob("updateStaffWithUserJob") public ReturnT updateStaffWithUser(String param) { XxlJobParam xxlJobParam = new XxlJobParam(); if (StringUtils.isNotEmpty(param)) { xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class); } if (xxlJobParam.getStartTime() == null) { UserExample userExample = new UserExample(); userExample.setOrderByClause("create_time desc"); userExample.setPage(new Page<>(1, 1)); List userList = userMapper.selectByExample(userExample); xxlJobParam.setStartTime(userList.get(0).getCreateTime().getTime() / 1000 - 2L * TimeConstant.HOUR); } if (xxlJobParam.getEndTime() == null) { xxlJobParam.setEndTime(System.currentTimeMillis() / 1000); } StaffExample example = new StaffExample(); StaffExample.Criteria criteria = example.createCriteria(); if (xxlJobParam.getStaffId() != null) { criteria.andIdEqualTo(xxlJobParam.getStaffId()); } List staffList = staffMapper.selectByExample(example); for (Staff staff : staffList) { updateUser(xxlJobParam.getStartTime(), xxlJobParam.getEndTime(), staff); } return ReturnT.SUCCESS; } private void updateUser(long startTime, long endTime, Staff staff) { try { Integer total = getUpdateUserTotal(startTime, endTime, staff.getStaffExtId()); 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, startTime, endTime, staff.getStaffExtId()); log.info("updateUser size={}, i={}, staffExtId = {}, startTime={}, endTime={}, res={}", size, i, staff.getStaffExtId(), startTime, endTime, res); 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); Long userId; if (CollectionUtils.isEmpty(list)) { //没有用户,走插入逻辑 String externalUserId = weComUserDataJob1.getExternalUserId(user.getExternalUserId3rdParty()); user.setExternalUserId(externalUserId); userMapper.insert(user); userId = user.getId(); } else { User oldUser = list.get(0); user.setId(oldUser.getId()); userMapper.updateByPrimaryKeySelective(user); userId = oldUser.getId(); } if (userId == null) { continue; } StaffWithUserExample staffWithUserExample = new StaffWithUserExample(); staffWithUserExample.createCriteria().andStaffIdEqualTo(staff.getId()).andUserIdEqualTo(userId); List staffWithUserList = staffWithUserMapper.selectByExample(staffWithUserExample); if (CollectionUtils.isEmpty(staffWithUserList)) { StaffWithUser staffWithUser = new StaffWithUser(); staffWithUser.setStaffId(staff.getId()); staffWithUser.setUserId(userId); staffWithUserMapper.insert(staffWithUser); } sum++; } if (jsonArray.size() < size) { break; } } if (total > sum) { log.error("updateUser插入数量不足 total = {}, sum={}", total, sum); } } catch (IOException e) { log.error("updateUser error", e); } } private Integer getUpdateUserTotal(Long startTime, Long endTime, String staffExtId) throws IOException { String res = getUpdateUser(1, 0, startTime, endTime, staffExtId); JSONObject jsonObject = JSONObject.parseObject(res); return jsonObject.getInteger("total"); } private String getUpdateUser(Integer limit, Integer offset, Long startTime, Long endTime, String staffExtId) 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"; if (StringUtils.isNotEmpty(staffExtId)) { url = url + "&staff_id=" + staffExtId; } return httpPoolClientDefault.get(url); } @XxlJob("deleteUserJob") public ReturnT deleteUserJob(String param) { XxlJobParam xxlJobParam = new XxlJobParam(); if (StringUtils.isNotEmpty(param)) { xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class); } if (xxlJobParam.getStartTime() == null) { UserExample userExample = new UserExample(); userExample.createCriteria().andIsDeleteEqualTo(1); userExample.setOrderByClause("deleted_at desc"); userExample.setPage(new Page<>(1, 1)); List userList = userMapper.selectByExample(userExample); if (CollectionUtils.isEmpty(userList)) { xxlJobParam.setStartTime(1720540800L); } else { xxlJobParam.setStartTime(userList.get(0).getDeletedAt()); } } if (xxlJobParam.getEndTime() == null) { xxlJobParam.setEndTime(System.currentTimeMillis() / 1000); } deleteUser(xxlJobParam.getStartTime(), xxlJobParam.getEndTime()); return ReturnT.SUCCESS; } //查询删除用户并更新 private void deleteUser(long startTime, long endTime) { try { Integer total = getDeleteUserTotal(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 = getDeleteUser(size, i * size, startTime, endTime); log.info("deleteUser size={}, i={}, startTime={}, endTime={}, res={}", size, i, startTime, endTime, res); 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++) { JSONObject staffRelation = jsonArray.getJSONObject(j).getJSONObject("staff_relation"); Long deletedAt = staffRelation.getLong("deleted_at"); String externalUserId3rdParty = staffRelation.getString("external_user_ext_id"); Long userId = userMapper.selectIdByExternalUserId3rdParty(externalUserId3rdParty); User updateUser = new User(); updateUser.setId(userId); updateUser.setIsDelete(1); updateUser.setDeletedAt(deletedAt); userMapper.updateByPrimaryKeySelective(updateUser); sum++; } } if (total > sum) { log.error("deleteUser数量不足 total = {}, sum={}", total, sum); } } catch (Exception e) { log.error("deleteUser error", e); } } private Integer getDeleteUserTotal(Long startTime, Long endTime) throws IOException { String res = getDeleteUser(1, 0, startTime, endTime); JSONObject jsonObject = JSONObject.parseObject(res); return jsonObject.getInteger("total_count"); } private String getDeleteUser(Integer limit, Integer offset, Long startTime, Long endTime) throws IOException { String accessToken = accessTokenService.getAccessToken(); String url = GET_DELETE_USER_URL + "?access_token=" + accessToken + "&limit=" + limit + "&offset=" + offset + "&start_time=" + startTime + "&end_time=" + endTime; return httpPoolClientDefault.get(url); } }