From cb58722ca8b5ac0e844c20ae001c763e4773f149 Mon Sep 17 00:00:00 2001
From: 陈昶聿 <chychen@nbjetron.com>
Date: 星期五, 18 九月 2026 16:48:27 +0800
Subject: [PATCH] 【市一】统计加上成功和总数

---
 smartor/src/main/java/com/smartor/service/impl/ServiceSubtaskServiceImpl.java |  319 +++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 258 insertions(+), 61 deletions(-)

diff --git a/smartor/src/main/java/com/smartor/service/impl/ServiceSubtaskServiceImpl.java b/smartor/src/main/java/com/smartor/service/impl/ServiceSubtaskServiceImpl.java
index fb0add6..ade1e54 100644
--- a/smartor/src/main/java/com/smartor/service/impl/ServiceSubtaskServiceImpl.java
+++ b/smartor/src/main/java/com/smartor/service/impl/ServiceSubtaskServiceImpl.java
@@ -12,7 +12,6 @@
 import com.ruoyi.common.dx.MessageSend;
 import com.ruoyi.common.exception.base.BaseException;
 import com.ruoyi.common.utils.*;
-import com.ruoyi.common.core.service.IConfigService;
 import com.ruoyi.common.utils.http.HttpUtils;
 import com.ruoyi.common.utils.spring.SpringUtils;
 import com.ruoyi.system.domain.SysConfig;
@@ -23,7 +22,6 @@
 import com.smartor.common.ScheduleSubtaskBuilder;
 import com.smartor.config.PhoneUtils;
 import com.smartor.domain.DTO.QuestionCountKsOrBqDTO;
-import com.smartor.domain.DTO.QuestionResultDTO;
 import com.smartor.domain.DTO.ServiceSubtaskDetailDTO;
 import com.smartor.domain.*;
 import com.smartor.domain.VO.*;
@@ -34,9 +32,7 @@
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.collections4.MapUtils;
 import org.apache.commons.lang3.ObjectUtils;
-import org.checkerframework.checker.nullness.qual.NonNull;
 import org.junit.Test;
-import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.cache.annotation.Cacheable;
@@ -44,10 +40,8 @@
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
-import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 
-import java.io.IOException;
 import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.text.DecimalFormat;
@@ -207,12 +201,11 @@
 
     @Value("${spring.profiles.active}")
     private String active;
-    @Autowired
-    private PatMedOperationMapper patMedOperationMapper;
-    @Autowired
-    private PatMedOperationItemMapper patMedOperationItemMapper;
+
     @Autowired
     private ServiceOutPathMapper serviceOutPathMapper;
