|
|
@@ -859,73 +859,106 @@ public class WxTaskService {
|
|
|
* @param accountIdList 企微成员id
|
|
|
*/
|
|
|
public void qwAddWx(List<Long> accountIdList) {
|
|
|
- log.info("==========执行企微申请加个微任务开始==========");
|
|
|
+ log.info("==========执行申请企微加好友任务开始==========");
|
|
|
try {
|
|
|
- // 获取需要添加微信的企微客户列表
|
|
|
- List<CompanyWxClient> clientList = getFilteredClientList(accountIdList);
|
|
|
- if (clientList.isEmpty()) {
|
|
|
- log.info("没有符合条件的客户需要添加微信");
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // 获取CompanyWxClient信息
|
|
|
- Map<Long, CompanyWxClient> clientMap = PubFun.listToMapByGroupObject(clientList, CompanyWxClient::getAccountId);
|
|
|
+ // 需要添加微信的列表
|
|
|
+ List<CompanyWxClient4WorkFlowVO> list = companyWxClientService.getQwAddWxList4Workflow(accountIdList);
|
|
|
+ log.info("申请企微加好友任务需要添加微信的数量:{}", list.size());
|
|
|
+ if (list.isEmpty()) return;
|
|
|
+ List<CompanyWxClient> addList = new ArrayList<>();
|
|
|
+ Map<Long, CompanyWxClient4WorkFlowVO> clientMap = PubFun.listToMapByGroupObject(list, CompanyWxClient4WorkFlowVO::getAccountId);
|
|
|
// 获取实际企微用户信息
|
|
|
- List<QwUser> qwUserList = qwUserMapper.selectBatchIds(clientMap.keySet()).stream()
|
|
|
+ List<QwUser> addAccountList = qwUserMapper.selectBatchIds(clientMap.keySet()).stream()
|
|
|
.filter(this::isValidQwUser)
|
|
|
.collect(Collectors.toList());
|
|
|
-
|
|
|
- log.info("需要企微添加的账号数量:{}", qwUserList.size());
|
|
|
- if (qwUserList.isEmpty()) return;
|
|
|
-
|
|
|
- // 处理加微逻辑
|
|
|
- List<CompanyWxClient> upClientList = processQwAddWx(qwUserList, clientMap);
|
|
|
-
|
|
|
- // 批量更新客户状态
|
|
|
- if (!upClientList.isEmpty()) {
|
|
|
- companyWxClientService.updateBatchById(upClientList);
|
|
|
- log.info("成功更新{}个客户的加微状态", upClientList.size());
|
|
|
+ log.info("企微申请加好友任务需要企微的账号数量:{}", addAccountList.size());
|
|
|
+ addAccountList.forEach(qwUser -> {
|
|
|
+ CompanyWxClient4WorkFlowVO client = clientMap.get(qwUser.getId());
|
|
|
+ if (client != null) {
|
|
|
+ CrmCustomer crmCustomer = crmCustomerService.selectCrmCustomerById(client.getCustomerId());
|
|
|
+ // 开始申请加微
|
|
|
+ WxWorkResponseDTO<String> resp = qwAddWxInvokeIpad(crmCustomer.getMobile(), qwUser.getUid(), qwUser.getServerId());
|
|
|
+ JSONObject runParam = new JSONObject();
|
|
|
+ runParam.put("qwId", qwUser.getId());
|
|
|
+ runParam.put("mobile", crmCustomer.getMobile());
|
|
|
+ runParam.put("qwUid", qwUser.getUid());
|
|
|
+ runParam.put("clientId", client.getId());
|
|
|
+ CompanyVoiceRoboticCallLogAddwx addLog = CompanyVoiceRoboticCallLogAddwx.initCallLog(
|
|
|
+ runParam.toJSONString(), client.getId(), client.getRoboticId(), qwUser.getId(), qwUser.getCompanyId());
|
|
|
+ if (resp != null && resp.getErrcode() == 0) {
|
|
|
+ // 加微消息已发送成功
|
|
|
+ client.setIsAdd(2);
|
|
|
+ client.setAddTime(LocalDateTime.now());
|
|
|
+ CompanyWxClient addItem = new CompanyWxClient();
|
|
|
+ BeanUtils.copyProperties(client, addItem);
|
|
|
+ addList.add(addItem);
|
|
|
+ addLog.setStatus(1);
|
|
|
+ addLog.setResult(JSON.toJSONString(resp));
|
|
|
+ addLog.setIsWeCom(2);
|
|
|
+ log.info("ROBOTIC-ID:{},企微申请加好友任务申请成功", client.getRoboticId());
|
|
|
+ } else {
|
|
|
+ log.error("ROBOTIC-ID:{},企微申请加好友任务加微失败:{}", client.getRoboticId(), runParam);
|
|
|
+ addLog.setStatus(3);
|
|
|
+ addLog.setResult(JSON.toJSONString(runParam));
|
|
|
+ }
|
|
|
+ asyncSaveCompanyVoiceRoboticCallLog(addLog);
|
|
|
+ } else {
|
|
|
+ log.error("企微申请加好友任务当前账号暂无需要添加微信:{}-{}", qwUser.getId(), qwUser.getQwUserName());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if (!addList.isEmpty()) {
|
|
|
+ companyWxClientService.updateBatchById(addList);
|
|
|
+ for (CompanyWxClient client : addList) {
|
|
|
+ CompanyWxClient4WorkFlowVO vo = clientMap.get(client.getAccountId());
|
|
|
+ IWorkflowNode node = workflowNodeFactory.createNode(vo.getCurrentNodeKey(),
|
|
|
+ NodeTypeEnum.fromValue(vo.getCurrentNodeType()),
|
|
|
+ vo.getCurrentNodeName(), null);
|
|
|
+ if (node instanceof AiQwAddWxTaskNode) {
|
|
|
+ CompletableFuture.runAsync(() -> {
|
|
|
+ AiQwAddWxTaskNode qwAddWxNode = (AiQwAddWxTaskNode) node;
|
|
|
+ qwAddWxNode.doneQwAddWx(vo.getWorkflowInstanceId());
|
|
|
+ }, cidExcutor);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- log.error("企微加微信任务执行异常", e);
|
|
|
+ log.error("企微申请加好友任务执行异常", e);
|
|
|
}
|
|
|
- log.info("==========执行企微申请加个微任务结束==========");
|
|
|
+ log.info("==========执行企微申请加好友任务结束==========");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 企微加微结果处理
|
|
|
*/
|
|
|
public void qwAddWxResult(List<Long> accountIdList) {
|
|
|
- log.info("==========执行企微申请加个微结果查询任务开始==========");
|
|
|
+ log.info("==========执行企微申请加微结果查询任务开始==========");
|
|
|
try {
|
|
|
//is_add = 2,状态为加微中且是企微类型
|
|
|
List<CompanyWxClient> clients = companyWxClientService.getQwAddWxList(accountIdList, 2);
|
|
|
- log.info("需要查询企微加个微结果的数量:{}", clients.size());
|
|
|
-
|
|
|
+ log.info("企微申请加微结果查询任务需要查询的数量:{}", clients.size());
|
|
|
+
|
|
|
if (clients.isEmpty()) return;
|
|
|
-
|
|
|
// 处理每个客户的加微结果
|
|
|
List<CompanyWxClient> upClientList = new ArrayList<>();
|
|
|
clients.parallelStream().forEach(client -> {
|
|
|
try {
|
|
|
processSingleClientResult(client, upClientList);
|
|
|
} catch (Exception e) {
|
|
|
- log.error("处理客户{}加微结果异常", client.getId(), e);
|
|
|
+ log.error("企微申请加微结果查询任务处理客户{}加微结果异常", client.getId(), e);
|
|
|
}
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
// 批量更新和后续处理
|
|
|
if (!upClientList.isEmpty()) {
|
|
|
batchUpdateClients(upClientList);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
} catch (Exception e) {
|
|
|
- log.error("企微加微结果处理异常", e);
|
|
|
+ log.error("企微申请加微结果查询任务处理异常", e);
|
|
|
}
|
|
|
- log.info("==========执行企微申请加个微结果查询任务结束==========");
|
|
|
+ log.info("==========执行企微申请加微结果查询任务结束==========");
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 扫描企微加微工作流延时任务
|
|
|
*/
|
|
|
@@ -957,24 +990,7 @@ public class WxTaskService {
|
|
|
log.info("===========工作流延时任务扫描结束===========");
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 获取过滤后的企微客户列表
|
|
|
- */
|
|
|
- private List<CompanyWxClient> getFilteredClientList(List<Long> accountIdList) {
|
|
|
- List<CompanyWxClient> list = companyWxClientService.getAddWxList(accountIdList, 2);
|
|
|
-
|
|
|
- // 排除掉没到达加微步骤的人
|
|
|
- List<CompanyVoiceRoboticCallees> excludeList = companyVoiceRoboticCalleesMapper.selectExcludeList(list,2);
|
|
|
- Set<String> excludeKeys = excludeList.stream()
|
|
|
- .filter(e -> !Constants.QW_ADD_WX.equals(getNextTaskOptimized(e.getTaskFlow(), e.getRunTaskFlow())))
|
|
|
- .map(callee -> callee.getRoboticId() + "_" + callee.getUserId())
|
|
|
- .collect(Collectors.toSet());
|
|
|
-
|
|
|
- return list.stream()
|
|
|
- .filter(client -> !excludeKeys.contains(client.getRoboticId() + "_" + client.getCustomerId()))
|
|
|
- .collect(Collectors.toList());
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 验证企微用户有效性
|
|
|
*/
|
|
|
@@ -985,55 +1001,7 @@ public class WxTaskService {
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * 处理企微加微逻辑
|
|
|
- */
|
|
|
- private List<CompanyWxClient> processQwAddWx(List<QwUser> qwUserList, Map<Long, CompanyWxClient> clientMap) {
|
|
|
- List<CompanyWxClient> upClientList = Collections.synchronizedList(new ArrayList<>());
|
|
|
-
|
|
|
- qwUserList.parallelStream().forEach(qwUser -> {
|
|
|
- try {
|
|
|
- processSingleQwUser(qwUser, clientMap, upClientList);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("处理企微用户{}异常", qwUser.getId(), e);
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- return upClientList;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 处理单个企微用户
|
|
|
- */
|
|
|
- private void processSingleQwUser(QwUser qwUser, Map<Long, CompanyWxClient> clientMap,
|
|
|
- List<CompanyWxClient> upClientList) {
|
|
|
- CompanyWxClient client = clientMap.get(qwUser.getId());
|
|
|
- if (client == null) {
|
|
|
- log.error("当前账号暂无需要添加微信:{}-{}", qwUser.getId(), qwUser.getQwUserName());
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- CrmCustomer crmCustomer = crmCustomerService.selectCrmCustomerById(client.getCustomerId());
|
|
|
- if (crmCustomer == null || StringUtils.isBlank(crmCustomer.getMobile())) {
|
|
|
- log.info("查询客户{}手机号为空,跳过执行", crmCustomer == null ? "" : crmCustomer.getCustomerName());
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- String task = redisCache.getCacheObject(Constants.TASK_ID + client.getRoboticId());
|
|
|
- log.info("ROBOTIC-ID:{},CLIENT-ID:{},当前任务执行状态:{}",
|
|
|
- client.getRoboticId(), client.getId(), task);
|
|
|
-
|
|
|
- if (!StringUtils.isNotEmpty(task) || !Constants.QW_ADD_WX.equals(task)) {
|
|
|
- log.error("ROBOTIC-ID:{},当前任务没有执行加微任务", client.getRoboticId());
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // 开始申请加微
|
|
|
- WxWorkResponseDTO<String> resp = qwAddWxInvokeIpad(crmCustomer.getMobile(), qwUser.getUid(), qwUser.getServerId());
|
|
|
- //处理申请加微结果
|
|
|
- handleAddWxResult(resp, client, qwUser, crmCustomer, upClientList);
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 企微加个微调用ipad端
|
|
|
@@ -1044,7 +1012,7 @@ public class WxTaskService {
|
|
|
*/
|
|
|
private WxWorkResponseDTO<String> qwAddWxInvokeIpad(String mobile, String qwUid, Long serverId) {
|
|
|
if (StringUtils.isBlank(mobile) || StringUtils.isBlank(qwUid) || serverId == null) {
|
|
|
- log.warn("参数校验失败: mobile={}, qwUid={}, serverId={}", mobile, qwUid, serverId);
|
|
|
+ log.warn("企微申请加好友任务参数校验失败: mobile={}, qwUid={}, serverId={}", mobile, qwUid, serverId);
|
|
|
return null;
|
|
|
}
|
|
|
try {
|
|
|
@@ -1057,69 +1025,46 @@ public class WxTaskService {
|
|
|
wxAddSearchDTO.setTicket(null);
|
|
|
|
|
|
WxWorkResponseDTO<String> response = wxWorkService.addSearch(wxAddSearchDTO, serverId);
|
|
|
- log.debug("企微加微接口调用结果: errcode={}, errmsg={}",
|
|
|
+ log.debug("企微申请加好友任务调用结果: errcode={}, errmsg={}",
|
|
|
response != null ? response.getErrcode() : "null",
|
|
|
response != null ? response.getErrmsg() : "null");
|
|
|
|
|
|
return response;
|
|
|
+
|
|
|
+ // 测试代码
|
|
|
+// WxWorkResponseDTO<String> response = new WxWorkResponseDTO<>();
|
|
|
+// response.setErrcode(0);
|
|
|
+// return response;
|
|
|
} catch (Exception e) {
|
|
|
- log.error("企微加个微请求接口异常: mobile={}, qwUid={}, serverId={}", mobile, qwUid, serverId, e);
|
|
|
+ log.error("企微申请加好友任务请求接口异常: mobile={}, qwUid={}, serverId={}", mobile, qwUid, serverId, e);
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 处理加微结果
|
|
|
- */
|
|
|
- private void handleAddWxResult(WxWorkResponseDTO<String> resp, CompanyWxClient client,
|
|
|
- QwUser qwUser, CrmCustomer crmCustomer,
|
|
|
- List<CompanyWxClient> upClientList) {
|
|
|
- JSONObject runParam = new JSONObject();
|
|
|
- runParam.put("qwId", qwUser.getId());
|
|
|
- runParam.put("mobile", crmCustomer.getMobile());
|
|
|
- runParam.put("qwUid", qwUser.getUid());
|
|
|
- runParam.put("clientId", client.getId());
|
|
|
-
|
|
|
- CompanyVoiceRoboticCallLogAddwx addLog = CompanyVoiceRoboticCallLogAddwx.initCallLog(
|
|
|
- runParam.toJSONString(), client.getId(), client.getRoboticId(), qwUser.getId(), qwUser.getCompanyId());
|
|
|
-
|
|
|
- if (resp != null && resp.getErrcode() == 0) {
|
|
|
- // 加微消息已发送成功
|
|
|
- client.setIsAdd(2);
|
|
|
- client.setAddTime(LocalDateTime.now());
|
|
|
- upClientList.add(client);
|
|
|
- addLog.setStatus(1);
|
|
|
- addLog.setResult(JSON.toJSONString(resp));
|
|
|
- addLog.setIsWeCom(2);
|
|
|
- log.info("ROBOTIC-ID:{},加微成功", client.getRoboticId());
|
|
|
- } else {
|
|
|
- // 加微消息发送失败,补偿重试
|
|
|
- handleFailedAddWxWithRetry(client, upClientList);
|
|
|
- }
|
|
|
-
|
|
|
- asyncSaveCompanyVoiceRoboticCallLog(addLog);
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 处理单个客户加微结果
|
|
|
*/
|
|
|
private void processSingleClientResult(CompanyWxClient client, List<CompanyWxClient> upClientList) {
|
|
|
if (StringUtils.isBlank(client.getPhone())) {
|
|
|
- handleFailedAddWx(client, upClientList, "无电话号码");
|
|
|
+ handleFailedAddWx(client, upClientList, "无电话号码",0);
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 查询外部联系人表是否有数据
|
|
|
QwExternalContact qwExternalContact = qwExternalContactMapper.queryQwUserIdIsAddContact(
|
|
|
client.getAccountId(), client.getPhone(), 2);
|
|
|
-
|
|
|
+
|
|
|
if (qwExternalContact != null && qwExternalContact.getId() > 0) {
|
|
|
handleSuccessfulAddWx(client, upClientList);
|
|
|
} else {
|
|
|
handleFailedAddWxWithRetry(client, upClientList);
|
|
|
}
|
|
|
+
|
|
|
+ //测试代码
|
|
|
+// handleSuccessfulAddWx(client, upClientList);
|
|
|
+// handleFailedAddWx(client, upClientList, "无电话号码",0);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 处理加微成功的情况
|
|
|
*/
|
|
|
@@ -1132,24 +1077,24 @@ public class WxTaskService {
|
|
|
.eq(CompanyVoiceRoboticCallLogAddwx::getIsWeCom, 2)
|
|
|
.set(CompanyVoiceRoboticCallLogAddwx::getStatus, 2)
|
|
|
.update();
|
|
|
-
|
|
|
+
|
|
|
client.setIsAdd(1);
|
|
|
client.setAddTime(LocalDateTime.now());
|
|
|
upClientList.add(client);
|
|
|
redisCache.deleteObject("qwAddWx_" + client.getId());
|
|
|
- log.info("ROBOTIC-ID:{},加微成功:{}", client.getRoboticId(), client.getId());
|
|
|
+ log.info("ROBOTIC-ID:{},企微申请加微结果查询任务加微成功:{}", client.getRoboticId(), client.getId());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 处理加微失败的情况
|
|
|
*/
|
|
|
- private void handleFailedAddWx(CompanyWxClient client, List<CompanyWxClient> upClientList, String reason) {
|
|
|
- log.error("ROBOTIC-ID:{},{}加微失败:{}", client.getRoboticId(), reason, client.getId());
|
|
|
+ private void handleFailedAddWx(CompanyWxClient client, List<CompanyWxClient> upClientList, String reason,Integer isAdd) {
|
|
|
+ String taskName = isAdd == 1 ? "企微申请加好友任务" : "加微结果查询任务";
|
|
|
+ log.error("ROBOTIC-ID:{},{}:{},clientId={}", client.getRoboticId(),taskName, reason, client.getId());
|
|
|
client.setIsAdd(3);
|
|
|
client.setUpdateTime(new Date());
|
|
|
upClientList.add(client);
|
|
|
-
|
|
|
- // 更新记录
|
|
|
+ // 更新日志记录
|
|
|
companyVoiceRoboticCallLogAddwxService.lambdaUpdate()
|
|
|
.eq(CompanyVoiceRoboticCallLogAddwx::getRoboticId, client.getRoboticId())
|
|
|
.eq(CompanyVoiceRoboticCallLogAddwx::getWxClientId, client.getId())
|
|
|
@@ -1159,106 +1104,103 @@ public class WxTaskService {
|
|
|
.set(CompanyVoiceRoboticCallLogAddwx::getResult, reason)
|
|
|
.update();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 处理加微失败并重试计数
|
|
|
*/
|
|
|
private void handleFailedAddWxWithRetry(CompanyWxClient client, List<CompanyWxClient> upClientList) {
|
|
|
- log.error("ROBOTIC-ID:{},加微失败:{}", client.getRoboticId(), client.getId());
|
|
|
+ String taskName = 0 == 1 ? "企微申请加好友任务" : "加微结果查询任务";
|
|
|
+ log.error("ROBOTIC-ID:{},{}失败:{}", client.getRoboticId(),taskName, client.getId());
|
|
|
String failCountStr = redisCache.getCacheObject("qwAddWx_" + client.getId());
|
|
|
int failCount = 1;
|
|
|
-
|
|
|
+
|
|
|
if (StringUtils.isNotBlank(failCountStr)) {
|
|
|
- failCount += Integer.parseInt(failCountStr);
|
|
|
- if (failCount >= 60 * 24) { // 超过一天
|
|
|
- handleFailedAddWx(client, upClientList, "超过最大重试次数");
|
|
|
+ if (Integer.parseInt(failCountStr) >= 60 * 24) { // 超过一天
|
|
|
+ handleFailedAddWx(client, upClientList, "超过最大重试次数", 0);
|
|
|
+ redisCache.deleteObject("qwAddWx_" + client.getId());
|
|
|
} else {
|
|
|
- redisCache.setCacheObject("qwAddWx_" + client.getId(), String.valueOf(failCount));
|
|
|
+ failCount += Integer.parseInt(failCountStr);
|
|
|
+ redisCache.setCacheObject("qwAddWx_" + client.getId(), String.valueOf(failCount-1));
|
|
|
}
|
|
|
} else {
|
|
|
redisCache.setCacheObject("qwAddWx_" + client.getId(), String.valueOf(failCount), 25, TimeUnit.HOURS);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 批量更新客户和相关数据
|
|
|
*/
|
|
|
private void batchUpdateClients(List<CompanyWxClient> upClientList) {
|
|
|
companyWxClientService.updateBatchById(upClientList);
|
|
|
-
|
|
|
- // 从 upClientList 中筛选出 isAdd=1即加微成功的数据
|
|
|
+ // 从 upClientList 中筛选出 isAdd=1和3加微失败的数据
|
|
|
List<CompanyWxClient> successClients = upClientList.stream()
|
|
|
- .filter(client -> client.getIsAdd() != null && client.getIsAdd() == 1)
|
|
|
+ .filter(client -> client.getIsAdd() != null && (client.getIsAdd() == 1 || client.getIsAdd() == 3))
|
|
|
.collect(Collectors.toList());
|
|
|
-
|
|
|
- // 根据加微成功的用户,判定是否加入延时执行下一步任务
|
|
|
- Set<Long> roboticIdSet = successClients.stream()
|
|
|
- .map(CompanyWxClient::getRoboticId)
|
|
|
- .collect(Collectors.toSet());
|
|
|
- Set<Long> userIdSet = successClients.stream()
|
|
|
- .map(CompanyWxClient::getCustomerId)
|
|
|
- .collect(Collectors.toSet());
|
|
|
-
|
|
|
- // 获取任务和callees数据
|
|
|
- List<CompanyVoiceRobotic> robotList = companyVoiceRoboticMapper.selectBatchIds(roboticIdSet);
|
|
|
- Map<Long, CompanyVoiceRobotic> roboticsMap = robotList.stream()
|
|
|
- .collect(Collectors.toMap(CompanyVoiceRobotic::getId, Function.identity(), (a, b) -> a));
|
|
|
-
|
|
|
- List<CompanyVoiceRoboticCallees> calleesList = companyVoiceRoboticCalleesMapper
|
|
|
- .selectCalleesListByRoboticIdsAndUserIds(userIdSet, roboticIdSet);
|
|
|
- Map<String, CompanyVoiceRoboticCallees> calleesMap = calleesList.stream()
|
|
|
- .collect(Collectors.toMap(e -> e.getUserId() + "-" + e.getRoboticId(), Function.identity(), (a, b) -> a));
|
|
|
-
|
|
|
- // 设置延时任务
|
|
|
- setupDelayTasks(successClients, roboticsMap, calleesMap);
|
|
|
-
|
|
|
- // 更新任务流程状态
|
|
|
- updateTaskFlows(calleesList, roboticIdSet);
|
|
|
+ if(!successClients.isEmpty()){
|
|
|
+ successClients.forEach(client -> {
|
|
|
+ triggerWorkflowOnAddWxSuccess(client.getId());
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
- * 设置延时任务
|
|
|
+ * 加微结果触发工作流继续执行
|
|
|
+ * @param wxClientId 加微客户ID
|
|
|
*/
|
|
|
- private void setupDelayTasks(List<CompanyWxClient> upClientList,
|
|
|
- Map<Long, CompanyVoiceRobotic> roboticsMap,
|
|
|
- Map<String, CompanyVoiceRoboticCallees> calleesMap) {
|
|
|
- upClientList.forEach(client -> {
|
|
|
- CompanyVoiceRobotic robotic = roboticsMap.get(client.getRoboticId());
|
|
|
- if (robotic == null) {
|
|
|
- log.error("ROBOTIC-ID:{},CLIENT-ID:{},没有找到任务", client.getRoboticId(), client.getId());
|
|
|
+ private void triggerWorkflowOnAddWxSuccess(Long wxClientId) {
|
|
|
+ try {
|
|
|
+ // 查找等待中的加微工作流实例
|
|
|
+ CompanyAiWorkflowExec waitingExec = companyAiWorkflowExecMapper.selectWaitingAddWxWorkflowByWxClientId(
|
|
|
+ wxClientId,
|
|
|
+ ExecutionStatusEnum.WAITING.getValue(),
|
|
|
+ NodeTypeEnum.AI_QW_ADD_WX_TASK.getValue());
|
|
|
+ if (waitingExec == null) {
|
|
|
+ log.info("未找到等待中的加微工作流实例 - wxClientId: {}", wxClientId);
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
- CompanyVoiceRoboticCallees callee = calleesMap.get(client.getCustomerId() + "-" + client.getRoboticId());
|
|
|
- if (callee == null) {
|
|
|
- log.error("ROBOTIC-ID:{},CLIENT-ID:{},没有找到任务", client.getRoboticId(), client.getId());
|
|
|
+ //查询工作流加微执行日志是否未更新状态
|
|
|
+ CompanyAiWorkflowExecLog queryP = new CompanyAiWorkflowExecLog();
|
|
|
+ queryP.setWorkflowInstanceId(waitingExec.getWorkflowInstanceId());
|
|
|
+ queryP.setNodeType(NodeTypeEnum.AI_QW_ADD_WX_TASK.getValue());
|
|
|
+ queryP.setStatus(ExecutionStatusEnum.WAITING.getValue());
|
|
|
+ List<CompanyAiWorkflowExecLog> companyAiWorkflowExecLogs = companyAiWorkflowExecLogMapper.selectCompanyAiWorkflowExecLogList(queryP);
|
|
|
+ companyAiWorkflowExecLogs.forEach(log -> {
|
|
|
+ log.setStatus(ExecutionStatusEnum.SUCCESS.getValue());
|
|
|
+ companyAiWorkflowExecLogMapper.updateById(log);
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
+ String workflowInstanceId = waitingExec.getWorkflowInstanceId();
|
|
|
+ String currentNodeKey = waitingExec.getCurrentNodeKey();
|
|
|
+
|
|
|
+ log.info("加微成功回调,尝试触发工作流继续执行 - workflowInstanceId: {}, nodeKey: {}, wxClientId: {}",
|
|
|
+ workflowInstanceId, currentNodeKey, wxClientId);
|
|
|
+
|
|
|
+ // 互斥检查:如果已经被执行过(超时路径或其他回调),则不再执行
|
|
|
+ if (!AiQwAddWxTaskNode.tryMarkAsExecuted(workflowInstanceId, wxClientId)) {
|
|
|
+ log.info("企微申请加微结果查询任务工作流已被其他路径执行,跳过 - workflowInstanceId: {}, wxClientId: {}",
|
|
|
+ workflowInstanceId, wxClientId);
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
- Integer addWxTime = robotic.getAddWxTime();
|
|
|
- if (addWxTime != null) {
|
|
|
- long endTime = System.currentTimeMillis() + addWxTime * 60 * 1000;
|
|
|
- String key = Constants.CID_NEXT_TASK_ID + callee.getRoboticId() + ":" + callee.getId();
|
|
|
- redisCache.setCacheObject(key, String.valueOf(endTime));
|
|
|
- } else {
|
|
|
- log.error("ROBOTIC-ID:{},CLIENT-ID:{},没有设置加微后置等待时间", client.getRoboticId(), client.getId());
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 更新任务流程状态
|
|
|
- */
|
|
|
- private void updateTaskFlows(List<CompanyVoiceRoboticCallees> calleesList, Set<Long> roboticIdSet) {
|
|
|
- calleesList.forEach(callee ->
|
|
|
- callee.setRunTaskFlow(
|
|
|
- StringUtils.isBlank(callee.getRunTaskFlow()) ?
|
|
|
- Constants.QW_ADD_WX : callee.getRunTaskFlow() + "," + Constants.QW_ADD_WX
|
|
|
- )
|
|
|
- );
|
|
|
-
|
|
|
- companyVoiceRoboticCalleesServiceImpl.updateBatchById(calleesList);
|
|
|
- companyVoiceRoboticServiceImpl.finishAddWxByCallees(roboticIdSet);
|
|
|
+
|
|
|
+ // 清除超时检测Key(回调成功了,不需要超时检测了)
|
|
|
+ AiQwAddWxTaskNode.clearTimeoutKey(workflowInstanceId, wxClientId);
|
|
|
+
|
|
|
+ // 触发工作流继续执行
|
|
|
+ Map<String, Object> inputData = new HashMap<>();
|
|
|
+ inputData.put("addWxSuccess", true);
|
|
|
+ inputData.put("wxClientId", wxClientId);
|
|
|
+ inputData.put("triggerType", "callback"); // 回调触发
|
|
|
+
|
|
|
+ companyWorkflowEngine.resumeFromBlockingNode(workflowInstanceId, currentNodeKey, inputData);
|
|
|
+
|
|
|
+ log.info("企微申请加微结果查询任务加微成功回调触发工作流继续执行完成 - workflowInstanceId: {}, wxClientId: {}",
|
|
|
+ workflowInstanceId, wxClientId);
|
|
|
+
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.error("企微申请加微结果查询任务加微成功回调触发工作流异常 - wxClientId: {}", wxClientId, ex);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|