|
@@ -75,6 +75,7 @@ public class WxTaskService {
|
|
|
private final CompanyVoiceRoboticCallLogCallphoneServiceImpl companyVoiceRoboticCallLogCallphoneService;
|
|
private final CompanyVoiceRoboticCallLogCallphoneServiceImpl companyVoiceRoboticCallLogCallphoneService;
|
|
|
private final CompanyAiWorkflowExecMapper companyAiWorkflowExecMapper;
|
|
private final CompanyAiWorkflowExecMapper companyAiWorkflowExecMapper;
|
|
|
private final CompanyWorkflowEngine companyWorkflowEngine;
|
|
private final CompanyWorkflowEngine companyWorkflowEngine;
|
|
|
|
|
+ private final CompanyVoiceRoboticBusinessMapper companyVoiceRoboticBusinessMapper;
|
|
|
private final ExecutorService cidExcutor = new ThreadPoolExecutor(
|
|
private final ExecutorService cidExcutor = new ThreadPoolExecutor(
|
|
|
32,
|
|
32,
|
|
|
64,
|
|
64,
|
|
@@ -222,6 +223,126 @@ public class WxTaskService {
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public void addWx4Workflow(List<Long> accountIdList) {
|
|
|
|
|
+ log.info("==========执行加微信任务开始==========");
|
|
|
|
|
+ String json = sysConfigService.selectConfigByKey("wx.config");
|
|
|
|
|
+ WxConfig config = JSONUtil.toBean(json, WxConfig.class);
|
|
|
|
|
+ // 需要添加微信的列表
|
|
|
|
|
+ List<CompanyWxClient> list = companyWxClientService.getAddWxList4Workflow(accountIdList);
|
|
|
|
|
+ log.info("需要添加微信的数量:{}", list.size());
|
|
|
|
|
+ if (list.isEmpty()) return;
|
|
|
|
|
+ List<CompanyWxClient> addList = new ArrayList<>();
|
|
|
|
|
+ Map<Long, CompanyWxClient> clientMap = PubFun.listToMapByGroupObject(list, CompanyWxClient::getAccountId);
|
|
|
|
|
+ List<CompanyWxAccount> accountList = new ArrayList<>(companyWxAccountService.listByIds(clientMap.keySet()));
|
|
|
|
|
+ log.info("查询加微的账号数量:{}", list.size());
|
|
|
|
|
+ List<CompanyWxAccount> addAccountList = accountList.stream().filter(e -> {
|
|
|
|
|
+ int newAddWxMinute = RandomUtil.randomInt(config.getNewAccountAddWxMin(), config.getNewAccountAddWxMax());
|
|
|
|
|
+ int addWxMinute = RandomUtil.randomInt(config.getAccountAddWxMin(), config.getAccountAddWxMax());
|
|
|
|
|
+ if (e.getLastAddWxTime() == null) {
|
|
|
|
|
+ log.info("加微时间为空,可以加微:{}", e.getWxNickName());
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ int minute = addWxMinute;
|
|
|
|
|
+ if (e.getIsNew() == 0) {
|
|
|
|
|
+ minute = newAddWxMinute;
|
|
|
|
|
+ }
|
|
|
|
|
+ long until = e.getLastAddWxTime().until(LocalDateTime.now(), ChronoUnit.MINUTES);
|
|
|
|
|
+ log.info("判断{}是否达到最低加微时间:上次加微时间:{},时间间隔:{},当前时间:{}", e.getWxNickName(), e.getLastAddWxTime(), until, LocalDateTime.now());
|
|
|
|
|
+ return until > minute;
|
|
|
|
|
+ }).collect(Collectors.toList());
|
|
|
|
|
+ log.info("实际加微的账号数量:{}", addAccountList.size());
|
|
|
|
|
+ addAccountList.forEach(e -> {
|
|
|
|
|
+ CompanyWxClient client = clientMap.get(e.getId());
|
|
|
|
|
+ if (client != null) {
|
|
|
|
|
+ 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.ADD_WX.equals(task)) {
|
|
|
|
|
+ CompanyWxDialog dialog = companyWxDialogService.getById(client.getDialogId());
|
|
|
|
|
+ CrmCustomer crmCustomer = crmCustomerService.selectCrmCustomerById(client.getCustomerId());
|
|
|
|
|
+ String newTxt = objectPlaceholderResolver.resolvePlaceholders(crmCustomer, dialog.getTemplateDetails());
|
|
|
|
|
+ AddContactVo vo = friendService.addContact(e.getId(), crmCustomer.getMobile(), newTxt, client.getId());
|
|
|
|
|
+ JSONObject runParam = new JSONObject();
|
|
|
|
|
+ runParam.put("id", e.getId());
|
|
|
|
|
+ runParam.put("mobile", crmCustomer.getMobile());
|
|
|
|
|
+ runParam.put("txt", newTxt);
|
|
|
|
|
+ runParam.put("clientId", client.getId());
|
|
|
|
|
+ CompanyVoiceRoboticCallLogAddwx addLog = CompanyVoiceRoboticCallLogAddwx.initCallLog(
|
|
|
|
|
+ runParam.toJSONString(), client.getId(), client.getRoboticId(), e.getId(), e.getCompanyId());
|
|
|
|
|
+ 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());
|
|
|
|
|
+ e.setIsAddNum(e.getIsAddNum() + 1);
|
|
|
|
|
+ client.setIsAdd(2);
|
|
|
|
|
+ client.setAddTime(LocalDateTime.now());
|
|
|
|
|
+ client.setWxV3(vo.getV3());
|
|
|
|
|
+ client.setWxV4(vo.getV4());
|
|
|
|
|
+ addList.add(client);
|
|
|
|
|
+ addLog.setStatus(2);
|
|
|
|
|
+ addLog.setResult(JSON.toJSONString(vo));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.error("ROBOTIC-ID:{},加微失败:{}", client.getRoboticId(), vo);
|
|
|
|
|
+ addLog.setStatus(3);
|
|
|
|
|
+ addLog.setResult(JSON.toJSONString(vo));
|
|
|
|
|
+ }
|
|
|
|
|
+ asyncSaveCompanyVoiceRoboticCallLog(addLog);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.error("ROBOTIC-ID:{},当前任务没有执行加微任务", client.getRoboticId());
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.error("当前账号暂无需要添加微信:{}-{}", e.getId(), e.getWxNickName());
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ if (!addList.isEmpty()) {
|
|
|
|
|
+ companyWxClientService.updateBatchById(addList);
|
|
|
|
|
+ //根据加微成功的用户,判定是否加入延时执行下一步任务
|
|
|
|
|
+ Set<Long> roboticIdSet = addList.stream().map(CompanyWxClient::getRoboticId).collect(Collectors.toSet());
|
|
|
|
|
+ Set<Long> userIdSet = addList.stream().map(CompanyWxClient::getCustomerId).collect(Collectors.toSet());
|
|
|
|
|
+
|
|
|
|
|
+ //找到任务
|
|
|
|
|
+ List<CompanyVoiceRobotic> companyVoiceRobotics = companyVoiceRoboticMapper.selectBatchIds(roboticIdSet);
|
|
|
|
|
+ Map<Long, CompanyVoiceRobotic> roboticsMp = companyVoiceRobotics.stream().collect(Collectors.toMap(CompanyVoiceRobotic::getId, Function.identity(), (existing, replacement) -> existing));
|
|
|
|
|
+ //找到callees数据
|
|
|
|
|
+ List<CompanyVoiceRoboticCallees> companyVoiceRoboticCallees = companyVoiceRoboticCalleesMapper.selectCalleesListByRoboticIdsAndUserIds(userIdSet, roboticIdSet);
|
|
|
|
|
+ Map<String, CompanyVoiceRoboticCallees> calleesMp = companyVoiceRoboticCallees.stream().collect(Collectors.toMap(e -> e.getUserId() + "-" + e.getRoboticId(), Function.identity(), (existing, replacement) -> existing));
|
|
|
|
|
+
|
|
|
|
|
+ long l = System.currentTimeMillis();
|
|
|
|
|
+
|
|
|
|
|
+ //根据加微成功
|
|
|
|
|
+ for (CompanyWxClient client : addList) {
|
|
|
|
|
+ CompanyVoiceRobotic clientRobotic = roboticsMp.getOrDefault(client.getRoboticId(), null);
|
|
|
|
|
+ if (null == clientRobotic) {
|
|
|
|
|
+ log.error("ROBOTIC-ID:{},CLIENT-ID:{},没有找到任务", client.getRoboticId(), client.getId());
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ CompanyVoiceRoboticCallees callees = calleesMp.getOrDefault(client.getCustomerId() + "-" + client.getRoboticId(), null);
|
|
|
|
|
+ if (null == callees) {
|
|
|
|
|
+ log.error("ROBOTIC-ID:{},CLIENT-ID:{},没有找到任务", client.getRoboticId(), client.getId());
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ Integer addWxTime = clientRobotic.getAddWxTime();
|
|
|
|
|
+ if (null == addWxTime) {
|
|
|
|
|
+ log.error("ROBOTIC-ID:{},CLIENT-ID:{},没有设置加微后置等待时间", client.getRoboticId(), client.getId());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ long endT = System.currentTimeMillis() + addWxTime * 60 * 1000;
|
|
|
|
|
+ StringBuilder sb = new StringBuilder(Constants.CID_NEXT_TASK_ID).append(callees.getRoboticId()).append(":").append(callees.getId());
|
|
|
|
|
+ redisCache.setCacheObject(sb.toString(), String.valueOf(endT));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ companyVoiceRoboticCallees.forEach(robotic ->
|
|
|
|
|
+ robotic.setRunTaskFlow(
|
|
|
|
|
+ StringUtils.isBlank(robotic.getRunTaskFlow()) ?
|
|
|
|
|
+ Constants.ADD_WX : robotic.getRunTaskFlow() + "," + Constants.ADD_WX
|
|
|
|
|
+ )
|
|
|
|
|
+ );
|
|
|
|
|
+ companyVoiceRoboticCalleesServiceImpl.updateBatchById(companyVoiceRoboticCallees);
|
|
|
|
|
+ companyVoiceRoboticServiceImpl.finishAddWxByCallees(roboticIdSet);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!addAccountList.isEmpty()) {
|
|
|
|
|
+ companyWxAccountService.updateBatchById(addAccountList);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public void initAccountNum() {
|
|
public void initAccountNum() {
|
|
|
LocalDateTime now = LocalDateTime.now();
|
|
LocalDateTime now = LocalDateTime.now();
|
|
|
String json = sysConfigService.selectConfigByKey("wx.config");
|
|
String json = sysConfigService.selectConfigByKey("wx.config");
|