+
+    private static final String KEY_SEP = "\u0001";
 
     /**
      * 鏌ヨ鍗曚竴浠诲姟锛堥殢璁匡級
@@ -3345,6 +3338,24 @@
                             serviceSubtaskStatistic.setWeChat(serviceSubtaskStatistic.getWeChat() + 1L);
                         }
                     }
+
+                    //棣栨闅忚浜哄伐鎴愬姛
+                    if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate().equals(6L) && ObjectUtils.isNotEmpty(serviceSubtask.getCurrentPreachform()) && serviceSubtask.getCurrentPreachform().equals("1")) {
+                        serviceSubtaskStatistic.setManualSuccess(serviceSubtaskStatistic.getManualSuccess() + 1L);
+                    }
+                    //棣栨闅忚璇煶鎴愬姛
+                    if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate().equals(6L) && ObjectUtils.isNotEmpty(serviceSubtask.getCurrentPreachform()) && serviceSubtask.getCurrentPreachform().equals("3")) {
+                        serviceSubtaskStatistic.setVoiceSuccess(serviceSubtaskStatistic.getVoiceSuccess() + 1L);
+                    }
+                    //棣栨闅忚鐭俊鎴愬姛
+                    if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate().equals(6L) && ObjectUtils.isNotEmpty(serviceSubtask.getCurrentPreachform()) && serviceSubtask.getCurrentPreachform().equals("4")) {
+                        serviceSubtaskStatistic.setSmsSuccess(serviceSubtaskStatistic.getSmsSuccess() + 1L);
+                    }
+                    //棣栨闅忚寰俊鎴愬姛
+                    if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate().equals(6L) && ObjectUtils.isNotEmpty(serviceSubtask.getCurrentPreachform()) && serviceSubtask.getCurrentPreachform().equals("5")) {
+                        serviceSubtaskStatistic.setWeChatSuccess(serviceSubtaskStatistic.getWeChatSuccess() + 1L);
+                    }
+
                     if (serviceSubtaskStatistic.getNeedFollowUp() > 0) {
                         double rate = (double) (serviceSubtaskStatistic.getFollowUpSuccess() + serviceSubtaskStatistic.getFollowUpFail()) / serviceSubtaskStatistic.getNeedFollowUp();
                         serviceSubtaskStatistic.setFollowUpRate(percentFormat.format(rate));
@@ -3440,6 +3451,24 @@
                             serviceSubtaskStatistic.setWeChatAgain(serviceSubtaskStatistic.getWeChatAgain() + 1L);
                         }
                     }
+
+                    //鍐嶆闅忚浜哄伐鎴愬姛
+                    if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate().equals(6L) && ObjectUtils.isNotEmpty(serviceSubtask.getCurrentPreachform()) && serviceSubtask.getCurrentPreachform().equals("1")) {
+                        serviceSubtaskStatistic.setManualAgainSuccess(serviceSubtaskStatistic.getManualAgainSuccess() + 1L);
+                    }
+                    //鍐嶆闅忚璇煶鎴愬姛
+                    if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate().equals(6L) && ObjectUtils.isNotEmpty(serviceSubtask.getCurrentPreachform()) && serviceSubtask.getCurrentPreachform().equals("3")) {
+                        serviceSubtaskStatistic.setVoiceAgainSuccess(serviceSubtaskStatistic.getVoiceAgainSuccess() + 1L);
+                    }
+                    //鍐嶆闅忚鐭俊鎴愬姛
+                    if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate().equals(6L) && ObjectUtils.isNotEmpty(serviceSubtask.getCurrentPreachform()) && serviceSubtask.getCurrentPreachform().equals("4")) {
+                        serviceSubtaskStatistic.setSmsAgainSuccess(serviceSubtaskStatistic.getSmsAgainSuccess() + 1L);
+                    }
+                    //鍐嶆闅忚寰俊鎴愬姛
+                    if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate().equals(6L) && ObjectUtils.isNotEmpty(serviceSubtask.getCurrentPreachform()) && serviceSubtask.getCurrentPreachform().equals("5")) {
+                        serviceSubtaskStatistic.setWeChatAgainSuccess(serviceSubtaskStatistic.getWeChatAgainSuccess() + 1L);
+                    }
+
                     if (serviceSubtaskStatistic.getNeedFollowUp() > 0) {
                         double rate = (double) (serviceSubtaskStatistic.getFollowUpSuccessAgain() + serviceSubtaskStatistic.getFollowUpFailAgain()) / serviceSubtaskStatistic.getNeedFollowUpAgain();
                         serviceSubtaskStatistic.setFollowUpRateAgain(percentFormat.format(rate));
@@ -3594,6 +3623,24 @@
                         serviceSubtaskStatistic.setWeChat(serviceSubtaskStatistic.getWeChat() + 1L);
                     }
                 }
+
+                //棣栨闅忚浜哄伐鎴愬姛
+                if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate().equals(6L) && ObjectUtils.isNotEmpty(serviceSubtask.getCurrentPreachform()) && serviceSubtask.getCurrentPreachform().equals("1")) {
+                    serviceSubtaskStatistic.setManualSuccess(serviceSubtaskStatistic.getManualSuccess() + 1L);
+                }
+                //棣栨闅忚璇煶鎴愬姛
+                if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate().equals(6L) && ObjectUtils.isNotEmpty(serviceSubtask.getCurrentPreachform()) && serviceSubtask.getCurrentPreachform().equals("3")) {
+                    serviceSubtaskStatistic.setVoiceSuccess(serviceSubtaskStatistic.getVoiceSuccess() + 1L);
+                }
+                //棣栨闅忚鐭俊鎴愬姛
+                if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate().equals(6L) && ObjectUtils.isNotEmpty(serviceSubtask.getCurrentPreachform()) && serviceSubtask.getCurrentPreachform().equals("4")) {
+                    serviceSubtaskStatistic.setSmsSuccess(serviceSubtaskStatistic.getSmsSuccess() + 1L);
+                }
+                //棣栨闅忚寰俊鎴愬姛
+                if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate().equals(6L) && ObjectUtils.isNotEmpty(serviceSubtask.getCurrentPreachform()) && serviceSubtask.getCurrentPreachform().equals("5")) {
+                    serviceSubtaskStatistic.setWeChatSuccess(serviceSubtaskStatistic.getWeChatSuccess() + 1L);
+                }
+
                 if (serviceSubtaskStatistic.getNeedFollowUp() > 0) {
                     double rate = (double) (serviceSubtaskStatistic.getFollowUpSuccess() + serviceSubtaskStatistic.getFollowUpFail()) / serviceSubtaskStatistic.getNeedFollowUp();
                     serviceSubtaskStatistic.setFollowUpRate(percentFormat.format(rate));
@@ -3745,6 +3792,24 @@
                         serviceSubtaskStatistic.setWeChatAgain(serviceSubtaskStatistic.getWeChatAgain() + 1L);
                     }
                 }
+
+                //鍐嶆闅忚浜哄伐鎴愬姛
+                if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate().equals(6L) && ObjectUtils.isNotEmpty(serviceSubtask.getCurrentPreachform()) && serviceSubtask.getCurrentPreachform().equals("1")) {
+                    serviceSubtaskStatistic.setManualAgainSuccess(serviceSubtaskStatistic.getManualAgainSuccess() + 1L);
+                }
+                //鍐嶆闅忚璇煶鎴愬姛
+                if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate().equals(6L) && ObjectUtils.isNotEmpty(serviceSubtask.getCurrentPreachform()) && serviceSubtask.getCurrentPreachform().equals("3")) {
+                    serviceSubtaskStatistic.setVoiceAgainSuccess(serviceSubtaskStatistic.getVoiceAgainSuccess() + 1L);
+                }
+                //鍐嶆闅忚鐭俊鎴愬姛
+                if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate().equals(6L) && ObjectUtils.isNotEmpty(serviceSubtask.getCurrentPreachform()) && serviceSubtask.getCurrentPreachform().equals("4")) {
+                    serviceSubtaskStatistic.setSmsAgainSuccess(serviceSubtaskStatistic.getSmsAgainSuccess() + 1L);
+                }
+                //鍐嶆闅忚寰俊鎴愬姛
+                if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate().equals(6L) && ObjectUtils.isNotEmpty(serviceSubtask.getCurrentPreachform()) && serviceSubtask.getCurrentPreachform().equals("5")) {
+                    serviceSubtaskStatistic.setWeChatAgainSuccess(serviceSubtaskStatistic.getWeChatAgainSuccess() + 1L);
+                }
+
                 if (serviceSubtaskStatistic.getNeedFollowUp() > 0) {
                     double rate = (double) (serviceSubtaskStatistic.getFollowUpSuccessAgain() + serviceSubtaskStatistic.getFollowUpFailAgain()) / serviceSubtaskStatistic.getNeedFollowUpAgain();
                     serviceSubtaskStatistic.setFollowUpRateAgain(percentFormat.format(rate));
@@ -4082,12 +4147,15 @@
         iServiceOutPathService.insertServiceOutPath(serviceOutPath);
         //杞垚16杩涘埗
         String format = String.format("%03X", serviceOutPath.getId());
+        String url = "";
+        SendMagParam sendMagParam = new SendMagParam();
+        url = localIP + ":" + req_path + "/sf?p=" + format;
+        sendMagParam.setUrl(url);
+        sendMagParam.setContent("銆愭柊鍗庡尰闄€�戞偍濂斤紝閭�璇锋偍濉啓鍑洪櫌闅忚璋冩煡琛紝璇风偣鍑�" + sendMagParam.getUrl() + "濉啓銆傛劅璋㈡偍閰嶅悎锛�");
         serviceOutPath.setRadix(format);
         serviceOutPath.setUpdateTime(new Date());
+        serviceOutPath.setUrl(url);
         iServiceOutPathService.updateServiceOutPath(serviceOutPath);
-        SendMagParam sendMagParam = new SendMagParam();
-        sendMagParam.setUrl(localIP + ":" + req_path + "/sf?p=" + format);
-        sendMagParam.setContent("銆愭柊鍗庡尰闄€�戞偍濂斤紝閭�璇锋偍濉啓鍑洪櫌闅忚璋冩煡琛紝璇风偣鍑�" + sendMagParam.getUrl() + "濉啓銆傛劅璋㈡偍閰嶅悎锛�");
         Map<String, String> map = new HashMap<>();
         map.put("phone", serviceSubtask.getPhone());
         map.put("content", sendMagParam.getContent());
@@ -4172,6 +4240,10 @@
      * voiceInfo - visitCount = 1 && currentPreachForm = "3"
      * smsInfo - visitCount = 1 && currentPreachForm = "4"
      * wechatInfo - visitCount = 1 && currentPreachForm = "5"
+     * manualSuccessInfo - visitCount = 1 && currentPreachForm = "1" && sendstate = 6
+     * voiceSuccessInfo - visitCount = 1 && currentPreachForm = "3" && sendstate = 6
+     * smsSuccessInfo - visitCount = 1 && currentPreachForm = "4" && sendstate = 6
+     * wechatSuccessInfo - visitCount = 1 && currentPreachForm = "5" && sendstate = 6
      * abnormalInfo - visitCount = 1 && excep = "1"
      * needFollowUpAgainInfo - visitCount > 1 && sendstate != 4
      * pendingFollowUpAgainInfo -  visitCount > 1 && sendstate = 2
@@ -4181,6 +4253,10 @@
      * voiceAgainInfo - visitCount > 1 && currentPreachForm = "3"
      * smsAgainInfo - visitCount > 1 && currentPreachForm = "4"
      * wechatAgainInfo -  visitCount > 1 && currentPreachForm = "5"
+     * manualAgainSuccessInfo - visitCount > 1 && currentPreachForm = "1" && sendstate = 6
+     * voiceAgainSuccessInfo - visitCount > 1 && currentPreachForm = "3" && sendstate = 6
+     * smsAgainSuccessInfo - visitCount > 1 && currentPreachForm = "4" && sendstate = 6
+     * wechatAgainSuccessInfo - visitCount > 1&& currentPreachForm = "5" && sendstate = 6
      * abnormalAgainInfo- visitCount = 1 && excep = "1"
      * taskSituation1Info - taskSituation = 1
      * taskSituation2Info - taskSituation = 2
@@ -4264,6 +4340,26 @@
                 serviceSubtaskCountReq.setVisitCount(1);
                 serviceSubtaskCountReq.setCurrentPreachform("5");
                 break;
+            case "manualSuccessInfo":
+                serviceSubtaskCountReq.setVisitCount(1);
+                serviceSubtaskCountReq.setSendstates(new ArrayList<>(Collections.singletonList(6L)));
+                serviceSubtaskCountReq.setCurrentPreachform("1");
+                break;
+            case "voiceSuccessInfo":
+                serviceSubtaskCountReq.setVisitCount(1);
+                serviceSubtaskCountReq.setSendstates(new ArrayList<>(Collections.singletonList(6L)));
+                serviceSubtaskCountReq.setCurrentPreachform("3");
+                break;
+            case "smsSuccessInfo":
+                serviceSubtaskCountReq.setVisitCount(1);
+                serviceSubtaskCountReq.setSendstates(new ArrayList<>(Collections.singletonList(6L)));
+                serviceSubtaskCountReq.setCurrentPreachform("4");
+                break;
+            case "wechatSuccessInfo":
+                serviceSubtaskCountReq.setVisitCount(1);
+                serviceSubtaskCountReq.setSendstates(new ArrayList<>(Collections.singletonList(6L)));
+                serviceSubtaskCountReq.setCurrentPreachform("5");
+                break;
             case "abnormalInfo":
                 serviceSubtaskCountReq.setVisitCount(1);
                 serviceSubtaskCountReq.setExcep("1");
@@ -4298,6 +4394,26 @@
                 break;
             case "wechatAgainInfo":
                 serviceSubtaskCountReq.setVisitCount(2);
+                serviceSubtaskCountReq.setCurrentPreachform("5");
+                break;
+            case "manualAgainSuccessInfo":
+                serviceSubtaskCountReq.setVisitCount(1);
+                serviceSubtaskCountReq.setSendstates(new ArrayList<>(Collections.singletonList(6L)));
+                serviceSubtaskCountReq.setCurrentPreachform("1");
+                break;
+            case "voiceAgainSuccessInfo":
+                serviceSubtaskCountReq.setVisitCount(2);
+                serviceSubtaskCountReq.setSendstates(new ArrayList<>(Collections.singletonList(6L)));
+                serviceSubtaskCountReq.setCurrentPreachform("3");
+                break;
+            case "smsAgainSuccessInfo":
+                serviceSubtaskCountReq.setVisitCount(2);
+                serviceSubtaskCountReq.setSendstates(new ArrayList<>(Collections.singletonList(6L)));
+                serviceSubtaskCountReq.setCurrentPreachform("4");
+                break;
+            case "wechatAgainSuccessInfo":
+                serviceSubtaskCountReq.setVisitCount(2);
+                serviceSubtaskCountReq.setSendstates(new ArrayList<>(Collections.singletonList(6L)));
                 serviceSubtaskCountReq.setCurrentPreachform("5");
                 break;
             case "abnormalAgainInfo":
@@ -4973,15 +5089,17 @@
         serviceOutPath.setOrgid(serviceSubtask.getOrgid());
         iServiceOutPathService.insertServiceOutPath(serviceOutPath);
         String format = String.format("%03X", serviceOutPath.getId());
-        serviceOutPath.setRadix(format);
-        serviceOutPath.setUpdateTime(new Date());
-        iServiceOutPathService.updateServiceOutPath(serviceOutPath);
-
+        String url = "";
         SendMagParam sendMagParam = new SendMagParam();
         sendMagParam.setType("4");
         sendMagParam.setPhone(serviceSubtask.getPhone());
-        sendMagParam.setUrl(StringUtils.isNotEmpty(req_path) ? localIP + ":" + req_path + "/wt?p=" + format : localIP + "/wt?p=" + format);
+        url = StringUtils.isNotEmpty(req_path) ? localIP + ":" + req_path + "/wt?p=" + format : localIP + "/wt?p=" + format;
+        sendMagParam.setUrl(url);
 
+        serviceOutPath.setRadix(format);
+        serviceOutPath.setUpdateTime(new Date());
+        serviceOutPath.setUrl(url);
+        iServiceOutPathService.updateServiceOutPath(serviceOutPath);
         SysConfig sysConfig = new SysConfig();
 
         sysConfig = sysConfigMapper.checkConfigKeyUnique("wj.sms.template");
@@ -5057,48 +5175,78 @@
      * 鏍规嵁绉戝鎴栫梾鍖虹粺璁¢棶鍗烽�夐」(绉戝銆侀鐩煡璇㈢殑绉戝璇勫垎琛�)
      */
     @Override
