From 33e1839c6214ad72cc5a76ae254ce0bb542e9263 Mon Sep 17 00:00:00 2001
From: liusheng <337615773@qq.com>
Date: 星期六, 10 十月 2026 09:31:11 +0800
Subject: [PATCH] 提交

---
 smartor/src/main/java/com/smartor/service/impl/ServiceSubtaskDetailTraceServiceImpl.java |  186 +++++++++++++++++++++++++++++++---------------
 1 files changed, 126 insertions(+), 60 deletions(-)

diff --git a/smartor/src/main/java/com/smartor/service/impl/ServiceSubtaskDetailTraceServiceImpl.java b/smartor/src/main/java/com/smartor/service/impl/ServiceSubtaskDetailTraceServiceImpl.java
index b19a590..985cbbb 100644
--- a/smartor/src/main/java/com/smartor/service/impl/ServiceSubtaskDetailTraceServiceImpl.java
+++ b/smartor/src/main/java/com/smartor/service/impl/ServiceSubtaskDetailTraceServiceImpl.java
@@ -80,78 +80,115 @@
 
     @Override
     public Map<String, Object> tracedeallist(DetailTraceDealVO detailTraceDealVO) {
-        // 鍒嗛〉鏌ヨ
-        List<ServiceSubtaskDetailTrace> tracedeallist = serviceSubtaskDetailTraceMapper.tracedeallist(detailTraceDealVO);
-
-        // 鎸� questiontext 鍒嗙粍
-        Map<String, List<ServiceSubtaskDetailTrace>> groupByQuestion = tracedeallist.stream().filter(t -> t.getQuestiontext() != null).collect(Collectors.groupingBy(ServiceSubtaskDetailTrace::getQuestiontext));
+        // 浼樺寲鍓嶏細鈶� 杩欐潯 SQL 娌℃湁 limit锛屾瘡娆¢兘鎶婃暣琛� trace锛堝惈 patdesc/recordurl 澶у瓧娈碉級鎷夊洖 Java 鍐嶅垎缁勶紱
+        //        鈶� 姣忎釜闂閮芥嬁 scriptid 鍘� service_subtask_detail 鏌ヤ竴娆″叏琛ㄥ彧涓哄彇 size锛圢+1锛夛紱
+        //        鈶� controller 鎶婅繖涓柟娉曡皟浜嗕袱閬嶏紙绗簩閬嶄负浜嗘�绘暟锛夛紝绛変簬涓婇潰涓ゆ鍚勮窇涓ゆ銆�
+        // 浼樺寲鍚庯細鍥哄畾鐨� 5 鏉¤仛鍚� SQL锛孌B 鍙鐞嗗綋鍓嶉〉鐨勯鐩��
+        //  1) 褰撳墠椤电殑闂鍒嗙粍鑱氬悎锛堝垎椤典笅鎺ㄥ埌 SQL锛�
+        List<Map<String, Object>> groupRows = serviceSubtaskDetailTraceMapper.selectTraceDealGroups(detailTraceDealVO);
+        //  2) 闂鎬绘暟锛堝垎椤� total锛屽叏閲忓彛寰勶級
+        long questionTotal = serviceSubtaskDetailTraceMapper.countTraceDealQuestions(detailTraceDealVO);
+        //  3) 姹囨�荤粺璁★紙鍏ㄩ噺鍙e緞锛屼笉鍙楀垎椤靛奖鍝嶏級
+        Map<String, Object> sumRow = serviceSubtaskDetailTraceMapper.sumTraceDeal(detailTraceDealVO);
+        long totalException = toLong(sumRow == null ? null : sumRow.get("totalException"), 0L);
+        long yesDealException = toLong(sumRow == null ? null : sumRow.get("yesDeal"), 0L);
+        long noDealException = totalException - yesDealException;
 
         List<DetailTraceDealDTO> detailTraceDealDTOList = new ArrayList<>();
-        long totalException = 0L;
-        long noDealException = 0L;
-        long yesDealException = 0L;
+        if (CollectionUtils.isNotEmpty(groupRows)) {
+            // 鏈〉娑夊強鐨勯棶棰樻枃鏈紝鍚庨潰 3 鏉℃煡璇㈤兘鎸夊畠杩囨护
+            List<String> questiontexts = groupRows.stream()
+                    .map(row -> row.get("questiontext"))
+                    .filter(Objects::nonNull)
+                    .map(String::valueOf)
+                    .distinct()
+                    .collect(Collectors.toList());
 
-        for (Map.Entry<String, List<ServiceSubtaskDetailTrace>> entry : groupByQuestion.entrySet()) {
-            List<ServiceSubtaskDetailTrace> group = entry.getValue();
-            DetailTraceDealDTO dto = new DetailTraceDealDTO();
+            //  4) 鏈〉闂鐨勮礋璐g瀹わ紙鎸夌瀹ょ紪鍙峰幓閲嶏級
+            Map<String, List<Map<String, Object>>> deptMap = new HashMap<>();
+            //  5) 鏈〉闂鐨勬渶鏂板鐞嗕汉/澶勭悊鏃堕棿
+            Map<String, Map<String, Object>> latestHandleMap = new HashMap<>();
+            if (CollectionUtils.isNotEmpty(questiontexts)) {
+                for (Map<String, Object> row : serviceSubtaskDetailTraceMapper.selectTraceDealDepts(detailTraceDealVO, questiontexts)) {
+                    String questiontext = row.get("questiontext") == null ? null : String.valueOf(row.get("questiontext"));
+                    if (questiontext == null) continue;
+                    Map<String, Object> deptMapItem = new HashMap<>();
+                    deptMapItem.put("deptName", row.get("deptName"));
+                    deptMapItem.put("deptCode", row.get("deptCode"));
+                    deptMap.computeIfAbsent(questiontext, key -> new ArrayList<>()).add(deptMapItem);
+                }
 
-            // 闂鍐呭
-            dto.setQuestiontext(entry.getKey());
-            if (CollectionUtils.isNotEmpty(group)) {
-                dto.setScriptid(group.get(0).getScriptid());
-                dto.setTemplateType(group.get(0).getTemplateType());
-            }
-            // 璐熻矗绉戝锛堝幓閲嶏級
-            List<Map<String, Object>> responsibleDept = group.stream().filter(t -> t.getTodeptcode() != null).collect(Collectors.collectingAndThen(Collectors.toMap(ServiceSubtaskDetailTrace::getTodeptcode, t -> {
-                Map<String, Object> deptMap = new HashMap<>();
-                deptMap.put("deptName", t.getTodeptname());
-                deptMap.put("deptCode", t.getTodeptcode());
-                return deptMap;
-            }, (existing, replacement) -> existing), map -> new ArrayList<>(map.values())));
-            dto.setResponsibleDept(responsibleDept);
-
-            // 鏈夋晥濉啓鏁帮細閫氳繃缁勫唴鐨� scriptid 鍘� service_subtask_detail 琛ㄦ煡璇�
-            List<Long> scriptIds = group.stream().map(ServiceSubtaskDetailTrace::getScriptid).filter(Objects::nonNull).distinct().collect(Collectors.toList());
-            long effectiveFillNum = 0L;
-            if (CollectionUtils.isNotEmpty(scriptIds)) {
-                ServiceSubtaskDetail query = new ServiceSubtaskDetail();
-                query.setDelFlag("0");
-                for (Long scriptId : scriptIds) {
-                    query.setScriptid(scriptId);
-                    List<ServiceSubtaskDetail> details = serviceSubtaskDetailMapper.selectServiceSubtaskDetailList(query);
-                    effectiveFillNum += CollectionUtils.isEmpty(details) ? 0 : details.size();
+                for (Map<String, Object> row : serviceSubtaskDetailTraceMapper.selectTraceDealLatestHandle(detailTraceDealVO, questiontexts)) {
+                    String questiontext = row.get("questiontext") == null ? null : String.valueOf(row.get("questiontext"));
+                    if (questiontext == null) continue;
+                    // 鍚屼竴涓渶澶у鐞嗘椂闂村彲鑳芥湁澶氭潯锛屼繚鐣欑涓�鏉″嵆鍙紙鍘熷疄鐜板彇缁勫唴 handle_time 鏈�澶х殑閭f潯锛�
+                    latestHandleMap.putIfAbsent(questiontext, row);
                 }
             }
 
-            // 寮傚父濉啓鏁� = 褰撳墠鍒嗙粍鐨� List 鏁伴噺
-            long exceptionFillNum = group.size();
+            //  6) 鏈〉闂鐨勬湁鏁堝~鍐欐暟锛氫竴娆� group by 鏌ュ洖锛屾浛浠i�愪釜 scriptid 鏌ヨ
+            // 涓�涓棶棰樺彲鑳藉搴斿涓� scriptid锛屾墍浠ュ厛鎶� questiontext -> scriptid 鍒楄〃瑙e紑锛�
+            // 鍐嶆妸璇ラ棶棰樻墍鏈� scriptid 鐨勫~鍐欐暟鐩稿姞锛堜笌鍘熷疄鐜� for 寰幆绱姞鐨勫彛寰勪竴鑷达級
+            Map<String, List<Long>> questionScriptIds = new LinkedHashMap<>();
+            Set<Long> scriptIdSet = new LinkedHashSet<>();
+            for (Map<String, Object> row : groupRows) {
+                String questiontext = row.get("questiontext") == null ? null : String.valueOf(row.get("questiontext"));
+                if (questiontext == null) continue;
+                List<Long> ids = parseScriptIds(row.get("scriptids"));
+                questionScriptIds.put(questiontext, ids);
+                scriptIdSet.addAll(ids);
+            }
+            Map<Long, Long> effectiveFillMap = new HashMap<>();
+            if (CollectionUtils.isNotEmpty(scriptIdSet)) {
+                for (Map<String, Object> row : serviceSubtaskDetailMapper.countDetailByScriptIds(new ArrayList<>(scriptIdSet))) {
+                    Long scriptId = toLongObj(row.get("scriptid"));
+                    if (scriptId == null) continue;
+                    effectiveFillMap.put(scriptId, toLong(row.get("cnt"), 0L));
+                }
+            }
 
-            Map<String, Object> fillSituation = new HashMap<>();
-            fillSituation.put("effectiveFillNum", effectiveFillNum);
-            fillSituation.put("exceptionFillNum", exceptionFillNum);
-            dto.setFillSituation(fillSituation);
+            //  7) 缁勮杩斿洖缁撴瀯锛堥『搴� = SQL 鐨� order by questiontext锛屼繚璇佸垎椤电ǔ瀹氾級
+            for (Map<String, Object> row : groupRows) {
+                String questiontext = row.get("questiontext") == null ? null : String.valueOf(row.get("questiontext"));
+                if (questiontext == null) continue;
 
-            // 寮傚父闂澶勭悊鏁伴噺锛氫粠 handleFlag 缁熻
-            long yesDeal = group.stream().filter(t -> "1".equals(t.getHandleFlag())).count();
-            long noDeal = group.stream().filter(t -> !"1".equals(t.getHandleFlag())).count();
-            Map<String, Object> exceptionQuesNum = new HashMap<>();
-            exceptionQuesNum.put("yesDeal", yesDeal);
-            exceptionQuesNum.put("noDeal", noDeal);
-            exceptionQuesNum.put("all", exceptionFillNum);
-            dto.setExceptionQuesNum(exceptionQuesNum);
+                DetailTraceDealDTO dto = new DetailTraceDealDTO();
+                dto.setQuestiontext(questiontext);
+                Long scriptId = toLongObj(row.get("scriptid"));
+                dto.setScriptid(scriptId);
+                dto.setScriptids(parseScriptIds(row.get("scriptids")));
+                Object templateType = row.get("templateType");
+                dto.setTemplateType(templateType == null ? null : Integer.valueOf(String.valueOf(templateType)));
+                dto.setResponsibleDept(deptMap.getOrDefault(questiontext, new ArrayList<>()));
 
-            // 姹囨�荤粺璁$疮鍔�
-            totalException += exceptionFillNum;
-            noDealException += noDeal;
-            yesDealException += yesDeal;
+                long exceptionFillNum = toLong(row.get("exceptionFillNum"), 0L);
+                // 璇ラ棶棰樹笅鎵�鏈� scriptid 鐨勬湁鏁堝~鍐欐暟涔嬪拰锛堜竴涓� questiontext 鍙兘瀵瑰簲澶氫釜 scriptid锛�
+                long effectiveFillNum = 0L;
+                for (Long sid : questionScriptIds.getOrDefault(questiontext, new ArrayList<>())) {
+                    effectiveFillNum += effectiveFillMap.getOrDefault(sid, 0L);
+                }
+                Map<String, Object> fillSituation = new HashMap<>();
+                fillSituation.put("effectiveFillNum", effectiveFillNum);
+                fillSituation.put("exceptionFillNum", exceptionFillNum);
+                dto.setFillSituation(fillSituation);
 
-            // 鏈�鏂板鐞嗕汉鍜屽鐞嗘椂闂达紙鍙� handleTime 鏈�鏂扮殑涓�鏉★級
-            group.stream().filter(t -> t.getHandleTime() != null).max(Comparator.comparing(ServiceSubtaskDetailTrace::getHandleTime)).ifPresent(latest -> {
-                dto.setHandleBy(latest.getHandleBy());
-                dto.setHandleTime(latest.getHandleTime());
-            });
+                long yesDeal = toLong(row.get("yesDeal"), 0L);
+                long noDeal = toLong(row.get("noDeal"), 0L);
+                Map<String, Object> exceptionQuesNum = new HashMap<>();
+                exceptionQuesNum.put("yesDeal", yesDeal);
+                exceptionQuesNum.put("noDeal", noDeal);
+                exceptionQuesNum.put("all", exceptionFillNum);
+                dto.setExceptionQuesNum(exceptionQuesNum);
 
-            detailTraceDealDTOList.add(dto);
+                Map<String, Object> latestHandle = latestHandleMap.get(questiontext);
+                if (latestHandle != null) {
+                    dto.setHandleBy(latestHandle.get("handleBy") == null ? null : String.valueOf(latestHandle.get("handleBy")));
+                    Object handleTime = latestHandle.get("handleTime");
+                    dto.setHandleTime(handleTime instanceof Date ? (Date) handleTime : null);
+                }
+
+                detailTraceDealDTOList.add(dto);
+            }
         }
 
         Map<String, Object> result = new HashMap<>();
@@ -159,9 +196,38 @@
         result.put("totalException", totalException);
         result.put("noDealException", noDealException);
         result.put("yesDealException", yesDealException);
+        // 鍒嗛〉鎬绘暟锛堟寜闂缁村害锛夛紝controller 鐩存帴鐢ㄥ畠锛屼笉鐢ㄥ啀鍏ㄩ噺鏌ヤ竴閬�
+        result.put("questionTotal", questionTotal);
         return result;
     }
 
+    private static Long toLongObj(Object value) {
+        if (value == null) return null;
+        if (value instanceof Number) return ((Number) value).longValue();
+        String text = String.valueOf(value).trim();
+        return text.isEmpty() ? null : Long.valueOf(text);
+    }
+
+    private static long toLong(Object value, long defaultValue) {
+        Long result = toLongObj(value);
+        return result == null ? defaultValue : result;
+    }
+
+    /**
+     * 瑙f瀽 group_concat(distinct scriptid) 鐨勭粨鏋滐紙濡� "101,102"锛変负 scriptid 鍒楄〃
+     */
+    private static List<Long> parseScriptIds(Object value) {
+        List<Long> scriptIds = new ArrayList<>();
+        if (value == null) return scriptIds;
+        for (String item : String.valueOf(value).split(",")) {
+            Long scriptId = toLongObj(item);
+            if (scriptId != null && !scriptIds.contains(scriptId)) {
+                scriptIds.add(scriptId);
+            }
+        }
+        return scriptIds;
+    }
+
     /**
      * 鏂板闅忚浠诲姟寮傚父璁板綍璺熻釜
      *

--
Gitblit v1.9.3