陈昶聿
11 小时以前 bebb8f4a668c79cd1fc536aeb4a3821115ac15c1
smartor/src/main/java/com/smartor/service/impl/ServiceSubtaskServiceImpl.java
@@ -6,14 +6,19 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.redis.RedisCache;
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;
import com.ruoyi.system.mapper.SysConfigMapper;
import com.ruoyi.system.service.ISysConfigService;
import com.smartor.common.DeepSeekApi;
import com.smartor.common.FtpService;
import com.smartor.common.MtSubmitSmUtil;
import com.smartor.config.PhoneUtils;
@@ -28,6 +33,8 @@
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.ibatis.annotations.Param;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
@@ -35,17 +42,17 @@
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;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
@@ -130,6 +137,9 @@
    @Autowired
    private SvyLibScriptCategoryMapper svyLibScriptCategoryMapper;
    @Autowired
    private IPatMedInhospService patMedInhospService;
    @Value("${pri_key}")
    private String pri_key;
@@ -138,6 +148,9 @@
    @Value("${ASRCallBackPath}")
    private String ASRCallBackPath;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private MtSubmitSmUtil mtSubmitSmUtil;
@@ -176,6 +189,8 @@
    @Autowired
    private IPatMedOuthospService patMedOuthospService;
    @Autowired
    private SysDept2Mapper sysDept2Mapper;
    @Value("${spring.profiles.active}")
    private String active;
@@ -402,9 +417,81 @@
                serviceSubtaskRes.setSendstateView(1L);
            if (serviceSubtaskRes.getSendstate() == 6) serviceSubtaskRes.setSendstateView(2L);
            if (serviceSubtaskRes.getSendstate() == 4) serviceSubtaskRes.setSendstateView(3L);
            // 填充当前补偿
            if (StringUtils.isEmpty(serviceSubtask.getCurrentPreachform())) {
                if (StringUtils.isNotEmpty(serviceSubtask.getPreachform())) {
                    //默认取第一个
                    serviceSubtaskRes.setCurrentPreachform(serviceSubtask.getPreachform().split(",")[0]);
                }
            }
            PatMedInhosp inhosp = patMedInhospMapper.selectPatMedInhospByInhospid(serviceSubtask.getInhospid());
            if (ObjectUtils.isNotEmpty(inhosp)) {
                PatArchive patArchive = patArchiveMapper.selectPatArchiveByPatid(inhosp.getPatid());
                if (ObjectUtils.isNotEmpty(patArchive)) {
                    Map<String, String> stringStringMap = calculateAge(patArchive.getBirthdate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate(), LocalDate.now());
                    serviceSubtaskRes.setAge(StringUtils.isNotEmpty(stringStringMap.get("age")) ? Long.valueOf(stringStringMap.get("age")) : null);
                    serviceSubtaskRes.setSex(patArchive.getSex());
                    serviceSubtaskRes.setSexName(patArchive.getSex() != null ? (patArchive.getSex() == 1 ? "男" : "女") : null);
                }
                serviceSubtaskRes.setHospno(inhosp.getInhospno());
                serviceSubtaskRes.setPatno(inhosp.getPatno());
            }
            serviceSubtaskResList.add(serviceSubtaskRes);
        }
        return serviceSubtaskResList;
    }
    public Map<String, String> calculateAge(LocalDate birthdate, LocalDate today) {
        if (birthdate == null || today.isBefore(birthdate)) {
            return null;
        }
        Map<String, String> ageMap = new HashMap<>();
        Period period = Period.between(birthdate, today);
        long totalDays = ChronoUnit.DAYS.between(birthdate, today);
        long totalMonths = ChronoUnit.MONTHS.between(birthdate, today);
        int years = period.getYears();
        int months = period.getMonths();
        int days = period.getDays();
        String ageUnit;
        Integer age;
        String ageUnit2 = null;
        Integer age2 = null;
        if (totalDays < 90) {
            // 小于 1 个月,按天计算
            ageUnit = "天";
            age = (int) totalDays;
            ageMap.put("age", age != null ? age.toString() : null);
            ageMap.put("ageUnit", ageUnit);
            ageMap.put("age2", null);
            ageMap.put("ageUnit2", null);
        } else if (totalMonths < 36) {
            // 小于 1 年,按月 + 天计算
            ageUnit = "月";
            age = (int) totalMonths;
            ageUnit2 = "天";
            age2 = days;
            ageMap.put("age", age != null ? age.toString() : null);
            ageMap.put("ageUnit", ageUnit);
            ageMap.put("age2", age2 != null ? age2.toString() : null);
            ageMap.put("ageUnit2", ageUnit2);
        } else {
            // 大于 1 年,按年 + 月计算
            ageUnit = "岁";
            age = years;
            ageUnit2 = "月";
            age2 = months;
            ageMap.put("age", age != null ? age.toString() : null);
            ageMap.put("ageUnit", ageUnit);
            ageMap.put("age2", age2 != null ? age2.toString() : null);
            ageMap.put("ageUnit2", ageUnit2);
        }
        return ageMap;
    }
    @Override
@@ -576,9 +663,16 @@
        ServiceSubtask serviceSubtask = serviceSubtaskMapper.selectServiceSubtaskById(serviceSubtaskTemplateVO.getSubId());
        if (serviceSubtask == null || serviceSubtask.getSendstate() != 2L)
            throw new BaseException("该任务已发送给患者,不能再进行修改模板");
        serviceSubtaskTemplateVO.getSvyTaskTemplateVO().setIntroduce("该问卷是替换后的问题,替换前的问题是:" + serviceSubtask.getTemplateid());
        Integer taskTempid = svyTaskTemplateService.saveOrUpdateTemplate(serviceSubtaskTemplateVO.getSvyTaskTemplateVO());
        if (taskTempid == null) return false;
        //每次都新建一个也可以,但新建之后要把之前的删除
        if (serviceSubtask.getType().equals("2")) {
            svyTaskTemplateService.deleteSvyTaskTemplateBySvyid(serviceSubtask.getTemplateid());
        } else if (serviceSubtask.getType().equals("1"))
            ivrTaskTemplateService.deleteIvrTaskTemplateByID(serviceSubtask.getTemplateid());
        String tempName = svyTaskTemplateService.selectSvyTaskTemplateBySvyid(Long.valueOf(taskTempid)).getSvyname();
        serviceSubtask.setTemplateid(taskTempid.longValue());
