SyncCustomerUnionId.java
5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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());
}
}
}