WeComUserDataJob.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package com.tzld.piaoquan.wecom.job;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.tzld.piaoquan.wecom.component.HttpPoolClient;
  5. import com.tzld.piaoquan.wecom.dao.mapper.CorpMapper;
  6. import com.tzld.piaoquan.wecom.dao.mapper.StaffMapper;
  7. import com.tzld.piaoquan.wecom.dao.mapper.StaffWithUserMapper;
  8. import com.tzld.piaoquan.wecom.dao.mapper.UserMapper;
  9. import com.tzld.piaoquan.wecom.model.bo.XxlJobParam;
  10. import com.tzld.piaoquan.wecom.model.po.*;
  11. import com.tzld.piaoquan.wecom.service.AccessTokenService;
  12. import com.tzld.piaoquan.wecom.service.UserService;
  13. import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
  14. import com.tzld.piaoquan.wecom.utils.LarkRobotUtil;
  15. import com.tzld.piaoquan.wecom.utils.page.Page;
  16. import com.xxl.job.core.biz.model.ReturnT;
  17. import com.xxl.job.core.handler.annotation.XxlJob;
  18. import lombok.Data;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.apache.commons.lang3.StringUtils;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.stereotype.Component;
  23. import org.springframework.util.CollectionUtils;
  24. import java.io.IOException;
  25. import java.util.List;
  26. import java.util.stream.Collectors;
  27. import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.*;
  28. @Slf4j
  29. @Component
  30. public class WeComUserDataJob {
  31. @Autowired
  32. private HttpPoolClient httpPoolClient;
  33. @Autowired
  34. private StaffMapper staffMapper;
  35. @Autowired
  36. private UserMapper userMapper;
  37. @Autowired
  38. private AccessTokenService accessTokenService;
  39. @Autowired
  40. private StaffWithUserMapper staffWithUserMapper;
  41. @Autowired
  42. private UserService userService;
  43. @Autowired
  44. private CorpMapper corpMapper;
  45. @XxlJob("updateStaffWithUserJob")
  46. public ReturnT<String> updateStaffWithUser(String param) {
  47. XxlJobParam xxlJobParam = new XxlJobParam();
  48. if (StringUtils.isNotEmpty(param)) {
  49. xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class);
  50. }
  51. CorpExample corpExample = new CorpExample();
  52. CorpExample.Criteria corpCriteria = corpExample.createCriteria();
  53. if (xxlJobParam.getCorpId() != null) {
  54. corpCriteria.andIdEqualTo(xxlJobParam.getCorpId());
  55. }
  56. List<Corp> corps = corpMapper.selectByExample(corpExample);
  57. if (CollectionUtils.isEmpty(corps)) {
  58. return ReturnT.SUCCESS;
  59. }
  60. for (Corp corp : corps) {
  61. StaffExample staffExample = new StaffExample();
  62. StaffExample.Criteria staffCriteria = staffExample.createCriteria();
  63. staffCriteria.andCorpIdEqualTo(corp.getId());
  64. if (xxlJobParam.getStaffId() != null) {
  65. staffCriteria.andIdEqualTo(xxlJobParam.getStaffId());
  66. }
  67. List<Staff> staffList = staffMapper.selectByExample(staffExample);
  68. if (CollectionUtils.isEmpty(staffList)) {
  69. continue;
  70. }
  71. for (Staff staff : staffList) {
  72. updateUserList(staff, corp.getId());
  73. }
  74. }
  75. return ReturnT.SUCCESS;
  76. }
  77. private void updateUserList(Staff staff, Long corpId) {
  78. try {
  79. String cursor = "";
  80. do {
  81. String res = getUserDetailList(staff.getCarrierId(), cursor, corpId);
  82. log.info("updateUserList res={} cursor={}", res, cursor);
  83. JSONObject jsonObject = JSONObject.parseObject(res);
  84. Integer errCode = jsonObject.getInteger("errcode");
  85. if (errCode != 0) {
  86. log.error("updateUserList error carrierId={} cursor={}", staff.getCarrierId(), cursor);
  87. return;
  88. }
  89. JSONArray externalContactList = jsonObject.getJSONArray("external_contact_list");
  90. for (int i = 0; i < externalContactList.size(); i++) {
  91. JSONObject externalContact = externalContactList.getJSONObject(i).getJSONObject("external_contact");
  92. JSONObject followInfo = externalContactList.getJSONObject(i).getJSONObject("follow_info");
  93. Long createAt = followInfo.getLong("createtime");
  94. String externalUserId = externalContact.getString("external_userid");
  95. String name = externalContact.getString("name");
  96. String unionId = externalContact.getString("unionid");
  97. String avatar = externalContact.getString("avatar");
  98. Integer type = externalContact.getInteger("type");
  99. Integer gender = externalContact.getInteger("gender");
  100. UserExample example = new UserExample();
  101. example.createCriteria().andExternalUserIdEqualTo(externalUserId);
  102. List<User> userList = userMapper.selectByExample(example);
  103. User user = new User();
  104. user.setExternalUserId(externalUserId);
  105. user.setName(name);
  106. user.setType(type);
  107. user.setUnionId(unionId);
  108. user.setGender(gender);
  109. user.setAvatar(avatar);
  110. user.setCreatedAt(createAt);
  111. Long userId;
  112. if (CollectionUtils.isEmpty(userList)) {
  113. userMapper.insert(user);
  114. userId = user.getId();
  115. } else {
  116. User oldUser = userList.get(0);
  117. user.setId(oldUser.getId());
  118. userMapper.updateByPrimaryKeySelective(user);
  119. userId = oldUser.getId();
  120. }
  121. if (userId == null) {
  122. continue;
  123. }
  124. StaffWithUserExample staffWithUserExample = new StaffWithUserExample();
  125. staffWithUserExample.createCriteria().andStaffIdEqualTo(staff.getId()).andUserIdEqualTo(userId);
  126. List<StaffWithUser> staffWithUserList = staffWithUserMapper.selectByExample(staffWithUserExample);
  127. if (CollectionUtils.isEmpty(staffWithUserList)) {
  128. StaffWithUser staffWithUser = new StaffWithUser();
  129. staffWithUser.setStaffId(staff.getId());
  130. staffWithUser.setUserId(userId);
  131. staffWithUserMapper.insert(staffWithUser);
  132. }
  133. }
  134. String nextCursor = jsonObject.getString("next_cursor");
  135. if (cursor.equals(nextCursor)) {
  136. break;
  137. }
  138. cursor = nextCursor;
  139. } while (StringUtils.isNotEmpty(cursor));
  140. } catch (IOException e) {
  141. LarkRobotUtil.sendMessage("updateUser error" + e);
  142. log.error("updateUser error", e);
  143. }
  144. }
  145. private String getUserDetailList(String userId, String cursor, Long corpId) throws IOException {
  146. String accessToken = accessTokenService.getWeComAccessToken(corpId);
  147. String url = POST_WE_COM_GET_BY_USER
  148. + "?access_token=" + accessToken;
  149. JSONObject param = new JSONObject();
  150. JSONArray userIdList = new JSONArray();
  151. userIdList.add(userId);
  152. param.put("userid_list", userIdList);
  153. param.put("limit", 100);
  154. if (StringUtils.isNotEmpty(cursor)) {
  155. param.put("cursor", cursor);
  156. }
  157. return httpPoolClient.post(url, param.toJSONString());
  158. }
  159. @XxlJob("insertStaffWithUserJob")
  160. public ReturnT<String> insertStaffWithUserJob(String param) {
  161. try {
  162. XxlJobParam xxlJobParam = new XxlJobParam();
  163. if (StringUtils.isNotEmpty(param)) {
  164. xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class);
  165. }
  166. CorpExample corpExample = new CorpExample();
  167. CorpExample.Criteria corpCriteria = corpExample.createCriteria();
  168. if (xxlJobParam.getCorpId() != null) {
  169. corpCriteria.andIdEqualTo(xxlJobParam.getCorpId());
  170. }
  171. List<Corp> corps = corpMapper.selectByExample(corpExample);
  172. for (Corp corp : corps) {
  173. StaffExample staffExample = new StaffExample();
  174. StaffExample.Criteria staffCriteria = staffExample.createCriteria();
  175. staffCriteria.andCorpIdEqualTo(corp.getId());
  176. if (xxlJobParam.getStaffId() != null) {
  177. staffCriteria.andIdEqualTo(xxlJobParam.getStaffId());
  178. }
  179. List<Staff> staffList = staffMapper.selectByExample(staffExample);
  180. for (Staff staff : staffList) {
  181. List<String> existExternalUserIds = getUserList(staff.getCarrierId(), corp.getId());
  182. List<String> allExternalUserIds = staffWithUserMapper.selectExternalUserIdByStaffId(staff.getId());
  183. allExternalUserIds.removeAll(existExternalUserIds);
  184. if (!CollectionUtils.isEmpty(allExternalUserIds)) {
  185. for (String delExternalUserId : allExternalUserIds) {
  186. userService.delStaffWithUser(delExternalUserId, staff.getCarrierId(), System.currentTimeMillis());
  187. }
  188. }
  189. if (CollectionUtils.isEmpty(existExternalUserIds)) {
  190. continue;
  191. }
  192. for (String externalUserId : existExternalUserIds) {
  193. userService.insertStaffWithUser(externalUserId, staff, corp.getId());
  194. }
  195. }
  196. }
  197. } catch (Exception e) {
  198. LarkRobotUtil.sendMessage("insertStaffWithUserJob error" + e);
  199. log.error("insertStaffWithUserJob error", e);
  200. }
  201. return ReturnT.SUCCESS;
  202. }
  203. public List<String> getUserList(String userId, Long corpId) throws IOException {
  204. String weComAccessToken = accessTokenService.getWeComAccessToken(corpId);
  205. String url = String.format(GET_WE_COM_EXTERNAL_CONTACT_LIST + "?access_token=%s&userid=%s", weComAccessToken, userId);
  206. String res = httpPoolClient.get(url);
  207. JSONObject jsonObject = JSONObject.parseObject(res);
  208. Integer errcode = jsonObject.getInteger("errcode");
  209. if (errcode == 0) {
  210. return jsonObject.getJSONArray("external_userid").stream().map(String::valueOf).collect(Collectors.toList());
  211. }
  212. return null;
  213. }
  214. }