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
package com.iplatform.chat.util;
 
import com.iplatform.chat.ChatSession;
import com.iplatform.model.po.ChatDialog;
import com.iplatform.model.vo.ChatViewVo;
import com.iplatform.model.vo.ChatVo;
import com.walker.infrastructure.utils.DateUtils;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.jdbc.mongo.ChatRecord;
 
public class ChatUtils {
 
    public static final String SESSION_ID_PREFIX = "CS";
 
    /**
     * 把mongo数据库中记录转换成平台聊天记录。
     * @param record
     * @param me 我的ID,我的聊天表
     * @param isCustomerService 我是客服吗?
     * @return
     * @date 2023-07-17
     */
    public static final ChatVo acquireChatVo(ChatRecord record, long me, boolean isCustomerService){
        ChatVo vo = new ChatVo();
//        vo.setSessionId();
        vo.setBizId(record.getBizId());
        vo.setMessage(record.getMessage());
        vo.setCreateTime(record.getCreateDate());
        vo.setMsgType(record.getMsgType());
        vo.setId(record.getId());
        vo.setIsMeMsg(record.getType() == ChatRecord.TYPE_SEND? 1:0);   // 是否我发送的消息,0表示我接收的
        if(isCustomerService){
            // 我是客服
            vo.setCustomerService(me);
            vo.setMe(record.getUserId());
            vo.setCustomerServiceSend(record.getType() == ChatRecord.TYPE_SEND? true:false);
        } else {
            // 我是C端用户
            vo.setMe(me);
            vo.setCustomerService(record.getUserId());
            vo.setCustomerServiceSend(record.getType() == ChatRecord.TYPE_SEND? false:true);
        }
        return vo;
    }
 
    public static final ChatViewVo acquireChatViewVo(ChatVo chatVo){
        ChatViewVo viewVo = new ChatViewVo();
        viewVo.setIsMeMsg(chatVo.getIsMeMsg() == 1? 0:1);
        viewVo.setCreateTime(DateUtils.toShowDate(chatVo.getCreateTime()));
        viewVo.setBizId(chatVo.getBizId());
        viewVo.setMsgType(chatVo.getMsgType());
        viewVo.setMessage(chatVo.getMessage());
        viewVo.setNickName(chatVo.getNickName());
        viewVo.setMsgUserAvatar(chatVo.getMsgUserAvatar());
        viewVo.setSessionId(chatVo.getSessionId());
        if(chatVo.isCustomerServiceSend()){
            viewVo.setCustomer(0);
            viewVo.setSendId(chatVo.getCustomerService());
            viewVo.setReceiverId(chatVo.getMe());
        } else {
            viewVo.setCustomer(1);
            viewVo.setSendId(chatVo.getMe());
            viewVo.setReceiverId(chatVo.getCustomerService());
        }
        viewVo.setChatType(chatVo.getChatType());
        return viewVo;
    }
 
    public static final ChatVo acquireChatVo(ChatViewVo chatViewVo){
        ChatVo chatVo = new ChatVo();
        chatVo.setCreateTime(DateUtils.getDateTimeNumber());
        chatVo.setNickName(chatViewVo.getNickName());
        chatVo.setMsgUserAvatar(chatViewVo.getMsgUserAvatar());
        chatVo.setMsgType(chatViewVo.getMsgType());
        chatVo.setMessage(chatViewVo.getMessage());
        chatVo.setBizId(chatViewVo.getBizId());
        chatVo.setSessionId(chatViewVo.getSessionId());
        chatVo.setIsMeMsg(chatViewVo.getIsMeMsg());
        if(chatViewVo.getCustomer() == 1){
            // 用户 发送的消息
            chatVo.setMe(chatViewVo.getSendId());
            chatVo.setCustomerService(chatViewVo.getReceiverId());
            chatVo.setCustomerServiceSend(false);
 
        } else {
            // 客服 发送的消息
            chatVo.setMe(chatViewVo.getReceiverId());
            chatVo.setCustomerService(chatViewVo.getSendId());
            chatVo.setCustomerServiceSend(true);
        }
        chatVo.setChatType(chatViewVo.getChatType());
        return chatVo;
    }
 
    public static final ChatSession acquireChatSession(ChatDialog dialog){
        ChatSession session = new ChatSession(dialog.getId(), dialog.getUserId(), dialog.getCustomerService(), true);
        session.setBizId(dialog.getBizId());
        session.setEndTime(dialog.getEndTime());
        return session;
    }
 
    /**
     * 获得对话的唯一ID,算法如下:
     * <pre>
     *     (客服id + 用户id + 业务id).hashCode();
     * </pre>
     * @param customerService
     * @param userId
     * @param bizId
     * @return
     * @date 2023-07-12
     */
    public static final long acquireChatDialogKey(long customerService, long userId, String bizId){
        return new StringBuilder().append(customerService)
                .append(StringUtils.DEFAULT_SPLIT_SEPARATOR).append(userId)
                .append(StringUtils.DEFAULT_SPLIT_SEPARATOR).append(bizId).toString().hashCode();
    }
}