@@ -677,7 +771,10 @@
                serviceTask.setTemplatename(tempName);
            }
            serviceTask.setLeaveldeptcodes(serviceTaskVO.getLeaveldeptcodes());
            serviceTask.setLeavehospitaldistrictcode(serviceTask.getLeavehospitaldistrictcode());
            if (StringUtils.isEmpty(serviceTask.getLeavehospitaldistrictname()) && StringUtils.isNotEmpty(serviceTask.getLeavehospitaldistrictcode())) {
                SysDept sysDept = sysDept2Mapper.selectDeptByCode(serviceTask.getLeavehospitaldistrictcode(), null);
                serviceTask.setLeavehospitaldistrictname(sysDept.getDeptName());
            }
            if (serviceTaskVO.getLongTask() == 1) serviceTask.setSendState(2L);
            serviceTask.setCreateTime(new Date());
            serviceTask.setUpdateTime(new Date());
@@ -703,8 +800,8 @@
                    //南华附一的icd10code是有重复的,所以不能用南华附一的icd10code去查询
                    log.info("----serviceTaskdiag的值为:{}", serviceTaskdiag);
                    if (!StringUtils.isEmpty(serviceTaskVO.getIcd10name())) {
                        String[] Icd10Names = serviceTaskVO.getIcd10name().split("$");
                        String[] Icd10codes = serviceTaskVO.getIcd10code().split("$");
                        String[] Icd10Names = serviceTaskVO.getIcd10name().split(",");
                        String[] Icd10codes = serviceTaskVO.getIcd10code().split(",");
                        for (int i = 0; i < Icd10Names.length; i++) {
                            serviceTaskdiag.setIcd10code(Icd10codes[i]);
                            serviceTaskdiag.setIcd10name(Icd10Names[i]);
@@ -715,18 +812,28 @@
                    ServiceTaskoper serviceTaskoper = new ServiceTaskoper();
                    serviceTaskoper.setOpcode(serviceTaskVO.getOpcode());
                    serviceTaskoper.setOpdesc(serviceTaskVO.getOpdesc());
                    serviceTaskoper.setOplevelcode(serviceTaskVO.getOplevelcode());
//                    serviceTaskoper.setOplevelcode(serviceTaskVO.getOplevelcode());
                    serviceTaskoper.setTaskId(serviceTask.getTaskid());
                    serviceTaskoper.setTaskName(serviceTask.getTaskName());
                    serviceTaskoper.setLongtask(Long.valueOf(serviceTask.getLongTask()));
                    serviceTaskoper.setGuid(serviceTask.getGuid());
                    serviceTaskoper.setOrgid(serviceTask.getOrgid());
                    serviceTaskoper.setCreateTime(new Date());
                    //配置科室、病区
                    serviceTaskoper.setDeptCode(serviceTaskVO.getDeptcode());
                    serviceTaskoper.setDeptName(serviceTaskVO.getDeptname());
                    serviceTaskoper.setWardCode(serviceTaskVO.getLeavehospitaldistrictcode());
                    serviceTaskoper.setWardName(serviceTaskVO.getLeavehospitaldistrictname());
                    //多选手术等级
                    if (StringUtils.isNotEmpty(serviceTaskVO.getOplevelcode())) {
                        log.info("----serviceTaskoper的值为:{}", serviceTaskoper);
                        serviceTaskoperService.insertServiceTaskoper(serviceTaskoper);
                        String[] opLevelcodes = serviceTaskVO.getOplevelcode().split(",");
                        for (String opLevelcode : opLevelcodes) {
                            //先查询一下是否存在
                            log.info("----serviceTaskoper的值为:{}", serviceTaskoper);
                            serviceTaskoper.setOplevelcode(opLevelcode);
                            serviceTaskoperService.insertServiceTaskoper(serviceTaskoper);
                        }
                    }
                } else {
                    ServiceTaskdept serviceTaskdept = new ServiceTaskdept();
                    serviceTaskdept.setTaskId(serviceTask.getTaskid());
@@ -1632,12 +1739,28 @@
            if (ivrTaskTemplateScriptVO.getScriptType().equals("1")) {
                //用来标记,是否有匹配上的
                Integer flag = 0;
                //1-AI识别
                SysConfig configVoiceMatchAi = new SysConfig();
                configVoiceMatchAi.setOrgid(phoneCallReqYQVO.getOrgid());
                configVoiceMatchAi.setConfigKey("sys.voice.match.ai");
                SysConfig matchAi = sysConfigMapper.selectConfig(configVoiceMatchAi);
            if (ObjectUtils.isNotEmpty(matchAi) && StringUtils.isNotEmpty(matchAi.getConfigValue())
                    && matchAi.getConfigValue().equals("1")) {
                PhoneCallBackYQVO aiMatchResult = AiMatch(phoneCallReqYQVO, phoneCallBackYQVO,
                        ivrTaskTemplateScriptVO, serviceSubtask, ivrTaskTemplate,
                        ivrTaskTemplateScripts, scriptId, flag, phoneCallReqYQVO.getUuid());
                if(aiMatchResult != null){
                    return aiMatchResult;
                }
            } else {
                //是选择题
                for (int j = 0; j < ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().size(); j++) {
                    log.error("phoneCallReqYQVO.getAsrtext()的值为:{}", phoneCallReqYQVO.getAsrtext());
                    if (StringUtils.isEmpty(phoneCallReqYQVO.getAsrtext())) {
                        continue;
                    }
                    boolean matchedFlag = false;
                    //包含
                    Matcher matcher = null;
                    if (StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex())) {
@@ -1650,8 +1773,12 @@
                        Pattern pattern2 = Pattern.compile(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2());
                        matcher2 = pattern2.matcher(phoneCallReqYQVO.getAsrtext());
                    }
                    if (StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2()) && matcher2.matches() && StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex()) && matcher.matches() || StringUtils.isEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex()) && StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2()) && matcher2.matches() || StringUtils.isEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2()) && StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex()) && matcher.matches()) {
                        //说明匹配正确了
                        matchedFlag = true;
                    }
                    //说明匹配正确了
                    if(matchedFlag){
                        //这里应该先判断类型,去再修改,设置IsUserOperation是单选题的改法
                        log.info("匹配正确了吗--------------");
                        ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).setIsUserOperation(1);
