From 10aaa035f5be0312304d20f022bdb714a8f4900a Mon Sep 17 00:00:00 2001
From: liusheng <337615773@qq.com>
Date: 星期三, 11 十二月 2024 22:16:23 +0800
Subject: [PATCH] 代码提交

---
 ruoyi-admin/src/main/java/com/ruoyi/web/controller/hanler/ChatWebSocketHandler.java |  423 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 417 insertions(+), 6 deletions(-)

diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/hanler/ChatWebSocketHandler.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/hanler/ChatWebSocketHandler.java
index 678eec7..4f31ebc 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/hanler/ChatWebSocketHandler.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/hanler/ChatWebSocketHandler.java
@@ -1,8 +1,16 @@
 package com.ruoyi.web.controller.hanler;
 
+import com.alibaba.fastjson2.JSON;
 import com.fasterxml.jackson.databind.ObjectMapper;
-import com.smartor.domain.SocketMessage;
+import com.google.gson.Gson;
+import com.ruoyi.common.core.redis.RedisCache;
+import com.ruoyi.common.utils.StringUtils;
+import com.smartor.domain.*;
+import com.smartor.mapper.IvrLibaExtemplatescriptMapper;
+import com.smartor.service.IIvrLibaTemplateService;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.ObjectUtils;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.springframework.web.socket.BinaryMessage;
 import org.springframework.web.socket.CloseStatus;
@@ -11,16 +19,40 @@
 import org.springframework.web.socket.handler.AbstractWebSocketHandler;
 
 import java.io.IOException;
-import java.util.Map;
+import java.nio.charset.StandardCharsets;
+import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.TimeUnit;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 @Slf4j
 @Component
 public class ChatWebSocketHandler extends AbstractWebSocketHandler {
 
+    private static RedisCache redisCache;
+
+    private static IIvrLibaTemplateService ivrLibaTemplateService;
+
+    private static IvrLibaExtemplatescriptMapper ivrLibaExtemplatescriptMapper;
+
     // 鐢ㄤ簬瀛樺偍鐢ㄦ埛ID鍜學ebSocketSession鐨勬槧灏勫叧绯�
     private static Map<String, WebSocketSession> sessions = new ConcurrentHashMap<>();
 
+    @Autowired
+    public void setIEMapper(IvrLibaExtemplatescriptMapper ivrLibaExtemplatescriptMapper) {
+        ChatWebSocketHandler.ivrLibaExtemplatescriptMapper = ivrLibaExtemplatescriptMapper;
+    }
+
+    @Autowired
+    public void setTodayReaderService(IIvrLibaTemplateService ivrLibaTemplateService) {
+        ChatWebSocketHandler.ivrLibaTemplateService = ivrLibaTemplateService;
+    }
+
+    @Autowired
+    public void setRedisCache(RedisCache redisCache) {
+        ChatWebSocketHandler.redisCache = redisCache;
+    }
 
     @Override
     public void afterConnectionEstablished(WebSocketSession session) throws Exception {
@@ -42,7 +74,7 @@
         ObjectMapper mapper = new ObjectMapper();
         SocketMessage parsedMessage = mapper.readValue(payload, SocketMessage.class);
         if (parsedMessage != null) {
-            sendMessageToUser(parsedMessage.getUserId(), parsedMessage.getContent());
+            sendMessageToUser(parsedMessage.getUserId(), parsedMessage);
         }
 
     }
@@ -79,12 +111,391 @@
         return userId;
     }
 
