|
|
@@ -6,6 +6,7 @@ import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.fs.common.constant.Constants;
|
|
|
+import com.fs.common.core.domain.R;
|
|
|
import com.fs.common.core.redis.RedisCache;
|
|
|
import com.fs.common.core.redis.RedisCacheT;
|
|
|
import com.fs.common.utils.PubFun;
|
|
|
@@ -20,6 +21,7 @@ import com.fs.company.service.impl.call.node.AiQwAddWxTaskNode;
|
|
|
import com.fs.company.service.impl.call.node.WorkflowNodeFactory;
|
|
|
import com.fs.company.vo.CompanyWxClient4WorkFlowVO;
|
|
|
import com.fs.course.config.RedisKeyScanner;
|
|
|
+import com.fs.crm.param.SmsSendBatchParam;
|
|
|
import com.fs.enums.ExecutionStatusEnum;
|
|
|
import com.fs.enums.NodeTypeEnum;
|
|
|
import com.fs.company.util.ObjectPlaceholderResolver;
|
|
|
@@ -31,7 +33,11 @@ import com.fs.qw.domain.QwExternalContact;
|
|
|
import com.fs.qw.domain.QwUser;
|
|
|
import com.fs.qw.mapper.QwExternalContactMapper;
|
|
|
import com.fs.qw.mapper.QwUserMapper;
|
|
|
+import com.fs.qwApi.domain.QwLinkCreateResult;
|
|
|
+import com.fs.qwApi.param.QwLinkCreateParam;
|
|
|
+import com.fs.qwApi.service.QwApiService;
|
|
|
import com.fs.system.service.ISysConfigService;
|
|
|
+import com.fs.voice.utils.StringUtil;
|
|
|
import com.fs.wxcid.dto.friend.AddContactParam;
|
|
|
import com.fs.wxcid.service.FriendService;
|
|
|
import com.fs.wxcid.vo.AddContactVo;
|
|
|
@@ -40,6 +46,8 @@ import com.fs.wxwork.dto.WxSearchContactDTO;
|
|
|
import com.fs.wxwork.dto.WxSearchContactResp;
|
|
|
import com.fs.wxwork.dto.WxWorkResponseDTO;
|
|
|
import com.fs.wxwork.service.WxWorkService;
|
|
|
+import com.google.gson.JsonObject;
|
|
|
+import com.google.gson.JsonParser;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
@@ -90,6 +98,11 @@ public class WxTaskService {
|
|
|
private final CompanyWorkflowEngine companyWorkflowEngine;
|
|
|
private final CompanyVoiceRoboticBusinessMapper companyVoiceRoboticBusinessMapper;
|
|
|
private final WorkflowNodeFactory workflowNodeFactory;
|
|
|
+ private final CompanySmsTempServiceImpl smsTempService;
|
|
|
+ private final ICompanySmsService companySmsService;
|
|
|
+ private final ICompanyUserService companyUserService;
|
|
|
+ private final CompanyVoiceRoboticCallLogSendmsgServiceImpl companyVoiceRoboticCallLogSendmsgService;
|
|
|
+ private final QwApiService qwApiService;
|
|
|
private final RedisCache redisCache2;
|
|
|
private final ExecutorService cidExcutor = new ThreadPoolExecutor(
|
|
|
32,
|
|
|
@@ -171,7 +184,7 @@ public class WxTaskService {
|
|
|
runParam.put("txt", newTxt);
|
|
|
runParam.put("clientId", client.getId());
|
|
|
CompanyVoiceRoboticCallLogAddwx addLog = CompanyVoiceRoboticCallLogAddwx.initCallLog(
|
|
|
- runParam.toJSONString(), client.getId(), client.getRoboticId(), e.getId(), e.getCompanyId());
|
|
|
+ runParam.toJSONString(), client.getId(), client.getRoboticId(), e.getId(), e.getCompanyId(),0);
|
|
|
log.info("ROBOTIC-ID:{},CLIENT-ID:{},执行加微:{},客户:{}-{},使用话术:{}", client.getRoboticId(), client.getId(), e.getId(), client.getCustomerId(), crmCustomer.getCustomerName(), dialog.getName());
|
|
|
if (vo.isSuccess()) {
|
|
|
e.setLastAddWxTime(LocalDateTime.now());
|
|
|
@@ -293,7 +306,7 @@ public class WxTaskService {
|
|
|
runParam.put("txt", newTxt);
|
|
|
runParam.put("clientId", client.getId());
|
|
|
CompanyVoiceRoboticCallLogAddwx addLog = CompanyVoiceRoboticCallLogAddwx.initCallLog(
|
|
|
- runParam.toJSONString(), client.getId(), client.getRoboticId(), e.getId(), e.getCompanyId());
|
|
|
+ runParam.toJSONString(), client.getId(), client.getRoboticId(), e.getId(), e.getCompanyId(),0);
|
|
|
log.info("ROBOTIC-ID:{},CLIENT-ID:{},执行加微:{},客户:{}-{},使用话术:{}", client.getRoboticId(), client.getId(), e.getId(), client.getCustomerId(), crmCustomer.getCustomerName(), dialog.getName());
|
|
|
if (vo.isSuccess()) {
|
|
|
e.setLastAddWxTime(LocalDateTime.now());
|
|
|
@@ -860,75 +873,681 @@ public class WxTaskService {
|
|
|
*
|
|
|
* @param accountIdList 企微成员id
|
|
|
*/
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 企微加微方式:主动申请
|
|
|
+ */
|
|
|
+ private static final int QW_ADD_WX_TYPE_APPLY = 1;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 企微加微方式:短信获客链接
|
|
|
+ */
|
|
|
+ private static final int QW_ADD_WX_TYPE_SMS_LINK = 2;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 默认加微方式 ID
|
|
|
+ */
|
|
|
+ private static final int DEFAULT_QW_WX_ADD_WAY_ID = 1;
|
|
|
+
|
|
|
public void qwAddWx(List<Long> accountIdList) {
|
|
|
log.info("==========执行申请企微加好友任务开始==========");
|
|
|
try {
|
|
|
- // 需要添加微信的列表
|
|
|
+ // 获取需要添加微信的列表
|
|
|
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);
|
|
|
- // 获取实际企微用户信息
|
|
|
+ if (list.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建客户映射
|
|
|
+ Map<Long, CompanyWxClient4WorkFlowVO> clientMap = PubFun.listToMapByGroupObject(
|
|
|
+ list, CompanyWxClient4WorkFlowVO::getAccountId);
|
|
|
+
|
|
|
+ // 获取有效的企微用户列表
|
|
|
List<QwUser> addAccountList = qwUserMapper.selectBatchIds(clientMap.keySet()).stream()
|
|
|
.filter(this::isValidQwUser)
|
|
|
.collect(Collectors.toList());
|
|
|
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(),qwUser.getVid(),qwUser.getQwUserName());
|
|
|
- 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);
|
|
|
- }
|
|
|
- }
|
|
|
+ if (addAccountList.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理每个企微用户的加微请求
|
|
|
+ List<CompanyWxClient> updateList = processQwAddWxRequests(addAccountList, clientMap);
|
|
|
+
|
|
|
+ // 批量更新并触发后续流程
|
|
|
+ if (!updateList.isEmpty()) {
|
|
|
+ companyWxClientService.updateBatchById(updateList);
|
|
|
+ triggerNextWorkflowSteps(updateList, clientMap);
|
|
|
}
|
|
|
+
|
|
|
} catch (Exception e) {
|
|
|
log.error("企微申请加好友任务执行异常", e);
|
|
|
}
|
|
|
log.info("==========执行企微申请加好友任务结束==========");
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 处理所有企微用户的加微请求
|
|
|
+ */
|
|
|
+ private List<CompanyWxClient> processQwAddWxRequests(
|
|
|
+ List<QwUser> addAccountList,
|
|
|
+ Map<Long, CompanyWxClient4WorkFlowVO> clientMap) {
|
|
|
+
|
|
|
+ List<CompanyWxClient> updateList = new ArrayList<>();
|
|
|
+
|
|
|
+ for (QwUser qwUser : addAccountList) {
|
|
|
+ CompanyWxClient4WorkFlowVO client = clientMap.get(qwUser.getId());
|
|
|
+ if (client == null) {
|
|
|
+ log.error("企微申请加好友任务当前账号暂无需要添加微信:{}-{}",
|
|
|
+ qwUser.getId(), qwUser.getQwUserName());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 解析配置参数
|
|
|
+ NodeConfig config = parseNodeConfig(client.getNodeConfig());
|
|
|
+
|
|
|
+ // 根据加微方式处理
|
|
|
+ CompanyWxClient result = handleQwAddWxByType(qwUser, client, config);
|
|
|
+ if (result != null) {
|
|
|
+ updateList.add(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("处理企微加微请求失败 - accountId: {}, qwUserId: {}",
|
|
|
+ client.getAccountId(), qwUser.getId(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return updateList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解析节点配置
|
|
|
+ */
|
|
|
+ private NodeConfig parseNodeConfig(String nodeConfigStr) {
|
|
|
+ NodeConfig config = new NodeConfig();
|
|
|
+ config.qwWxAddWayId = DEFAULT_QW_WX_ADD_WAY_ID;
|
|
|
+ config.smsTempId = DEFAULT_QW_WX_ADD_WAY_ID;
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(nodeConfigStr)) {
|
|
|
+ return config;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ JsonObject configJson = JsonParser.parseString(nodeConfigStr).getAsJsonObject();
|
|
|
+ if (configJson.has("qwWxAddWayId") && !configJson.get("qwWxAddWayId").isJsonNull()) {
|
|
|
+ config.qwWxAddWayId = configJson.get("qwWxAddWayId").getAsInt();
|
|
|
+ }
|
|
|
+ if (configJson.has("smsTempId") && !configJson.get("smsTempId").isJsonNull()) {
|
|
|
+ config.smsTempId = configJson.get("smsTempId").getAsInt();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("解析配置出错,使用默认值:{}", e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return config;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据加微方式类型处理
|
|
|
+ */
|
|
|
+ private CompanyWxClient handleQwAddWxByType(
|
|
|
+ QwUser qwUser,
|
|
|
+ CompanyWxClient4WorkFlowVO client,
|
|
|
+ NodeConfig config) {
|
|
|
+
|
|
|
+ switch (config.qwWxAddWayId) {
|
|
|
+ case QW_ADD_WX_TYPE_APPLY:
|
|
|
+ return handleApplyAddWx(qwUser, client, config);
|
|
|
+ case QW_ADD_WX_TYPE_SMS_LINK:
|
|
|
+ return handleSmsLinkAddWx(qwUser, client, config);
|
|
|
+ default:
|
|
|
+ log.warn("未知的加微方式:{}", config.qwWxAddWayId);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理主动申请加微
|
|
|
+ */
|
|
|
+ private CompanyWxClient handleApplyAddWx(
|
|
|
+ QwUser qwUser,
|
|
|
+ CompanyWxClient4WorkFlowVO client,
|
|
|
+ NodeConfig config) {
|
|
|
+
|
|
|
+ CrmCustomer crmCustomer = crmCustomerService.selectCrmCustomerById(client.getCustomerId());
|
|
|
+
|
|
|
+ // 发起加微申请
|
|
|
+ WxWorkResponseDTO<String> resp = qwAddWxInvokeIpad(
|
|
|
+ crmCustomer.getMobile(),
|
|
|
+ qwUser.getUid(),
|
|
|
+ qwUser.getServerId(),
|
|
|
+ qwUser.getVid(),
|
|
|
+ qwUser.getQwUserName()
|
|
|
+ );
|
|
|
+
|
|
|
+ // 准备日志参数
|
|
|
+ JSONObject runParam = buildAddWxRunParam(qwUser, client, crmCustomer.getMobile());
|
|
|
+ CompanyVoiceRoboticCallLogAddwx addLog = CompanyVoiceRoboticCallLogAddwx.initCallLog(
|
|
|
+ runParam.toJSONString(),
|
|
|
+ client.getId(),
|
|
|
+ client.getRoboticId(),
|
|
|
+ qwUser.getId(),
|
|
|
+ qwUser.getCompanyId(),
|
|
|
+ config.qwWxAddWayId
|
|
|
+ );
|
|
|
+
|
|
|
+ // 处理结果
|
|
|
+ if (resp != null && resp.getErrcode() == 0) {
|
|
|
+ // 加微成功
|
|
|
+ client.setIsAdd(2);
|
|
|
+ client.setAddTime(LocalDateTime.now());
|
|
|
+
|
|
|
+ CompanyWxClient addItem = new CompanyWxClient();
|
|
|
+ BeanUtils.copyProperties(client, addItem);
|
|
|
+
|
|
|
+ addLog.setStatus(2);
|
|
|
+ addLog.setResult(JSON.toJSONString(resp));
|
|
|
+ addLog.setIsWeCom(2);
|
|
|
+ log.info("ROBOTIC-ID:{},企微申请加好友任务申请成功", client.getRoboticId());
|
|
|
+
|
|
|
+ asyncSaveCompanyVoiceRoboticCallLog(addLog);
|
|
|
+ return addItem;
|
|
|
+ } else {
|
|
|
+ // 加微失败
|
|
|
+ client.setIsAdd(3);
|
|
|
+ client.setAddTime(LocalDateTime.now());
|
|
|
+
|
|
|
+
|
|
|
+ CompanyWxClient addItem = new CompanyWxClient();
|
|
|
+ BeanUtils.copyProperties(client, addItem);
|
|
|
+
|
|
|
+
|
|
|
+ addLog.setStatus(3);
|
|
|
+ addLog.setResult(JSON.toJSONString(runParam));
|
|
|
+ log.error("ROBOTIC-ID:{},企微申请加好友任务加微失败:{}",
|
|
|
+ client.getRoboticId(), runParam);
|
|
|
+
|
|
|
+ asyncSaveCompanyVoiceRoboticCallLog(addLog);
|
|
|
+ return addItem;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理短信获客链接加微
|
|
|
+ */
|
|
|
+ private CompanyWxClient handleSmsLinkAddWx(
|
|
|
+ QwUser qwUser,
|
|
|
+ CompanyWxClient4WorkFlowVO client,
|
|
|
+ NodeConfig config) {
|
|
|
+
|
|
|
+ // 查询短信模板
|
|
|
+ CompanySmsTemp temp = smsTempService.selectCompanySmsTempById((long) config.smsTempId);
|
|
|
+ if (temp == null || !temp.getStatus().equals(1) || !temp.getIsAudit().equals(1)) {
|
|
|
+ log.error("短信模板无效或未审核:{}", temp);
|
|
|
+ throw new RuntimeException("短信模板无效或未审核");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询公司短信信息
|
|
|
+ CompanySms sms = companySmsService.selectCompanySmsByCompanyId(qwUser.getCompanyId());
|
|
|
+ if (sms == null) {
|
|
|
+ log.error("公司短信信息不存在:companyId: {}", qwUser.getCompanyId());
|
|
|
+ throw new RuntimeException("公司短信信息不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (sms.getRemainSmsCount() <= 0) {
|
|
|
+ log.error("剩余短信数量不足:companyId: {}", qwUser.getCompanyId());
|
|
|
+ throw new RuntimeException("剩余短信数量不足,请充值");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ String linkUrl = getLinkUrl(qwUser);
|
|
|
+
|
|
|
+ if (StringUtil.strIsNullOrEmpty(linkUrl)) {
|
|
|
+ return handleLinkGenerationFailure(client, qwUser, config);
|
|
|
+ }
|
|
|
+
|
|
|
+ return handleSmsSendAndAddWx(qwUser, client, temp, linkUrl, config);
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 处理链接生成失败
|
|
|
+ */
|
|
|
+ private CompanyWxClient handleLinkGenerationFailure(
|
|
|
+ CompanyWxClient4WorkFlowVO client,
|
|
|
+ QwUser qwUser,
|
|
|
+ NodeConfig config) {
|
|
|
+
|
|
|
+ client.setIsAdd(3);
|
|
|
+ client.setAddTime(LocalDateTime.now());
|
|
|
+
|
|
|
+ CompanyWxClient addItem = new CompanyWxClient();
|
|
|
+ BeanUtils.copyProperties(client, addItem);
|
|
|
+
|
|
|
+ CompanyVoiceRoboticCallLogAddwx addLogAddWx = CompanyVoiceRoboticCallLogAddwx.initCallLog(
|
|
|
+ "获客链接生成失败",
|
|
|
+ client.getId(),
|
|
|
+ client.getRoboticId(),
|
|
|
+ qwUser.getId(),
|
|
|
+ qwUser.getCompanyId(),
|
|
|
+ config.qwWxAddWayId
|
|
|
+ );
|
|
|
+
|
|
|
+ addLogAddWx.setStatus(3);
|
|
|
+ addLogAddWx.setResult(JSON.toJSONString(R.ok()));
|
|
|
+ addLogAddWx.setIsWeCom(2);
|
|
|
+
|
|
|
+ asyncSaveCompanyVoiceRoboticCallLog(addLogAddWx);
|
|
|
+ return addItem;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理短信发送和加微
|
|
|
+ */
|
|
|
+ private CompanyWxClient handleSmsSendAndAddWx(
|
|
|
+ QwUser qwUser,
|
|
|
+ CompanyWxClient4WorkFlowVO client,
|
|
|
+ CompanySmsTemp temp,
|
|
|
+ String linkUrl,
|
|
|
+ NodeConfig config) {
|
|
|
+
|
|
|
+ SmsSendBatchParam smsSendBatchParam = buildSmsSendParam(qwUser, client, temp, linkUrl);
|
|
|
+ JSONObject runParamSms = (JSONObject) JSON.toJSON(smsSendBatchParam);
|
|
|
+ runParamSms.put("temp", temp);
|
|
|
+
|
|
|
+ //发送短信并记录日志
|
|
|
+ sendSmsWithLog(smsSendBatchParam, runParamSms, client, qwUser, temp);
|
|
|
+
|
|
|
+ //保存加微日志
|
|
|
+ saveAddWxLog(runParamSms, client, qwUser, config);
|
|
|
+
|
|
|
+ client.setIsAdd(2);
|
|
|
+ client.setAddTime(LocalDateTime.now());
|
|
|
+
|
|
|
+
|
|
|
+ CompanyWxClient addItem = new CompanyWxClient();
|
|
|
+ BeanUtils.copyProperties(client, addItem);
|
|
|
+
|
|
|
+ return addItem;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送短信并记录日志
|
|
|
+ */
|
|
|
+ private void sendSmsWithLog(
|
|
|
+ SmsSendBatchParam smsSendBatchParam,
|
|
|
+ JSONObject runParamSms,
|
|
|
+ CompanyWxClient4WorkFlowVO client,
|
|
|
+ QwUser qwUser,
|
|
|
+ CompanySmsTemp temp) {
|
|
|
+
|
|
|
+ CompanyVoiceRoboticCallLogSendmsg addLogSms = CompanyVoiceRoboticCallLogSendmsg.initCallLog(
|
|
|
+ runParamSms.toJSONString(),
|
|
|
+ client.getCalleeId(),
|
|
|
+ client.getRoboticId(),
|
|
|
+ qwUser.getCompanyId(),
|
|
|
+ qwUser.getCompanyUserId(),
|
|
|
+ temp.getTempId()
|
|
|
+ );
|
|
|
+
|
|
|
+ try {
|
|
|
+ String callbackUuid = UUID.randomUUID().toString();
|
|
|
+ int smsContentLen = getSmsContentLen(smsSendBatchParam);
|
|
|
+
|
|
|
+ addLogSms.setContentLen(smsContentLen);
|
|
|
+ companyVoiceRoboticServiceImpl.sendMsgBatch(temp, smsSendBatchParam);
|
|
|
+
|
|
|
+ addLogSms.setStatus(2);
|
|
|
+ addLogSms.setCallbackUuid(callbackUuid);
|
|
|
+ } catch (Exception ex) {
|
|
|
+ addLogSms.setStatus(3);
|
|
|
+ addLogSms.setResult(ex.getMessage());
|
|
|
+ log.error("sendMsgBatch 异常:", ex);
|
|
|
+ } finally {
|
|
|
+ companyVoiceRoboticCallLogSendmsgService.asyncInsertCompanyVoiceRoboticCallLog(addLogSms);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存加微日志
|
|
|
+ */
|
|
|
+ private void saveAddWxLog(
|
|
|
+ JSONObject runParamSms,
|
|
|
+ CompanyWxClient4WorkFlowVO client,
|
|
|
+ QwUser qwUser,
|
|
|
+ NodeConfig config) {
|
|
|
+
|
|
|
+ CompanyVoiceRoboticCallLogAddwx addLogAddWx = CompanyVoiceRoboticCallLogAddwx.initCallLog(
|
|
|
+ runParamSms.toJSONString(),
|
|
|
+ client.getId(),
|
|
|
+ client.getRoboticId(),
|
|
|
+ qwUser.getId(),
|
|
|
+ qwUser.getCompanyId(),
|
|
|
+ config.qwWxAddWayId
|
|
|
+ );
|
|
|
+
|
|
|
+ addLogAddWx.setStatus(1);
|
|
|
+ addLogAddWx.setResult(JSON.toJSONString(R.ok()));
|
|
|
+ addLogAddWx.setIsWeCom(2);
|
|
|
+
|
|
|
+ asyncSaveCompanyVoiceRoboticCallLog(addLogAddWx);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取获客链接
|
|
|
+ */
|
|
|
+ private String getLinkUrl(QwUser qwUser){
|
|
|
+
|
|
|
+ String link = redisCache2.getCacheObject("customerLink:"+qwUser.getId());
|
|
|
+ if (link!=null && !StringUtil.strIsNullOrEmpty(link)){
|
|
|
+ return link;
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取获客链接
|
|
|
+ QwLinkCreateParam createParam=new QwLinkCreateParam();
|
|
|
+ createParam.setLink_name(qwUser.getQwUserName()+"的获客链接");
|
|
|
+
|
|
|
+ QwLinkCreateParam.Range range=new QwLinkCreateParam.Range();
|
|
|
+ range.setUser_list(Collections.singletonList(qwUser.getQwUserId()));
|
|
|
+ createParam.setRange(range);
|
|
|
+
|
|
|
+ QwLinkCreateResult result = qwApiService.linkCreate(createParam, qwUser.getCorpId());
|
|
|
+
|
|
|
+ if (result.getErrcode()==0){
|
|
|
+
|
|
|
+ redisCache2.setCacheObject("customerLink:"+qwUser.getId(),result.getUrl());
|
|
|
+
|
|
|
+ return result.getUrl();
|
|
|
+ }else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建加微请求参数
|
|
|
+ */
|
|
|
+ private JSONObject buildAddWxRunParam(QwUser qwUser, CompanyWxClient4WorkFlowVO client, String mobile) {
|
|
|
+ JSONObject runParam = new JSONObject();
|
|
|
+ runParam.put("qwId", qwUser.getId());
|
|
|
+ runParam.put("mobile", mobile);
|
|
|
+ runParam.put("qwUid", qwUser.getUid());
|
|
|
+ runParam.put("clientId", client.getId());
|
|
|
+ return runParam;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建短信发送参数
|
|
|
+ */
|
|
|
+ private SmsSendBatchParam buildSmsSendParam(
|
|
|
+ QwUser qwUser,
|
|
|
+ CompanyWxClient4WorkFlowVO client,
|
|
|
+ CompanySmsTemp temp,
|
|
|
+ String cardUrl) {
|
|
|
+
|
|
|
+ SmsSendBatchParam param = new SmsSendBatchParam();
|
|
|
+ param.setCompanyId(qwUser.getCompanyId());
|
|
|
+ param.setCompanyUserId(qwUser.getCompanyUserId());
|
|
|
+ param.setSmsType(temp.getTempType());
|
|
|
+ param.setTempCode(temp.getTempCode());
|
|
|
+ param.setContent(temp.getContent());
|
|
|
+ param.setSenderName(qwUser.getQwUserName());
|
|
|
+ param.setCustomerIds(new Long[]{client.getCustomerId()});
|
|
|
+ if (!StringUtil.strIsNullOrEmpty(client.getTraceId())){
|
|
|
+ param.setCardUrl(cardUrl+"?customer_channel="+client.getTraceId());
|
|
|
+ }else {
|
|
|
+ param.setCardUrl(cardUrl);
|
|
|
+ }
|
|
|
+
|
|
|
+ return param;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 触发后续工作流步骤
|
|
|
+ */
|
|
|
+ private void triggerNextWorkflowSteps(
|
|
|
+ List<CompanyWxClient> updateList,
|
|
|
+ Map<Long, CompanyWxClient4WorkFlowVO> clientMap) {
|
|
|
+
|
|
|
+ for (CompanyWxClient client : updateList) {
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 节点配置内部类
|
|
|
+ */
|
|
|
+ private static class NodeConfig {
|
|
|
+ private int qwWxAddWayId;
|
|
|
+ private int smsTempId;
|
|
|
+ }
|
|
|
+
|
|
|
+// ... existing code ...
|
|
|
+
|
|
|
+// public void qwAddWx(List<Long> accountIdList) {
|
|
|
+// log.info("==========执行申请企微加好友任务开始==========");
|
|
|
+// try {
|
|
|
+// // 需要添加微信的列表
|
|
|
+// 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> addAccountList = qwUserMapper.selectBatchIds(clientMap.keySet()).stream()
|
|
|
+// .filter(this::isValidQwUser)
|
|
|
+// .collect(Collectors.toList());
|
|
|
+// log.info("企微申请加好友任务需要企微的账号数量:{}", addAccountList.size());
|
|
|
+// addAccountList.forEach(qwUser -> {
|
|
|
+// CompanyWxClient4WorkFlowVO client = clientMap.get(qwUser.getId());
|
|
|
+// if (client != null) {
|
|
|
+// CrmCustomer crmCustomer = crmCustomerService.selectCrmCustomerById(client.getCustomerId());
|
|
|
+//
|
|
|
+// int qwWxAddWayId = 1; // 先固定为1
|
|
|
+// int smsTempId= 1; // 先固定为1
|
|
|
+// try {
|
|
|
+// String nodeConfig = client.getNodeConfig();
|
|
|
+// if (nodeConfig != null && !nodeConfig.trim().isEmpty()) {
|
|
|
+// JsonObject configJson = JsonParser.parseString(nodeConfig).getAsJsonObject();
|
|
|
+// if (configJson.has("qwWxAddWayId") && !configJson.get("qwWxAddWayId").isJsonNull()) {
|
|
|
+// qwWxAddWayId = configJson.get("qwWxAddWayId").getAsInt();
|
|
|
+// }
|
|
|
+// smsTempId = configJson.get("smsTempId").getAsInt();
|
|
|
+// }
|
|
|
+// } catch (Exception e) {
|
|
|
+// // 报错了也不处理,继续使用1
|
|
|
+// log.error("解析配置出错,但保持加微使用默认值1: " + e.getMessage());
|
|
|
+// }
|
|
|
+//
|
|
|
+// switch (qwWxAddWayId){
|
|
|
+// case 1:
|
|
|
+// // 开始主动申请加微
|
|
|
+// WxWorkResponseDTO<String> resp = qwAddWxInvokeIpad(crmCustomer.getMobile(), qwUser.getUid(),qwUser.getServerId(),qwUser.getVid(),qwUser.getQwUserName());
|
|
|
+// 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(),qwWxAddWayId);
|
|
|
+// 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));
|
|
|
+//
|
|
|
+// client.setIsAdd(3);
|
|
|
+// client.setAddTime(LocalDateTime.now());
|
|
|
+// CompanyWxClient addItem = new CompanyWxClient();
|
|
|
+// BeanUtils.copyProperties(client, addItem);
|
|
|
+// addList.add(addItem);
|
|
|
+// }
|
|
|
+// asyncSaveCompanyVoiceRoboticCallLog(addLog);
|
|
|
+// break;
|
|
|
+// case 2:
|
|
|
+// //短信-获客链接
|
|
|
+//
|
|
|
+// CompanySmsTemp temp = smsTempService.selectCompanySmsTempById((long) smsTempId);
|
|
|
+//
|
|
|
+// if (temp != null && temp.getStatus().equals(1) && temp.getIsAudit().equals(1)) {
|
|
|
+//
|
|
|
+// CompanySms sms=companySmsService.selectCompanySmsByCompanyId(qwUser.getCompanyId());
|
|
|
+// if(sms!=null){
|
|
|
+// if(sms.getRemainSmsCount()>0){
|
|
|
+// SmsSendBatchParam smsSendBatchParam = new SmsSendBatchParam();
|
|
|
+// smsSendBatchParam.setCompanyId(qwUser.getCompanyId());
|
|
|
+// smsSendBatchParam.setCompanyUserId(qwUser.getCompanyUserId());
|
|
|
+// smsSendBatchParam.setSmsType(temp.getTempType());
|
|
|
+// smsSendBatchParam.setTempCode(temp.getTempCode());
|
|
|
+// smsSendBatchParam.setContent(temp.getContent());
|
|
|
+// smsSendBatchParam.setSenderName(client.getWxNickName());
|
|
|
+// smsSendBatchParam.setCustomerIds(new Long[]{client.getCustomerId()});
|
|
|
+// //记录工作流级短信日志
|
|
|
+// JSONObject runParamSms = (JSONObject) JSON.toJSON(smsSendBatchParam);
|
|
|
+// runParamSms.put("temp", temp);
|
|
|
+// CompanyVoiceRoboticCallLogSendmsg addLogSms = CompanyVoiceRoboticCallLogSendmsg.initCallLog(
|
|
|
+// runParamSms.toJSONString(),
|
|
|
+// null,
|
|
|
+// null,
|
|
|
+// qwUser.getCompanyId(),
|
|
|
+// qwUser.getCompanyUserId(),
|
|
|
+// temp.getTempId()
|
|
|
+// );
|
|
|
+//
|
|
|
+// addLogSms.setStatus(1);
|
|
|
+// try{
|
|
|
+// String callbackUuid = UUID.randomUUID().toString();
|
|
|
+// int smsContentLen = getSmsContentLen(smsSendBatchParam);
|
|
|
+// addLogSms.setContentLen(smsContentLen);
|
|
|
+// companyVoiceRoboticServiceImpl.sendMsgBatch(temp,smsSendBatchParam);
|
|
|
+// addLogSms.setStatus(2);
|
|
|
+// addLogSms.setCallbackUuid(callbackUuid);
|
|
|
+// } catch(Exception ex){
|
|
|
+// addLogSms.setStatus(3);
|
|
|
+// addLogSms.setResult(ex.getMessage());
|
|
|
+// log.error("sendMsgOne异常:",ex);
|
|
|
+// } finally {
|
|
|
+// //短信记录表
|
|
|
+// companyVoiceRoboticCallLogSendmsgService.asyncInsertCompanyVoiceRoboticCallLog(addLogSms);
|
|
|
+// }
|
|
|
+//
|
|
|
+// CompanyVoiceRoboticCallLogAddwx addLogAddWx = CompanyVoiceRoboticCallLogAddwx.initCallLog(
|
|
|
+// runParamSms.toJSONString(), client.getId(), client.getRoboticId(), qwUser.getId(), qwUser.getCompanyId(),qwWxAddWayId);
|
|
|
+//
|
|
|
+// // 加微中
|
|
|
+// client.setIsAdd(2);
|
|
|
+// client.setAddTime(LocalDateTime.now());
|
|
|
+// CompanyWxClient addItem = new CompanyWxClient();
|
|
|
+// BeanUtils.copyProperties(client, addItem);
|
|
|
+// addList.add(addItem);
|
|
|
+//
|
|
|
+// addLogAddWx.setStatus(1);
|
|
|
+// addLogAddWx.setResult(JSON.toJSONString(R.ok()));
|
|
|
+// addLogAddWx.setIsWeCom(2);
|
|
|
+//
|
|
|
+// asyncSaveCompanyVoiceRoboticCallLog(addLogAddWx);
|
|
|
+//
|
|
|
+// }
|
|
|
+// else{
|
|
|
+// log.error("qw剩余短信数量不足,请充值:{}",client.getCompanyId());
|
|
|
+// throw new RuntimeException("剩余短信数量不足,请充值");
|
|
|
+// }
|
|
|
+// }
|
|
|
+// else{
|
|
|
+// log.error("qw请充值:companyId:{}",client.getCompanyId());
|
|
|
+// throw new RuntimeException("请充值");
|
|
|
+// }
|
|
|
+//
|
|
|
+// } else {
|
|
|
+// log.error("模板未审核:smsTemp:{}", temp);
|
|
|
+// throw new RuntimeException("模板未审核");
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// break;
|
|
|
+// default:
|
|
|
+// break;
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// } 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.info("==========执行企微申请加好友任务结束==========");
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public int getSmsContentLen(SmsSendBatchParam param){
|
|
|
+ CompanyUser companyUser=companyUserService.selectCompanyUserById(param.getCompanyUserId());
|
|
|
+
|
|
|
+ CrmCustomer crmCustomer=crmCustomerService.selectCrmCustomerById(param.getCustomerIds()[0].longValue());
|
|
|
+ String content=param.getContent();
|
|
|
+ if(StringUtils.isNotEmpty(crmCustomer.getCustomerName())){
|
|
|
+ content=content.replace("${sms.csName}",crmCustomer.getCustomerName());
|
|
|
+ }
|
|
|
+ if(companyUser!=null&& StringUtils.isNotEmpty(companyUser.getPhonenumber())){
|
|
|
+ content=content.replace("${sms.phoneNumber}",companyUser.getPhonenumber());
|
|
|
+ }
|
|
|
+ if(StringUtils.isNotEmpty(param.getCardUrl())){
|
|
|
+ content=content.replace("${sms.cardUrl}",param.getCardUrl());
|
|
|
+ }
|
|
|
+ if(StringUtils.isNotEmpty(param.getSenderName())){
|
|
|
+ content=content.replace("${sms.senderName}",param.getSenderName());
|
|
|
+ }
|
|
|
+
|
|
|
+ return content.length();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 企微加微结果处理
|
|
|
*/
|