|
@ -0,0 +1,295 @@
|
|
|
package com.yihu.jw.im.service;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yihu.jw.entity.hospital.consult.WlyyHospitalSysDictDO;
|
|
|
import com.yihu.jw.entity.knowledge.*;
|
|
|
import com.yihu.jw.im.util.ImUtil;
|
|
|
import com.yihu.jw.knowledge.dao.*;
|
|
|
import com.yihu.jw.sms.dao.HospitalSysDictDao;
|
|
|
import com.yihu.jw.util.date.DateUtil;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 机器人
|
|
|
* Created by yeshijie on 2023/4/12.
|
|
|
*/
|
|
|
@Service
|
|
|
public class RobotService {
|
|
|
|
|
|
@Autowired
|
|
|
private ImUtil imUtil;
|
|
|
@Autowired
|
|
|
private BaseSystemDialogSettingDao dialogSettingDao;
|
|
|
@Autowired
|
|
|
private BaseKnowledgeDictDao knowledgeDictDao;
|
|
|
@Autowired
|
|
|
private BaseKnowledgeDictRelationDao knowledgeDictRelationDao;
|
|
|
@Autowired
|
|
|
private BaseKnowledgeQuestionDao knowledgeQuestionDao;
|
|
|
@Autowired
|
|
|
private BaseKnowledgeFlowConfigurationRelationDao knowledgeFlowConfigurationRelationDao;
|
|
|
@Autowired
|
|
|
private BaseKnowledgeQuestionsDao knowledgeQuestionsDao;
|
|
|
@Autowired
|
|
|
private BaseKnowledgeQuestionsRelationDao knowledgeQuestionsRelationDao;
|
|
|
@Autowired
|
|
|
private JdbcTemplate jdbcTemplate;
|
|
|
@Autowired
|
|
|
private ImService imService;
|
|
|
@Autowired
|
|
|
private HospitalSysDictDao hospitalSysDictDao;
|
|
|
|
|
|
//获取发送者
|
|
|
public WlyyHospitalSysDictDO getSender(){
|
|
|
WlyyHospitalSysDictDO dictDO = hospitalSysDictDao.findById("customer").orElse(null);
|
|
|
if(dictDO==null){
|
|
|
dictDO = new WlyyHospitalSysDictDO();
|
|
|
dictDO.setDictCode("customer");
|
|
|
dictDO.setDictValue("客服");
|
|
|
}
|
|
|
return dictDO;
|
|
|
}
|
|
|
|
|
|
//发送回复消息
|
|
|
public void sendReply(JSONObject json){
|
|
|
try {
|
|
|
WlyyHospitalSysDictDO dictDO = getSender();
|
|
|
imService.patientGuaidenceAppend(dictDO.getDictCode(),dictDO.getDictValue(),json.getString("session_id"),
|
|
|
json.getString("content_type"),json.getString("content"),"1",null);
|
|
|
}catch (Exception e){
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
json = null;
|
|
|
}
|
|
|
|
|
|
public void sendReplyMap(Map<String,JSONObject> map){
|
|
|
try {
|
|
|
WlyyHospitalSysDictDO dictDO = getSender();
|
|
|
for (JSONObject json:map.values()){
|
|
|
imService.patientGuaidenceAppend(dictDO.getDictCode(),dictDO.getDictValue(),json.getString("session_id"),
|
|
|
json.getString("content_type"),json.getString("content"),"1",null);
|
|
|
}
|
|
|
}catch (Exception e){
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
map = null;
|
|
|
}
|
|
|
|
|
|
//智能导致助手回复
|
|
|
public Map<String,JSONObject> robotReply(String session_id,String type,String content){
|
|
|
Map<String,JSONObject> map = new HashMap<>();
|
|
|
try {
|
|
|
List<BaseSystemDialogSetting> settingList = dialogSettingDao.selectBySystemTypeAndFlag(sessionTypeToType(type),1);
|
|
|
if(settingList.size()==0){
|
|
|
//未配置自动回复内容
|
|
|
return map;
|
|
|
}
|
|
|
Map<String,BaseSystemDialogSetting> mapSetting = settingList.stream().collect(Collectors.toMap(BaseSystemDialogSetting::getFunctionType, v -> v,(o1,o2)->o1));
|
|
|
//欢迎语
|
|
|
welcome(session_id,mapSetting,map);
|
|
|
//自动匹配回复文字内容
|
|
|
replyContent(session_id,type,content,mapSetting,map);
|
|
|
|
|
|
}catch (Exception e){
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
//常见问题匹配
|
|
|
public void replyContent(String type,String content,String consult){
|
|
|
try {
|
|
|
BaseSystemDialogSetting setting = dialogSettingDao.findBySystemTypeAndFunctionType(sessionTypeToType(type),"7",1);
|
|
|
if(setting==null){
|
|
|
return;
|
|
|
}
|
|
|
//查找相似问题
|
|
|
BaseKnowledgeQuestion question = findQuestionLikeName(content);
|
|
|
if(question!=null){
|
|
|
String answer = question.getAnswer();
|
|
|
WlyyHospitalSysDictDO dictDO = getSender();
|
|
|
imUtil.sendTopicIM(dictDO.getDictCode(), dictDO.getDictName(), consult, "1", answer,null);
|
|
|
}
|
|
|
}catch (Exception e){
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void replyContent(String session_id,String type,String content,Map<String,BaseSystemDialogSetting> mapSetting,Map<String,JSONObject> map){
|
|
|
try {
|
|
|
BaseSystemDialogSetting setting = null;
|
|
|
if(mapSetting==null){
|
|
|
setting = dialogSettingDao.findBySystemTypeAndFunctionType(sessionTypeToType(type),"7",1);
|
|
|
}else {
|
|
|
setting = mapSetting.get("7");
|
|
|
}
|
|
|
if(setting==null){
|
|
|
return;
|
|
|
}
|
|
|
//查找相似问题
|
|
|
BaseKnowledgeQuestion question = findQuestionLikeName(content);
|
|
|
if(question!=null){
|
|
|
String answer = question.getAnswer();
|
|
|
JSONObject result = new JSONObject();
|
|
|
result.put("content",answer);
|
|
|
result.put("content_type","1");
|
|
|
result.put("session_id",session_id);
|
|
|
map.put("7",result);
|
|
|
}
|
|
|
}catch (Exception e){
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void welcome(String session_id,Map<String,BaseSystemDialogSetting> mapSetting,Map<String,JSONObject> map){
|
|
|
try {
|
|
|
Boolean re = imUtil.sessionIsExist(session_id);
|
|
|
if(re){
|
|
|
//欢迎语
|
|
|
BaseSystemDialogSetting welcomeSetting = mapSetting.get("1");
|
|
|
if(welcomeSetting!=null){
|
|
|
String welcomeContent = welcomeSetting.getContent();
|
|
|
JSONObject result = new JSONObject();
|
|
|
result.put("content",welcomeContent);
|
|
|
result.put("content_type","1");
|
|
|
result.put("session_id",session_id);
|
|
|
map.put("1",result);
|
|
|
}
|
|
|
//欢迎消息关联问题
|
|
|
BaseSystemDialogSetting welcomeQueSetting = mapSetting.get("2");
|
|
|
if(welcomeQueSetting!=null){
|
|
|
Integer relaitonCodeType = welcomeQueSetting.getRelaitonCodeType();//1常见问题2字典中心3问题集
|
|
|
relaitonCodeType = relaitonCodeType==null?-1:relaitonCodeType;
|
|
|
String relationCode = welcomeQueSetting.getRelationCode();//relationType2对应业务code,多个逗号隔开
|
|
|
if(relationCode==null){
|
|
|
return;
|
|
|
}
|
|
|
relationCode = relationCode.replace(",","','");
|
|
|
if(relaitonCodeType==1){
|
|
|
//1常见问题
|
|
|
List<BaseKnowledgeQuestion> questionList = findTopQuestionList(5,relationCode);
|
|
|
JSONArray jsonArray = new JSONArray();
|
|
|
for (BaseKnowledgeQuestion question:questionList){
|
|
|
JSONObject json = new JSONObject();
|
|
|
json.put("id",question.getId());
|
|
|
json.put("questionName",question.getQuestionName());
|
|
|
jsonArray.add(jsonArray);
|
|
|
}
|
|
|
JSONObject result = new JSONObject();
|
|
|
result.put("content",jsonArray);
|
|
|
result.put("content_type","3001");
|
|
|
result.put("session_id",session_id);
|
|
|
map.put("2",result);
|
|
|
}else if(relaitonCodeType==2){
|
|
|
List<BaseKnowledgeDict> dictList = findTopDictList(5,relationCode);
|
|
|
JSONArray jsonArray = new JSONArray();
|
|
|
for (BaseKnowledgeDict dict:dictList){
|
|
|
JSONObject json = new JSONObject();
|
|
|
json.put("id",dict.getId());
|
|
|
json.put("name",dict.getName());
|
|
|
jsonArray.add(jsonArray);
|
|
|
}
|
|
|
JSONObject result = new JSONObject();
|
|
|
result.put("content",jsonArray);
|
|
|
result.put("content_type","3003");
|
|
|
result.put("session_id",session_id);
|
|
|
map.put("2",result);
|
|
|
}else if(relaitonCodeType==3){
|
|
|
List<BaseKnowledgeQuestions> questionsList = findTopQuestionsList(5,relationCode);
|
|
|
JSONArray jsonArray = new JSONArray();
|
|
|
for (BaseKnowledgeQuestions questions:questionsList){
|
|
|
JSONObject json = new JSONObject();
|
|
|
json.put("id",questions.getId());
|
|
|
json.put("questionsName",questions.getQuestionsName());
|
|
|
json.put("businessType",questions.getBusinessType());
|
|
|
//查找关联问题
|
|
|
JSONArray relationArray = new JSONArray();
|
|
|
List<BaseKnowledgeQuestionsRelation> relationList = knowledgeQuestionsRelationDao.selectByQuestionsId(questions.getId());
|
|
|
for (BaseKnowledgeQuestionsRelation relation:relationList){
|
|
|
JSONObject rel = new JSONObject();
|
|
|
rel.put("questionId",relation.getQuestionId());
|
|
|
rel.put("questionName",relation.getQuestionName());
|
|
|
relationArray.add(relationArray);
|
|
|
}
|
|
|
json.put("relation",relationArray);
|
|
|
jsonArray.add(jsonArray);
|
|
|
}
|
|
|
JSONObject result = new JSONObject();
|
|
|
result.put("content",jsonArray);
|
|
|
result.put("content_type","3002");
|
|
|
result.put("session_id",session_id);
|
|
|
map.put("2",result);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}catch (Exception e){
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//查找常见问题
|
|
|
public BaseKnowledgeQuestion findQuestionLikeName(String content){
|
|
|
if(StringUtils.isNotEmpty(content)){
|
|
|
return null;
|
|
|
}
|
|
|
String sql = "select p.* from base_knowledge_question p where p.status = 1 and p.del=1 and " +
|
|
|
" (p.time=1 or (p.time=2 and p.date<'"+DateUtil.getStringDate()+"')) " +
|
|
|
" and (p.question_name like '%"+content+"%' or p.similar_question_name like '"+content+"') order by sort limit 1";
|
|
|
List<BaseKnowledgeQuestion> questionList = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(BaseKnowledgeQuestion.class));
|
|
|
if(questionList.size()>0){
|
|
|
return questionList.get(0);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
public List<BaseKnowledgeQuestions> findTopQuestionsList(Integer limit,String ids){
|
|
|
String sql = "select p.* from base_knowledge_questions p where p.status = 1 and p.del=1 and p.id in ('"+ids+"')" +
|
|
|
"(p.time=1 or (p.time=2 and p.date<'"+DateUtil.getStringDate()+"')) order by sort limit "+limit;
|
|
|
List<BaseKnowledgeQuestions> questionList = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(BaseKnowledgeQuestions.class));
|
|
|
return questionList;
|
|
|
}
|
|
|
|
|
|
public List<BaseKnowledgeQuestion> findTopQuestionList(Integer limit,String ids){
|
|
|
String sql = "select p.* from base_knowledge_question p where p.status = 1 and p.del=1 and p.id in ('"+ids+"')" +
|
|
|
"(p.time=1 or (p.time=2 and p.date<'"+DateUtil.getStringDate()+"')) order by sort limit "+limit;
|
|
|
List<BaseKnowledgeQuestion> questionList = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(BaseKnowledgeQuestion.class));
|
|
|
return questionList;
|
|
|
}
|
|
|
|
|
|
public List<BaseKnowledgeDict> findTopDictList(Integer limit,String ids){
|
|
|
String sql = "select p.* from base_knowledge_dict p where p.del=1 and p.id in ('"+ids+"') order by sort limit "+limit;
|
|
|
List<BaseKnowledgeDict> dictList = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(BaseKnowledgeDict.class));
|
|
|
return dictList;
|
|
|
}
|
|
|
|
|
|
//会话类型转类型
|
|
|
private String sessionTypeToType(String type){
|
|
|
if(StringUtils.isNotEmpty(type)){
|
|
|
return type;
|
|
|
}
|
|
|
if("18".equals(type)){
|
|
|
return "1";
|
|
|
}
|
|
|
if("26".equals(type)){
|
|
|
return "2";
|
|
|
}
|
|
|
if("27".equals(type)){
|
|
|
return "3";
|
|
|
}
|
|
|
if("1".equals(type)||"5".equals(type)||"9".equals(type)||"12".equals(type)||"15".equals(type)||"16".equals(type)){
|
|
|
return "4";
|
|
|
}
|
|
|
return type;
|
|
|
}
|
|
|
|
|
|
}
|