-    private void sendMessageToUser(String userId, String message) throws IOException {
+    private void sendMessageToUser(String userId, SocketMessage socketMessage) throws IOException {
         WebSocketSession session = sessions.get(userId);
         if (session != null && session.isOpen()) {
-            session.sendMessage(new TextMessage(message));
+            if (socketMessage.getTemplateId() != null) {
+                //閫氳繃妯℃澘ID鑾峰彇妯℃澘闂
+                IvrLibaTemplateVO ivrLibaTemplateVO = new IvrLibaTemplateVO();
+                ivrLibaTemplateVO.setId(socketMessage.getTemplateId());
+                IvrLibaTemplateVO ivrLibaTemplateVO1 = ivrLibaTemplateService.selectInfoByCondition(ivrLibaTemplateVO);
+                redisCache.setCacheObject(userId + "ivrLibaTemplateVO1", ivrLibaTemplateVO1, 120, TimeUnit.MINUTES);
+                //灏嗘煡鍑烘潵鐨勯棶棰�,鏀惧埌鍏ㄥ眬鍙橀噺涓幓
+                List<IvrLibaTemplateScriptVO> ivrLibaTemplateScriptVOList = ivrLibaTemplateVO1.getIvrLibaTemplateScriptVOList();
+                //鏍规嵁搴忓彿杩涜鎺掑簭
+                Collections.sort(ivrLibaTemplateScriptVOList, Comparator.comparing(IvrLibaTemplateScriptVO::getSort));
+
+                //灏嗘煡鍑烘潵鐨勬暟鎹斁鍒皉edis涓�
+                redisCache.setCacheObject(userId + "ivrLibaTemplateScriptVOList", ivrLibaTemplateScriptVOList, 120, TimeUnit.MINUTES);
+                //鐢ㄦ潵杩斿洖璋冩煡鐨勬�荤粨鏋�
+                redisCache.setCacheObject(userId + "backQuset", new ArrayList<>(), 120, TimeUnit.MINUTES);
+                //鐢ㄦ潵璁板綍鏃犲0鍝嶅簲娆℃暟
+                redisCache.setCacheObject(userId + "noVoice", 0, 120, TimeUnit.MINUTES);
+                //鐢ㄦ潵璁板綍鏃犲尮閰嶆鏁�
+                redisCache.setCacheObject(userId + "mateNum", 0, 120, TimeUnit.MINUTES);
+                //鐢ㄦ潵璁板綍鏁翠釜闂嵎鏄惁缁撴潫 0 锛氭湭缁撴潫     1锛氱粨鏉�
+                redisCache.setCacheObject(userId + "isOver", 0, 120, TimeUnit.MINUTES);
+
+                //鐢ㄦ潵杩斿洖褰撳墠闇�瑕侀棶鐨勯棶棰樹俊鎭�
+                QuestionMessagePhone returnQues = new QuestionMessagePhone();
+
+                for (IvrLibaTemplateScriptVO ivrLibaTemplateScriptVO : ivrLibaTemplateScriptVOList) {
+                    if (ivrLibaTemplateScriptVO.getSort() == ivrLibaTemplateVO1.getFirstQuestionNum().intValue())
+                        //鑾峰彇绗竴棰樼殑闂
+                        returnQues.setNowQuestion(ivrLibaTemplateScriptVO);
+//                        returnQues.setCode(ivrTaskTemplateScriptVO.getTargetid() + 1);
+                }
+//              璁剧疆寮�鍦虹櫧
+                returnQues.setKcb(ivrLibaTemplateVO1.getRevisitBefore());
+
+                //璁板綍鍝嶅簲
+                redisCache.setCacheObject(userId + "returnQues", returnQues, 120, TimeUnit.MINUTES);
+            } else {
+                //鑾峰彇socketMessage閲岀殑QuestionMessage,瀵圭瓟妗堣繘琛屽鐞嗙劧鍚�,鍐嶉棶涓嬩竴閬�
+                Gson gson = new Gson();
+                if (!StringUtils.isEmpty(socketMessage.getContent())) {
+                    socketMessage.setQuestionMessage(gson.fromJson(socketMessage.getContent(), QuestionMessagePhone.class));
+                }
+
+                QuestionMessagePhone questionMessage = socketMessage.getQuestionMessage();
+                List<IvrLibaTemplateScriptVO> ivrLibaTemplateScriptVOList = redisCache.getCacheObject(userId + "ivrLibaTemplateScriptVOList");
+                //鏍规嵁鍓嶇浼犲洖鐨勬潯浠讹紝鑾峰彇褰撳墠棰樼洰
+                IvrLibaTemplateScriptVO ivrLibaTemplateScriptVO = questionMessage.getNowQuestion();
+                if (ivrLibaTemplateScriptVO.getId() == null) {
+                    QuestionMessagePhone returnQues = redisCache.getCacheObject(userId + "returnQues");
+                    ivrLibaTemplateScriptVO = returnQues.getNowQuestion();
+                }
+                //鑾峰彇妯℃澘淇℃伅
+                IvrLibaTemplateVO ivrLibaTemplateVO1 = redisCache.getCacheObject(userId + "ivrLibaTemplateVO1");
+
+
+                if (StringUtils.isEmpty(questionMessage.getContent())) {
+                    //璋冪敤ivrTaskTemplateScriptVO涓殑slienceText(闈欓粯璇濇湳)
+                    Integer noVoice = redisCache.getCacheObject(userId + "noVoice");
+                    //瀹為檯闈欓粯娆℃暟鏄惁 绛変簬 妯℃澘涓厤缃殑娆℃暟
+                    if (noVoice == ivrLibaTemplateVO1.getNoVoiceNum().intValue()) {
+                        //鐢ㄦ潵杩斿洖璋冩煡鐨勬�荤粨鏋�
+                        List<IvrLibaTemplateScriptVO> backQuset = new ArrayList<>();
+                        backQuset.add(ivrLibaTemplateScriptVO);
+                        redisCache.setCacheObject(userId + "backQuset", backQuset, 120, TimeUnit.MINUTES);
+
+//                        //濡傛灉鍑犳閮芥病鍝嶅簲锛屽氨涓嬩竴棰橈紙闇�瑕佸厛鍒ゆ柇鏄惁鏈変笅涓�棰橈級
+//                        if (ivrLibaTemplateScriptVO.getSort() == 0) {
+                        //濡傛灉涓嬩竴棰樻病鏈変簡锛屽氨缁撴潫浜�
+
+                        //娌℃湁鍝嶅簲锛屽氨缁撴潫浜�
+                        QuestionMessagePhone returnQues = new QuestionMessagePhone();
+//                            returnQues.setCode(null);
+                        List<IvrLibaTemplateScriptVO> backQusetRedis = redisCache.getCacheObject(userId + "backQuset");
+                        returnQues.setContent(JSON.toJSONString(backQusetRedis));
+                        returnQues.setJsy(ivrLibaTemplateVO1.getRevisitAfter());
+                        redisCache.setCacheObject(userId + "returnQues", returnQues, 120, TimeUnit.MINUTES);
+                        redisCache.setCacheObject(userId + "isOver", 1, 120, TimeUnit.MINUTES);
+
+//                        } else {
+//                            //濡傛灉鏈変笅涓�棰橈紝灏辨牴鎹綋鍓峵eagetID+1 鑾峰彇涓嬩竴棰�
+//                            QuestionMessagePhone returnQues = getNextQuestion(ivrLibaTemplateScriptVOList, ivrLibaTemplateScriptVO);
+//                            redisCache.setCacheObject(userId + "returnQues", returnQues, 120, TimeUnit.MINUTES);
+//                            redisCache.setCacheObject(userId + "noVoice", 0, 120, TimeUnit.MINUTES);
+//                        }
+                    } else {
+                        //瀹為檯闈欓粯娆℃暟鏄惁 涓嶇瓑浜� 妯℃澘涓厤缃殑娆℃暟
+//                        QuestionMessage returnQues = redisCache.getCacheObject(userId + "returnQues");
+//                        returnQues.setContent(null);
+//                        redisCache.setCacheObject(userId + "returnQues", returnQues, 120, TimeUnit.MINUTES);
+
+                        Integer noVoice2 = redisCache.getCacheObject(userId + "noVoice");
+                        noVoice2 = noVoice2 + 1;
+
+                        //缁欓棶棰樺姞涓婇潤榛樿瘽鏈�
+                        QuestionMessagePhone returnQues = redisCache.getCacheObject(userId + "returnQues");
+                        IvrLibaTemplateScriptVO nowQuestion = returnQues.getNowQuestion();
+                        boolean contains = nowQuestion.getScriptContent().contains(nowQuestion.getSlienceText());
+                        if (!contains) {
+                            String scriptContent = nowQuestion.getSlienceText() + "," + nowQuestion.getScriptContent();
+                            nowQuestion.setScriptContent(scriptContent);
+                        }
+                        returnQues.setNowQuestion(nowQuestion);
+                        redisCache.setCacheObject(userId + "returnQues", returnQues, 120, TimeUnit.MINUTES);
+                        redisCache.setCacheObject(userId + "noVoice", noVoice2, 120, TimeUnit.MINUTES);
+                    }
+                } else {
+
+                    String extemplateID = ivrLibaTemplateVO1.getSubmoduleID();
+                    if (org.apache.commons.lang3.StringUtils.isNotEmpty(extemplateID)) {
+                        String[] split = extemplateID.split(",");
+                        List<String> list = Arrays.asList(split);
+                        List<Long> list1 = new ArrayList<>();
+                        for (String str : list) {
+                            list1.add(Long.valueOf(str));
+                        }
+                        List<IvrLibaExtemplatescript> ivrLibaExtemplatescripts = ivrLibaExtemplatescriptMapper.queryIvrLibaExtemplatescriptList(list1);
+                        IvrLibaExtemplatescript les = null;
+                        for (IvrLibaExtemplatescript ivrLibaExtemplatescript : ivrLibaExtemplatescripts) {
+                            Matcher matcher = null;
+                            if (org.apache.commons.lang3.StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex())) {
+                                Pattern pattern = Pattern.compile(ivrLibaExtemplatescript.getSelfRegex());
+                                matcher = pattern.matcher(questionMessage.getContent());
+                            }
+
+                            Matcher matcher2 = null;
+                            if (org.apache.commons.lang3.StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex2())) {
+                                Pattern pattern2 = Pattern.compile(ivrLibaExtemplatescript.getSelfRegex2());
+                                matcher2 = pattern2.matcher(questionMessage.getContent());
+                            }
+                            if (org.apache.commons.lang3.StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex()) && matcher.matches() && org.apache.commons.lang3.StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex2()) && matcher2.matches() || org.apache.commons.lang3.StringUtils.isEmpty(ivrLibaExtemplatescript.getSelfRegex()) && org.apache.commons.lang3.StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex2()) && matcher2.matches() || org.apache.commons.lang3.StringUtils.isEmpty(ivrLibaExtemplatescript.getSelfRegex2()) && org.apache.commons.lang3.StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex()) && matcher.matches()) {
+                                //灏嗛�氱敤搴撳尮閰嶇殑锛屾斁鍒拌繑鍥炲�间腑
+                                ivrLibaTemplateScriptVO.setCommonValue(ivrLibaExtemplatescript.getSwitchText());
+                                les = ivrLibaExtemplatescript;
+                            }
+                            break;
+                        }
+                        if (ObjectUtils.isNotEmpty(les)) {
+                            //瀵瑰尮閰嶇殑缁撴灉澶勭悊涓�涓�(鐪嬬湅鏄笉鏄渶瑕佹寕鏈�)
+                            if (les.getIsEnd() != null && les.getIsEnd() == 1) {
+                                //鍙互鎸傛満浜�
+                                redisCache.deleteObject(userId + "ivrLibaTemplateScriptVOList");
+                                redisCache.deleteObject(userId + "backQuset");
+                                redisCache.deleteObject(userId + "noVoice");
+                                redisCache.deleteObject(userId + "returnQues");
+                                redisCache.deleteObject(userId + "isOver");
+                                redisCache.deleteObject(userId + "ivrLibaTemplateVO1");
+                                QuestionMessagePhone returnQues = new QuestionMessagePhone();
+                                List<IvrLibaTemplateScriptVO> backQuset = new ArrayList<>();
+                                IvrLibaTemplateScriptVO ls = new IvrLibaTemplateScriptVO();
+                                ls.setScriptContent(les.getSwitchText());
+                                backQuset.add(ls);
+                                returnQues.setQuestionList(backQuset);
+                                returnQues.setNowQuestion(ls);
+                                session.sendMessage(new TextMessage(JSON.toJSONString(returnQues).getBytes(StandardCharsets.UTF_8)));
+                                return;
+                            }
+                        }
+                    }
+
+
+                    //閫氳繃valueType鍒ゆ柇鏄粈涔堥鍨嬶紙鍊肩被鍨嬶紙1 閫夐」  2 鏂囨湰  3 鏁板�硷級锛�
+                    if (ivrLibaTemplateScriptVO.getValueType() == 2 || ivrLibaTemplateScriptVO.getValueType() == 3) {
+                        ivrLibaTemplateScriptVO.setQuestionResult(questionMessage.getContent());
+
+                        List<IvrLibaTemplateScriptVO> backQusetList = redisCache.getCacheObject(userId + "backQuset");
+                        //鍖归厤瀹屾垚鍚�,灏嗙粨鏋滀繚瀛樺埌鏂扮殑鏁扮粍
+                        backQusetList.add(ivrLibaTemplateScriptVO);
+                        redisCache.setCacheObject(userId + "backQuset", backQusetList, 120, TimeUnit.MINUTES);
+                        //灏嗛潤榛樼疆涓�0
+                        redisCache.setCacheObject(userId + "noVoice", 0, 120, TimeUnit.MINUTES);
+                        //鑾峰彇涓嬩竴棰�
+                        Long nextQuestion = Long.valueOf(ivrLibaTemplateScriptVO.getSort() + 1);
+                        if (nextQuestion > ivrLibaTemplateScriptVOList.size()) {
+                            //濡傛灉涓嬩竴棰樻病鏈変簡锛屽氨缁撴潫浜�
+                            QuestionMessagePhone returnQues = new QuestionMessagePhone();
+                            List<IvrLibaTemplateScriptVO> backQusetRedis = redisCache.getCacheObject(userId + "backQuset");
+                            returnQues.setContent(JSON.toJSONString(backQusetRedis));
+                            returnQues.setJsy(ivrLibaTemplateVO1.getRevisitAfter());
+                            redisCache.setCacheObject(userId + "returnQues", returnQues, 120, TimeUnit.MINUTES);
+                            redisCache.setCacheObject(userId + "isOver", 1, 120, TimeUnit.MINUTES);
+                        } else {
+                            for (IvrLibaTemplateScriptVO script : ivrLibaTemplateScriptVOList) {
+                                if (Long.valueOf(script.getSort()) == nextQuestion) {
+                                    QuestionMessagePhone returnQues = new QuestionMessagePhone();
+                                    returnQues.setNowQuestion(script);
+                                    redisCache.setCacheObject(userId + "returnQues", returnQues, 120, TimeUnit.MINUTES);
+                                }
+                            }
+                        }
+
+                    } else if (ivrLibaTemplateScriptVO.getValueType() == 1) {
+
+                        //瀵瑰洖绛旂殑闂,杩涜姝e垯鍖归厤锛堣繖閲屽彧閽堝閫夋嫨棰橈紝鍏跺畠棰樺瀷涓嶈锛墄
+                        for (int j = 0; j < ivrLibaTemplateScriptVO.getIvrLibaScriptTargetoptionList().size(); j++) {
+                            //鍖呭惈
+                            Matcher matcher = null;
+                            if (StringUtils.isNotEmpty(ivrLibaTemplateScriptVO.getIvrLibaScriptTargetoptionList().get(j).getTargetregex())) {
+                                Pattern pattern = Pattern.compile(ivrLibaTemplateScriptVO.getIvrLibaScriptTargetoptionList().get(j).getTargetregex());
+                                matcher = pattern.matcher(questionMessage.getContent());
+                            }
+                            //涓嶅寘鍚�
+                            Matcher matcher2 = null;
+                            if (StringUtils.isNotEmpty(ivrLibaTemplateScriptVO.getIvrLibaScriptTargetoptionList().get(j).getTargetregex2())) {
+                                Pattern pattern2 = Pattern.compile(ivrLibaTemplateScriptVO.getIvrLibaScriptTargetoptionList().get(j).getTargetregex2());
+                                matcher2 = pattern2.matcher(questionMessage.getContent());
+                            }
+                            log.info("==============================Targetregex鐨勫�间负锛歿},   Targetregex2鐨勫�间负锛歿}", ivrLibaTemplateScriptVO.getIvrLibaScriptTargetoptionList().get(j).getTargetregex(), ivrLibaTemplateScriptVO.getIvrLibaScriptTargetoptionList().get(j).getTargetregex2());
+
+                            if (StringUtils.isNotEmpty(ivrLibaTemplateScriptVO.getIvrLibaScriptTargetoptionList().get(j).getTargetregex2()) && matcher2.matches() &&
+                                    StringUtils.isNotEmpty(ivrLibaTemplateScriptVO.getIvrLibaScriptTargetoptionList().get(j).getTargetregex()) && matcher.matches() ||
+                                    StringUtils.isEmpty(ivrLibaTemplateScriptVO.getIvrLibaScriptTargetoptionList().get(j).getTargetregex2()) &&
+                                            StringUtils.isNotEmpty(ivrLibaTemplateScriptVO.getIvrLibaScriptTargetoptionList().get(j).getTargetregex()) &&
+                                            matcher.matches()) {
+                                log.error("鍖归厤姝g‘浜�");
+                                //璇存槑鍖归厤姝g‘浜�
+                                ivrLibaTemplateScriptVO.getIvrLibaScriptTargetoptionList().get(j).setIsUserOperation(1);
+                                List<IvrLibaTemplateScriptVO> backQuset = redisCache.getCacheObject(userId + "backQuset");
+                                //鍖归厤瀹屾垚鍚�,灏嗙粨鏋滀繚瀛樺埌鏂扮殑鏁扮粍
+                                backQuset.add(ivrLibaTemplateScriptVO);
+                                redisCache.setCacheObject(userId + "backQuset", backQuset, 120, TimeUnit.MINUTES);
+                                //灏嗛潤榛樼疆涓�0
+                                redisCache.setCacheObject(userId + "noVoice", 0, 120, TimeUnit.MINUTES);
+
+                                //鍒ゆ柇涓�涓嬶紝杩欎釜閫夐」缁撴灉鏄笉鏄繕鏈夌户缁棶涓嬪幓鐨勫繀瑕侊紝渚嬪閫夐」缁撴灉鏄埆浜轰笉鎯崇户缁洖绛旈棶棰橈紝灏辫缁撴潫鎺�
+                                if (ivrLibaTemplateScriptVO.getIvrLibaScriptTargetoptionList().get(j).getIsEnd() == 1) {
+                                    QuestionMessagePhone returnQues = new QuestionMessagePhone();
+                                    List<IvrLibaTemplateScriptVO> backQusetRedis = redisCache.getCacheObject(userId + "backQuset");
+                                    returnQues.setContent(JSON.toJSONString(backQusetRedis));
+                                    returnQues.setJsy(ivrLibaTemplateVO1.getRevisitAfter());
+                                    redisCache.setCacheObject(userId + "returnQues", returnQues, 120, TimeUnit.MINUTES);
+                                    redisCache.setCacheObject(userId + "isOver", 1, 120, TimeUnit.MINUTES);
+                                } else {
+                                    //鑾峰彇涓嬩竴棰�
+                                    Integer nextQuestion = ivrLibaTemplateScriptVO.getIvrLibaScriptTargetoptionList().get(j).getNextQuestion();
+                                    if (nextQuestion > ivrLibaTemplateScriptVOList.size()) {
+                                        //濡傛灉涓嬩竴棰樻病鏈変簡锛屽氨缁撴潫浜�
+                                        QuestionMessagePhone returnQues = new QuestionMessagePhone();
+                                        List<IvrLibaTemplateScriptVO> backQusetRedis = redisCache.getCacheObject(userId + "backQuset");
+                                        returnQues.setContent(JSON.toJSONString(backQusetRedis));
+                                        returnQues.setJsy(ivrLibaTemplateVO1.getRevisitAfter());
+                                        redisCache.setCacheObject(userId + "returnQues", returnQues, 120, TimeUnit.MINUTES);
+                                        redisCache.setCacheObject(userId + "isOver", 1, 120, TimeUnit.MINUTES);
+                                    } else {
+                                        for (IvrLibaTemplateScriptVO script : ivrLibaTemplateScriptVOList) {
+                                            if (script.getSort() == nextQuestion) {
+                                                QuestionMessagePhone returnQues = new QuestionMessagePhone();
+                                                returnQues.setNowQuestion(script);
+                                                redisCache.setCacheObject(userId + "returnQues", returnQues, 120, TimeUnit.MINUTES);
+                                                break;
+                                            }
+                                        }
+                                    }
+                                    break;
+                                }
+                            } else {
+                                log.error("鏃犲尮閰�");
+                                //娌℃湁鍖归厤鍒�
+                                Integer mateNum = redisCache.getCacheObject(userId + "mateNum");
+
+                                //鏃犲尮閰嶆鏁板幓鍒ゆ柇鏄惁鍒版渶澶ц闂鏁帮紝骞朵笖鎵�鏈夌殑閫夐」閮藉尮閰嶅畬浜�
+                                if (mateNum == ivrLibaTemplateVO1.getMateNum().intValue() && j == ivrLibaTemplateScriptVO.getIvrLibaScriptTargetoptionList().size() - 1) {
+                                    //闂簡鏈�澶ц闂鏁板悗锛岃繕鏄病鏈夊尮閰嶄笂,灏嗙粨鏋滀繚瀛樺埌鏂扮殑鏁扮粍
+                                    List<IvrLibaTemplateScriptVO> backQuset = redisCache.getCacheObject(userId + "backQuset");
+                                    backQuset.add(ivrLibaTemplateScriptVO);
+                                    redisCache.setCacheObject(userId + "backQuset", backQuset, 120, TimeUnit.MINUTES);
+
+                                    //濡傛灉涓嬩竴棰樹负绌�.鍒欐柊鐨勬暟鎹繑鍥�,骞跺姞涓婃劅璋㈣
+                                    if (ivrLibaTemplateScriptVO.getSort() == 0) {
+                                        QuestionMessagePhone returnQues = getNextQuestion(ivrLibaTemplateScriptVOList, ivrLibaTemplateScriptVO);
+                                        redisCache.setCacheObject(userId + "returnQues", returnQues, 120, TimeUnit.MINUTES);
+                                        redisCache.setCacheObject(userId + "mateNum", 0, 120, TimeUnit.MINUTES);
+                                    } else {
+                                        QuestionMessagePhone returnQues2 = new QuestionMessagePhone();
+//                                    returnQues2.setCode(null);
+                                        List<IvrLibaTemplateScriptVO> backQusetRedis = redisCache.getCacheObject(userId + "backQuset");
+                                        returnQues2.setQuestionList(backQusetRedis);
+                                        returnQues2.setJsy(ivrLibaTemplateVO1.getRevisitAfter());
+                                        redisCache.setCacheObject(userId + "returnQues", returnQues2, 120, TimeUnit.MINUTES);
+                                        redisCache.setCacheObject(userId + "isOver", 1, 120, TimeUnit.MINUTES);
+                                        break;
+                                    }
+                                } else if (mateNum < ivrLibaTemplateVO1.getMateNum().intValue() && j == ivrLibaTemplateScriptVO.getIvrLibaScriptTargetoptionList().size() - 1) {
+                                    //娌℃湁闂埌瑙勫畾娆℃暟
+                                    mateNum = mateNum + 1;
+                                    redisCache.setCacheObject(userId + "mateNum", mateNum, 120, TimeUnit.MINUTES);
+                                    //缁欓棶棰樺姞涓婃棤鍖归厤璇濇湳
+                                    QuestionMessagePhone returnQues = redisCache.getCacheObject(userId + "returnQues");
+                                    IvrLibaTemplateScriptVO nowQuestion = returnQues.getNowQuestion();
+                                    boolean contains = false;
+                                    if (StringUtils.isNotEmpty(nowQuestion.getNoMatchText()))
+                                        contains = nowQuestion.getScriptContent().contains(nowQuestion.getNoMatchText());
+                                    if (!contains) {
+                                        String scriptContent = nowQuestion.getNoMatchText() + "," + nowQuestion.getScriptContent();
+                                        nowQuestion.setScriptContent(scriptContent);
+                                    }
+                                    returnQues.setNowQuestion(nowQuestion);
+                                    redisCache.setCacheObject(userId + "returnQues", returnQues, 120, TimeUnit.MINUTES);
+                                }
+                            }
+                        }
+                    }
+//                    //閫夐」鍖归厤瀹屾垚鍚庯紝闇�瑕佸啀鍘婚�氳繃搴撳啀杩涜鍖归厤涓�娆�
+//                    String extemplateID = ivrLibaTemplateVO1.getSubmoduleID();
+//                    String[] split = extemplateID.split(",");
+//                    List<String> list = Arrays.asList(split);
+//                    List<Long> list1 = new ArrayList<>();
+//                    if (StringUtils.isNotEmpty(extemplateID)) {
+//                        for (String str : list) {
+//                            list1.add(Long.valueOf(str));
+//                        }
+//                        List<IvrLibaExtemplatescript> ivrLibaExtemplatescripts = ivrLibaExtemplatescriptMapper.queryIvrLibaExtemplatescriptList(list1);
+//                        for (IvrLibaExtemplatescript ivrLibaExtemplatescript : ivrLibaExtemplatescripts) {
+//                            Matcher matcher = null;
+//                            if (StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex())) {
+//                                Pattern pattern = Pattern.compile(ivrLibaExtemplatescript.getSelfRegex());
+//                                matcher = pattern.matcher(questionMessage.getContent());
+//                            }
+//
+//                            Matcher matcher2 = null;
+//                            if (StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex2())) {
+//                                Pattern pattern2 = Pattern.compile(ivrLibaExtemplatescript.getSelfRegex2());
+//                                matcher2 = pattern2.matcher(questionMessage.getContent());
+//                            }
+//                            log.info("++++++++++++++++++++++++++閫氱敤搴撴槸鍚︿负绌猴細selfRegex : {} , selfRegex2 : {}", ivrLibaExtemplatescript.getSelfRegex(), ivrLibaExtemplatescript.getSelfRegex2());
+//
+//                            if (StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex()) && matcher.matches() && StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex2()) && matcher2.matches() || StringUtils.isEmpty(ivrLibaExtemplatescript.getSelfRegex()) && StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex2()) && matcher2.matches() || StringUtils.isEmpty(ivrLibaExtemplatescript.getSelfRegex2()) && StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex()) && matcher.matches()) {
+//                                QuestionMessagePhone returnQues = redisCache.getCacheObject(userId + "returnQues");
+//                                IvrLibaTemplateScriptVO nowQuestion = returnQues.getNowQuestion();
+//                                nowQuestion.setSubmoduleText(ivrLibaExtemplatescript.getSwitchText());
+//                                nowQuestion.setSubmoduleVoice(ivrLibaExtemplatescript.getSwitchWav());
+//                                redisCache.setCacheObject(userId + "returnQues", returnQues, 120, TimeUnit.MINUTES);
+//                                if (ivrLibaExtemplatescript.getIsEnd() == 1) {
+//                                    //灏嗛棶棰樼疆绌�
+//                                    IvrLibaTemplateScriptVO nowQuestion1 = returnQues.getNowQuestion();
+//                                    nowQuestion1.setScriptContent(null);
+//                                    nowQuestion1.setScriptVoice(null);
+//                                    returnQues.setNowQuestion(nowQuestion1);
+//                                    redisCache.setCacheObject(userId + "returnQues", returnQues, 120, TimeUnit.MINUTES);
+//
+//                                    redisCache.setCacheObject(userId + "isOver", 1, 120, TimeUnit.MINUTES);
+//                                }
+//                            }
+//                            break;
+//                        }
+//                    }
+                }
+
+            }
+            QuestionMessagePhone returnQues = redisCache.getCacheObject(userId + "returnQues");
+            List<IvrLibaTemplateScriptVO> backQusetRedis = redisCache.getCacheObject(userId + "backQuset");
+            returnQues.setQuestionList(backQusetRedis);
+            session.sendMessage(new TextMessage(JSON.toJSONString(returnQues).getBytes(StandardCharsets.UTF_8)));
+
+            //鏁翠釜娴佺▼鏄惁缁撴潫锛屽皢redis涓殑璇serId涓嬬殑鏁版嵁鍒犻櫎;
+            Integer isOver = redisCache.getCacheObject(userId + "isOver");
+            if (isOver == 1) {
+                redisCache.deleteObject(userId + "ivrLibaTemplateScriptVOList");
+                redisCache.deleteObject(userId + "backQuset");
+                redisCache.deleteObject(userId + "noVoice");
+                redisCache.deleteObject(userId + "returnQues");
+                redisCache.deleteObject(userId + "isOver");
+                redisCache.deleteObject(userId + "ivrLibaTemplateVO1");
+                try {
+                    //鏂紑閾炬帴
+                    afterConnectionClosed(session, null);
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
         }
     }
 
+    //鑾峰彇涓嬩竴棰橈紙杩欎釜鏂规硶涓嶉�傚悎閫夐」涓殑nextQuestion锛�
+    private QuestionMessagePhone getNextQuestion(List<IvrLibaTemplateScriptVO> ivrLibaTemplateScriptVOList, IvrLibaTemplateScriptVO ivrLibaTemplateScriptVO) {
+        QuestionMessagePhone returnQues = new QuestionMessagePhone();
+        for (int j = 0; j < ivrLibaTemplateScriptVOList.size(); j++) {
+            if (ivrLibaTemplateScriptVOList.get(j).getSort() == ivrLibaTemplateScriptVO.getSort() + 1) {
+                // 瀵硅鏉emplateScriptVO杩涜澶勭悊
+                returnQues.setNowQuestion(ivrLibaTemplateScriptVOList.get(j));
+            }
+        }
+        return returnQues;
+    }
 
-}
\ No newline at end of file
+}

--
Gitblit v1.9.3