@@ -1868,6 +1995,7 @@
                        continue;
                    }
                }
            }
                //都没有匹配到
                if (StringUtils.isEmpty(phoneCallBackYQVO.getValue())) {
                    log.info("都没有匹配到-------------------------");
@@ -2342,7 +2470,6 @@
            serviceSubtask.setRemark("电话接通成功,患者拒绝随访");
            serviceSubtask.setId(Long.valueOf(phoneCallRecordVO.getTaskid()));
            serviceSubtask.setSendstate(6L);
            serviceSubtask.setFinishtime(new Date());
            serviceSubtaskMapper.updateServiceSubtask(serviceSubtask);
//            int startIndex = phoneCallRecordVO.getRecord_path().indexOf("voice") + "voice".length() + 1;  // 加1是跳过/符号
//            int endIndex = phoneCallRecordVO.getRecord_path().lastIndexOf("/");
@@ -2490,16 +2617,21 @@
    @Cacheable(value = "sfStatistics", key = "T(org.springframework.util.DigestUtils).md5DigestAsHex(#serviceSubtaskCountReq.toString().getBytes())", unless = "#result == null or #result.isEmpty()")
    public List<ServiceSubtaskStatistic> getSfStatistics(ServiceSubtaskCountReq serviceSubtaskCountReq) {
        log.info("getSfStatistics的入参为:{}", serviceSubtaskCountReq);
        String groupKey = "drcode";
        String drcode = "drcode";
        //缙云人民 根据经管医生分组
        if(serviceSubtaskCountReq.getOrgid().equals("47246116333112211A1001")){
            drcode = "management_doctor_code";
        }
        String groupKey = drcode;
        if (serviceSubtaskCountReq.getStatisticaltype() != null && serviceSubtaskCountReq.getStatisticaltype() == 1) {
            if (serviceSubtaskCountReq.getDrcode() != null && !serviceSubtaskCountReq.getDrcode().isEmpty()) {
                groupKey = "drcode";
                groupKey = drcode;
            } else {
                groupKey = "leavehospitaldistrictcode";
            }
        } else if (serviceSubtaskCountReq.getStatisticaltype() != null && serviceSubtaskCountReq.getStatisticaltype() == 2) {
            if (serviceSubtaskCountReq.getDrcode() != null && !serviceSubtaskCountReq.getDrcode().isEmpty()) {
                groupKey = "drcode";
                groupKey = drcode;
            } else {
                groupKey = "deptcode";
            }
@@ -2512,11 +2644,14 @@
        serviceSubtaskCountReq.setGroupKeyList(groupKeyList);
        List<ServiceSubtask> rawData = serviceSubtaskMapper.getSfStatistics(serviceSubtaskCountReq);
        switch (groupKey) {
            case "deptcode":
                collect = rawData.stream().collect(Collectors.groupingBy(subtask -> Optional.ofNullable(subtask.getDeptcode()).orElse("Unknown")));
                break;
            case "drcode":
                collect = rawData.stream().collect(Collectors.groupingBy(subtask -> Optional.ofNullable(subtask.getDrcode()).orElse("Unknown")));
                break;
            case "management_doctor_code":
                collect = rawData.stream().collect(Collectors.groupingBy(subtask -> Optional.ofNullable(subtask.getManagementDoctorCode()).orElse("Unknown")));
                break;
            case "deptcode":
                collect = rawData.stream().collect(Collectors.groupingBy(subtask -> Optional.ofNullable(subtask.getDeptcode()).orElse("Unknown")));
                break;
            case "leavehospitaldistrictcode":
                collect = rawData.stream().collect(Collectors.groupingBy(subtask -> Optional.ofNullable(subtask.getLeavehospitaldistrictcode()).orElse("Unknown")));
@@ -2845,15 +2980,15 @@
                //首次出院随访
                if (serviceSubtask.getVisitCount() != null && serviceSubtask.getVisitCount() == 1) {
                    //首次应随访
                    if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate() != 4) {
                    if (serviceSubtask.getSendstate() != null && !serviceSubtask.getSendstate().equals(4L)) {
                        serviceSubtaskStatistic.setNeedFollowUp(serviceSubtaskStatistic.getNeedFollowUp() + 1L);
                    }
                    //首次待随访
                    if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate() == 2) {
                        serviceSubtaskStatistic.setPendingFollowUp(serviceSubtaskStatistic.getPendingFollowUp() + 1L);
                    //首次无需随访
                    if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate().equals(4L)) {
                        serviceSubtaskStatistic.setNonFollowUpFirst(serviceSubtaskStatistic.getNonFollowUpFirst() + 1L);
                    }
                    //首次随访失败(包括随访失败+人工超时)
                    if (serviceSubtask.getSendstate() != null && (serviceSubtask.getSendstate() == 5 || serviceSubtask.getSendstate() == 7)) {
                    if (serviceSubtask.getSendstate() != null && (serviceSubtask.getSendstate().equals(5L) || serviceSubtask.getSendstate().equals(7L))) {
                        serviceSubtaskStatistic.setFollowUpFail(serviceSubtaskStatistic.getFollowUpFail() + 1L);
                    }
                    /**
@@ -2863,6 +2998,10 @@
                        //首次随访成功
                        if (serviceSubtask.getSendstate() != null && (serviceSubtask.getSendstate().equals(6L))) {
                            serviceSubtaskStatistic.setFollowUpSuccess(serviceSubtaskStatistic.getFollowUpSuccess() + 1L);
                        }
                        //首次无需随访
                        if (serviceSubtask.getSendstate() != null && (serviceSubtask.getSendstate().equals(4L))) {
                            serviceSubtaskStatistic.setNonFollowUpFirst(serviceSubtaskStatistic.getNonFollowUpFirst() + 1L);
                        }
                        //首次随访人工 (不统计不执行)
                        if (serviceSubtask.getSendstate() != null && !serviceSubtask.getSendstate().equals(4L) && ObjectUtils.isNotEmpty(serviceSubtask.getCurrentPreachform()) && serviceSubtask.getCurrentPreachform().equals("1")) {
@@ -2885,6 +3024,10 @@
                        if (serviceSubtask.getSendstate() != null && (serviceSubtask.getSendstate().equals(6L))) {
                            serviceSubtaskStatistic.setFollowUpSuccess(serviceSubtaskStatistic.getFollowUpSuccess() + 1L);
                        }
                        //首次无需随访
                        if (serviceSubtask.getSendstate() != null && (serviceSubtask.getSendstate().equals(4L))) {
                            serviceSubtaskStatistic.setNonFollowUpFirst(serviceSubtaskStatistic.getNonFollowUpFirst() + 1L);
                        }
                        //首次随访人工 (只统计已完成)
                        if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate().equals(6L) && ObjectUtils.isNotEmpty(serviceSubtask.getCurrentPreachform()) && serviceSubtask.getCurrentPreachform().equals("1")) {
                            serviceSubtaskStatistic.setManual(serviceSubtaskStatistic.getManual() + 1L);
@@ -2905,6 +3048,10 @@
                        //首次随访成功 - 统计时候加上不执行的,已发送的
                        if (serviceSubtask.getSendstate() != null && (serviceSubtask.getSendstate().equals(6L) || serviceSubtask.getSendstate().equals(3L) || serviceSubtask.getSendstate().equals(4L))) {
                            serviceSubtaskStatistic.setFollowUpSuccess(serviceSubtaskStatistic.getFollowUpSuccess() + 1L);
                        }
                        //首次无需随访
                        if (serviceSubtask.getSendstate() != null && (serviceSubtask.getSendstate().equals(4L))) {
                            serviceSubtaskStatistic.setNonFollowUpFirst(serviceSubtaskStatistic.getNonFollowUpFirst() + 1L);
                        }
                        //首次随访人工 (统计不执行)
                        if (serviceSubtask.getSendstate() != null && ObjectUtils.isNotEmpty(serviceSubtask.getCurrentPreachform()) && serviceSubtask.getCurrentPreachform().equals("1")) {
@@ -2938,6 +3085,10 @@
                    //再次应随访
                    if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate() != 4) {
                        serviceSubtaskStatistic.setNeedFollowUpAgain(serviceSubtaskStatistic.getNeedFollowUpAgain() + 1L);
                    }
                    //再次无需随访
                    if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate() == 4) {
                        serviceSubtaskStatistic.setNonFollowUpAgain(serviceSubtaskStatistic.getNonFollowUpAgain() + 1L);
                    }
                    //再次待随访
                    if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate() == 2) {
@@ -3089,6 +3240,10 @@
                if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate() != 4) {
                    serviceSubtaskStatistic.setNeedFollowUp(serviceSubtaskStatistic.getNeedFollowUp() + 1L);
                }
                //首次无需随访
                if (serviceSubtask.getSendstate() != null && (serviceSubtask.getSendstate().equals(4L))) {
                    serviceSubtaskStatistic.setNonFollowUpFirst(serviceSubtaskStatistic.getNonFollowUpFirst() + 1L);
                }
                //首次待随访
                if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate() == 2) {
                    serviceSubtaskStatistic.setPendingFollowUp(serviceSubtaskStatistic.getPendingFollowUp() + 1L);
@@ -3235,6 +3390,10 @@
                //再次应随访
                if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate() != 4) {
                    serviceSubtaskStatistic.setNeedFollowUpAgain(serviceSubtaskStatistic.getNeedFollowUpAgain() + 1L);
                }
                //再次无需随访
                if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate() == 4) {
                    serviceSubtaskStatistic.setNonFollowUpAgain(serviceSubtaskStatistic.getNonFollowUpAgain() + 1L);
                }
                //再次待随访
                if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate() == 2) {
@@ -3657,7 +3816,7 @@
        Map<String, String> map = new HashMap<>();
        map.put("phone", serviceSubtask.getPhone());
        map.put("content", sendMagParam.getContent());
        String result = HttpUtil.postJsonRequest(xhsmsPath, new Gson().toJson(map));
        HttpUtil.postJsonRequest(xhsmsPath, new Gson().toJson(map));
        redisCache.setCacheObject(subTaskId + "recordAccept-hungup", "1", 10, TimeUnit.MINUTES);
        if (StringUtils.isNotEmpty(serviceSubtask.getRemark()))
            serviceSubtask.setRemark("电话发送拒接,短信补偿发送成功");
@@ -4493,6 +4652,29 @@
    }
    @Override
    public List<ServiceSubtask> gethelibraryCountHyperlink(HeLibraryCountVO heLibraryCountVO) {
        String hyperLinkInfoType = heLibraryCountVO.getHyperLinkInfoType();
        if (StringUtils.isNotEmpty(hyperLinkInfoType)) {
            heLibraryCountVO.setSendstate(null);
            heLibraryCountVO.setIsFinished(null);
            switch (hyperLinkInfoType) {
                case "totalCountInfo":
                    break;
                case "sendSuccessCountInfo":
                    heLibraryCountVO.setSendstate(6L);
                    break;
                case "readCountInfo":
                    heLibraryCountVO.setIsFinished("1");
                    break;
                default:
                    break;
            }
        }
        List<ServiceSubtask> serviceSubtasks = serviceSubtaskMapper.gethelibraryCountHyperlink(heLibraryCountVO);
        return serviceSubtasks;
    }
    @Override
    public Map<String, Object> smsSubTask(Long subid) {
        log.info("smsSubTask的入参为:{}", subid);
        Map<String, Object> resultMap = new HashMap<>();
@@ -4545,6 +4727,11 @@
            }
        }
        return resultMap;
    }
    @Override
    public List<ServiceSubtaskDetailRatioExport> statQuestionOption(List<Long> taskIds, Date startOutHospTime, Date endOutHospTime) {
        return serviceSubtaskMapper.statQuestionOption(taskIds, startOutHospTime, endOutHospTime);
    }
    private Boolean saveServiceSubtaskDetail(ServiceSubtask serviceSubtask) {
@@ -4690,4 +4877,397 @@
        }
    }
    public String scanGenerateSubtask(ServiceTask serviceTaskVo) {
        String content = "";
        String ip = localIP;
        ServiceTask serviceTask = new ServiceTask();
        serviceTask = serviceTaskService.selectServiceTaskByTaskid(serviceTaskVo.getTaskid());
        if (ObjectUtils.isNotEmpty(serviceTask)) {
            try {
                ServiceTaskdept serviceTaskdept = new ServiceTaskdept();
                if (ObjectUtils.isNotEmpty(serviceTaskVo.getDeptcode())) {
                    serviceTaskdept.setDeptType("1");
                    serviceTaskdept.setDeptCode(serviceTaskVo.getDeptcode());
                }
                if (ObjectUtils.isNotEmpty(serviceTaskVo.getLeavehospitaldistrictcode())) {
                    serviceTaskdept.setDeptType("2");
                    serviceTaskdept.setDeptCode(serviceTaskVo.getLeavehospitaldistrictcode());
                }
                serviceTaskdept.setServiceType(serviceTask.getServiceType());
                serviceTaskdept.setOrgid(serviceTask.getOrgid());
                serviceTaskdept.setTaskId(serviceTaskVo.getTaskid());
                List<ServiceTaskdept> serviceTaskdepts = serviceTaskdeptService.selectServiceTaskdeptList(serviceTaskdept);
                if (!CollectionUtils.isEmpty(serviceTaskdepts)) {
                    PatArchive patArchive = new PatArchive();
                    patArchive.setId(999999999L);
                    patArchive.setName("虚拟患者");
                    //封装serviceSubtask
                    ServiceSubtask serviceSubtask = boxedServiceSubtask(serviceTask, serviceTaskdepts.get(0), patArchive);
                    int i = serviceSubtaskMapper.insertServiceSubtask(serviceSubtask);
                    //根据生成外链
                    if (ObjectUtils.isNotEmpty(serviceSubtask.getTaskid()) && ObjectUtils.isNotEmpty(serviceSubtask.getPatid()) && ObjectUtils.isNotEmpty(serviceSubtask.getId())) {
                        RSAPublicKeyExample rsaPublicKeyExample = new RSAPublicKeyExample();
                        String taskId = rsaPublicKeyExample.encryptedData(serviceSubtask.getTaskid().toString(), pub_key);
                        String patid = rsaPublicKeyExample.encryptedData(serviceSubtask.getPatid().toString(), pub_key);
                        String subId = rsaPublicKeyExample.encryptedData(serviceSubtask.getId().toString(), pub_key);
                        ServiceOutPath serviceOutPath = new ServiceOutPath();
                        serviceOutPath.setParam1(taskId);
                        serviceOutPath.setParam2(patid);
                        serviceOutPath.setParam3(serviceTask.getTaskName());
                        serviceOutPath.setParam6(subId);
                        serviceOutPath.setCreateTime(new Date());
                        serviceOutPath.setOrgid(serviceTask.getOrgid());
                        iServiceOutPathService.insertServiceOutPath(serviceOutPath);
                        String format = String.format("%03X", serviceOutPath.getId());
                        serviceOutPath.setRadix(format);
                        serviceOutPath.setUpdateTime(new Date());
                        String url = ip + ":" + req_path + "/wt?p=" + format;
                        content = url;
                        serviceOutPath.setUrl(url);
                        iServiceOutPathService.updateServiceOutPath(serviceOutPath);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("【扫码生成任务】处理异常:{}", e.getMessage());
            }
        }
        return content;
    }
    public ServiceSubtask boxedServiceSubtask(ServiceTask serviceTask, ServiceTaskdept serviceTaskdept, PatArchive patArchive) {
        ServiceSubtask serviceSubtask = DtoConversionUtils.sourceToTarget(serviceTask, ServiceSubtask.class);
        serviceSubtask.setTaskid(serviceTask.getTaskid());
        if (StringUtils.isNotEmpty(serviceTask.getLibtemplateid()))
            serviceSubtask.setLibtemplateid(Long.valueOf(serviceTask.getLibtemplateid()));
//        serviceSubtask.setNurseId(patMedInhosp1.getNurseId());
//        serviceSubtask.setNurseName(patMedInhosp1.getNurseName());
//        serviceSubtask.setDrcode(patMedInhosp1.getDrcode());
//        serviceSubtask.setDrname(patMedInhosp1.getDrname());
        if(StringUtils.isNotEmpty(serviceTaskdept.getDeptType()) && serviceTaskdept.getDeptType().equals("1")){
            serviceSubtask.setDeptcode(serviceTaskdept.getDeptCode());
            serviceSubtask.setDeptname(serviceTaskdept.getDeptName());
        }else {
            serviceSubtask.setLeavehospitaldistrictcode(serviceTaskdept.getDeptCode());
            serviceSubtask.setLeavehospitaldistrictname(serviceTaskdept.getDeptName());
        }
        serviceSubtask.setTemplateid(serviceTask.getTemplateid());
        serviceSubtask.setTemplatename(serviceTask.getTemplatename());
        serviceSubtask.setPatid(patArchive.getId());
        serviceSubtask.setSendname(patArchive.getName());
        serviceSubtask.setSfzh(patArchive.getIdcardno());
        serviceSubtask.setPhone(patArchive.getTelcode());
        if (StringUtils.isBlank(patArchive.getTelcode())) serviceSubtask.setPhone(patArchive.getRelativetelcode());
        serviceSubtask.setSex(patArchive.getSex());
        serviceSubtask.setAge(patArchive.getAge());
        serviceSubtask.setSendstate(3L);
//        serviceSubtask.setManagementDoctor(patMedInhosp1.getManagementDoctor());
//        serviceSubtask.setManagementDoctorCode(patMedInhosp1.getManagementDoctorCode());
        serviceSubtask.setServiceType(serviceTask.getServiceType());
        serviceSubtask.setPreachform(serviceTask.getPreachform());
        serviceSubtask.setHospType("8");
        serviceSubtask.setCreateTime(new Date());
        serviceSubtask.setUpdateTime(new Date());
//        serviceSubtask.setCreateBy(patMedInhosp1.getNurseName());
//        serviceSubtask.setLeavehospitaldistrictcode(patMedInhosp1.getLeavehospitaldistrictcode());
//        serviceSubtask.setLeavehospitaldistrictname(patMedInhosp1.getLeavehospitaldistrictname());
        serviceSubtask.setUpdateBy(serviceTask.getUpdateBy());
        serviceSubtask.setUpdateTime(new Date());
        serviceTask.setSendDay(1L);
        Date newDate = new Date();
        serviceSubtask.setLongSendTime(newDate);
        serviceSubtask.setVisitTime(newDate);
        return serviceSubtask;
    }
    /**
     * Ai 匹配选项
     * @param questionText
     * @param voiceText
     * @param options
     * @return
     */
    public Integer matchOptionIndex(String questionText, String voiceText, List<IvrTaskTemplateTargetoption> options, String uuid){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userQuestion",questionText);
        jsonObject.put("userAnswer",voiceText);
        jsonObject.put("options", options);
        jsonObject.put("uuid", uuid);
        String jsonString = jsonObject.toJSONString();
        String result = null;
        String url = "http://localhost:8099/matchOptionIndex";
//        String url = configService.selectConfigByKey("sys.voice.match.ai.url");
        if(StringUtils.isNotEmpty(url)){
            try {
                Map<String, String> headers = new HashMap<>();
                headers.put("Content-Type", "application/json");
                result = HttpUtils.sendPostByHeader(url, jsonString, headers);
            } catch (Exception e) {
                log.error("AI识别失败", e);
            }
        }
        if(StringUtils.isNotEmpty(result) && result.matches("^-?\\d+$")){
            return Integer.parseInt(result);
        }else {
            return -1;
        }
    }
    public PhoneCallBackYQVO AiMatch(PhoneCallReqYQVO phoneCallReqYQVO, PhoneCallBackYQVO phoneCallBackYQVO,
                        IvrTaskTemplateScriptVO ivrTaskTemplateScriptVO, ServiceSubtask serviceSubtask, IvrTaskTemplate ivrTaskTemplate,
                        List<IvrTaskTemplateScript> ivrTaskTemplateScripts, String scriptId, Integer flag, String uuid){
        PhoneCallBackYQVO back = null;
        List<IvrTaskTemplateTargetoption> options = ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList();
        String voiceText = phoneCallReqYQVO.getAsrtext();
        String questionText = StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrtext()) ? ivrTaskTemplateScriptVO.getIvrtext() : ivrTaskTemplateScriptVO.getScriptContent();
        Integer matched = -1;
        matched = matchOptionIndex(questionText, voiceText, options, uuid);
        //说明匹配正确了
        if(matched != null && matched >= 0){
            IvrTaskTemplateTargetoption chosenOption = options.get(matched);
            //这里应该先判断类型,去再修改,设置IsUserOperation是单选题的改法
            log.info("匹配正确了吗--------------");
            chosenOption.setIsUserOperation(1);
            serviceTaskScriptTargetoptionMapper.updateIvrTaskTemplateTargetoption(chosenOption);
            //将患者的回签写进service_subtask_detail中
            ServiceSubTaskDetailReq serviceSubTaskDetailReq = new ServiceSubTaskDetailReq();
            List<ServiceSubtaskDetail> serviceSubtaskDetailList = new ArrayList<>();
            ivrTaskTemplateScriptVO.setQuestionResult(chosenOption.getOptiondesc());
            ServiceSubtaskDetail serviceSubtaskDetail = getServiceSubtaskDetail(phoneCallReqYQVO, ivrTaskTemplateScriptVO, serviceSubtask, ivrTaskTemplate);
            //修改一下语音路径(改成前端可以访问的,存到数据库中)
            if (StringUtils.isNotEmpty(serviceSubtaskDetail.getQuestionvoice())) {
                String questionvoice = serviceSubtaskDetail.getQuestionvoice();
                String[] split = questionvoice.split("\\\\");
                if (split.length > 0) {
                    String lastPart = split[split.length - 1];
                    serviceSubtaskDetail.setQuestionvoice(voicePathPrefix + lastPart);
                }
            }
            serviceSubtaskDetailList.add(serviceSubtaskDetail);
            serviceSubTaskDetailReq.setServiceSubtaskDetailList(serviceSubtaskDetailList);
            serviceSubTaskDetailReq.setGuid(phoneCallReqYQVO.getGuid());
            serviceSubTaskDetailReq.setOrgid(phoneCallReqYQVO.getOrgid());
            saveQuestionAnswerPhone(serviceSubTaskDetailReq);
            //判断一下当前的问题是不是满意度问题,并且dutyDeptCode是否有值,并且选项是不是异常选项,如果全符合,则往满意度问题异常表新增
            if (StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getDutyDeptCode()) && 1 == chosenOption.getIsabnormal()) {
                ServiceSubtaskDetailTrace subtaskDetailTrace = new ServiceSubtaskDetailTrace();
                //用taskid、subid和scriptid去获取detailid
                ServiceSubtaskDetail ssd = new ServiceSubtaskDetail();
                ssd.setSubId(serviceSubtask.getId());
                ssd.setTaskid(serviceSubtask.getTaskid());
                ssd.setScriptid(StringUtils.isNotEmpty(scriptId) ? Long.valueOf(scriptId) : null);
                List<ServiceSubtaskDetail> serviceSubtaskDetails = serviceSubtaskDetailMapper.selectServiceSubtaskDetailList(ssd);
                subtaskDetailTrace.setDetailId(CollectionUtils.isNotEmpty(serviceSubtaskDetails) ? serviceSubtaskDetails.get(0).getId() : null);
                subtaskDetailTrace.setSubId(serviceSubtask.getId());
                subtaskDetailTrace.setTaskid(serviceSubtask.getTaskid());
                subtaskDetailTrace.setTemplateid(ivrTaskTemplateScriptVO.getId());
                subtaskDetailTrace.setTemplatequestionnum(ivrTaskTemplateScriptVO.getScriptno());
                subtaskDetailTrace.setSwitchid(chosenOption.getId());
                subtaskDetailTrace.setQuestiontext(ivrTaskTemplateScriptVO.getScriptContent());
                subtaskDetailTrace.setQuestionvoice(null);
                subtaskDetailTrace.setCategoryname(ivrTaskTemplateScriptVO.getScriptAssortname());
                subtaskDetailTrace.setCategoryid(ivrTaskTemplateScriptVO.getScriptAssortid());
                //获取所有选项
                String optionDescStr = Optional.ofNullable(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList()).filter(list -> !list.isEmpty()).map(list -> list.stream().map(IvrTaskTemplateTargetoption::getOptiondesc).filter(Objects::nonNull).collect(Collectors.joining("&"))).orElse("");
                subtaskDetailTrace.setTargetid(chosenOption.getId());
                subtaskDetailTrace.setTargetvalue(optionDescStr);
                subtaskDetailTrace.setMatchedtext(chosenOption.getOptiondesc());
                subtaskDetailTrace.setValueType(serviceSubtaskDetails.get(0).getValueType());
                subtaskDetailTrace.setTemplateType(1);
                SvyLibScriptCategory svyLibScriptCategory = svyLibScriptCategoryMapper.selectSvyLibScriptCategoryById(ivrTaskTemplateScriptVO.getScriptAssortid());
                subtaskDetailTrace.setType(svyLibScriptCategory.getType());
                subtaskDetailTrace.setScriptid(ivrTaskTemplateScriptVO.getId());
                subtaskDetailTrace.setAsrtext(phoneCallReqYQVO.getAsrtext());
                subtaskDetailTrace.setRecordpath(phoneCallReqYQVO.getRecordpath());
                subtaskDetailTrace.setPatid(serviceSubtask.getPatid());
                JSONObject patdescJson = new JSONObject();
                patdescJson.put("sendname", serviceSubtask.getSendname());
                patdescJson.put("phone", serviceSubtask.getPhone());
                patdescJson.put("age", serviceSubtask.getAge());
                patdescJson.put("sex", serviceSubtask.getSex() != null ? serviceSubtask.getSex() == 1 ? "男" : "女" : null);
                subtaskDetailTrace.setPatdesc(patdescJson.toJSONString());
                subtaskDetailTrace.setTodeptcode(ivrTaskTemplateScriptVO.getDutyDeptCode());
                subtaskDetailTrace.setTodeptname(ivrTaskTemplateScriptVO.getDutyDeptName());
                subtaskDetailTrace.setOrgid(serviceSubtask.getOrgid());
                subtaskDetailTrace.setHandleFlag("0");
                subtaskDetailTrace.setGuid(phoneCallReqYQVO.getGuid());
                subtaskDetailTrace.setCreateTime(new Date());
                subtaskDetailTrace.setUpdateTime(new Date());
                subtaskDetailTrace.setOrgid(phoneCallReqYQVO.getOrgid());
                traceService.insertServiceSubtaskDetailTtrace(subtaskDetailTrace);
            }
            //判断一下,这个选项结果是不是还有继续问下去的必要,例如选项结果是别人不想继续回答问题,就要结束掉
            if (chosenOption.getIsEnd() == 1) {
                redisCache.deleteObject(serviceSubtask.getId() + "-" + serviceSubtask.getPhone());
                redisCache.deleteObject(phoneCallReqYQVO.getUuid() + "SCORE");
                redisCache.deleteObject(phoneCallReqYQVO.getUuid() + "PlayEventCallbackPlaystop");
                redisCache.deleteObject(phoneCallReqYQVO.getUuid() + "noVoice");
                //在redis中保存一下结束语,在调用挂电话的方法时删除
                ServiceTask serviceTask = serviceTaskService.selectServiceTaskByTaskid(serviceSubtask.getTaskid());
                redisCache.setCacheObject(phoneCallReqYQVO.getUuid() + "- jsy", serviceTask.getJsy(), 120, TimeUnit.MINUTES);
                phoneCallBackYQVO.setType("text");
                phoneCallBackYQVO.setValue(phoneCallBackYQVO.getCommonValue() + serviceTask.getJsy());
                //记录状态
                setFailPreachForm(serviceSubtask, "3", "电话拨打已完成", "9");
                return phoneCallBackYQVO;
            }
            flag = 1;
            //将当前前的播报状态删除,给下一题让位
            redisCache.deleteObject(phoneCallReqYQVO.getUuid() + "PlayEventCallbackPlaystop");
//                        //获取下一题
            log.error("获取下一题的信息:{}", ivrTaskTemplateScriptVO);
            if (ivrTaskTemplateScriptVO.getBranchFlag().equals("1") || ivrTaskTemplateScriptVO.getBranchFlag().equals("0") && ivrTaskTemplateScriptVO.getNextScriptno() != null && ivrTaskTemplateScriptVO.getNextScriptno() != 0) {
                Long nextQuestion = null;
                if (ivrTaskTemplateScriptVO.getBranchFlag().equals("1")) {
                    nextQuestion = chosenOption.getNextQuestion();
                    //更新分数
                    double score = redisCache.getCacheObject(phoneCallReqYQVO.getUuid() + "SCORE");
                    score = BigDecimal.valueOf(score).add(chosenOption.getScore()).doubleValue();
                    redisCache.setCacheObject(phoneCallReqYQVO.getUuid() + "SCORE", score);
                } else {
                    nextQuestion = ivrTaskTemplateScriptVO.getNextScriptno();
                    //更新分数
                    Object obj = redisCache.getCacheObject(phoneCallReqYQVO.getUuid() + "SCORE");
                    Double score = (obj == null ? new Double(0.00) : new Double(((Double) obj).doubleValue()));
                    score = BigDecimal.valueOf(score).add(chosenOption.getScore()).doubleValue();
                    redisCache.setCacheObject(phoneCallReqYQVO.getUuid() + "SCORE", score, 120, TimeUnit.MINUTES);
                }
                if (nextQuestion == null || nextQuestion == 0L) {
                    //如果下一题为空,或者为0,则挂机
                    ServiceSubtask ss = new ServiceSubtask();
                    ss.setId(serviceSubtask.getId());
                    ss.setSendstate(6L);
                    ss.setRemark("电话拨打已完成");
                    serviceSubtaskMapper.updateServiceSubtask(ss);
                    //记录状态
                    setFailPreachForm(serviceSubtask, "3", "电话拨打已完成", "9");
                    redisCache.deleteObject(serviceSubtask.getId() + "-" + serviceSubtask.getPhone());
                    redisCache.deleteObject(phoneCallReqYQVO.getUuid() + "SCORE");
                    redisCache.deleteObject(phoneCallReqYQVO.getUuid() + "PlayEventCallbackPlaystop");
                    redisCache.deleteObject(phoneCallReqYQVO.getUuid() + "noVoice");
                    //在redis中保存一下结束语,在调用挂电话的方法时删除
                    ServiceTask serviceTask = serviceTaskService.selectServiceTaskByTaskid(serviceSubtask.getTaskid());
                    redisCache.setCacheObject(phoneCallReqYQVO.getUuid() + "- jsy", serviceTask.getJsy(), 120, TimeUnit.MINUTES);
                    phoneCallBackYQVO.setType("text");
                    phoneCallBackYQVO.setValue(phoneCallBackYQVO.getCommonValue() + serviceTask.getJsy());
                    //将结果写进detail表
                    ServiceSubTaskDetailReq ssdReq = new ServiceSubTaskDetailReq();
                    List<ServiceSubtaskDetail> serviceSubtaskDetailList2 = new ArrayList<>();
                    serviceSubtaskDetailList.add(getServiceSubtaskDetail(phoneCallReqYQVO, ivrTaskTemplateScriptVO, serviceSubtask, ivrTaskTemplate));
                    serviceSubTaskDetailReq.setServiceSubtaskDetailList(serviceSubtaskDetailList2);
                    ssdReq.setGuid(phoneCallReqYQVO.getGuid());
                    ssdReq.setOrgid(phoneCallReqYQVO.getOrgid());
                    saveQuestionAnswerPhone(ssdReq);
                    return phoneCallBackYQVO;
                }
                for (IvrTaskTemplateScript script : ivrTaskTemplateScripts) {
                    if (script.getSort() == nextQuestion.intValue()) {
                        phoneCallBackYQVO.setType("text");
                        phoneCallBackYQVO.setSilent_interval(ivrTaskTemplate.getSilencetime().intValue());
                        String scriptContent = StringUtils.isNotEmpty(script.getIvrtext()) ? script.getIvrtext() : script.getScriptContent();
                        log.error("下一题问题:{}", scriptContent);
                        log.error("下一题的子任务是:{}", serviceSubtask);
                        phoneCallBackYQVO.setValue(phoneCallBackYQVO.getCommonValue() + getObject(serviceSubtask, scriptContent));
                        //将该患者的Redis中的题目ID,进行修改
                        redisCache.setCacheObject(phoneCallReqYQVO.getTaskid().trim() + "-" + phoneCallReqYQVO.getPhone().trim(), script.getId().toString(), 120, TimeUnit.MINUTES);
                        //删除无响应
                        redisCache.deleteObject(phoneCallReqYQVO.getTaskid().trim() + "&&" + "mate" + "&&" + phoneCallReqYQVO.getUuid());
                    }
                }
            } else if (ivrTaskTemplateScriptVO.getNextScriptno() == null || ivrTaskTemplateScriptVO.getNextScriptno() == 0) {
                ServiceTask serviceTask1 = serviceTaskService.selectServiceTaskByTaskid(serviceSubtask.getTaskid());
                phoneCallBackYQVO.setType("text");
                phoneCallBackYQVO.setSilent_interval(ivrTaskTemplate.getSilencetime().intValue());
                //更新一下分数
                double score = 0.0;
                Object scoreObj = redisCache.getCacheObject(phoneCallReqYQVO.getUuid() + "SCORE");
                if (ObjectUtils.isNotEmpty(scoreObj)) score = (double) scoreObj;
                serviceSubtask.setScore(BigDecimal.valueOf(score));
                serviceSubtask.setFinishtime(new Date());
                serviceSubtask.setSendstate(6L);
                serviceSubtask.setRemark("电话拨打已完成");
                serviceSubtaskMapper.updateServiceSubtask(serviceSubtask);
                //记录状态
                setFailPreachForm(serviceSubtask, "3", "电话拨打已完成", "9");
                //设置结束语
                phoneCallBackYQVO.setValue(phoneCallBackYQVO.getCommonValue() + serviceTask1.getJsy());
                Long id = serviceSubtask.getId();
                Map<String, String> map = delRedisValue(null, id.toString());
                log.error("map的值为:{}", map);
                if (ObjectUtils.isNotEmpty(map))
                    redisCache.setCacheObject(map.get("cacheName"), map.get("val"));
                redisCache.deleteObject(serviceSubtask.getId() + "-" + serviceSubtask.getPhone());
                redisCache.deleteObject(phoneCallReqYQVO.getTaskid().trim() + "&&" + "mate" + "&&" + phoneCallReqYQVO.getUuid());
                redisCache.deleteObject(phoneCallReqYQVO.getUuid() + "SCORE");
                redisCache.deleteObject(phoneCallReqYQVO.getUuid() + "PlayEventCallbackPlaystop");
                //在redis中保存一下结束语,在调用挂电话的方法时删除
                ServiceTask serviceTask = serviceTaskService.selectServiceTaskByTaskid(serviceSubtask.getTaskid());
                redisCache.setCacheObject(phoneCallReqYQVO.getUuid() + "- jsy", serviceTask.getJsy(), 120, TimeUnit.MINUTES);
//                                return phoneCallBackYQVO;
            }
        } else {
            log.info("没有匹配上----------------------------");
            //flag=0,说明没 匹配上,也要把患者说的话记录下来
            if (matched == ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().size() - 1 && flag == 0) {
                ServiceSubTaskDetailReq serviceSubTaskDetailReq = new ServiceSubTaskDetailReq();
                List<ServiceSubtaskDetail> serviceSubtaskDetailList = new ArrayList<>();
                ServiceSubtaskDetail serviceSubtaskDetail = getServiceSubtaskDetail(phoneCallReqYQVO, ivrTaskTemplateScriptVO, serviceSubtask, ivrTaskTemplate);
                //修改一下语音路径(改成前端可以访问的,存到数据库中)
                if (StringUtils.isNotEmpty(serviceSubtaskDetail.getQuestionvoice())) {
                    String questionvoice = serviceSubtaskDetail.getQuestionvoice();
                    String[] split = questionvoice.split("\\\\");
                    if (split.length > 0) {
                        String lastPart = split[split.length - 1];
                        serviceSubtaskDetail.setQuestionvoice(voicePathPrefix + lastPart);
                    }
                }
                serviceSubtaskDetailList.add(serviceSubtaskDetail);
                //如果没有 匹配上,这个必须为null
                serviceSubtaskDetailList.get(0).setMatchedtext("");
                serviceSubTaskDetailReq.setServiceSubtaskDetailList(serviceSubtaskDetailList);
                serviceSubTaskDetailReq.setGuid(phoneCallReqYQVO.getGuid());
                serviceSubTaskDetailReq.setOrgid(phoneCallReqYQVO.getOrgid());
                saveQuestionAnswerPhone(serviceSubTaskDetailReq);
            }
        }
        return back;
    }
    @Test
    public void TestMatch(){
        String questionText = "您好,请问您是患者本人还是家属?\n";
        String voiceText = "我自己";
        String matchedText = "";
        List<IvrTaskTemplateTargetoption> options = new ArrayList<>();
        IvrTaskTemplateTargetoption ivrTaskTemplateTargetoption1 = new IvrTaskTemplateTargetoption();
        ivrTaskTemplateTargetoption1.setTargetvalue("本人");
        options.add(ivrTaskTemplateTargetoption1);
        IvrTaskTemplateTargetoption ivrTaskTemplateTargetoption2 = new IvrTaskTemplateTargetoption();
        ivrTaskTemplateTargetoption2.setTargetvalue("家属");
        options.add(ivrTaskTemplateTargetoption2);
        IvrTaskTemplateTargetoption ivrTaskTemplateTargetoption3 = new IvrTaskTemplateTargetoption();
        ivrTaskTemplateTargetoption3.setTargetvalue("其他");
        options.add(ivrTaskTemplateTargetoption3);
        Integer matched = matchOptionIndex(questionText,voiceText,options,"1");
        if(ObjectUtils.isNotEmpty(matched)){
            if(matched >= 0){
                matchedText = options.get(matched).getTargetvalue();
            }
        }
        log.info("ai匹配成功,匹配结果是:" + matchedText);
    }
}