|
|
|
@ -2,21 +2,16 @@ package com.supervision.service.impl;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.codec.Base64Encoder;
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
|
|
import cn.hutool.core.io.IoUtil;
|
|
|
|
|
import cn.hutool.core.lang.Assert;
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
import cn.hutool.crypto.digest.MD5;
|
|
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
|
import cn.hutool.setting.dialect.Props;
|
|
|
|
|
import com.supervision.domain.GlobalResult;
|
|
|
|
|
import com.supervision.exception.BusinessException;
|
|
|
|
|
import com.supervision.feign.RasaManageFeignClient;
|
|
|
|
|
import com.supervision.model.Process;
|
|
|
|
|
import com.supervision.model.*;
|
|
|
|
|
import com.supervision.pojo.vo.TalkReqVO;
|
|
|
|
|
import com.supervision.pojo.vo.TalkResultResVO;
|
|
|
|
|
import com.supervision.pojo.vo.TalkVideoReqVO;
|
|
|
|
|
import com.supervision.service.*;
|
|
|
|
@ -26,12 +21,8 @@ import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Optional;
|
|
|
|
@ -55,11 +46,7 @@ public class AskServiceImpl implements AskService {
|
|
|
|
|
|
|
|
|
|
private final RasaManageFeignClient rasaManageFeignClient;
|
|
|
|
|
|
|
|
|
|
@Value("${answer.defaultNoMatchId}")
|
|
|
|
|
private String defaultNoMatchId;
|
|
|
|
|
|
|
|
|
|
@Value("${answer.defaultNoMatchAnswerMessage:}")
|
|
|
|
|
private String defaultNoMatchAnswerMessage;
|
|
|
|
|
private final CommonDicService commonDicService;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String receiveVoiceFile(MultipartFile file) {
|
|
|
|
@ -127,6 +114,7 @@ public class AskServiceImpl implements AskService {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public TalkResultResVO talkByVideo(TalkVideoReqVO talkReqVO) {
|
|
|
|
|
|
|
|
|
|
// 根据processId找到对应的病人
|
|
|
|
|
Process process = Optional.ofNullable(processService.getById(talkReqVO.getProcessId())).orElseThrow(() -> new BusinessException("未找到诊疗进程"));
|
|
|
|
|
// 调用rasa获取文字内容
|
|
|
|
@ -135,9 +123,12 @@ public class AskServiceImpl implements AskService {
|
|
|
|
|
TalkResultResVO talkResultResVO = new TalkResultResVO();
|
|
|
|
|
// 如果rasa没有识别出来,则返回默认值
|
|
|
|
|
if (StrUtil.isBlank(rasaResult)) {
|
|
|
|
|
talkResultResVO.setVideoBase64(getAnswerVideoBase64OrDefault(defaultNoMatchId));
|
|
|
|
|
talkResultResVO.setAnswerMessage(defaultNoMatchAnswerMessage);
|
|
|
|
|
saveQaRecord(talkReqVO.getProcessId(), "default", null, talkReqVO.getText(), null, "您好,我没有听懂您说什么");
|
|
|
|
|
AskPatientAnswer medicalRecErrorAnswer = getMedicalRecErrorAnswer(process.getMedicalRecId());
|
|
|
|
|
talkResultResVO.setVideoBase64(getAnswerVideoBase64OrDefault(medicalRecErrorAnswer.getAnswerResourceId()));
|
|
|
|
|
talkResultResVO.setAnswerMessage(medicalRecErrorAnswer.getAnswer());
|
|
|
|
|
saveQaRecord(talkReqVO.getProcessId(), "default", null, talkReqVO.getText(), null, medicalRecErrorAnswer.getAnswer());
|
|
|
|
|
talkResultResVO.setType(1);
|
|
|
|
|
return talkResultResVO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 这里校验,rasa回复的结果是不是action
|
|
|
|
@ -157,9 +148,10 @@ public class AskServiceImpl implements AskService {
|
|
|
|
|
AskTemplateQuestionLibrary library = askTemplateQuestionLibraryService.getById(rasaResult);
|
|
|
|
|
if (ObjectUtil.isEmpty(library)) {
|
|
|
|
|
log.info("{}:未从问题库中找到,回答未识别语句", rasaResult);
|
|
|
|
|
talkResultResVO.setVideoBase64(getAnswerVideoBase64OrDefault(defaultNoMatchId));
|
|
|
|
|
talkResultResVO.setAnswerMessage(defaultNoMatchAnswerMessage);
|
|
|
|
|
saveQaRecord(talkReqVO.getProcessId(), "default", null, talkReqVO.getText(), null, defaultNoMatchAnswerMessage);
|
|
|
|
|
AskPatientAnswer medicalRecErrorAnswer = getMedicalRecErrorAnswer(process.getMedicalRecId());
|
|
|
|
|
talkResultResVO.setVideoBase64(getAnswerVideoBase64OrDefault(medicalRecErrorAnswer.getAnswerResourceId()));
|
|
|
|
|
talkResultResVO.setAnswerMessage(medicalRecErrorAnswer.getAnswer());
|
|
|
|
|
saveQaRecord(talkReqVO.getProcessId(), "default", null, talkReqVO.getText(), null, medicalRecErrorAnswer.getAnswer());
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
AskPatientAnswer askPatientAnswer = askPatientAnswerService.lambdaQuery().eq(AskPatientAnswer::getMedicalId, process.getMedicalRecId())
|
|
|
|
@ -174,9 +166,10 @@ public class AskServiceImpl implements AskService {
|
|
|
|
|
saveQaRecord(talkReqVO.getProcessId(), "patient", askPatientAnswer.getId(), talkReqVO.getText(), library, resText);
|
|
|
|
|
}else {
|
|
|
|
|
log.info("{}:病历配置,从AskPatientAnswer中未找到回答结果,回复未识别到语句", rasaResult);
|
|
|
|
|
talkResultResVO.setVideoBase64(getAnswerVideoBase64OrDefault(defaultNoMatchId));
|
|
|
|
|
talkResultResVO.setAnswerMessage(defaultNoMatchAnswerMessage);
|
|
|
|
|
saveQaRecord(talkReqVO.getProcessId(), "default", null, talkReqVO.getText(), library, defaultNoMatchAnswerMessage);
|
|
|
|
|
AskPatientAnswer medicalRecErrorAnswer = getMedicalRecErrorAnswer(process.getMedicalRecId());
|
|
|
|
|
talkResultResVO.setVideoBase64(getAnswerVideoBase64OrDefault(medicalRecErrorAnswer.getAnswerResourceId()));
|
|
|
|
|
talkResultResVO.setAnswerMessage(medicalRecErrorAnswer.getAnswer());
|
|
|
|
|
saveQaRecord(talkReqVO.getProcessId(), "default", null, talkReqVO.getText(), library, medicalRecErrorAnswer.getAnswer());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -190,17 +183,29 @@ public class AskServiceImpl implements AskService {
|
|
|
|
|
rasaTalkVo.setSessionId(sessionId);
|
|
|
|
|
// 默认为1
|
|
|
|
|
rasaTalkVo.setModelId("1");
|
|
|
|
|
GlobalResult<List<String>> talk = rasaManageFeignClient.talk(rasaTalkVo);
|
|
|
|
|
log.info("调用rasa对话返回结果:{}", talk);
|
|
|
|
|
try {
|
|
|
|
|
GlobalResult<List<String>> talk = rasaManageFeignClient.talk(rasaTalkVo);
|
|
|
|
|
log.info("调用rasa对话返回结果:{}", talk);
|
|
|
|
|
if (talk.getCode() != 200 || CollUtil.isEmpty(talk.getData())) {
|
|
|
|
|
return "对不起,我没有听懂您说的是什么意思";
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return CollUtil.getFirst(talk.getData());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("talkRasa error ", e);
|
|
|
|
|
}
|
|
|
|
|
return "对不起,我没有听懂您说的是什么意思";
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private AskPatientAnswer getMedicalRecErrorAnswer(String medicalRecId) {
|
|
|
|
|
//Optional.ofNullable(medicalRecErrorAnswer).orElseGet(() ->new AskPatientAnswer()).getAnswer()
|
|
|
|
|
Assert.notEmpty(medicalRecId, "病历id不能为空");
|
|
|
|
|
CommonDic commonDic = commonDicService.lambdaQuery().eq(CommonDic::getGroupCode, "AQT").eq(CommonDic::getCode, "system_error").one();
|
|
|
|
|
Assert.notNull(commonDic, "字典未配置系统内置异常识别场景");
|
|
|
|
|
|
|
|
|
|
AskTemplateQuestionLibrary askTemplateQuestionLibrary = askTemplateQuestionLibraryService.lambdaQuery().eq(AskTemplateQuestionLibrary::getDictId, commonDic.getId()).one();
|
|
|
|
|
Assert.notNull(askTemplateQuestionLibrary, "知识库未配置系统内置异常识别场景");
|
|
|
|
|
|
|
|
|
|
return askPatientAnswerService.lambdaQuery().eq(AskPatientAnswer::getMedicalId, medicalRecId).eq(AskPatientAnswer::getLibraryQuestionId, askTemplateQuestionLibrary.getId()).one();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -211,14 +216,7 @@ public class AskServiceImpl implements AskService {
|
|
|
|
|
* @return 应答视频base64位编码
|
|
|
|
|
*/
|
|
|
|
|
private String getAnswerVideoBase64OrDefault(String fileResourceId) {
|
|
|
|
|
if (StrUtil.isBlank(fileResourceId)) {
|
|
|
|
|
fileResourceId = defaultNoMatchId;
|
|
|
|
|
}
|
|
|
|
|
FileResource fileResource = fileResourceService.getById(fileResourceId);
|
|
|
|
|
if (ObjectUtil.isEmpty(fileResource)) {
|
|
|
|
|
log.info("未找到视频,走默认视频");
|
|
|
|
|
fileResource = fileResourceService.getById(defaultNoMatchId);
|
|
|
|
|
}
|
|
|
|
|
try (InputStream inputStream = MinioUtil.download(fileResource.getMinioId())) {
|
|
|
|
|
return Base64Encoder.encode(IoUtil.readBytes(inputStream));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|