-    public List<QuestionCountKsOrBqDTO> statQuestionOptionByKsOrBq(QuestionCountKsOrBqVO questionCountKsOrBqVO) {
-        List<QuestionCountKsOrBqDTO> questionCountKsOrBqDTOS = serviceSubtaskMapper.statQuestionOptionByKsOrBq(questionCountKsOrBqVO);
-
-        // 澶勭悊 targetvalue锛氭寜 & 鍒嗗壊鎴愬鏉¤褰�
-        List<QuestionCountKsOrBqDTO> result = new ArrayList<>();
-        for (QuestionCountKsOrBqDTO questionCountKsOrBqDTO : questionCountKsOrBqDTOS) {
-            if (StringUtils.isNotEmpty(questionCountKsOrBqDTO.getTargetvalue()) && questionCountKsOrBqDTO.getTargetvalue().contains("&")) {
-                String[] values = questionCountKsOrBqDTO.getTargetvalue().split("&");
-                // 濡傛灉鏈夊鏉$洰鏍囧�硷紝鎷嗗垎鎴愬琛�
-                for (String value : values) {
-                    //鍏堝幓 result 涓煡涓�涓嬶紝鏈夋病鏈夎繖涓� questiontext 鍜屽搴旂殑 targetvalue
-                    QuestionCountKsOrBqDTO existingExport = result.stream().filter(e -> e.getQeustionText().equals(questionCountKsOrBqDTO.getQeustionText()) && value.trim().equals(e.getTargetvalue())).findFirst().orElse(null);
-                    if (existingExport != null && existingExport.getAnswerCount().equals("0") && existingExport.getSingleScore().equals("0")) {
-                        //濡傛灉瀛樺湪锛屽垯灏� result 閲岀殑杩欎釜闂鎵�瀵瑰簲鐨� Targetvalue=value 杩欐潯鏁版嵁鏇存柊鎴愬綋鍓嶈繖鏉℃暟鎹�
-                        if (value.equals(questionCountKsOrBqDTO.getAsrtext())) {
-                            existingExport.setAsrtext(questionCountKsOrBqDTO.getAsrtext());
-                            existingExport.setAnswerCount(questionCountKsOrBqDTO.getAnswerCount());
-                            existingExport.setSingleScore(questionCountKsOrBqDTO.getSingleScore());
-                            existingExport.setAvgScore(questionCountKsOrBqDTO.getAvgScore());
-                        }
-                    } else if (existingExport != null && !existingExport.getAnswerCount().equals("0")) {
-                        continue;
-                    } else {
-                        QuestionCountKsOrBqDTO qcDto = DtoConversionUtils.sourceToTarget(questionCountKsOrBqDTO, QuestionCountKsOrBqDTO.class);
-                        qcDto.setTargetvalue(value.trim()); // targetvalue 鏄剧ず鍗曚釜閫夐」
-                        if (!value.equals(qcDto.getAsrtext())) {
-                            existingExport.setAsrtext(null);
-                            existingExport.setAnswerCount("0");
-                            existingExport.setSingleScore("0");
-                            existingExport.setAvgScore("0");
-                        }
-                        result.add(qcDto);
-                    }
-                }
-            } else {
-                // targetvalue 涓嶅寘鍚� &锛岀洿鎺ユ坊鍔犲埌缁撴灉
-                result.add(questionCountKsOrBqDTO);
-            }
+    public List<QuestionCountKsOrBqDTO> statQuestionOptionByKsOrBq(QuestionCountKsOrBqVO vo) {
+        List<QuestionCountKsOrBqDTO> sourceList = serviceSubtaskMapper.statQuestionOptionByKsOrBq(vo);
+        if (CollectionUtils.isEmpty(sourceList)) {
+            return Collections.emptyList();
         }
 
-        return result;
+        boolean byDept = vo.getLeaveldeptcodes() != null;
+        boolean byDistrict = !byDept && vo.getLeavehospitaldistrictcodes() != null;
+        if (!byDept && !byDistrict) {
+            return Collections.emptyList();
+        }
+
+        Map<String, QuestionCountKsOrBqDTO> map = new LinkedHashMap<>();
+
+        for (QuestionCountKsOrBqDTO dto : sourceList) {
+            if (StringUtils.isEmpty(dto.getTargetvalue())) {
+                continue;
+            }
+
+            String groupName = byDept ? dto.getDeptname() : dto.getLeavehospitaldistrictname();
+            if (StringUtils.isEmpty(groupName)) {
+                continue;
+            }
+
+            // 鍏抽敭锛氫袱杈归兘鐢� trimAll锛屽吋瀹瑰叏瑙掔┖鏍�
+            String asrtext = trimAll(dto.getAsrtext());
+            String questionKey = dto.getTaskid() + "\u0001" + trimAll(dto.getQeustionText());
+
+            for (String raw : dto.getTargetvalue().split("&")) {
+                String option = trimAll(raw);      // 鈫� 淇鐐癸細鐢� trimAll 鑰屼笉鏄� trim
+                if (option.isEmpty()) {
+                    continue;
+                }
+
+                String key = buildKey(groupName, option, questionKey);
+                QuestionCountKsOrBqDTO item = map.get(key);
+                if (item == null) {
+                    item = DtoConversionUtils.sourceToTarget(dto, QuestionCountKsOrBqDTO.class);
+                    item.setTargetvalue(option);       // 杈撳嚭鐨� targetvalue 灏辨槸褰撳墠閫夐」
+                    item.setAsrtext(option);
+                    item.setAnswerCount("0");
+                    map.put(key, item);
+                }
+
+                if (option.equals(asrtext)) {
+                    item.setAnswerCount(String.valueOf(parseCount(item.getAnswerCount()) + 1));
+                    item.setSingleScore(String.valueOf(new BigDecimal(item.getSingleScore()).add(new BigDecimal(dto.getSingleScore()))));
+                }
+            }
+        }
+        ArrayList<QuestionCountKsOrBqDTO> questionCountKsOrBqDTOS = new ArrayList<>(map.values());
+        for (QuestionCountKsOrBqDTO questionCountKsOrBqDTO : questionCountKsOrBqDTOS) {
+            String singleScore = questionCountKsOrBqDTO.getSingleScore();
+            String answerCount = questionCountKsOrBqDTO.getAnswerCount();
+
+            BigDecimal avg;
+            if (StringUtils.isEmpty(answerCount)
+                    || BigDecimal.ZERO.compareTo(new BigDecimal(answerCount.trim())) == 0) {
+                // 闄ゆ暟涓� 0锛岄伩鍏� / by zero
+                avg = BigDecimal.ZERO;
+            } else {
+                avg = new BigDecimal(singleScore)
+                        .divide(new BigDecimal(answerCount.trim()), 2, RoundingMode.HALF_UP);
+            }
+            questionCountKsOrBqDTO.setAvgScore(avg.toPlainString());
+
+        }
+
+        return questionCountKsOrBqDTOS;
     }
+
+
 
     @Override
     public List<ServiceSubtaskDetailRatioExport> statQuestionOptionBySubtaskIds(List<Long> subtaskIds, Date startOutHospTime, Date endOutHospTime, String scriptContent, Double scoreStart, Double scoreEnd, String valueType) {
@@ -5117,8 +5265,17 @@
                 // 濡傛灉鏈夊鏉$洰鏍囧�硷紝鎷嗗垎鎴愬琛�
                 for (String value : values) {
                     //鍏堝幓 result 涓煡涓�涓嬶紝鏈夋病鏈夎繖涓� questiontext 鍜屽搴旂殑 targetvalue
-                    ServiceSubtaskDetailRatioExport existingExport = result.stream().filter(e -> e.getQuestiontext().equals(export.getQuestiontext()) && value.trim().equals(e.getTargetvalue())).findFirst().orElse(null);
-                    if (existingExport != null && existingExport.getRatio().equals("0") && existingExport.getCount().equals("0")) {
+                    ServiceSubtaskDetailRatioExport existingExport = result.stream().filter(e -> {
+                        if (e == null || export == null) {
+                            return false;
+                        }
+                        String q1 = e.getQuestiontext() != null ? e.getQuestiontext() : "";
+                        String q2 = export.getQuestiontext() != null ? export.getQuestiontext() : "";
+                        String t1 = value.trim();
+                        String t2 = e.getTargetvalue() != null ? e.getTargetvalue() : "";
+                        return q1.equals(q2) && t1.equals(t2);
+                    }).findFirst().orElse(null);
+                    if (existingExport != null && "0".equals(existingExport.getRatio()) && "0".equals(existingExport.getCount())) {
                         //濡傛灉瀛樺湪锛屽垯灏� result 閲岀殑杩欎釜闂鎵�瀵瑰簲鐨� Targetvalue=value 杩欐潯鏁版嵁鏇存柊鎴愬綋鍓嶈繖鏉℃暟鎹�
                         if (value.equals(export.getOptionresult())) {
                             existingExport.setRatio(export.getRatio());
@@ -5126,7 +5283,7 @@
                             existingExport.setCount(export.getCount());
                             existingExport.setOptionresult(export.getOptionresult());
                         }
-                    } else if (existingExport != null && !existingExport.getRatio().equals("0")) {
+                    } else if (existingExport != null && !"0".equals(existingExport.getRatio())) {
                         continue;
                     } else {
                         ServiceSubtaskDetailRatioExport newExport = DtoConversionUtils.sourceToTarget(export, ServiceSubtaskDetailRatioExport.class);
@@ -5837,7 +5994,7 @@
     /**
      * 闂缁撴灉鍗犳瘮
      *
-     * @param serviceSubtaskEntity
+     * @param questionResultVO
      * @param serviceSubtaskRes
      * @return
      */
@@ -5894,4 +6051,44 @@
         }
         log.info("ai鍖归厤鎴愬姛锛屽尮閰嶇粨鏋滄槸:" + matchedText);
     }
+
+    private static String trimAll(String s) {
+        if (s == null) {
+            return "";
+        }
+        int start = 0;
+        int end = s.length();
+        while (start < end && isBlankChar(s.charAt(start))) {
+            start++;
+        }
+        while (end > start && isBlankChar(s.charAt(end - 1))) {
+            end--;
+        }
+        return s.substring(start, end);
+    }
+
+    private static Boolean isBlankChar(char c) {
+        return c == '\u3000'          // 鍏ㄨ绌烘牸
+                || c == ' '            // 鍗婅绌烘牸
+                || c == '\t'
+                || c == '\n'
+                || c == '\r'
+                || c == '\f'
+                || Character.isWhitespace(c);
+    }
+
+    private String buildKey(String groupName, String option, String questionKey) {
+        return questionKey + KEY_SEP + groupName + KEY_SEP + option;
+    }
+
+    private static int parseCount(String s) {
+        if (StringUtils.isEmpty(s)) {
+            return 0;
+        }
+        try {
+            return Integer.parseInt(s.trim());
+        } catch (NumberFormatException e) {
+            return 0;
+        }
+    }
 }

--
Gitblit v1.9.3