Commit 59b38906 authored by gaozhaochen's avatar gaozhaochen

update: 更新智能机器人问答接口

parent be0b34f5
......@@ -39,7 +39,7 @@ public class WoaUtil {
/**
* 获取用户信息请求地址
*/
private final static String WOA_XSZY_USER_INFO_API_URL = WOA_XSZY_PROXY_PASS_URL + WOA_XSZY_GATEWAY_URL;
private final static String WOA_XSZY_USER_INFO_API_URL = WOA_XSZY_URL + WOA_XSZY_GATEWAY_URL;
/**
* 接口授权信息
......
package cn.sh.stc.sict.cloud.upms.dto;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/**
* @Description 当前登陆人信息
......@@ -35,4 +38,9 @@ public class CurrentUser implements Serializable {
*/
private String xszyUserInfo;
/**
* 机器人问答
*/
private List<List<String>> qaHistory;
}
package cn.sh.stc.sict.theme.hpgp.controller.mobile;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.HttpRequest;
import cn.sh.stc.sict.cloud.common.core.util.R;
import cn.sh.stc.sict.cloud.common.log.annotation.SysLog;
import cn.sh.stc.sict.cloud.common.security.util.SecurityUtils;
import cn.sh.stc.sict.cloud.upms.dto.CurrentUser;
import cn.sh.stc.sict.theme.hpgp.vo.IntelligentAnswerVO;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.api.client.util.Lists;
import com.google.common.collect.Sets;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
import java.util.List;
import java.util.Set;
/**
......@@ -29,54 +28,111 @@ import java.util.Set;
*/
@Slf4j
@RestController
@AllArgsConstructor
@Api(tags = "[C]黄浦精准预约——智能问答")
@RequestMapping("/hpgp/c/intelligent/qa")
public class HpGpIntelligentQAController {
private final static String CONTENT = "Content";
@Value("${hpgp-guide.qa-url:http://180.169.131.147:8000/}")
private String qaUrl;
@Value("${hpgp-guide.qa-history-limit:3}")
private Integer qaHistoryLimit;
private final static String QA_RESULT_FLAG = "Helper: ";
/**
* 打浦桥科室
*/
private final static Set<String> DPQ_DEPARTMENT = Sets.newHashSet("皮肤性病科", "中医科", "儿保科", "全科医疗科");
// @SysLog
// @ApiOperation("智能问答")
// @PostMapping
// public R<?> qa(String question) {
// CurrentUser current = SecurityUtils.getCurrentUser();
// String from = StrUtil.isNotBlank(current.getOpenId()) ? current.getOpenId() : current.getId().toString();
//
// String xml = "<xml>\n" +
// " <ToUserName><![CDATA[huangpurobot]]></ToUserName>\n" +
// " <FromUserName><![CDATA[" + from + "]]></FromUserName>\n" +
// " <CreateTime>" + DateUtil.now() + "</CreateTime>\n" +
// " <MsgType><![CDATA[text]]></MsgType>\n" +
// " <Content><![CDATA[" + question + "]]></Content>\n" +
// " <MsgId>" + IdWorker.getId() + "</MsgId>\n" +
// "</xml>";
// // 162 为黄浦高血压专病导医机器人
// // 51 黄浦高血压全科导诊机器人
// String body = HttpRequest.post(" http://www.365jqr.com/jqrsvr.ashx?Command=talk_51")
// .body(xml)
// .execute()
// .body();
// log.error("孙总接口请求 xml = {}", xml);
// log.error("孙总接口返回 body = {}", body);
// IntelligentAnswerVO answerVO = new IntelligentAnswerVO();
// Map<String, Object> result = XmlUtil.xmlToMap(body);
// if (result.containsKey(CONTENT)) {
// answerVO.setAnswer(MapUtil.getStr(result, CONTENT));
// if (StrUtil.isNotBlank(current.getHospitalCode())
// && "42502942300".equals(current.getHospitalCode())) {
// if (StrUtil.isNotBlank(answerVO.getDeptName())
// && !DPQ_DEPARTMENT.contains(answerVO.getDeptName())) {
// String oldDeptName = answerVO.getDeptName();
// answerVO.setAnswer(answerVO.getAnswer().replace(oldDeptName, "全科医疗科"));
// answerVO.setDeptName("全科医疗科");
// }
// }
// }
// return new R<>(answerVO);
// }
@SysLog
@ApiOperation("智能问答")
@PostMapping
public R<?> qa(String question) {
public R qaV2(String question) {
CurrentUser current = SecurityUtils.getCurrentUser();
String from = StrUtil.isNotBlank(current.getOpenId()) ? current.getOpenId() : current.getId().toString();
String xml = "<xml>\n" +
" <ToUserName><![CDATA[huangpurobot]]></ToUserName>\n" +
" <FromUserName><![CDATA[" + from + "]]></FromUserName>\n" +
" <CreateTime>" + DateUtil.now() + "</CreateTime>\n" +
" <MsgType><![CDATA[text]]></MsgType>\n" +
" <Content><![CDATA[" + question + "]]></Content>\n" +
" <MsgId>" + IdWorker.getId() + "</MsgId>\n" +
"</xml>";
List<List<String>> qaHistory = current.getQaHistory();
String from = current.getId().toString();
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", from);
if (CollUtil.isEmpty(qaHistory)) {
qaHistory = Lists.newArrayListWithCapacity(1);
}
if (qaHistory.size() > qaHistoryLimit) {
qaHistory = qaHistory.subList(0, qaHistoryLimit - 1);
}
List<String> currentQa = Lists.newArrayList();
currentQa.add(question);
currentQa.add(null);
qaHistory.add(currentQa);
jsonObject.put("history", qaHistory);
String req = JSON.toJSONString(jsonObject);
// 162 为黄浦高血压专病导医机器人
// 51 黄浦高血压全科导诊机器人
String body = HttpRequest.post("http://30.30.5.74:9988/qa/jqrsvr.ashx?Command=talk_51")
.body(xml)
String body = HttpRequest.post(qaUrl)
.body(req, "application/json")
.execute()
.body();
log.error("孙总接口请求 xml = {}", xml);
log.error("孙总接口返回 body = {}", body);
IntelligentAnswerVO answerVO = new IntelligentAnswerVO();
Map<String, Object> result = XmlUtil.xmlToMap(body);
if (result.containsKey(CONTENT)) {
answerVO.setAnswer(MapUtil.getStr(result, CONTENT));
if (StrUtil.isNotBlank(current.getHospitalCode())
&& "42502942300".equals(current.getHospitalCode())) {
if (StrUtil.isNotBlank(answerVO.getDeptName())
&& !DPQ_DEPARTMENT.contains(answerVO.getDeptName())) {
String oldDeptName = answerVO.getDeptName();
answerVO.setAnswer(answerVO.getAnswer().replace(oldDeptName, "全科医疗科"));
answerVO.setDeptName("全科医疗科");
}
}
log.error("接口请求 req = {}", req);
log.error("接口返回 body = {}", body);
if (StrUtil.isBlank(body) || !body.contains(QA_RESULT_FLAG)) {
return new R();
}
return new R<>(answerVO);
body = body.replaceAll(QA_RESULT_FLAG, "");
body = body.replaceAll("<[.[^>]]*>", "");
currentQa.set(1, body);
current.setQaHistory(qaHistory);
SecurityUtils.updateCurrent(current);
IntelligentAnswerVO answerVO = new IntelligentAnswerVO();
answerVO.setAnswer(body);
return new R(answerVO);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment