WeComStaffDataJob.java 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package com.tzld.piaoquan.wecom.job;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.tzld.piaoquan.wecom.component.HttpPoolClient;
  4. import com.tzld.piaoquan.wecom.dao.mapper.StaffMapper;
  5. import com.tzld.piaoquan.wecom.model.po.Staff;
  6. import com.tzld.piaoquan.wecom.model.po.StaffExample;
  7. import com.tzld.piaoquan.wecom.service.AccessTokenService;
  8. import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
  9. import com.tzld.piaoquan.wecom.utils.LarkRobotUtil;
  10. import com.xxl.job.core.biz.model.ReturnT;
  11. import com.xxl.job.core.handler.annotation.XxlJob;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Component;
  15. import org.springframework.util.CollectionUtils;
  16. import java.io.IOException;
  17. import java.util.List;
  18. import java.util.stream.Collectors;
  19. import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.GET_WE_COM_FOLLOW_USER_LIST;
  20. @Slf4j
  21. @Component
  22. public class WeComStaffDataJob {
  23. @Autowired
  24. private HttpPoolClient httpPoolClient;
  25. @Autowired
  26. private AccessTokenService accessTokenService;
  27. @Autowired
  28. private StaffMapper staffMapper;
  29. @XxlJob("insertStaffJob")
  30. public ReturnT<String> insertStaff(String param) {
  31. try {
  32. List<String> carrierIdList = getCarrierIdList();
  33. for (String carrierId : carrierIdList) {
  34. StaffExample example = new StaffExample();
  35. example.createCriteria().andCarrierIdEqualTo(carrierId);
  36. List<Staff> staffList = staffMapper.selectByExample(example);
  37. if (CollectionUtils.isEmpty(staffList)) {
  38. Staff staff = new Staff();
  39. staff.setCarrierId(carrierId);
  40. staff.setRemark("");
  41. staffMapper.insert(staff);
  42. }
  43. }
  44. } catch (Exception e) {
  45. LarkRobotUtil.sendMessage("更新员工失败");
  46. log.error("insertStaff error", e);
  47. }
  48. return ReturnT.SUCCESS;
  49. }
  50. public List<String> getCarrierIdList() throws IOException {
  51. String weComAccessToken = accessTokenService.getWeComAccessToken();
  52. String url = String.format(GET_WE_COM_FOLLOW_USER_LIST + "?access_token=%s", weComAccessToken);
  53. String res = httpPoolClient.get(url);
  54. JSONObject jsonObject = JSONObject.parseObject(res);
  55. Integer errcode = jsonObject.getInteger("errcode");
  56. if (errcode == 0) {
  57. return jsonObject.getJSONArray("follow_user").stream().map(String::valueOf).collect(Collectors.toList());
  58. }
  59. return null;
  60. }
  61. }