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 |  362 ++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 310 insertions(+), 52 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 a79ba27..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;
@@ -22,11 +21,10 @@
 import com.smartor.common.MtSubmitSmUtil;
 import com.smartor.common.ScheduleSubtaskBuilder;
 import com.smartor.config.PhoneUtils;
+import com.smartor.domain.DTO.QuestionCountKsOrBqDTO;
 import com.smartor.domain.DTO.ServiceSubtaskDetailDTO;
 import com.smartor.domain.*;
-import com.smartor.domain.VO.HeLibraryCountVO;
-import com.smartor.domain.VO.ServiceSubtaskCotinueCountVO;
-import com.smartor.domain.VO.TransferDeptVO;
+import com.smartor.domain.VO.*;
 import com.smartor.domain.entity.ServiceSubtaskEntity;
 import com.smartor.mapper.*;
 import com.smartor.service.*;
@@ -34,7 +32,6 @@
 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.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
@@ -43,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;
@@ -206,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";
 
     /**
      * 鏌ヨ鍗曚竴浠诲姟锛堥殢璁匡級
@@ -414,26 +408,7 @@
 
     @Override
     public List<ServiceSubtaskRes> patItem(ServiceSubtaskEntity serviceSubtaskEntity) {
-        if (serviceSubtaskEntity.getSendstateView() != null) {
-            //            1锛氬緟闅忚锛�1 琚鍙栥��2 寰呭彂閫併��3 宸插彂閫併��5 鍙戦�佸け璐ャ��7銆佽秴鏃讹級锛�2锛氬凡瀹屾垚锛� 6 宸插畬鎴愶級銆�3锛氭棤闇�闅忚锛�4 涓嶆墽琛岋級
-            if (serviceSubtaskEntity.getSendstateView() == 1)
-                serviceSubtaskEntity.setSendstates(new ArrayList<>(Arrays.asList(1L, 2L, 3L, 5L, 7L)));
-            if (serviceSubtaskEntity.getSendstateView() == 2)
-                serviceSubtaskEntity.setSendstates(new ArrayList<>(Arrays.asList(6L)));
-            if (serviceSubtaskEntity.getSendstateView() == 3)
-                serviceSubtaskEntity.setSendstates(new ArrayList<>(Arrays.asList(4L)));
-        }
-        //鎮h�呰幏鍙栭殢璁夸换鍔�-闇�瑕佹煡璇㈠悓涓偅鑰咃紙韬唤璇侊級鎵�鏈夌殑浠诲姟 鏅畞
-        if (StringUtils.isNotEmpty(serviceSubtaskEntity.getOrgid()) && serviceSubtaskEntity.getOrgid().equals("47255004333112711A1001")) {
-            if (ObjectUtils.isNotEmpty(serviceSubtaskEntity.getPatid())) {
-                PatArchive patArchive = patArchiveService.selectPatArchiveByPatid(serviceSubtaskEntity.getPatid());
-                if (ObjectUtils.isNotEmpty(patArchive) && StringUtils.isNotEmpty(patArchive.getIdcardno())) {
-                    serviceSubtaskEntity.setPatid(null);
-                    serviceSubtaskEntity.setSfzh(patArchive.getIdcardno());
-                }
-            }
-        }
-        List<ServiceSubtask> selectServiceSubtaskList = this.selectServiceSubtaskList(serviceSubtaskEntity);
+        List<ServiceSubtask> selectServiceSubtaskList = getServiceSubtasks(serviceSubtaskEntity);
 
         List<ServiceSubtaskRes> serviceSubtaskResList = new ArrayList<>();
         for (ServiceSubtask serviceSubtask : selectServiceSubtaskList) {
@@ -469,6 +444,30 @@
             serviceSubtaskResList.add(serviceSubtaskRes);
         }
         return serviceSubtaskResList;
+    }
+
+    public List<ServiceSubtask> getServiceSubtasks(ServiceSubtaskEntity serviceSubtaskEntity) {
+        if (serviceSubtaskEntity.getSendstateView() != null) {
+            //            1锛氬緟闅忚锛�1 琚鍙栥��2 寰呭彂閫併��3 宸插彂閫併��5 鍙戦�佸け璐ャ��7銆佽秴鏃讹級锛�2锛氬凡瀹屾垚锛� 6 宸插畬鎴愶級銆�3锛氭棤闇�闅忚锛�4 涓嶆墽琛岋級
+            if (serviceSubtaskEntity.getSendstateView() == 1)
+                serviceSubtaskEntity.setSendstates(new ArrayList<>(Arrays.asList(1L, 2L, 3L, 5L, 7L)));
+            if (serviceSubtaskEntity.getSendstateView() == 2)
+                serviceSubtaskEntity.setSendstates(new ArrayList<>(Arrays.asList(6L)));
+            if (serviceSubtaskEntity.getSendstateView() == 3)
+                serviceSubtaskEntity.setSendstates(new ArrayList<>(Arrays.asList(4L)));
+        }
+        //鎮h�呰幏鍙栭殢璁夸换鍔�-闇�瑕佹煡璇㈠悓涓偅鑰咃紙韬唤璇侊級鎵�鏈夌殑浠诲姟 鏅畞
+        if (StringUtils.isNotEmpty(serviceSubtaskEntity.getOrgid()) && serviceSubtaskEntity.getOrgid().equals("47255004333112711A1001")) {
+            if (ObjectUtils.isNotEmpty(serviceSubtaskEntity.getPatid())) {
+                PatArchive patArchive = patArchiveService.selectPatArchiveByPatid(serviceSubtaskEntity.getPatid());
+                if (ObjectUtils.isNotEmpty(patArchive) && StringUtils.isNotEmpty(patArchive.getIdcardno())) {
+                    serviceSubtaskEntity.setPatid(null);
+                    serviceSubtaskEntity.setSfzh(patArchive.getIdcardno());
+                }
+            }
+        }
+        List<ServiceSubtask> selectServiceSubtaskList = this.selectServiceSubtaskList(serviceSubtaskEntity);
+        return selectServiceSubtaskList;
     }
 
     @Override
@@ -636,14 +635,19 @@
      */
     @Override
     public Map<String, Object> insertServiceSubtask(ServiceSubtask serviceSubtask) {
+        List<ServiceSubtask> serviceSubtasks = null;
         //鍏堟煡璇竴涓嬭鎮h�呮槸鍚﹀凡缁忓垱寤烘牴鎹畇erviceType,sendstate,inhospid,patid,orgid
-        ServiceSubtaskEntity serviceSubtaskVO = new ServiceSubtaskEntity();
-        serviceSubtaskVO.setServiceType(serviceSubtask.getServiceType());
-        serviceSubtaskVO.setSendstates(new ArrayList<>(Arrays.asList(1L, 2L, 3L, 5L, 7L)));
-        serviceSubtaskVO.setPatid(serviceSubtask.getPatid());
-        serviceSubtaskVO.setOrgid(serviceSubtask.getOrgid());
-        serviceSubtaskVO.setCampusid(serviceSubtask.getCampusid());
-        List<ServiceSubtask> serviceSubtasks = serviceSubtaskMapper.selectServiceSubtaskList(serviceSubtaskVO);
+        //杩欐牱鍒ゆ柇涓昏鏄负浜嗘兂璁╂柊鍗庡尰闄㈢殑澶栭儴瀵煎叆鎮h�呭垱寤轰换鍔★紝涔熻兘鐢ㄨ繖涓帴鍙�
+        if (StringUtils.isEmpty(serviceSubtask.getPatfrom()) || !serviceSubtask.getPatfrom().equals("2")) {
+            ServiceSubtaskEntity serviceSubtaskVO = new ServiceSubtaskEntity();
+            serviceSubtaskVO.setServiceType(serviceSubtask.getServiceType());
+            serviceSubtaskVO.setInhospid(serviceSubtask.getInhospid());
+            serviceSubtaskVO.setSendstates(new ArrayList<>(Arrays.asList(1L, 2L, 3L, 5L, 7L)));
+            serviceSubtaskVO.setPatid(serviceSubtask.getPatid());
+            serviceSubtaskVO.setOrgid(serviceSubtask.getOrgid());
+            serviceSubtaskVO.setCampusid(serviceSubtask.getCampusid());
+            serviceSubtasks = serviceSubtaskMapper.selectServiceSubtaskList(serviceSubtaskVO);
+        }
         RSAPublicKeyExample rsaPublicKeyExample = new RSAPublicKeyExample();
         String format = null;
         if (CollectionUtils.isNotEmpty(serviceSubtasks)) {
@@ -657,8 +661,25 @@
             serviceSubtask = serviceSubtask1;
         } else {
             //濡傛灉涓嶅瓨鍦紝鍒欐柊澧�
+            ServiceTask serviceTask = serviceTaskService.selectServiceTaskByTaskid(serviceSubtask.getTaskid());
+            if (ObjectUtils.isNotEmpty(serviceTask)) {
+                serviceSubtask.setTaskDesc(serviceTask.getTaskDesc());
+                serviceSubtask.setTaskName(serviceTask.getTaskName());
+                serviceSubtask.setTemplatename(serviceTask.getTemplatename());
+            }
+
             serviceSubtask.setCreateTime(DateUtils.getNowDate());
             serviceSubtaskMapper.insertServiceSubtask(serviceSubtask);
+            //淇敼浣忛櫌婊℃剰搴︾姸鎬侊紙閬垮厤閲嶅鏌ヨ锛�
+            if (serviceSubtask.getServiceType() != null && "6".equals(serviceSubtask.getServiceType())) {
+                Long inhospid = serviceSubtask.getInhospid();
+                if (inhospid != null) {
+                    PatMedInhosp patMedInhosp = new PatMedInhosp();
+                    patMedInhosp.setInhospMydFlag("1");
+                    patMedInhosp.setInhospid(inhospid);
+                    patMedInhospMapper.updatePatMedInhosp(patMedInhosp);
+                }
+            }
             format = getOutPath(serviceSubtask, rsaPublicKeyExample);
         }
 
@@ -706,8 +727,12 @@
         patMedInhosp.setInhospstate("0");
         List<PatMedInhosp> patMedInhospList = patMedInhospMapper.selectPatMedInhospList(patMedInhosp);
         PatArchive patArchive = patArchiveMapper.selectPatArchiveByPatid(serviceSubtask1.getPatid());
-
-        if (patMedInhospList.size() > 0) {
+        //鏅畞 鏂板鍐嶆闅忚涓嶈兘瑙﹀彂鎮h�呭啀鍏ラ櫌
+        boolean inhospAgain = true;
+        if (serviceSubtask1.getOrgid().equals("47255004333112711A1001")) {
+            inhospAgain = false;
+        }
+        if (patMedInhospList.size() > 0 && inhospAgain) {
             serviceSubtask1.setIsVisitAgain(2);
             serviceSubtask1.setRemark(StringUtils.isNotEmpty(serviceSubtask.getRemark()) ? serviceSubtask.getRemark() + ";鎮h�呭啀鍏ラ櫌浜�" : "鎮h�呭啀鍏ラ櫌浜�");
             serviceSubtask1.setSendstate(4L);
@@ -2887,6 +2912,10 @@
                 collect = rawData.stream().collect(Collectors.groupingBy(subtask -> Optional.ofNullable(subtask.getDrcode()).orElse("Unknown")));
                 break;
             case "management_doctor_code":
+                rawData = rawData.stream().peek(item -> {
+                    item.setDrname(item.getManagementDoctor());
+                    item.setDrcode(item.getManagementDoctorCode());
+                }).collect(Collectors.toList());
                 collect = rawData.stream().collect(Collectors.groupingBy(subtask -> Optional.ofNullable(subtask.getManagementDoctorCode()).orElse("Unknown")));
                 break;
             case "deptcode":
@@ -4046,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());
@@ -4937,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");
@@ -4970,23 +5004,185 @@
     }
 
     @Override
-    public List<ServiceSubtaskDetailRatioExport> statQuestionOption(List<Long> taskIds, Date startOutHospTime, Date endOutHospTime) {
+    public List<ServiceSubtaskDetailRatioExport> statQuestionOption(List<Long> taskIds, Date startOutHospTime, Date endOutHospTime, String scriptContent, Double scoreStart, Double scoreEnd, String valueType) {
         //鍏堟竻鐞嗕竴涓嬮噸澶嶆暟鎹�
         List<Long> repeatIds = serviceSubtaskDetailMapper.selectRepeatDetailIds();
         if (!CollectionUtils.isEmpty(repeatIds)) {
             serviceSubtaskDetailMapper.deleteRepeatDetailByIds(repeatIds);
         }
-        return serviceSubtaskMapper.statQuestionOption(taskIds, startOutHospTime, endOutHospTime);
+        List<ServiceSubtaskDetailRatioExport> serviceSubtaskDetailRatioExports = serviceSubtaskMapper.statQuestionOption(taskIds, startOutHospTime, endOutHospTime, scoreStart, scoreEnd, scriptContent, valueType);
+
+        // 澶勭悊 targetvalue锛氭寜 & 鍒嗗壊鎴愬鏉¤褰�
+        List<ServiceSubtaskDetailRatioExport> result = new ArrayList<>();
+        for (ServiceSubtaskDetailRatioExport export : serviceSubtaskDetailRatioExports) {
+            if (StringUtils.isNotEmpty(export.getTargetvalue()) && export.getTargetvalue().contains("&")) {
+                String[] values = export.getTargetvalue().split("&");
+                // 濡傛灉鏈夊鏉$洰鏍囧�硷紝鎷嗗垎鎴愬琛�
+                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")) {
+                        //濡傛灉瀛樺湪锛屽垯灏� result 閲岀殑杩欎釜闂鎵�瀵瑰簲鐨� Targetvalue=value 杩欐潯鏁版嵁鏇存柊鎴愬綋鍓嶈繖鏉℃暟鎹�
+                        if (value.equals(export.getOptionresult())) {
+                            existingExport.setRatio(export.getRatio());
+                            existingExport.setScore(export.getScore());
+                            existingExport.setCount(export.getCount());
+                            existingExport.setOptionresult(export.getOptionresult());
+                        }
+                    } else if (existingExport != null && !existingExport.getRatio().equals("0")) {
+                        continue;
+                    } else {
+                        ServiceSubtaskDetailRatioExport newExport = DtoConversionUtils.sourceToTarget(export, ServiceSubtaskDetailRatioExport.class);
+                        newExport.setTargetvalue(value.trim()); // targetvalue 鏄剧ず鍗曚釜閫夐」
+                        if (!value.equals(export.getOptionresult())) {
+                            newExport.setOptionresult(null);
+                            newExport.setCount("0");
+                            newExport.setRatio("0");
+                        }
+                        result.add(newExport);
+                    }
+                }
+            } else {
+                // targetvalue 涓嶅寘鍚� &锛岀洿鎺ユ坊鍔犲埌缁撴灉
+                result.add(export);
+            }
+        }
+
+        return result;
     }
 
