smartor/src/main/java/com/smartor/service/impl/ServiceSubtaskServiceImpl.java
@@ -6,7 +6,6 @@
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;
@@ -18,9 +17,9 @@
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.common.ScheduleSubtaskBuilder;
import com.smartor.config.PhoneUtils;
import com.smartor.domain.DTO.ServiceSubtaskDetailDTO;
import com.smartor.domain.*;
@@ -33,7 +32,6 @@
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;
@@ -88,6 +86,8 @@
    @Autowired
    private IServiceTaskdeptService serviceTaskdeptService;
    @Autowired
    private IServiceTaskmedicalService serviceTaskmedicalService;
    @Autowired
    private IServiceTaskdiagService serviceTaskdiagService;
@@ -121,6 +121,15 @@
    @Autowired
    private IServiceSubtaskDetailTraceService traceService;
    @Autowired
    private IServiceTaskScheduleService serviceTaskScheduleService;
    @Autowired
    private IPatArchiveService patArchiveService;
    @Autowired
    private ScheduleSubtaskBuilder scheduleSubtaskBuilder;
    @Autowired
    private FtpService ftpService;
@@ -403,7 +412,16 @@
            if (serviceSubtaskEntity.getSendstateView() == 3)
                serviceSubtaskEntity.setSendstates(new ArrayList<>(Arrays.asList(4L)));
        }
        //患者获取随访任务-需要查询同个患者(身份证)所有的任务 景宁
        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<ServiceSubtaskRes> serviceSubtaskResList = new ArrayList<>();
