lmx пре 3 недеља
родитељ
комит
4fb2eb806d

+ 2 - 0
fs-service/src/main/java/com/fs/company/mapper/CompanyVoiceRoboticBusinessMapper.java

@@ -84,4 +84,6 @@ public interface CompanyVoiceRoboticBusinessMapper extends BaseMapper<CompanyVoi
     CompanyVoiceRoboticBusiness selectCompanyVoiceRoboticBusinessByWorkflowInstanceId(String workflowInstanceId);
 
     CompanyVoiceRoboticCallees selectCalleesByBusinessId(@Param("businessId") Long businessId);
+
+
 }

+ 2 - 0
fs-service/src/main/java/com/fs/company/mapper/CompanyWxClientMapper.java

@@ -72,6 +72,8 @@ public interface CompanyWxClientMapper extends BaseMapper<CompanyWxClient> {
 
     List<CompanyWxClient> getAddWxList(@Param("accountIdList") List<Long> accountIdList);
 
+    List<CompanyWxClient> getAddWxList4Workflow(@Param("accountIdList") List<Long> accountIdList, @Param("execStatus") Integer execStatus, @Param("execNodeType") Integer execNodeType);
+
     CompanyWxClient selectWx(@Param("accountId") Long accountId, @Param("v3") String v3);
 
     List<CompanyWxClient> selectListByRoboticId(@Param("roboticId") Long roboticId);

+ 2 - 0
fs-service/src/main/java/com/fs/company/service/ICompanyWxClientService.java

@@ -69,4 +69,6 @@ public interface ICompanyWxClientService extends IService<CompanyWxClient> {
     void addWxTrueResult(AddWxResultVo vo);
 
     List<CompanyWxClient> getAddWxList(List<Long> accountIdList);
+
+    List<CompanyWxClient> getAddWxList4Workflow(List<Long> accountIdList);
 }

+ 7 - 0
fs-service/src/main/java/com/fs/company/service/impl/CompanyWxClientServiceImpl.java

@@ -14,6 +14,8 @@ import com.fs.company.service.ICompanyWxClientService;
 import com.fs.company.vo.AddWxResultVo;
 import com.fs.crm.domain.CrmCustomer;
 import com.fs.crm.service.impl.CrmCustomerServiceImpl;
+import com.fs.enums.ExecutionStatusEnum;
+import com.fs.enums.NodeTypeEnum;
 import com.fs.wxUser.domain.CompanyWxUser;
 import com.fs.wxUser.mapper.CompanyWxUserMapper;
 import lombok.AllArgsConstructor;
@@ -228,4 +230,9 @@ public class CompanyWxClientServiceImpl extends ServiceImpl<CompanyWxClientMappe
     public List<CompanyWxClient> getAddWxList(List<Long> accountIdList) {
         return baseMapper.getAddWxList(accountIdList);
     }
+
+    @Override
+    public  List<CompanyWxClient> getAddWxList4Workflow(List<Long> accountIdList){
+        return baseMapper.getAddWxList4Workflow(accountIdList, ExecutionStatusEnum.PAUSED.getValue(), NodeTypeEnum.AI_ADD_WX_TASK.getValue());
+    }
 }

+ 16 - 1
fs-service/src/main/java/com/fs/company/service/impl/call/node/AiAddWxTaskNode.java

@@ -16,6 +16,7 @@ import com.fs.enums.ExecutionStatusEnum;
 import com.fs.enums.NodeTypeEnum;
 import lombok.extern.slf4j.Slf4j;
 
