shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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;
}