WeComStaffDataJob.java 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package com.tzld.piaoquan.offline.job;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.tzld.piaoquan.growth.common.component.HttpPoolClient;
  4. import com.tzld.piaoquan.growth.common.dao.mapper.CorpMapper;
  5. import com.tzld.piaoquan.growth.common.dao.mapper.StaffMapper;
  6. import com.tzld.piaoquan.growth.common.model.bo.XxlJobParam;
  7. import com.tzld.piaoquan.growth.common.model.po.Corp;
  8. import com.tzld.piaoquan.growth.common.model.po.CorpExample;
  9. import com.tzld.piaoquan.growth.common.model.po.Staff;
  10. import com.tzld.piaoquan.growth.common.model.po.StaffExample;
  11. import com.tzld.piaoquan.growth.common.service.WeComAccessTokenService;
  12. import com.tzld.piaoquan.growth.common.utils.LarkRobotUtil;
  13. import com.xxl.job.core.biz.model.ReturnT;
  14. import com.xxl.job.core.handler.annotation.XxlJob;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.apache.commons.lang3.StringUtils;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Component;
  19. import org.springframework.util.CollectionUtils;
  20. import java.io.IOException;
  21. import java.util.List;
  22. import java.util.stream.Collectors;
  23. import static com.tzld.piaoquan.growth.common.common.constant.WeComConstant.GET_WE_COM_FOLLOW_USER_LIST;
  24. @Slf4j
  25. @Component
  26. public class WeComStaffDataJob {
  27. @Autowired
  28. private HttpPoolClient httpPoolClient;
  29. @Autowired
  30. private WeComAccessTokenService weComAccessTokenService;
  31. @Autowired
  32. private StaffMapper staffMapper;
  33. @Autowired
  34. private CorpMapper corpMapper;
  35. @XxlJob("insertStaffJob")
  36. public ReturnT<String> insertStaff(String param) {
  37. try {
  38. XxlJobParam xxlJobParam = new XxlJobParam();
  39. if (StringUtils.isNotEmpty(param)) {
  40. xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class);
  41. }
  42. CorpExample corpExample = new CorpExample();
  43. if (xxlJobParam.getCorpId() != null) {
  44. corpExample.createCriteria().andIdEqualTo(xxlJobParam.getCorpId());
  45. }
  46. List<Corp> corps = corpMapper.selectByExample(corpExample);
  47. if (CollectionUtils.isEmpty(corps)) {
  48. return ReturnT.SUCCESS;
  49. }
  50. for (Corp corp : corps) {
  51. List<String> carrierIdList = getCarrierIdList(corp.getId());
  52. if (CollectionUtils.isEmpty(carrierIdList)) {
  53. continue;
  54. }
  55. for (String carrierId : carrierIdList) {
  56. StaffExample example = new StaffExample();
  57. example.createCriteria().andCarrierIdEqualTo(carrierId).andCorpIdEqualTo(corp.getId());
  58. List<Staff> staffList = staffMapper.selectByExample(example);
  59. if (CollectionUtils.isEmpty(staffList)) {
  60. Staff staff = new Staff();
  61. staff.setCorpId(corp.getId());
  62. staff.setCarrierId(carrierId);
  63. staff.setRemark("");
  64. staffMapper.insert(staff);
  65. }
  66. }
  67. }
  68. } catch (Exception e) {
  69. LarkRobotUtil.sendMessage("更新员工失败");
  70. log.error("insertStaff error", e);
  71. }
  72. return ReturnT.SUCCESS;
  73. }
  74. private List<String> getCarrierIdList(Long corpId) throws IOException {
  75. String weComAccessToken = weComAccessTokenService.getWeComAccessToken(corpId);
  76. String url = String.format(GET_WE_COM_FOLLOW_USER_LIST + "?access_token=%s", weComAccessToken);
  77. String res = httpPoolClient.get(url);
  78. log.info("getCarrierIdList corp = {}, res={}", corpId, res);
  79. JSONObject jsonObject = JSONObject.parseObject(res);
  80. Integer errcode = jsonObject.getInteger("errcode");
  81. if (errcode == 0) {
  82. return jsonObject.getJSONArray("follow_user").stream().map(String::valueOf).collect(Collectors.toList());
  83. }
  84. return null;
  85. }
  86. }