+import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -32,7 +33,7 @@ public class AiAddWxTaskNode extends AbstractWorkflowNode {
     private static final CompanyWorkflowNodeMapper companyWorkflowNodeMapper = SpringUtils.getBean(CompanyWorkflowNodeMapper.class);
     @SuppressWarnings("unchecked")
     private static final RedisCacheT<String> redisCache = SpringUtils.getBean(RedisCacheT.class);
-
+    public static final String DELAY_ADD_WX_KEY = "addWxTask:delay:%s:%s:";
     /**
      * 默认加微超时时间(分钟)
      */
@@ -250,4 +251,18 @@ public class AiAddWxTaskNode extends AbstractWorkflowNode {
         redisCache.deleteObject(timeoutKey);
         log.info("清除加微超时检测 Key: {}", timeoutKey);
     }
+
+    /**
+     * getRedisCacheKey
+     *
+     * @param time
+     * @return
+     */
+    public static String getDelayAddWxKeyPrefix(Long time) {
+        Date nowDay = new Date();
+        if (null != time) {
+            nowDay = new Date(time);
+        }
+        return String.format(DELAY_ADD_WX_KEY, nowDay.getHours(), nowDay.getMinutes());
+    }
 }

+ 1 - 1
fs-service/src/main/java/com/fs/company/service/impl/call/node/AiSendMsgTaskNode.java

@@ -72,7 +72,7 @@ public class AiSendMsgTaskNode extends AbstractWorkflowNode {
                     business.getCalleeId(), null);
             
             log.info("短信发送成功 - workflowInstanceId: {}", context.getWorkflowInstanceId());
-            
+            //CompanyVoiceRoboticServiceImpl.sendMsgOne()
             // 获取下一个节点并执行
             CompanyAiWorkflowExec exec = companyAiWorkflowExecMapper.selectByWorkflowInstanceId(context.getWorkflowInstanceId());
             List<CompanyWorkflowEdge> edges = companyWorkflowEdgeMapper.selectListByWorkflowIdAndNodeKey(

+ 1 - 0
fs-service/src/main/java/com/fs/company/vo/AiCallWorkflowConditionVo.java

@@ -17,4 +17,5 @@ public class AiCallWorkflowConditionVo {
     // 加微条件-超时时间(分钟)
     private Integer addTime;
 
+
 }

+ 12 - 0
fs-service/src/main/resources/mapper/company/CompanyWxClientMapper.xml

@@ -160,6 +160,18 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         </if>
         group by account_id
     </select>
+    <select id="getAddWxList4Workflow" resultType="com.fs.company.domain.CompanyWxClient">
+
+        SELECT t1.* FROM company_wx_client t1
+                             inner join company_voice_robotic_business t2 on t1.id = t2.wx_client_id and t1.robotic_id = t2.robotic_id
+                             inner join company_ai_workflow_exec t3 on t3.business_key = t2.id
+        where t1.is_add = 0 and t1.account_id is not null
+        and t3.current_node_type = #{execNodeType} And t3.status = #{execStatus}
+        <if test="accountIdList != null and !accountIdList.isEmpty()">
+            and t1.account_id in <foreach collection="accountIdList" open="(" separator="," close=")" item="item">#{item}</foreach>
+        </if>
+        group by t1.account_id
+    </select>
     <select id="selectWx" resultType="com.fs.company.domain.CompanyWxClient">
         select * from company_wx_client where account_id = #{accountId} and wx_v3 = #{v3}
     </select>

+ 121 - 0
fs-wx-task/src/main/java/com/fs/app/service/WxTaskService.java

@@ -75,6 +75,7 @@ public class WxTaskService {
     private final CompanyVoiceRoboticCallLogCallphoneServiceImpl companyVoiceRoboticCallLogCallphoneService;
     private final CompanyAiWorkflowExecMapper companyAiWorkflowExecMapper;
     private final CompanyWorkflowEngine companyWorkflowEngine;
+    private final CompanyVoiceRoboticBusinessMapper companyVoiceRoboticBusinessMapper;
     private final ExecutorService cidExcutor = new ThreadPoolExecutor(
             32,
             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() {
         LocalDateTime now = LocalDateTime.now();
         String json = sysConfigService.selectConfigByKey("wx.config");