SyncCustomerUnionId.java 5.93 KB
package cn.com.glsx.entmicro.job;

import cn.com.glsx.entmicro.mapper.CustomerReportMapper;
import cn.com.glsx.entmicro.service.CustomerService;
import cn.com.glsx.entmicro.service.EntmicroStaffService;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.scrm.wechat.dto.workwechat.external.request.GetWorkWechatExternalContactRequestDTO;
import cn.com.glsx.scrm.wechat.dto.workwechat.external.response.WorkWechatExternalContactDTO;
import cn.com.glsx.scrm.wechat.dto.workwechat.external.response.WorkWechatExternalContactResponseDTO;
import cn.com.glsx.scrm.wechat.remote.WorkWechatExternalContactRemote;
import com.alibaba.fastjson.JSONArray;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.elasticjob.lite.annotation.ElasticSimpleJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description 同步客户unionId
 * @author chizw
 * @create 2022/7/10 14:48
 * @version ver.1.0.0
 */
@Slf4j
@Component
@ElasticSimpleJob(jobName = "SyncCustomerUnionId", cron = "0 0 1 ? * 1")
public class SyncCustomerUnionId implements SimpleJob {

    @Resource
    private CustomerReportMapper customerReportMapper;

    // 定时任务开启
    @Value("${scrm_subscribe_job_enabled}")
    private String jobEnabled;

    @Autowired
    private Environment environment;

    @Resource
    private EntmicroStaffService entmicroStaffService;

    @Resource
    private CustomerService customerService;

    @Autowired
    private WorkWechatExternalContactRemote contactRemote;

    @Override
    public void execute(ShardingContext shardingContext) {
        try {
            if (!StringUtils.isEmpty(jobEnabled) && Boolean.parseBoolean(jobEnabled)){
                String merchantIdScan = environment.getProperty("scrm_subscribe_merchantId_scan");
                List<Integer> merchantIdList = new ArrayList<>();
                if (StringUtils.isEmpty(merchantIdScan)){
                    merchantIdList = customerReportMapper.selectAllMerchantId();
                }else {
                    String[] split = StringUtils.split(merchantIdScan, ",");
                    for (String s : split) {
                        merchantIdList.add(Integer.valueOf(s));
                    }
                }
                log.info("同步unionId商户号 merchantIdList:{}",merchantIdList);
                if (!CollectionUtils.isEmpty(merchantIdList)){
                    for (Integer merchantId : merchantIdList) {
                        // 同步商户unionId
                        List<String> userIdList = customerService.findUseridByUnionIdIsNull(merchantId);
                        log.info("同步关注客户列表 merchant:{},userIdList:{}",merchantId, userIdList);
                        Integer supMerchantNo = entmicroStaffService.findSupMerchantNo(merchantId);
                        if(!CollectionUtils.isEmpty(userIdList) && supMerchantNo != null){
//                            List<WorkWechatExternalContactListDTO> customerList = syncDataService.batchGetCustomerByStaff(userIdList, supMerchantNo);
//                            for (WorkWechatExternalContactListDTO dto : customerList) {
//                                if (dto.isSuccess()){
//                                    WorkWechatExternalContactDTO externalContact = dto.getExternalContact();
//                                    customerService.updateCustomerUnionIdByUserId(externalContact.getExternalUserId(), externalContact.getUnionid());
//                                }
//                            }
                            for (String userId : userIdList) {
                                GetWorkWechatExternalContactRequestDTO getWorkWechatExternalContactRequestDTO = new GetWorkWechatExternalContactRequestDTO();
                                getWorkWechatExternalContactRequestDTO.setMerchantId(supMerchantNo);
                                getWorkWechatExternalContactRequestDTO.setExternalUserId(userId);
                                log.info("获取企微客户详情入参:{}", JSONArray.toJSONString(getWorkWechatExternalContactRequestDTO));
                                RpcResponse<WorkWechatExternalContactResponseDTO> externalContact = contactRemote.getExternalContact(getWorkWechatExternalContactRequestDTO);
                                log.info("获取企微客户详情:{}", JSONArray.toJSONString(externalContact));
                                if (externalContact == null || !externalContact.isSuccess()) continue;
                                WorkWechatExternalContactResponseDTO result = externalContact.getResult();
                                if (result == null || !(result.getErrCode() == null || Objects.equals(result.getErrCode(),0))) continue;
                                WorkWechatExternalContactDTO dto = result.getExternalContact();
                                if (dto == null) continue;
                                String unionId = dto.getUnionid();
                                if (StringUtils.isEmpty(unionId)) continue;
                                customerService.updateCustomerUnionIdByUserId(userId, unionId);
                                log.info("客户信息更新成功 merchant:{},userId:{},unionId:{}",merchantId, userId, unionId);
                            }
                        }
                    }
                }
                log.info("同步客户unionId成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("同步客户unionId异常,msg:"+e.getMessage());
        }
    }
}