package com.iplatform.chat;
|
|
import com.iplatform.chat.util.ChatUtils;
|
import com.iplatform.model.vo.ChatVo;
|
import com.walker.db.page.GenericPager;
|
import com.walker.infrastructure.utils.StringUtils;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
|
import java.util.List;
|
|
/**
|
* 聊天引擎定义,抽象实现。
|
* <p>暂时不定义接口,简化操作,后续要根据需求重构。</p>
|
* @author 时克英
|
* @date 2023-07-12
|
*/
|
@Deprecated
|
public abstract class ChatEngine {
|
|
protected final transient Logger logger = LoggerFactory.getLogger(getClass());
|
|
/**
|
* 发送聊天消息,该方法必须由人工调用,即:用户或客服发起消息。
|
* @param chatVo
|
* @return
|
*/
|
public boolean sendChat(ChatVo chatVo){
|
if(chatVo == null || chatVo.getMe() <= 0){
|
throw new IllegalArgumentException("消息(或发送人)不存在");
|
}
|
|
if(StringUtils.isEmpty(chatVo.getMessage())){
|
throw new ChatRuntimeException("不能发送空消息");
|
}
|
|
// 用户发起的消息,此时没有指定人工客服,需要机器人处理
|
if(chatVo.getCustomerService() == Constants.USER_ID_ROBOT){
|
if(!this.autoRobotReply){
|
logger.warn("系统配置:无机器人自动应答,无法回复内容。");
|
return false;
|
}
|
if(this.robot == null){
|
throw new ChatRuntimeException("未配置应答机器人");
|
}
|
// 先保存用户发送聊天内容,机器人发送内容不保存,仅在对方接收时保存
|
this.saveChatData(chatVo);
|
this.robot.autoReply(chatVo);
|
return true;
|
}
|
|
// 存在人工(客服),只需要推送消息
|
long chatDialogId = ChatUtils.acquireChatDialogKey(chatVo.getCustomerService(), chatVo.getMe(), chatVo.getBizId());
|
ChatSession chatSession = this.acquireChatDialog(chatDialogId);
|
if(chatSession == null){
|
logger.debug("未找到对话记录,需要创建:" + chatDialogId);
|
try {
|
chatSession = this.createChatDialog(chatDialogId, chatVo);
|
} catch (Exception ex){
|
throw new ChatRuntimeException("聊天对话创建失败,chatDialogId=" + chatDialogId, ex);
|
}
|
}
|
|
// 保存聊天记录
|
try {
|
this.saveChatData(chatVo);
|
} catch (Exception ex){
|
throw new ChatRuntimeException("保存聊天记录失败,chatDialogId=" + chatDialogId, ex, chatVo.getMe(), chatVo.getCustomerService());
|
}
|
return true;
|
}
|
|
public List<ChatSession> getChatSessionList(String customerService, String bizId){
|
|
return null;
|
}
|
|
public GenericPager getPageRecentChatList(String me, String bizId){
|
|
return null;
|
}
|
|
/**
|
* 保存聊天记录。
|
* @param chatVo
|
* @return
|
*/
|
protected abstract boolean saveChatData(ChatVo chatVo);
|
|
/**
|
* 创建聊天对话记录。
|
* @param chatDialogId
|
* @param chatVo
|
* @return
|
*/
|
protected abstract ChatSession createChatDialog(long chatDialogId, ChatVo chatVo);
|
|
/**
|
* 获取已有对话记录。
|
* @param chatDialogId
|
* @return
|
*/
|
protected abstract ChatSession acquireChatDialog(long chatDialogId);
|
|
/**
|
* 是否使用机器人自动响应?
|
* @return
|
*/
|
public boolean isAutoRobotReply() {
|
return autoRobotReply;
|
}
|
|
public void setAutoRobotReply(boolean autoRobotReply) {
|
this.autoRobotReply = autoRobotReply;
|
}
|
|
public Robot getRobot() {
|
return robot;
|
}
|
|
public void setRobot(Robot robot) {
|
this.robot = robot;
|
}
|
|
private Robot robot;
|
private boolean autoRobotReply = true;
|
}
|