123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- package com.tzld.piaoquan.wecom.job;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.tzld.piaoquan.wecom.component.HttpPoolClient;
- import com.tzld.piaoquan.wecom.dao.mapper.CorpMapper;
- 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.service.UserService;
- import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
- import com.tzld.piaoquan.wecom.utils.LarkRobotUtil;
- 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.Data;
- import lombok.extern.slf4j.Slf4j;
- 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.List;
- import java.util.stream.Collectors;
- import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.*;
- @Slf4j
- @Component
- public class WeComUserDataJob {
- @Autowired
- private HttpPoolClient httpPoolClient;
- @Autowired
- private StaffMapper staffMapper;
- @Autowired
- private UserMapper userMapper;
- @Autowired
- private AccessTokenService accessTokenService;
- @Autowired
- private StaffWithUserMapper staffWithUserMapper;
- @Autowired
- private UserService userService;
- @Autowired
- private CorpMapper corpMapper;
- @XxlJob("updateStaffWithUserJob")
- public ReturnT<String> updateStaffWithUser(String param) {
- XxlJobParam xxlJobParam = new XxlJobParam();
- if (StringUtils.isNotEmpty(param)) {
- xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class);
- }
- CorpExample corpExample = new CorpExample();
- CorpExample.Criteria corpCriteria = corpExample.createCriteria();
- if (xxlJobParam.getCorpId() != null) {
- corpCriteria.andIdEqualTo(xxlJobParam.getCorpId());
- }
- List<Corp> corps = corpMapper.selectByExample(corpExample);
- if (CollectionUtils.isEmpty(corps)) {
- return ReturnT.SUCCESS;
- }
- for (Corp corp : corps) {
- StaffExample staffExample = new StaffExample();
- StaffExample.Criteria staffCriteria = staffExample.createCriteria();
- staffCriteria.andCorpIdEqualTo(corp.getId());
- if (xxlJobParam.getStaffId() != null) {
- staffCriteria.andIdEqualTo(xxlJobParam.getStaffId());
- }
- List<Staff> staffList = staffMapper.selectByExample(staffExample);
- if (CollectionUtils.isEmpty(staffList)) {
- continue;
- }
- for (Staff staff : staffList) {
- updateUserList(staff, corp.getId());
- }
- }
- return ReturnT.SUCCESS;
- }
- private void updateUserList(Staff staff, Long corpId) {
- try {
- String cursor = "";
- do {
- String res = getUserDetailList(staff.getCarrierId(), cursor, corpId);
- log.info("updateUserList res={} cursor={}", res, cursor);
- JSONObject jsonObject = JSONObject.parseObject(res);
- Integer errCode = jsonObject.getInteger("errcode");
- if (errCode != 0) {
- log.error("updateUserList error carrierId={} cursor={}", staff.getCarrierId(), cursor);
- return;
- }
- JSONArray externalContactList = jsonObject.getJSONArray("external_contact_list");
- for (int i = 0; i < externalContactList.size(); i++) {
- JSONObject externalContact = externalContactList.getJSONObject(i).getJSONObject("external_contact");
- JSONObject followInfo = externalContactList.getJSONObject(i).getJSONObject("follow_info");
- Long createAt = followInfo.getLong("createtime");
- String externalUserId = externalContact.getString("external_userid");
- String name = externalContact.getString("name");
- String unionId = externalContact.getString("unionid");
- String avatar = externalContact.getString("avatar");
- Integer type = externalContact.getInteger("type");
- Integer gender = externalContact.getInteger("gender");
- UserExample example = new UserExample();
- example.createCriteria().andExternalUserIdEqualTo(externalUserId);
- List<User> userList = userMapper.selectByExample(example);
- User user = new User();
- user.setExternalUserId(externalUserId);
- user.setName(name);
- user.setType(type);
- user.setUnionId(unionId);
- user.setGender(gender);
- user.setAvatar(avatar);
- user.setCreatedAt(createAt);
- Long userId;
- if (CollectionUtils.isEmpty(userList)) {
- userMapper.insert(user);
- userId = user.getId();
- } else {
- User oldUser = userList.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<StaffWithUser> staffWithUserList = staffWithUserMapper.selectByExample(staffWithUserExample);
- if (CollectionUtils.isEmpty(staffWithUserList)) {
- StaffWithUser staffWithUser = new StaffWithUser();
- staffWithUser.setStaffId(staff.getId());
- staffWithUser.setUserId(userId);
- staffWithUserMapper.insert(staffWithUser);
- }
- }
- String nextCursor = jsonObject.getString("next_cursor");
- if (cursor.equals(nextCursor)) {
- break;
- }
- cursor = nextCursor;
- } while (StringUtils.isNotEmpty(cursor));
- } catch (IOException e) {
- LarkRobotUtil.sendMessage("updateUser error" + e);
- log.error("updateUser error", e);
- }
- }
- private String getUserDetailList(String userId, String cursor, Long corpId) throws IOException {
- String accessToken = accessTokenService.getWeComAccessToken(corpId);
- String url = POST_WE_COM_GET_BY_USER
- + "?access_token=" + accessToken;
- JSONObject param = new JSONObject();
- JSONArray userIdList = new JSONArray();
- userIdList.add(userId);
- param.put("userid_list", userIdList);
- param.put("limit", 100);
- if (StringUtils.isNotEmpty(cursor)) {
- param.put("cursor", cursor);
- }
- return httpPoolClient.post(url, param.toJSONString());
- }
- @XxlJob("insertStaffWithUserJob")
- public ReturnT<String> insertStaffWithUserJob(String param) {
- try {
- XxlJobParam xxlJobParam = new XxlJobParam();
- if (StringUtils.isNotEmpty(param)) {
- xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class);
- }
- CorpExample corpExample = new CorpExample();
- CorpExample.Criteria corpCriteria = corpExample.createCriteria();
- if (xxlJobParam.getCorpId() != null) {
- corpCriteria.andIdEqualTo(xxlJobParam.getCorpId());
- }
- List<Corp> corps = corpMapper.selectByExample(corpExample);
- for (Corp corp : corps) {
- StaffExample staffExample = new StaffExample();
- StaffExample.Criteria staffCriteria = staffExample.createCriteria();
- staffCriteria.andCorpIdEqualTo(corp.getId());
- if (xxlJobParam.getStaffId() != null) {
- staffCriteria.andIdEqualTo(xxlJobParam.getStaffId());
- }
- List<Staff> staffList = staffMapper.selectByExample(staffExample);
- for (Staff staff : staffList) {
- List<String> existExternalUserIds = getUserList(staff.getCarrierId(), corp.getId());
- List<String> allExternalUserIds = staffWithUserMapper.selectExternalUserIdByStaffId(staff.getId());
- allExternalUserIds.removeAll(existExternalUserIds);
- if (!CollectionUtils.isEmpty(allExternalUserIds)) {
- for (String delExternalUserId : allExternalUserIds) {
- userService.delStaffWithUser(delExternalUserId, staff.getCarrierId(), System.currentTimeMillis());
- }
- }
- if (CollectionUtils.isEmpty(existExternalUserIds)) {
- continue;
- }
- for (String externalUserId : existExternalUserIds) {
- userService.insertStaffWithUser(externalUserId, staff, corp.getId());
- }
- }
- }
- } catch (Exception e) {
- LarkRobotUtil.sendMessage("insertStaffWithUserJob error" + e);
- log.error("insertStaffWithUserJob error", e);
- }
- return ReturnT.SUCCESS;
- }
- public List<String> getUserList(String userId, Long corpId) throws IOException {
- String weComAccessToken = accessTokenService.getWeComAccessToken(corpId);
- String url = String.format(GET_WE_COM_EXTERNAL_CONTACT_LIST + "?access_token=%s&userid=%s", weComAccessToken, userId);
- String res = httpPoolClient.get(url);
- JSONObject jsonObject = JSONObject.parseObject(res);
- Integer errcode = jsonObject.getInteger("errcode");
- if (errcode == 0) {
- return jsonObject.getJSONArray("external_userid").stream().map(String::valueOf).collect(Collectors.toList());
- }
- return null;
- }
- }
|