package com.tzld.piaoquan.offline.job; import com.alibaba.fastjson.JSONObject; import com.tzld.piaoquan.growth.common.component.HttpPoolClient; import com.tzld.piaoquan.growth.common.dao.mapper.CorpMapper; import com.tzld.piaoquan.growth.common.dao.mapper.StaffMapper; import com.tzld.piaoquan.growth.common.model.bo.XxlJobParam; import com.tzld.piaoquan.growth.common.model.po.Corp; import com.tzld.piaoquan.growth.common.model.po.CorpExample; import com.tzld.piaoquan.growth.common.model.po.Staff; import com.tzld.piaoquan.growth.common.model.po.StaffExample; import com.tzld.piaoquan.growth.common.service.WeComAccessTokenService; 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.List; import java.util.stream.Collectors; import static com.tzld.piaoquan.growth.common.common.constant.WeComConstant.GET_WE_COM_FOLLOW_USER_LIST; @Slf4j @Component public class WeComStaffDataJob { @Autowired private HttpPoolClient httpPoolClient; @Autowired private WeComAccessTokenService weComAccessTokenService; @Autowired private StaffMapper staffMapper; @Autowired private CorpMapper corpMapper; @XxlJob("insertStaffJob") public ReturnT insertStaff(String param) { try { XxlJobParam xxlJobParam = new XxlJobParam(); if (StringUtils.isNotEmpty(param)) { xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class); } CorpExample corpExample = new CorpExample(); if (xxlJobParam.getCorpId() != null) { corpExample.createCriteria().andIdEqualTo(xxlJobParam.getCorpId()); } List corps = corpMapper.selectByExample(corpExample); if (CollectionUtils.isEmpty(corps)) { return ReturnT.SUCCESS; } for (Corp corp : corps) { List carrierIdList = getCarrierIdList(corp.getId()); if (CollectionUtils.isEmpty(carrierIdList)) { continue; } for (String carrierId : carrierIdList) { StaffExample example = new StaffExample(); example.createCriteria().andCarrierIdEqualTo(carrierId).andCorpIdEqualTo(corp.getId()); List staffList = staffMapper.selectByExample(example); if (CollectionUtils.isEmpty(staffList)) { Staff staff = new Staff(); staff.setCorpId(corp.getId()); staff.setCarrierId(carrierId); staff.setRemark(""); staffMapper.insert(staff); } } } } catch (Exception e) { LarkRobotUtil.sendMessage("更新员工失败"); log.error("insertStaff error", e); } return ReturnT.SUCCESS; } private List getCarrierIdList(Long corpId) throws IOException { String weComAccessToken = weComAccessTokenService.getWeComAccessToken(corpId); String url = String.format(GET_WE_COM_FOLLOW_USER_LIST + "?access_token=%s", weComAccessToken); String res = httpPoolClient.get(url); log.info("getCarrierIdList corp = {}, res={}", corpId, res); JSONObject jsonObject = JSONObject.parseObject(res); Integer errcode = jsonObject.getInteger("errcode"); if (errcode == 0) { return jsonObject.getJSONArray("follow_user").stream().map(String::valueOf).collect(Collectors.toList()); } return null; } }