123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521 |
- package com.tzld.piaoquan.offline.job;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.tzld.piaoquan.growth.common.component.HttpPoolClient;
- import com.tzld.piaoquan.growth.common.dao.mapper.*;
- import com.tzld.piaoquan.growth.common.model.bo.XxlJobParam;
- import com.tzld.piaoquan.growth.common.model.po.*;
- import com.tzld.piaoquan.growth.common.service.WeComAccessTokenService;
- import com.tzld.piaoquan.growth.common.service.WeComStaffService;
- import com.tzld.piaoquan.growth.common.service.WeComUserService;
- import com.tzld.piaoquan.growth.common.utils.DateUtil;
- import com.tzld.piaoquan.growth.common.utils.LarkRobotUtil;
- 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.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.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.Collectors;
- import static com.tzld.piaoquan.growth.common.common.constant.WeComConstant.*;
- @Slf4j
- @Component
- public class WeComUserDataJob {
- @Autowired
- private HttpPoolClient httpPoolClient;
- @Autowired
- private StaffMapper staffMapper;
- @Autowired
- private WeComUserMapper weComUserMapper;
- @Autowired
- private WeComAccessTokenService weComAccessTokenService;
- @Autowired
- private StaffWithUserMapper staffWithUserMapper;
- @Autowired
- private WeComUserService weComUserService;
- @Autowired
- private CorpMapper corpMapper;
- @Autowired
- private UserCountMapper userCountMapper;
- @Autowired
- private TagMapper tagMapper;
- @Autowired
- private UserWithTagMapper userWithTagMapper;
- @Autowired
- private WeComStaffService weComStaffService;
- @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");
- WeComUserExample example = new WeComUserExample();
- example.createCriteria().andExternalUserIdEqualTo(externalUserId);
- List<WeComUser> weComUserList = weComUserMapper.selectByExample(example);
- WeComUser weComUser = new WeComUser();
- weComUser.setExternalUserId(externalUserId);
- weComUser.setName(name);
- weComUser.setType(type);
- weComUser.setUnionId(unionId);
- weComUser.setGender(gender);
- weComUser.setAvatar(avatar);
- weComUser.setCreatedAt(createAt);
- Long userId;
- if (CollectionUtils.isEmpty(weComUserList)) {
- weComUserMapper.insertSelective(weComUser);
- userId = weComUser.getId();
- } else {
- WeComUser oldWeComUser = weComUserList.get(0);
- weComUser.setId(oldWeComUser.getId());
- weComUserMapper.updateByPrimaryKeySelective(weComUser);
- userId = oldWeComUser.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.insertSelective(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 = weComAccessTokenService.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());
- if (!CollectionUtils.isEmpty(existExternalUserIds)) {
- allExternalUserIds.removeAll(existExternalUserIds);
- }
- if (!CollectionUtils.isEmpty(allExternalUserIds)) {
- for (String delExternalUserId : allExternalUserIds) {
- weComUserService.delStaffWithUser(delExternalUserId, staff.getCarrierId(), corp.getId(),
- System.currentTimeMillis());
- }
- }
- if (CollectionUtils.isEmpty(existExternalUserIds)) {
- continue;
- }
- for (String externalUserId : existExternalUserIds) {
- weComUserService.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 = weComAccessTokenService.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;
- }
- @XxlJob("statisticsUserCountJob")
- public ReturnT<String> statisticsUserCount(String param) {
- StaffExample staffExample = new StaffExample();
- List<Staff> staffList = staffMapper.selectByExample(staffExample);
- String date;
- if (StringUtils.isEmpty(param)) {
- date = DateUtil.getBeforeDayDateString1();
- } else {
- date = param;
- }
- for (Staff staff : staffList) {
- Map<String, Integer> userCountRes = getUserList(staff, 1L, date);
- if (userCountRes == null || userCountRes.get("count") == 0) {
- continue;
- }
- UserCountExample userCountExample = new UserCountExample();
- userCountExample.createCriteria().andStaffIdEqualTo(staff.getId()).andDateEqualTo(date);
- List<UserCount> userCounts = userCountMapper.selectByExample(userCountExample);
- if (CollectionUtils.isEmpty(userCounts)) {
- UserCount userCount = new UserCount();
- userCount.setStaffId(staff.getId());
- userCount.setStaffName(staff.getRemark());
- userCount.setAllCount(userCountRes.get("count"));
- userCount.setNewCount(userCountRes.get("newCount"));
- userCount.setDate(date);
- userCountMapper.insertSelective(userCount);
- } else {
- UserCount userCount = userCounts.get(0);
- userCount.setAllCount(userCountRes.get("count"));
- userCount.setNewCount(userCountRes.get("newCount"));
- userCountMapper.updateByPrimaryKeySelective(userCount);
- }
- }
- return ReturnT.SUCCESS;
- }
- private Map<String, Integer> getUserList(Staff staff, Long corpId, String date) {
- Map<String, Integer> result = new HashMap<>();
- int count = 0;
- int newCount = 0;
- try {
- String cursor = "";
- do {
- String res = getUserDetailList(staff.getCarrierId(), cursor, corpId);
- log.info("getUserList res={} cursor={}", res, cursor);
- JSONObject jsonObject = JSONObject.parseObject(res);
- Integer errCode = jsonObject.getInteger("errcode");
- if (errCode != 0) {
- log.error("getUserList error carrierId={} cursor={}", staff.getCarrierId(), cursor);
- return null;
- }
- JSONArray externalContactList = jsonObject.getJSONArray("external_contact_list");
- for (int i = 0; i < externalContactList.size(); i++) {
- JSONObject externalContact = externalContactList.getJSONObject(i).getJSONObject("external_contact");
- String externalUserId = externalContact.getString("external_userid");
- JSONObject followInfo = externalContactList.getJSONObject(i).getJSONObject("follow_info");
- Long createAt = followInfo.getLong("createtime");
- String dateString = DateUtil.getDateString(createAt * 1000, "yyyy-MM-dd");
- if (dateString.equals(date)) {
- newCount++;
- }
- count++;
- Long userId = weComUserMapper.selectIdByExternalUserId(externalUserId);
- userId = addAndUpdateUser(externalContact, corpId, externalUserId, createAt, staff.getId(), userId);
- JSONArray tagIds = followInfo.getJSONArray("tag_id");
- if (!CollectionUtils.isEmpty(tagIds)) {
- List<String> tagIdList = tagIds.toJavaList(String.class);
- for (String tagId : tagIdList) {
- TagExample example = new TagExample();
- example.createCriteria().andTagIdEqualTo(tagId);
- List<Tag> tags = tagMapper.selectByExample(example);
- if (!CollectionUtils.isEmpty(tags)) {
- Tag tag = tags.get(0);
- UserWithTag userWithTag = new UserWithTag();
- userWithTag.setUserId(userId);
- userWithTag.setTagId(tag.getId());
- addUserWithTag(userWithTag);
- }
- }
- }
- }
- String nextCursor = jsonObject.getString("next_cursor");
- if (cursor.equals(nextCursor)) {
- break;
- }
- cursor = nextCursor;
- } while (StringUtils.isNotEmpty(cursor));
- } catch (IOException e) {
- log.error("getUserList error", e);
- }
- result.put("count", count);
- result.put("newCount", newCount);
- return result;
- }
- @XxlJob("getUserTagJob")
- public ReturnT<String> getUserTag(String param) {
- try {
- refreshTag();
- 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());
- List<Long> staffIds = new ArrayList<>();
- if (xxlJobParam.getStaffId() != null) {
- staffIds.add(xxlJobParam.getStaffId());
- }
- if (!CollectionUtils.isEmpty(xxlJobParam.getStaffIds())) {
- staffIds.addAll(xxlJobParam.getStaffIds());
- }
- if (!CollectionUtils.isEmpty(staffIds)) {
- staffCriteria.andIdIn(staffIds);
- }
- List<Staff> staffList = staffMapper.selectByExample(staffExample);
- for (Staff staff : staffList) {
- List<String> existExternalUserIds = getUserList(staff.getCarrierId(), corp.getId());
- for (String externalUserId : existExternalUserIds) {
- JSONObject userDetail = weComUserService.getUserDetail(externalUserId, corp.getId());
- JSONObject externalContact = userDetail.getJSONObject("external_contact");
- JSONArray followUserList = userDetail.getJSONArray("follow_user");
- Long createAt = null;
- for (int i = 0; i < followUserList.size(); i++) {
- JSONObject followUser = followUserList.getJSONObject(i);
- if (createAt == null) {
- createAt = followUser.getLong("createtime");
- } else {
- createAt = Math.min(createAt, followUser.getLong("createtime"));
- }
- JSONArray tagIds = followUser.getJSONArray("tags");
- if (!CollectionUtils.isEmpty(tagIds)) {
- List<String> tagIdList = new ArrayList<>();
- for (int j = 0; j < tagIds.size(); j++) {
- JSONObject jsonObject = tagIds.getJSONObject(j);
- tagIdList.add(jsonObject.getString("tag_id"));
- }
- for (String tagId : tagIdList) {
- TagExample example = new TagExample();
- example.createCriteria().andTagIdEqualTo(tagId);
- List<Tag> tags = tagMapper.selectByExample(example);
- if (!CollectionUtils.isEmpty(tags)) {
- Long userId = weComUserMapper.selectIdByExternalUserId(externalUserId);
- userId = addAndUpdateUser(externalContact, corp.getId(), externalUserId,
- createAt, staff.getId(), userId);
- Tag tag = tags.get(0);
- UserWithTag userWithTag = new UserWithTag();
- userWithTag.setUserId(userId);
- userWithTag.setTagId(tag.getId());
- addUserWithTag(userWithTag);
- }
- }
- }
- }
- }
- }
- }
- } catch (Exception e) {
- log.error("getUserTag error", e);
- }
- return ReturnT.SUCCESS;
- }
- public void refreshTag() throws IOException {
- String weComAccessToken = weComAccessTokenService.getWeComAccessToken(1L);
- String url = String.format(POST_CORP_TAG_LIST + "?access_token=%s", weComAccessToken);
- String res = httpPoolClient.post(url);
- JSONObject jsonObject = JSONObject.parseObject(res);
- Integer errcode = jsonObject.getInteger("errcode");
- if (errcode == 0) {
- JSONArray tagGroup = jsonObject.getJSONArray("tag_group");
- if (!tagGroup.isEmpty()) {
- for (int i = 0; i < tagGroup.size(); i++) {
- JSONObject tagGroupJSONObject = tagGroup.getJSONObject(i);
- String groupId = tagGroupJSONObject.getString("group_id");
- String groupName = tagGroupJSONObject.getString("group_name");
- JSONArray tags = tagGroupJSONObject.getJSONArray("tag");
- if (tags.isEmpty()) {
- continue;
- }
- for (int j = 0; j < tags.size(); j++) {
- JSONObject tagsJSONObject = tags.getJSONObject(j);
- String tagId = tagsJSONObject.getString("id");
- String tagName = tagsJSONObject.getString("name");
- Tag tag = new Tag();
- tag.setTagGroupId(groupId);
- tag.setTagGroupName(groupName);
- tag.setTagId(tagId);
- tag.setTagName(tagName);
- addAndUpdateTag(tag);
- }
- }
- }
- }
- }
- private void addAndUpdateTag(Tag tag) {
- TagExample example = new TagExample();
- example.createCriteria().andTagGroupIdEqualTo(tag.getTagGroupId()).andTagIdEqualTo(tag.getTagId());
- List<Tag> tags = tagMapper.selectByExample(example);
- if (CollectionUtils.isEmpty(tags)) {
- tagMapper.insertSelective(tag);
- } else {
- tag.setId(tags.get(0).getId());
- tagMapper.updateByPrimaryKeySelective(tag);
- }
- }
- private void addUserWithTag(UserWithTag userWithTag) {
- UserWithTagExample userWithTagExample = new UserWithTagExample();
- userWithTagExample.createCriteria().andUserIdEqualTo(userWithTag.getUserId()).andTagIdEqualTo(userWithTag.getTagId());
- long l = userWithTagMapper.countByExample(userWithTagExample);
- if (l == 0) {
- userWithTagMapper.insertSelective(userWithTag);
- }
- }
- private Long addAndUpdateUser(JSONObject externalContact, Long corpId, String externalUserId, Long createAt, Long staffId, Long 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");
- if (userId == null) {
- WeComUser weComUser = new WeComUser();
- weComUser.setCorpId(corpId);
- weComUser.setExternalUserId(externalUserId);
- weComUser.setName(name);
- weComUser.setType(type);
- weComUser.setUnionId(unionId);
- weComUser.setGender(gender);
- weComUser.setAvatar(avatar);
- weComUser.setCreatedAt(createAt);
- weComUserMapper.insertSelective(weComUser);
- userId = weComUser.getId();
- StaffWithUser staffWithUser = new StaffWithUser();
- staffWithUser.setStaffId(staffId);
- staffWithUser.setUserId(userId);
- staffWithUserMapper.insertSelective(staffWithUser);
- } else {
- WeComUser weComUser = new WeComUser();
- weComUser.setId(userId);
- weComUser.setName(name);
- weComUser.setType(type);
- weComUser.setUnionId(unionId);
- weComUser.setGender(gender);
- weComUser.setAvatar(avatar);
- weComUserMapper.updateByPrimaryKeySelective(weComUser);
- StaffWithUserExample example = new StaffWithUserExample();
- example.createCriteria().andStaffIdEqualTo(staffId).andUserIdEqualTo(userId);
- List<StaffWithUser> staffWithUsers = staffWithUserMapper.selectByExample(example);
- if (CollectionUtils.isEmpty(staffWithUsers)) {
- StaffWithUser staffWithUser = new StaffWithUser();
- staffWithUser.setStaffId(staffId);
- staffWithUser.setUserId(userId);
- staffWithUserMapper.insertSelective(staffWithUser);
- }
- }
- return userId;
- }
- }
|