From e34e5ba8e0060a5c201254da7f358b50db7b0b3d Mon Sep 17 00:00:00 2001
From: 陈昶聿 <chychen@nbjetron.com>
Date: 星期四, 06 八月 2026 10:13:01 +0800
Subject: [PATCH] 【景宁】任务详情增加任务组信息

---
 smartor/src/main/java/com/smartor/service/impl/ServiceSubtaskAnswerServiceImpl.java |  360 +++++++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 246 insertions(+), 114 deletions(-)

diff --git a/smartor/src/main/java/com/smartor/service/impl/ServiceSubtaskAnswerServiceImpl.java b/smartor/src/main/java/com/smartor/service/impl/ServiceSubtaskAnswerServiceImpl.java
index 77b373a..63a5cda 100644
--- a/smartor/src/main/java/com/smartor/service/impl/ServiceSubtaskAnswerServiceImpl.java
+++ b/smartor/src/main/java/com/smartor/service/impl/ServiceSubtaskAnswerServiceImpl.java
@@ -5,6 +5,7 @@
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.ruoyi.common.core.redis.RedisCache;
+import com.ruoyi.common.exception.base.BaseException;
 import com.ruoyi.common.utils.DateUtils;
 import com.ruoyi.common.utils.DtoConversionUtils;
 import com.ruoyi.common.utils.RSAPublicKeyExample;
@@ -15,7 +16,6 @@
 import com.smartor.domain.entity.ServiceSubtaskEntity;
 import com.smartor.mapper.*;
 import com.smartor.service.*;
-import com.sun.org.apache.bcel.internal.generic.NEW;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.ObjectUtils;
@@ -79,6 +79,9 @@
 
     @Autowired
     private ISvyLibTemplateScriptService svyLibTemplateScriptService;
+
+    @Autowired
+    private ISvyTaskTemplateTargetoptionService svyTaskTemplateTargetoptionService;
 
     @Autowired
     private IIvrTaskTemplateService ivrTaskTemplateService;
@@ -208,6 +211,10 @@
                 List<SvyTaskTemplateScriptVO> cacheList = redisCache.getCacheList(key);
                 cacheList.sort(Comparator.comparingLong(SvyTaskTemplateScriptVO::getSort));
                 log.info("闂嵎鍥炵瓟缁撴灉锛歿}", CollectionUtils.isEmpty(cacheList) ? null : cacheList.size());
