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 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 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 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 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 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 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 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 staffList = staffMapper.selectByExample(staffExample); for (Staff staff : staffList) { List existExternalUserIds = getUserList(staff.getCarrierId(), corp.getId()); List 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 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; } }