+    /*
+     * 鏍规嵁绉戝鎴栫梾鍖虹粺璁¢棶鍗烽�夐」(绉戝銆侀鐩煡璇㈢殑绉戝璇勫垎琛�)
+     */
     @Override
-    public List<ServiceSubtaskDetailRatioExport> statQuestionOptionBySubtaskIds(List<Long> subtaskIds, Date startOutHospTime, Date endOutHospTime) {
+    public List<QuestionCountKsOrBqDTO> statQuestionOptionByKsOrBq(QuestionCountKsOrBqVO vo) {
+        List<QuestionCountKsOrBqDTO> sourceList = serviceSubtaskMapper.statQuestionOptionByKsOrBq(vo);
+        if (CollectionUtils.isEmpty(sourceList)) {
+            return Collections.emptyList();
+        }
+
+        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) {
         //鍏堟竻鐞嗕竴涓嬮噸澶嶆暟鎹�
         List<Long> repeatIds = serviceSubtaskDetailMapper.selectRepeatDetailIds();
         if (!CollectionUtils.isEmpty(repeatIds)) {
             serviceSubtaskDetailMapper.deleteRepeatDetailByIds(repeatIds);
         }
-        return serviceSubtaskMapper.statQuestionOptionBySubtaskIds(subtaskIds, startOutHospTime, endOutHospTime);
+
+        List<ServiceSubtaskDetailRatioExport> serviceSubtaskDetailRatioExports = serviceSubtaskMapper.statQuestionOptionBySubtaskIds(subtaskIds, startOutHospTime, endOutHospTime, scoreStart, scoreEnd, scriptContent, valueType);
+        // 澶勭悊 targetvalue锛氭寜 & 鍒嗗壊鎴愬鏉¤褰�
+        List<ServiceSubtaskDetailRatioExport> result = new ArrayList<>();
+        for (ServiceSubtaskDetailRatioExport export : serviceSubtaskDetailRatioExports) {
+            if (StringUtils.isNotEmpty(export.getTargetvalue()) && export.getTargetvalue().contains("&")) {
+                String[] values = export.getTargetvalue().split("&");
+                // 濡傛灉鏈夊鏉$洰鏍囧�硷紝鎷嗗垎鎴愬琛�
+                for (String value : values) {
+                    //鍏堝幓 result 涓煡涓�涓嬶紝鏈夋病鏈夎繖涓� questiontext 鍜屽搴旂殑 targetvalue
+                    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());
+                            existingExport.setScore(export.getScore());
+                            existingExport.setCount(export.getCount());
+                            existingExport.setOptionresult(export.getOptionresult());
+                        }
+                    } else if (existingExport != null && !"0".equals(existingExport.getRatio())) {
+                        continue;
+                    } else {
+                        ServiceSubtaskDetailRatioExport newExport = DtoConversionUtils.sourceToTarget(export, ServiceSubtaskDetailRatioExport.class);
+                        newExport.setTargetvalue(value.trim()); // targetvalue 鏄剧ず鍗曚釜閫夐」
+                        if (!value.equals(export.getOptionresult())) {
+                            newExport.setOptionresult(null);
+                            newExport.setCount("0");
+                            newExport.setRatio("0");
+                        }
+                        result.add(newExport);
+                    }
+                }
+            } else {
+                // targetvalue 涓嶅寘鍚� &锛岀洿鎺ユ坊鍔犲埌缁撴灉
+                result.add(export);
+            }
+        }
+
+        return result;
     }
 
     /**
@@ -5674,6 +5870,28 @@
         return back;
     }
 
+
+    /**
+     * 闂缁撴灉鍗犳瘮
+     *
+     * @param questionResultVO
+     * @param serviceSubtaskRes
+     * @return
+     */
+    public List<ServiceSubtaskDetailRatioExport> getDetailRatio(QuestionResultVO questionResultVO, List<ServiceSubtaskRes> serviceSubtaskRes) {
+        List<ServiceSubtaskDetailRatioExport> serviceSubtaskDetailRatioExports = null;
+        if (StringUtils.isNotEmpty(questionResultVO.getServiceType()) && questionResultVO.getServiceType().equals("2")) {
+            //鑾峰彇serviceSubtaskList涓殑鎵�鏈塼askid
+            List<Long> taskIds = serviceSubtaskRes.stream().map(ServiceSubtaskRes::getTaskid).filter(ObjectUtils::isNotEmpty).distinct().collect(Collectors.toList());
+            serviceSubtaskDetailRatioExports = statQuestionOption(taskIds, questionResultVO.getStartOutHospTime(), questionResultVO.getEndOutHospTime(), questionResultVO.getQuestionContent(), questionResultVO.getStartScore(), questionResultVO.getEndScore(), questionResultVO.getValueType());
+        } else if (StringUtils.isNotEmpty(questionResultVO.getServiceType()) && questionResultVO.getServiceType().equals("6")) {
+            //婊℃剰搴︾粺璁� 锛堥�氳繃 subtask 鐨� id 鍘荤粺璁★級
+            List<Long> subtaskIds = serviceSubtaskRes.stream().map(ServiceSubtaskRes::getId).collect(Collectors.toList());
+            serviceSubtaskDetailRatioExports = statQuestionOptionBySubtaskIds(subtaskIds, questionResultVO.getStartOutHospTime(), questionResultVO.getEndOutHospTime(), questionResultVO.getQuestionContent(), questionResultVO.getStartScore(), questionResultVO.getEndScore(), questionResultVO.getValueType());
+        }
+        return serviceSubtaskDetailRatioExports;
+    }
+
     //鑾峰彇浠诲姟缁勪俊鎭�
     private void findServiceTaskScheduleVO(ServiceTaskVO serviceTaskVO) {
         if (StringUtils.isNotEmpty(serviceTaskVO.getAppltype()) && serviceTaskVO.getAppltype().equals("5")) {
@@ -5713,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