+
+                //鏍¢獙涓�涓嬮鐨勫簭鍙锋槸鍚︽湁闂 (濡傛灉鏍规嵁閫夐」鏃犲簭璺宠浆锛屽氨浼氬鑷撮敊璇�)
+                cacheList = checkQuestionSort(cacheList);
+
                 for (int i = 0; i < cacheList.size(); i++) {
                     if (i == 0) {
                         //杩欎釜鏃堕棿瑕佹壘鍒扮涓�棰�
@@ -341,6 +348,8 @@
             String isabnormalFlag = "0";
             if (serviceSubTaskCacheReq.getType() == 2) {
                 List<SvyTaskTemplateScriptVO> cacheList = serviceSubTaskCacheReq.getSvyTaskTemplateScriptVOS();
+                //妫�鏌ュ苟淇闂嵎棰樼洰鎺掑簭
+                cacheList = checkQuestionSort(cacheList);
                 cacheList.sort(Comparator.comparingLong(SvyTaskTemplateScriptVO::getSort));
                 log.info("闂嵎鍥炵瓟缁撴灉锛歿}", CollectionUtils.isEmpty(cacheList) ? null : cacheList.size());
                 // 棰勬煡璇㈠惊鐜腑涓嶅彉鐨勬暟鎹紝閬垮厤姣忛閲嶅鏌ユ暟鎹簱
@@ -352,90 +361,89 @@
                 ServiceTask preServiceTask = serviceTaskService.selectServiceTaskByTaskid(tid);
                 Map<Long, Long> preDetailIdMap = getExistingDetailIdMap(subid);
                 Map<Long, Long> preTraceIdMap = getExistingTraceIdMap(subid);
+
+                // 鎻愬墠瀹氫綅绗竴棰樼殑绱㈠紩锛岄伩鍏嶅悗缁祵濂楀惊鐜�
+                Integer firstSortIndex = -1;
+                if (CollectionUtils.isNotEmpty(cacheList)) {
+                    for (int j = 0; j < cacheList.size(); j++) {
+                        if (cacheList.get(j).getSort() != null && cacheList.get(j).getSort() == 1L) {
+                            firstSortIndex = j;
+                            break;
+                        }
+                    }
+                }
+
                 for (int i = 0; i < cacheList.size(); i++) {
-                    if (i == 0) {
-                        //杩欎釜鏃堕棿瑕佹壘鍒扮涓�棰�
-                        for (SvyTaskTemplateScriptVO svyTaskTemplateScriptVO : cacheList) {
-                            if (svyTaskTemplateScriptVO.getSort() == 1L) {
-                                //璇存槑鏄涓�棰�
-                                List<SvyTaskTemplateTargetoption> svyTaskTemplateTargetoptions = svyTaskTemplateScriptVO.getSvyTaskTemplateTargetoptions();
-                                svyTaskTemplateScriptVO.setSvyTaskTemplateTargetoptions(svyTaskTemplateTargetoptions);
-                                Map<String, String> map = setWJInfo(svyTaskTemplateScriptVO, tid, pid, subid, preServiceSubtaskList, preServiceTask, preDetailIdMap, preTraceIdMap);
-                                if (map == null) continue;
-                                if (!Objects.isNull(map.get("nextScriptNo")))
-                                    nextScriptno = Long.valueOf(map.get("nextScriptNo"));
-                                if (score == null) {
-                                    score = Double.valueOf(map.get("score"));
-                                } else {
-                                    score = score + Double.valueOf(map.get("score"));
-                                }
-                                isabnormalFlag = map.get("isabnormal");
-                            }
-                        }
+                    SvyTaskTemplateScriptVO svyTaskTemplateScriptVO = cacheList.get(i);
+                    log.info("svyTaskTemplateScriptVO 鐨勪俊鎭负锛歿}锛宨 鐨勫�间负锛歿}", svyTaskTemplateScriptVO.getScriptno(), i);
+                    List<SvyTaskTemplateTargetoption> svyTaskTemplateTargetoptions = svyTaskTemplateScriptVO.getSvyTaskTemplateTargetoptions();
+                    svyTaskTemplateScriptVO.setSvyTaskTemplateTargetoptions(svyTaskTemplateTargetoptions);
 
-                    } else {
-                        SvyTaskTemplateScriptVO svyTaskTemplateScriptVO = cacheList.get(i);
-                        log.info("svyTaskTemplateScriptVO鐨勪俊鎭负锛歿}锛宨鐨勫�间负锛歿}", svyTaskTemplateScriptVO.getScriptno(), i);
-                        List<SvyTaskTemplateTargetoption> svyTaskTemplateTargetoptions = cacheList.get(i).getSvyTaskTemplateTargetoptions();
-                        svyTaskTemplateScriptVO.setSvyTaskTemplateTargetoptions(svyTaskTemplateTargetoptions);
+                    // 濡傛灉鏄涓�棰樻垨鑰呭尮閰� nextScriptno 鐨勯鐩�
+                    boolean shouldProcess = false;
+                    if (i == firstSortIndex) {
+                        shouldProcess = true;
+                    } else if (nextScriptno != null && svyTaskTemplateScriptVO.getSort() != null && svyTaskTemplateScriptVO.getSort().equals(nextScriptno)) {
+                        shouldProcess = true;
+                    }
 
-                        if (svyTaskTemplateScriptVO.getSort() != null && svyTaskTemplateScriptVO.getSort().equals(nextScriptno)) {
-                            Map<String, String> map = setWJInfo(svyTaskTemplateScriptVO, tid, pid, subid, preServiceSubtaskList, preServiceTask, preDetailIdMap, preTraceIdMap);
-                            log.error("nextScriptNo鐨勪俊鎭负锛歿}", map.get("nextScriptNo"));
-                            if (!Objects.isNull(map.get("nextScriptNo")))
-                                nextScriptno = Long.valueOf(map.get("nextScriptNo"));
-                            if (score == null) {
-                                score = Double.valueOf(map.get("score"));
-                            } else {
-                                score = score + Double.valueOf(map.get("score"));
-                            }
-                            isabnormalFlag = map.get("isabnormal");
+                    if (shouldProcess) {
+                        Map<String, String> map = setWJInfo(svyTaskTemplateScriptVO, tid, pid, subid, preServiceSubtaskList, preServiceTask, preDetailIdMap, preTraceIdMap);
+                        log.error("nextScriptNo 鐨勪俊鎭负锛歿}", map.get("nextScriptNo"));
+                        if (map == null) continue;
+
+                        if (!Objects.isNull(map.get("nextScriptNo")))
+                            nextScriptno = Long.valueOf(map.get("nextScriptNo"));
+
+                        if (score == null) {
+                            score = Double.valueOf(map.get("score"));
+                        } else {
+                            score = score + Double.valueOf(map.get("score"));
                         }
+                        isabnormalFlag = map.get("isabnormal");
                     }
                 }
             } else if (serviceSubTaskCacheReq.getType() == 1) {
                 List<IvrTaskTemplateScriptVO> cacheList = serviceSubTaskCacheReq.getIvrTaskTemplateScriptVOList();
                 cacheList.sort(Comparator.comparingLong(vo -> Optional.ofNullable(vo.getSort()).orElse(0)));
                 log.error("鐢佃瘽闅忚闂嵎鍥炵瓟缁撴灉锛歿}", CollectionUtils.isEmpty(cacheList) ? null : cacheList.size());
+
+                // 鎻愬墠瀹氫綅绗竴棰樼殑绱㈠紩
+                Integer firstIvrSortIndex = -1;
+                if (CollectionUtils.isNotEmpty(cacheList)) {
+                    for (int j = 0; j < cacheList.size(); j++) {
+                        if (cacheList.get(j).getSort() != null && cacheList.get(j).getSort() == 1L) {
+                            firstIvrSortIndex = j;
+                            break;
+                        }
+                    }
+                }
+
                 for (int i = 0; i < cacheList.size(); i++) {
-                    if (i == 0) {
-                        //杩欎釜鏃堕棿瑕佹壘鍒扮涓�棰�
-                        for (IvrTaskTemplateScriptVO ivrTaskTemplateScriptVO : cacheList) {
-                            if (ivrTaskTemplateScriptVO.getSort() != null && ivrTaskTemplateScriptVO.getSort() == 1L) {
-                                //璇存槑鏄涓�棰�
-                                List<IvrTaskTemplateTargetoption> ivrTaskTemplateTargetoptions = ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList();
-                                ivrTaskTemplateScriptVO.setIvrTaskScriptTargetoptionList(ivrTaskTemplateTargetoptions);
-                                //瀹冧咯閮芥槸ivr_task_templatescript_id,鎵�浠ヨ繖閲岄渶瑕佽缃竴涓�
-                                ivrTaskTemplateScriptVO.setId(ivrTaskTemplateScriptVO.getScriptID());
-                                Map<String, String> map = setSFInfo(ivrTaskTemplateScriptVO, tid, pid, subid);
-                                if (!Objects.isNull(map.get("nextScriptNo")))
-                                    nextScriptno = Long.valueOf(map.get("nextScriptNo"));
-                                if (score == null) {
-                                    score = Double.valueOf(map.get("score"));
-                                } else {
-                                    score = score + Double.valueOf(map.get("score"));
-                                }
-                                isabnormalFlag = map.get("isabnormal");
-                            }
-                        }
+                    IvrTaskTemplateScriptVO ivrTaskTemplateScriptVO = DtoConversionUtils.sourceToTarget(cacheList.get(i), IvrTaskTemplateScriptVO.class);
+                    List<IvrTaskTemplateTargetoption> ivrTaskTemplateTargetoptions = DtoConversionUtils.sourceToTarget(cacheList.get(i).getIvrTaskScriptTargetoptionList(), IvrTaskTemplateTargetoption.class);
+                    ivrTaskTemplateScriptVO.setIvrTaskScriptTargetoptionList(ivrTaskTemplateTargetoptions);
+                    //瀹冧咯閮芥槸 ivr_task_templatescript_id,鎵�浠ヨ繖閲岄渶瑕佽缃竴涓�
+                    ivrTaskTemplateScriptVO.setId(ivrTaskTemplateScriptVO.getScriptID());
 
-                    } else {
-                        IvrTaskTemplateScriptVO ivrTaskTemplateScriptVO = DtoConversionUtils.sourceToTarget(cacheList.get(i), IvrTaskTemplateScriptVO.class);
-                        List<IvrTaskTemplateTargetoption> ivrTaskTemplateTargetoptions = DtoConversionUtils.sourceToTarget(cacheList.get(i).getIvrTaskScriptTargetoptionList(), IvrTaskTemplateTargetoption.class);
-                        ivrTaskTemplateScriptVO.setIvrTaskScriptTargetoptionList(ivrTaskTemplateTargetoptions);
+                    boolean shouldProcess = false;
+                    if (i == firstIvrSortIndex) {
+                        shouldProcess = true;
+                    } else if (nextScriptno != null && ivrTaskTemplateScriptVO.getSort() != null && Long.valueOf(ivrTaskTemplateScriptVO.getSort()).equals(nextScriptno)) {
+                        shouldProcess = true;
+                    }
 
-                        if (ivrTaskTemplateScriptVO.getSort() != null && Long.valueOf(ivrTaskTemplateScriptVO.getSort()).equals(nextScriptno)) {
-                            Map<String, String> map = setSFInfo(ivrTaskTemplateScriptVO, tid, pid, subid);
-                            log.error("----nextScriptNo2鐨勪俊鎭负锛歿}", map.get("nextScriptNo"));
-                            if (!Objects.isNull(map.get("nextScriptNo")))
-                                nextScriptno = Long.valueOf(map.get("nextScriptNo"));
-                            if (score == null) {
-                                score = Double.valueOf(map.get("score"));
-                            } else {
-                                score = score + Double.valueOf(map.get("score"));
-                            }
-                            isabnormalFlag = map.get("isabnormal");
+                    if (shouldProcess) {
+                        Map<String, String> map = setSFInfo(ivrTaskTemplateScriptVO, tid, pid, subid);
+                        log.error("----nextScriptNo2 鐨勪俊鎭负锛歿}", map.get("nextScriptNo"));
+                        if (!Objects.isNull(map.get("nextScriptNo")))
+                            nextScriptno = Long.valueOf(map.get("nextScriptNo"));
+                        if (score == null) {
+                            score = Double.valueOf(map.get("score"));
+                        } else {
+                            score = score + Double.valueOf(map.get("score"));
                         }
+                        isabnormalFlag = map.get("isabnormal");
                     }
                 }
             }
@@ -515,52 +523,46 @@
             Double score = null;
             List<SvyTaskTemplateScriptVO> svyTaskTemplateScriptVOS = serviceSubTaskMYDAnswerReq.getSvyTaskTemplateScriptVOS();
             Long nextScriptno = null;
+
+            // 鎻愬墠瀹氫綅绗竴棰樼殑绱㈠紩
+            Integer firstMydSortIndex = -1;
+            if (CollectionUtils.isNotEmpty(svyTaskTemplateScriptVOS)) {
+                for (int j = 0; j < svyTaskTemplateScriptVOS.size(); j++) {
+                    if (svyTaskTemplateScriptVOS.get(j).getSort() != null && svyTaskTemplateScriptVOS.get(j).getSort() == 1L) {
+                        firstMydSortIndex = j;
+                        break;
+                    }
+                }
+            }
+
             for (int i = 0; i < svyTaskTemplateScriptVOS.size(); i++) {
-                if (i == 0) {
-                    //杩欎釜鏃堕棿瑕佹壘鍒扮涓�棰�
-                    for (SvyTaskTemplateScriptVO svyTaskTemplateScriptVO : svyTaskTemplateScriptVOS) {
-                        if (svyTaskTemplateScriptVO.getSort() == 1L) {
-                            //璇存槑鏄涓�棰�
-//                            SvyTaskTemplateScriptVO svyTaskTemplateScriptVO = DtoConversionUtils.sourceToTarget(svyLibTemplateScriptVOS.get(0), SvyTaskTemplateScriptVO.class);
-                            List<SvyTaskTemplateTargetoption> svyTaskTemplateTargetoptions = svyTaskTemplateScriptVOS.get(0).getSvyTaskTemplateTargetoptions();
-//                            List<SvyTaskTemplateTargetoption> svyTaskTemplateTargetoptions = DtoConversionUtils.sourceToTarget(svyTaskTemplateTargetoptions, SvyTaskTemplateTargetoption.class);
-                            svyTaskTemplateScriptVO.setSvyTaskTemplateTargetoptions(svyTaskTemplateTargetoptions);
-                            Map<String, String> map = setWJInfo(svyTaskTemplateScriptVO, Long.valueOf(serviceSubTaskMYDAnswerReq.getTaskId()), patid, serviceSubTaskMYDAnswerReq.getSubId());
-                            if (map == null) continue;
-                            if (!Objects.isNull(map.get("nextScriptNo")))
-                                nextScriptno = Long.valueOf(map.get("nextScriptNo"));
-                            if (score == null) {
-                                score = Double.valueOf(map.get("score"));
-                            } else {
-                                score = score + Double.valueOf(map.get("score"));
-                            }
-                            if (!Objects.isNull(map.get("id"))) {
-                                idList.add(Long.valueOf(map.get("id")));
-                            }
-                        }
+                SvyTaskTemplateScriptVO svyTaskTemplateScriptVO = svyTaskTemplateScriptVOS.get(i);
+                List<SvyTaskTemplateTargetoption> svyTaskTemplateTargetoptions = svyTaskTemplateScriptVOS.get(i).getSvyTaskTemplateTargetoptions();
+                svyTaskTemplateScriptVO.setSvyTaskTemplateTargetoptions(svyTaskTemplateTargetoptions);
+
+                boolean shouldProcess = false;
+                if (i == firstMydSortIndex) {
+                    shouldProcess = true;
+                } else if (nextScriptno != null && svyTaskTemplateScriptVO.getSort() != null && svyTaskTemplateScriptVO.getSort().equals(nextScriptno)) {
+                    shouldProcess = true;
+                }
+
+                if (shouldProcess) {
+                    Map<String, String> map = setWJInfo(svyTaskTemplateScriptVO, Long.valueOf(serviceSubTaskMYDAnswerReq.getTaskId()), patid, serviceSubTaskMYDAnswerReq.getSubId());
+                    if (map == null) continue;
+
+                    log.error("nextScriptNo 鐨勪俊鎭负锛歿}", map.get("nextScriptNo"));
+                    if (!Objects.isNull(map.get("nextScriptNo")))
+                        nextScriptno = Long.valueOf(map.get("nextScriptNo"));
+
+                    if (score == null) {
+                        score = Double.valueOf(map.get("score"));
+                    } else {
+                        score = score + Double.valueOf(map.get("score"));
                     }
 
-                } else {
-                    SvyTaskTemplateScriptVO svyTaskTemplateScriptVO = svyTaskTemplateScriptVOS.get(i);
-//                    SvyTaskTemplateScriptVO svyTaskTemplateScriptVO = DtoConversionUtils.sourceToTarget(svyLibTemplateScriptVOS.get(i), SvyTaskTemplateScriptVO.class);
-                    List<SvyTaskTemplateTargetoption> svyTaskTemplateTargetoptions = svyTaskTemplateScriptVOS.get(i).getSvyTaskTemplateTargetoptions();
-//                    List<SvyTaskTemplateTargetoption> svyTaskTemplateTargetoptions = DtoConversionUtils.sourceToTarget(svyLibTemplateScriptVOS.get(i).getSvyLibTemplateTargetoptions(), SvyTaskTemplateTargetoption.class);
-                    svyTaskTemplateScriptVO.setSvyTaskTemplateTargetoptions(svyTaskTemplateTargetoptions);
-
-                    if (svyTaskTemplateScriptVO.getSort() != null && svyTaskTemplateScriptVO.getSort().equals(nextScriptno)) {
-                        log.error("svyTaskTemplateScriptVO鐨勫�间负锛歿}", svyTaskTemplateScriptVO);
-                        Map<String, String> map = setWJInfo(svyTaskTemplateScriptVO, Long.valueOf(serviceSubTaskMYDAnswerReq.getTaskId()), patid, serviceSubTaskMYDAnswerReq.getSubId());
-                        log.error("map鐨勪俊鎭负锛歿}", map);
-                        if (!Objects.isNull(map.get("nextScriptNo")))
-                            nextScriptno = Long.valueOf(map.get("nextScriptNo"));
-                        if (score == null) {
-                            score = Double.valueOf(map.get("score"));
-                        } else {
-                            score = score + Double.valueOf(map.get("score"));
-                        }
-                        if (!Objects.isNull(map.get("id"))) {
-                            idList.add(Long.valueOf(map.get("id")));
-                        }
+                    if (!Objects.isNull(map.get("id"))) {
+                        idList.add(Long.valueOf(map.get("id")));
                     }
                 }
             }
@@ -739,6 +741,13 @@
     public Map<String, Object> selectPatQuestionResult(ServiceSubTaskQueryReq serviceSubTaskQueryReq) {
         log.info("selectPatQuesTionResult鐨勫叆鍙備负锛歿}", serviceSubTaskQueryReq);
         Map<String, Object> map = new HashMap<>();
+        if (serviceSubTaskQueryReq.getSubId() != null) {
+            ServiceSubtask serviceSubtask = serviceSubtaskMapper.selectServiceSubtaskById(serviceSubTaskQueryReq.getSubId());
+            if (serviceSubtask.getSendstate() == 4L) {
+                log.info("鎮h�呭瓙浠诲姟ID涓猴細{}锛岀姸鎬佷负锛歿}", serviceSubTaskQueryReq.getSubId(), serviceSubtask.getSendstate());
+                throw new BaseException("璇ヤ换鍔′笉鎵ц");
+            }
+        }
 
         //鑾峰彇鏈鐨勯棶绛旂粨鏋�
         Map<String, Object> scriptInfoByCondition = serviceTaskService.getScriptInfoByCondition(serviceSubTaskQueryReq.getTaskid(), serviceSubTaskQueryReq.getPatid(), serviceSubTaskQueryReq.getIsFinish(), serviceSubTaskQueryReq.getPatfrom(), serviceSubTaskQueryReq.getSubId());
@@ -1335,4 +1344,127 @@
         map.put("isabnormal", isabnormalFlag);
         return map;
     }
+
+    /**
+     * 妫�鏌ュ苟淇闂嵎棰樼洰鎺掑簭(濡傛灉鏍规嵁閫夐」鏃犲簭璺宠浆锛屽氨浼氬鑷撮敊璇�)
+     *
+     * @param svyTaskTemplateScriptVOList 闂嵎棰樼洰 VO 鍒楄〃
+     * @return 淇鍚庣殑闂嵎棰樼洰鍒楄〃
+     */
+    public List<SvyTaskTemplateScriptVO> checkQuestionSort(List<SvyTaskTemplateScriptVO> svyTaskTemplateScriptVOList) {
+        if (CollectionUtils.isEmpty(svyTaskTemplateScriptVOList)) {
+            return svyTaskTemplateScriptVOList;
+        }
+
+        // 鎸夊綋鍓� sort 鎺掑簭
+        svyTaskTemplateScriptVOList.sort(Comparator.comparingLong(SvyTaskTemplateScriptVO::getSort));
+
+        // 鍏堟鏌ヤ竴涓嬫槸鍚﹀凡缁忔槸姝g‘鐨勯『搴忥細杩炵画鐨勫簭鍙� 1,2,3... 涓旀渶鍚庝竴涓棶棰樼殑 nextScriptno 涓� "0"
+        boolean isCorrect = true;
+        int expectedValue = 1;
+
+        for (int i = 0; i < svyTaskTemplateScriptVOList.size(); i++) {
+            SvyTaskTemplateScriptVO vo = svyTaskTemplateScriptVOList.get(i);
+
+            // 妫�鏌� sort 鏄惁杩炵画
+            if (!Objects.equals(expectedValue, vo.getSort())) {
+                isCorrect = false;
+                break;
+            }
+
+            // 濡傛灉涓嶆槸鏈�鍚庝竴涓棶棰橈紝妫�鏌� nextScriptno 鏄惁鏄笅涓�棰樼殑搴忓彿
+            if (i < svyTaskTemplateScriptVOList.size() - 1) {
+                String expectedNextScriptNo = String.valueOf(expectedValue + 1);
+                if (!expectedNextScriptNo.equals(vo.getNextScriptno())) {
+                    isCorrect = false;
+                    break;
+                }
+            } else {
+                // 鏈�鍚庝竴涓棶棰樼殑 nextScriptno 蹇呴』鏄� "0"
+                if (!"0".equals(vo.getNextScriptno())) {
+                    isCorrect = false;
+                    break;
+                }
+
+                // 妫�鏌ユ渶鍚庝竴涓棶棰樼殑鎵�鏈夐�夐」鐨� nextQuestion 鏄惁涓� 0
+                if (CollectionUtils.isNotEmpty(vo.getSvyTaskTemplateTargetoptions())) {
+                    for (SvyTaskTemplateTargetoption option : vo.getSvyTaskTemplateTargetoptions()) {
+                        if (option.getNextQuestion() == null || !option.getNextQuestion().equals(0)) {
+                            isCorrect = false;
+                            break;
+                        }
+                    }
+                }
+            }
+
+            expectedValue++;
+        }
+
+        // 濡傛灉宸茬粡鏄纭殑锛岀洿鎺ヨ繑鍥烇紝涓嶅啀杩涜鏁版嵁搴撴洿鏂�
+        if (isCorrect) {
+            log.debug("闂嵎棰樼洰鎺掑簭姝g‘锛屾棤闇�淇锛屽叡 {} 涓鐩�", svyTaskTemplateScriptVOList.size());
+            return svyTaskTemplateScriptVOList;
+        }
+
+        log.info("妫�娴嬪埌闂嵎棰樼洰闇�瑕佷慨姝o紝寮�濮嬪鐞�...");
+
+        // 閲嶆柊鍒嗛厤杩炵画鐨� sort 鍊硷細1, 2, 3...
+        int newSortValue = 1;
+        List<SvyTaskTemplateScriptVO> updatedList = new ArrayList<>();
+
+        for (int i = 0; i < svyTaskTemplateScriptVOList.size(); i++) {
+            SvyTaskTemplateScriptVO vo = svyTaskTemplateScriptVOList.get(i);
+            Long oldSort = vo.getSort();
+
+            // 濡傛灉 sort 涓嶆槸棰勬湡鐨勮繛缁�硷紝鍒欓渶瑕佹洿鏂�
+            if (!Objects.equals(newSortValue, oldSort)) {
+                log.warn("妫�娴嬪埌棰樼洰搴忓彿涓嶈繛缁紝棰樼洰 ID: {}锛屽師 sort: {}, 鏂� sort: {}", vo.getId(), oldSort, newSortValue);
+                vo.setSort((long) newSortValue);
+            }
+
+            // 澶勭悊涓嬩竴涓棶棰樼殑 nextScriptno锛氶櫎鏈�鍚庝竴涓锛宯extScriptno = 褰撳墠 sort + 1
+            if (i < svyTaskTemplateScriptVOList.size() - 1) {
+                String expectedNextScriptNo = String.valueOf(newSortValue + 1);
+                String currentNextScriptNo = vo.getNextScriptno();
+                if (!Objects.equals(expectedNextScriptNo, currentNextScriptNo)) {
+                    log.warn("妫�娴嬪埌 nextScriptno 涓嶆纭紝棰樼洰 ID: {}锛屽師 nextScriptno: {}, 鏂� nextScriptno: {}", vo.getId(), currentNextScriptNo, expectedNextScriptNo);
+                    vo.setNextScriptno(expectedNextScriptNo);
+                }
+            } else {
+                // 鏈�鍚庝竴涓棶棰樼殑 nextScriptno 蹇呴』鏄� "0"
+                String currentNextScriptNo = vo.getNextScriptno();
+                if (!"0".equals(currentNextScriptNo)) {
+                    log.warn("妫�娴嬪埌鏈�鍚庝竴涓鐩殑 nextScriptno 搴斾负 0锛岄鐩� ID: {}锛屽師 nextScriptno: {}", vo.getId(), currentNextScriptNo);
+                    vo.setNextScriptno("0");
+                }
+            }
+
+            // 澶勭悊姣忎釜閫夐」鐨� nextQuestion
+            if (CollectionUtils.isNotEmpty(vo.getSvyTaskTemplateTargetoptions())) {
+                // 鍒ゆ柇鏄惁鏄渶鍚庝竴涓棶棰�
+                boolean isLastQuestion = (i == svyTaskTemplateScriptVOList.size() - 1);
+                int expectedNextQuestion = isLastQuestion ? 0 : newSortValue;
+
+                for (SvyTaskTemplateTargetoption option : vo.getSvyTaskTemplateTargetoptions()) {
+                    Integer optionNextQuestion = option.getNextQuestion();
+                    int finalNextQuestion = expectedNextQuestion;
+
+                    if (!isLastQuestion && (optionNextQuestion == null || !optionNextQuestion.equals(newSortValue))) {
+                        log.info("妫�娴嬪埌閫夐」鐨� nextQuestion 涓嶆纭紝閫夐」 ID: {}锛岄鐩� ID: {}锛屽師 nextQuestion: {}, 鏂� nextQuestion: {}", option.getId(), vo.getId(), optionNextQuestion, newSortValue);
+                        finalNextQuestion = newSortValue;
+                    } else if (isLastQuestion && (optionNextQuestion == null || !optionNextQuestion.equals(0))) {
+                        log.info("妫�娴嬪埌鏈�鍚庝竴涓棶棰樼殑閫夐」 nextQuestion 搴斾负 0锛岄�夐」 ID: {}锛岄鐩� ID: {}锛屽師 nextQuestion: {}, 鏂� nextQuestion: {}", option.getId(), vo.getId(), optionNextQuestion, 0);
+                    }
+
+                    option.setNextQuestion(finalNextQuestion);
+                }
+            }
+
+            updatedList.add(vo);
+            newSortValue++;
+        }
+
+        return updatedList;
+    }
+
 }

--
Gitblit v1.9.3