@@ -584,9 +602,30 @@
     * @return 结果
     */
    @Override
    public int insertServiceSubtask(ServiceSubtask serviceSubtask) {
    public Map<String, Object> insertServiceSubtask(ServiceSubtask serviceSubtask) {
        serviceSubtask.setCreateTime(DateUtils.getNowDate());
        return serviceSubtaskMapper.insertServiceSubtask(serviceSubtask);
        serviceSubtaskMapper.insertServiceSubtask(serviceSubtask);
        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.setCreateTime(new Date());
        serviceOutPath.setParam6(subId);
        serviceOutPath.setOrgid(serviceSubtask.getOrgid());
        iServiceOutPathService.insertServiceOutPath(serviceOutPath);
        //转成16进制
        String format = String.format("%03X", serviceOutPath.getId());
        serviceOutPath.setRadix(format);
        serviceOutPath.setUpdateTime(new Date());
        iServiceOutPathService.updateServiceOutPath(serviceOutPath);
        Map<String, Object> result = new HashMap<>();
        result.put("url", localIP + ":" + req_path + "/wt?p=" + format);
        result.put("subId",serviceSubtask.getId());
        return result;
    }
    /**
@@ -834,6 +873,59 @@
                            serviceTaskoperService.insertServiceTaskoper(serviceTaskoper);
                        }
                    }
                } else if (serviceTaskVO.getAppltype().equals("5")) {
                    // 任务组:必须为长期任务(否则 findMatchingTaskIds 扫不到,无法自动纳入新出院患者)
                    if (serviceTaskVO.getLongTask() == null || serviceTaskVO.getLongTask() != 1) {
                        throw new BaseException("appltype=5 任务组必须为长期任务(longTask=1)");
                    }
                    // 保存计划 + 明细
                    ServiceTaskScheduleVO scheduleVO = serviceTaskVO.getServiceTaskScheduleVO();
                    if (scheduleVO == null) {
                        throw new BaseException("appltype=5 任务组:serviceTaskScheduleVO 不能为空");
                    }
                    scheduleVO.setTaskid(serviceTask.getTaskid());
                    scheduleVO.setOrgid(serviceTask.getOrgid());
                    scheduleVO.setCampusid(serviceTask.getCampusid());
                    scheduleVO.setCreateBy(serviceTask.getCreateBy());
                    scheduleVO.setDeptCodes(serviceTaskVO.getDeptcode());
                    scheduleVO.setWardCodes(serviceTaskVO.getLeavehospitaldistrictcode());
                    scheduleVO.setIcd10Codes(serviceTaskVO.getIcd10code());
                    scheduleVO.setOplevelCodes(serviceTaskVO.getOplevelcode());
                    log.info("----serviceTaskSchedule的值为:{}", scheduleVO);
                    serviceTaskScheduleService.saveScheduleWithDetail(scheduleVO);
                } else if (serviceTaskVO.getAppltype().equals("6")) {
                    ServiceTaskmedical serviceTaskmedical = new ServiceTaskmedical();
                    serviceTaskmedical.setTaskId(serviceTask.getTaskid());
                    serviceTaskmedical.setTaskName(serviceTask.getTaskName());
                    serviceTaskmedical.setServiceType(serviceTaskVO.getServiceType());
                    serviceTaskmedical.setLongtask(Long.valueOf(serviceTask.getLongTask()));
                    if (serviceTaskVO.getLongTask() == 1) serviceTaskmedical.setLongtask(1L);
                    serviceTaskmedical.setMedicalCode(serviceTaskVO.getIcd10code());
                    serviceTaskmedical.setMedicalName(serviceTaskVO.getIcd10name());
                    serviceTaskmedical.setGuid(serviceTask.getGuid());
                    serviceTaskmedical.setOrgid(serviceTask.getOrgid());
                    serviceTaskmedical.setCreateTime(new Date());
                    serviceTaskmedical.setDeptCode(serviceTaskVO.getDeptcode());
                    serviceTaskmedical.setDeptName(serviceTaskVO.getDeptname());
                    serviceTaskmedical.setWardCode(serviceTaskVO.getLeavehospitaldistrictcode());
                    serviceTaskmedical.setWardName(serviceTaskVO.getLeavehospitaldistrictname());
                    //南华附一的icd10code是有重复的,所以不能用南华附一的icd10code去查询
                    log.info("----serviceTaskmedical的值为:{}", serviceTaskmedical);
                    if (!StringUtils.isEmpty(serviceTaskVO.getMedicalName())) {
                        String[] medicalNames = serviceTaskVO.getMedicalName().split(",");
                        String[] medicalcodes = serviceTaskVO.getMedicalCode().split(",");
                        String[] medicalCompanys = serviceTaskVO.getMedicalCompany().split(",");
                        for (int i = 0; i < medicalNames.length; i++) {
                            serviceTaskmedical.setMedicalCode(medicalNames[i]);
                            serviceTaskmedical.setMedicalName(medicalcodes[i]);
                            if (ObjectUtils.isNotEmpty(medicalCompanys))
                                serviceTaskmedical.setMedicalCompany(medicalcodes[i]);
                            serviceTaskmedicalService.insertServiceTaskmedical(serviceTaskmedical);
                        }
                    }
                } else {
                    ServiceTaskdept serviceTaskdept = new ServiceTaskdept();
                    serviceTaskdept.setTaskId(serviceTask.getTaskid());
@@ -919,6 +1011,20 @@
                        serviceSubtask.setLongSendTime(patTaskRelevance.getLongSendTime());
                        serviceSubtask.setVisitTime(patTaskRelevance.getVisittime());
                    }
                    // ===== appltype=5 任务组:按 schedule 的 detailList 派生多条 subtask =====
                    if ("5".equals(serviceTaskVO.getAppltype())) {
                        ServiceTaskSchedule schedule = serviceTaskScheduleService.selectByTaskid(serviceTask.getTaskid());
                        if (schedule == null) {
                            throw new BaseException("appltype=5 任务组:未找到 taskid=" + serviceTask.getTaskid() + " 的计划");
                        }
                        List<ServiceTaskScheduleDetail> detailList =
                                serviceTaskScheduleService.selectDetailByScheduleid(schedule.getId());
                        scheduleSubtaskBuilder.buildSubtasksForPatient(
                                serviceTask, serviceTaskVO.getPreachformList(), schedule, detailList, patTaskRelevance, serviceSubtask);
                        continue;
                    }
                    serviceSubtaskMapper.insertServiceSubtask(serviceSubtask);
                    serviceSubtask.getId().intValue();
@@ -1093,6 +1199,51 @@
                        }
                    }
                } else if (serviceTaskVO.getAppltype().equals("6")) {
                    ServiceTaskmedical serviceTaskmedical = new ServiceTaskmedical();
                    serviceTaskmedical.setTaskId(serviceTask.getTaskid());
                    serviceTaskmedical.setTaskName(serviceTask.getTaskName());
                    if (StringUtils.isEmpty(serviceTaskVO.getIcd10code())) {
                        throw new BaseException("药品编码为空");
                    }
                    serviceTaskmedical.setLongtask(Long.valueOf(serviceTask.getLongTask()));
                    serviceTaskmedical.setMedicalName(serviceTaskVO.getMedicalName());
                    serviceTaskmedical.setGuid(serviceTask1.getGuid());
                    serviceTaskmedical.setServiceType(serviceTask1.getServiceType());
                    serviceTaskmedical.setOrgid(serviceTask1.getOrgid());
                    serviceTaskmedical.setDeptCode(serviceTask1.getDeptcode());
                    serviceTaskmedical.setDeptName(serviceTask1.getDeptname());
                    serviceTaskmedical.setWardCode(serviceTask1.getLeavehospitaldistrictcode());
                    serviceTaskmedical.setWardName(serviceTask1.getLeavehospitaldistrictname());
                    serviceTaskmedical.setUpdateTime(new Date());
                    if (StringUtils.isNotEmpty(serviceTaskVO.getMedicalCode())) {
                        String[] medicalCodes = serviceTaskVO.getMedicalCode().split(",");
                        String[] medicalNames = serviceTaskVO.getMedicalName().split(",");
                        String[] medicalCompany = serviceTaskVO.getMedicalCompany().split(",");
                        if (!active.equals("nhfy") && !active.equals("druid")) {
                            if (medicalCodes.length != medicalNames.length) {
                                throw new BaseException("药品编码和药品名称数量不一致");
                            }
                        }
                        for (int i = 0; i < medicalCodes.length; i++) {
                            //先查询一下是否存在
                            ServiceTaskmedical serviceTaskmedical1 = new ServiceTaskmedical();
                            serviceTaskmedical1.setTaskId(serviceTaskVO.getTaskid());
                            serviceTaskmedical1.setMedicalCode(medicalCodes[i]);
                            serviceTaskmedical1.setMedicalName(medicalNames[i]);
                            List<ServiceTaskmedical> serviceTaskmedicals = serviceTaskmedicalService.selectServiceTaskmedicalList(serviceTaskmedical1);
                            serviceTaskmedical.setMedicalCode(medicalCodes[i]);
                            serviceTaskmedical.setMedicalName(medicalNames[i]);
                            log.info("----serviceTaskmedical更新的值为:{}", serviceTaskmedical);
                            if (CollectionUtils.isNotEmpty(serviceTaskmedicals)) {
                                serviceTaskmedical.setId(serviceTaskmedicals.get(0).getId());
                                serviceTaskmedicalService.updateServiceTaskmedical(serviceTaskmedical);
                            } else {
                                serviceTaskmedicalService.insertServiceTaskmedical(serviceTaskmedical);
                            }
                        }
                    }
                } else if (serviceTaskVO.getAppltype().equals("4")) {
                    ServiceTaskoper serviceTaskoper = new ServiceTaskoper();
//                    if (StringUtils.isEmpty(serviceTaskVO.getOpcode())) {
@@ -1133,6 +1284,24 @@
                            }
                        }
                    }
                } else if (serviceTaskVO.getAppltype().equals("5")) {
                    // 任务组修改:删旧 schedule + 明细,重建。subtask 上面已批量清理。
                    if (serviceTaskVO.getLongTask() == null || serviceTaskVO.getLongTask() != 1) {
                        throw new BaseException("appltype=5 任务组必须为长期任务(longTask=1)");
                    }
                    ServiceTaskScheduleVO scheduleVO = serviceTaskVO.getServiceTaskScheduleVO();
                    if (scheduleVO == null) {
                        throw new BaseException("appltype=5 任务组:serviceTaskScheduleVO 不能为空");
                    }
                    scheduleVO.setTaskid(serviceTask.getTaskid());
                    scheduleVO.setOrgid(serviceTask.getOrgid());
                    scheduleVO.setCampusid(serviceTask.getCampusid());
                    scheduleVO.setDeptCodes(serviceTaskVO.getDeptcode());
                    scheduleVO.setWardCodes(serviceTaskVO.getLeavehospitaldistrictcode());
                    scheduleVO.setIcd10Codes(serviceTaskVO.getIcd10code());
                    scheduleVO.setOplevelCodes(serviceTaskVO.getOplevelcode());
                    scheduleVO.setCreateBy(serviceTask.getUpdateBy());
                    serviceTaskScheduleService.saveScheduleWithDetail(scheduleVO);
                }
            }
            if (ObjectUtils.isNotEmpty(serviceTaskVO.getIvrTaskTemplateVO())) {
@@ -1186,6 +1355,20 @@
                    serviceSubtask.setVisitTime(patTaskRelevance.getVisittime());
                    serviceSubtask.setTextParam(new Gson().toJson(serviceTaskVO.getTextParam()));
                    if (patTaskRelevance.getIsoperation() != null) {
                        // appltype=5 任务组:追加患者(isoperation=1)走计划派生;修改(2)/删除(3)仍走原逻辑
                        if ("5".equals(serviceTaskVO.getAppltype()) && patTaskRelevance.getIsoperation() == 1) {
                            ServiceTaskSchedule schedule = serviceTaskScheduleService.selectByTaskid(serviceTask.getTaskid());
                            if (schedule == null) {
                                throw new BaseException("appltype=5 任务组:未找到 taskid=" + serviceTask.getTaskid() + " 的计划");
                            }
                            List<ServiceTaskScheduleDetail> detailList =
                                    serviceTaskScheduleService.selectDetailByScheduleid(schedule.getId());
                            serviceSubtask.setUpdateBy(serviceTask.getUpdateBy());
                            serviceSubtask.setCreateBy(serviceTask.getUpdateBy());
                            scheduleSubtaskBuilder.buildSubtasksForPatient(
                                    serviceTask, serviceTaskVO.getPreachformList(), schedule, detailList, patTaskRelevance, serviceSubtask);
                            continue;
                        }
                        if (patTaskRelevance.getIsoperation() == 2) {
                            serviceSubtask.setUpdateBy(serviceTask.getUpdateBy());
                            serviceSubtaskMapper.updateServiceSubtaskByCondition(serviceSubtask);
@@ -1238,7 +1421,6 @@
        map.put("taskId", serviceTask.getTaskid().intValue());
        return map;
    }
    @Override
    public void phoneCallBack(PhoneCallBackVO phoneCallBackVO) {
@@ -1740,14 +1922,10 @@
                //用来标记,是否有匹配上的
                Integer flag = 0;
                //1-AI识别
                SysConfig configVoiceMatchAi = new SysConfig();
                configVoiceMatchAi.setOrgid(phoneCallReqYQVO.getOrgid());
                configVoiceMatchAi.setConfigKey("sys.voice.match.ai");
                SysConfig matchAi = sysConfigMapper.selectConfig(configVoiceMatchAi);
                String url = configService.selectConfigByKey("sys.voice.match.ai.url",phoneCallReqYQVO.getOrgid());
            if (ObjectUtils.isNotEmpty(matchAi) && StringUtils.isNotEmpty(matchAi.getConfigValue())
                    && matchAi.getConfigValue().equals("1")) {
                PhoneCallBackYQVO aiMatchResult = AiMatch(phoneCallReqYQVO, phoneCallBackYQVO,
            if (ObjectUtils.isNotEmpty(url)) {
                PhoneCallBackYQVO aiMatchResult = AiMatch(url, phoneCallReqYQVO, phoneCallBackYQVO,
                        ivrTaskTemplateScriptVO, serviceSubtask, ivrTaskTemplate,
                        ivrTaskTemplateScripts, scriptId, flag, phoneCallReqYQVO.getUuid());
                if(aiMatchResult != null){
@@ -2617,16 +2795,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";
            }
@@ -2639,11 +2822,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")));
@@ -4981,7 +5167,7 @@
     * @param options
     * @return
     */
    public Integer matchOptionIndex(String questionText, String voiceText, List<IvrTaskTemplateTargetoption> options, String uuid){
    public Integer matchOptionIndex(String url, String questionText, String voiceText, List<IvrTaskTemplateTargetoption> options, String uuid){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userQuestion",questionText);
        jsonObject.put("userAnswer",voiceText);
@@ -4990,8 +5176,7 @@
        String jsonString = jsonObject.toJSONString();
        String result = null;
        String url = "http://localhost:8099/matchOptionIndex";
//        String url = configService.selectConfigByKey("sys.voice.match.ai.url");
//        String url = "http://localhost:8099/matchOptionIndex";
        if(StringUtils.isNotEmpty(url)){
            try {
                Map<String, String> headers = new HashMap<>();
@@ -5009,7 +5194,7 @@
        }
    }
    public PhoneCallBackYQVO AiMatch(PhoneCallReqYQVO phoneCallReqYQVO, PhoneCallBackYQVO phoneCallBackYQVO,
    public PhoneCallBackYQVO AiMatch(String url, PhoneCallReqYQVO phoneCallReqYQVO, PhoneCallBackYQVO phoneCallBackYQVO,
                        IvrTaskTemplateScriptVO ivrTaskTemplateScriptVO, ServiceSubtask serviceSubtask, IvrTaskTemplate ivrTaskTemplate,
                        List<IvrTaskTemplateScript> ivrTaskTemplateScripts, String scriptId, Integer flag, String uuid){
        PhoneCallBackYQVO back = null;
@@ -5017,7 +5202,7 @@
        String voiceText = phoneCallReqYQVO.getAsrtext();
        String questionText = StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrtext()) ? ivrTaskTemplateScriptVO.getIvrtext() : ivrTaskTemplateScriptVO.getScriptContent();
        Integer matched = -1;
        matched = matchOptionIndex(questionText, voiceText, options, uuid);
        matched = matchOptionIndex(url, questionText, voiceText, options, uuid);
        //说明匹配正确了
        if(matched != null && matched >= 0){
            IvrTaskTemplateTargetoption chosenOption = options.get(matched);
@@ -5253,8 +5438,9 @@
        IvrTaskTemplateTargetoption ivrTaskTemplateTargetoption3 = new IvrTaskTemplateTargetoption();
        ivrTaskTemplateTargetoption3.setTargetvalue("其他");
        options.add(ivrTaskTemplateTargetoption3);
        Integer matched = matchOptionIndex(questionText,voiceText,options,"1");
        //1-AI识别
        String url = configService.selectConfigByKey("sys.voice.match.ai.url");
        Integer matched = matchOptionIndex(url, questionText,voiceText,options,"1");
        if(ObjectUtils.isNotEmpty(matched)){
            if(matched >= 0){
                matchedText = options.get(matched).getTargetvalue();