From 235cf590b8ac4c77d0a9a715d026bc591d9df133 Mon Sep 17 00:00:00 2001
From: 陈昶聿 <chychen@nbjetron.com>
Date: 星期二, 15 九月 2026 15:31:33 +0800
Subject: [PATCH] 【丽水】采集处理只有诊断名称没有诊断码的数据

---
 smartor/src/main/java/com/smartor/service/impl/ServiceSubtaskServiceImpl.java |  199 ++++++++++++++++++++++++++++++++++---------------
 1 files changed, 138 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..48b966c 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";
 
     /**
      * 鏌ヨ鍗曚竴浠诲姟锛堥殢璁匡級
@@ -4082,12 +4075,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());
@@ -4973,15 +4969,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 +5055,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 +5145,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 +5163,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 +5874,7 @@
     /**
      * 闂缁撴灉鍗犳瘮
      *
-     * @param serviceSubtaskEntity
+     * @param questionResultVO
      * @param serviceSubtaskRes
      * @return
      */
@@ -5894,4 +5931,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