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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.iplatform.chat.support;
 
import com.iplatform.chat.ChatService;
import com.iplatform.chat.Constants;
import com.iplatform.chat.util.ChatUtils;
import com.iplatform.model.vo.ChatVo;
import com.walker.db.Sorts;
import com.walker.db.page.GenericPager;
import com.walker.db.page.ListPageContext;
import com.walker.db.page.PageSearch;
import com.walker.infrastructure.utils.DateUtils;
import com.walker.infrastructure.utils.NumberGenerator;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.jdbc.mongo.ChatRecord;
import com.walker.jdbc.mongo.MongoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
 
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
 
@Deprecated
public class MongoChatService implements ChatService {
 
    protected final transient Logger logger = LoggerFactory.getLogger(this.getClass());
 
    private Sorts.Sort timeDescSort = Sorts.DESC().setField("_id");
 
    @Override
    public void execInsertChat(ChatVo vo) {
        if(vo == null){
            return;
        }
        if(vo.getMe() <= 0){
            logger.error("聊天错误,当前用户ID必须设置");
            return;
        }
        if(StringUtils.isEmpty(vo.getMessage())){
            logger.error("聊天错误,无法发送空消息");
            return;
        }
 
        ChatRecord record = this.acquireChatRecord(vo);
        if(!vo.isCustomerServiceSend()){
            // ================= 我发的消息 =================
            // 写入我的表中
            record.setUserId(vo.getCustomerService());
            record.setType(ChatVo.TYPE_SEND);
            this.mongoService.insert(record, String.valueOf(vo.getMe()));
            // 写入对方表中
            if(vo.getCustomerService() != Constants.USER_ID_ROBOT){
                record.setUserId(vo.getMe());
                record.setType(ChatVo.TYPE_RECEIVED);
                this.mongoService.insert(record, String.valueOf(vo.getCustomerService()));
            }
 
        } else {
            // ================= 客服发的消息 =================
            // 写入我的表中
            record.setUserId(vo.getCustomerService());
            record.setType(ChatVo.TYPE_RECEIVED);
            this.mongoService.insert(record, String.valueOf(vo.getMe()));
            // 写入对方表中
            if(vo.getCustomerService() != Constants.USER_ID_ROBOT){
                record.setUserId(vo.getMe());
                record.setType(ChatVo.TYPE_SEND);
                this.mongoService.insert(record, String.valueOf(vo.getCustomerService()));
            }
        }
        /*// 写入我的表中
        record.setUserId(vo.getCustomerService());
        record.setType(ChatVo.TYPE_SEND);
        this.mongoService.insert(record, String.valueOf(vo.getMe()));
        // 写入对方表中
        record.setUserId(vo.getMe());
        record.setType(ChatVo.TYPE_RECEIVED);
        this.mongoService.insert(record, String.valueOf(vo.getCustomerService()));*/
    }
 
    /**
     * 生成一个聊天记录,不设置对方用户信息,在调用者中处理。
     * @param vo
     * @return
     */
    public ChatRecord acquireChatRecord(ChatVo vo){
        ChatRecord record = new ChatRecord();
        record.setId(NumberGenerator.getLongSequenceId());
        record.setCreateDate(DateUtils.getDateTimeNumber());
//        record.setUserId(vo.getUserId());
//        record.setType(vo.getType());
        record.setMsgType(vo.getMsgType());
        record.setMessage(vo.getMessage());
        record.setBizId(vo.getBizId());
        return record;
    }
 
    @Override
    public void execDeleteChat(String id) {
        throw new UnsupportedOperationException("未写代码");
    }
 
    @Override
    public GenericPager<ChatVo> queryPageChatList(long me
            , String bizId, long startTime, long endTime, long userId, String keyword, PageSearch pageSearch, boolean meIsCustomerService) {
        if(me <= 0){
            throw new IllegalArgumentException("缺少聊天人ID(me)");
        }
//        Criteria criteria = new Criteria();
        Query mongoQuery = new Query();
        if(StringUtils.isNotEmpty(bizId)){
            mongoQuery.addCriteria(Criteria.where(Constants.CHAT_FIELD_BIZ_ID).is(bizId));
        }
        if(userId > 0){
            mongoQuery.addCriteria(Criteria.where(Constants.CHAT_FIELD_USER_ID).is(userId));
        }
        if(startTime > 0){
            mongoQuery.addCriteria(Criteria.where(Constants.CHAT_FIELD_CREATE_TIME).gte(startTime));
        }
        if(endTime > 0){
            mongoQuery.addCriteria(Criteria.where(Constants.CHAT_FIELD_CREATE_TIME).lte(endTime));
        }
        if(StringUtils.isNotEmpty(keyword)){
            // 模糊查询名字中带有 三 的数据
            Pattern pattern = Pattern.compile(Constants.LIKE_PREFIX + keyword + Constants.LIKE_SUFFIX, Pattern.CASE_INSENSITIVE);
            mongoQuery.addCriteria(Criteria.where(Constants.CHAT_FIELD_MESSAGE).regex(pattern));
        }
 
        GenericPager<ChatRecord> pager = this.mongoService.queryPageList(mongoQuery, ChatRecord.class, String.valueOf(me), pageSearch, timeDescSort);
        List<ChatVo> chatVoList = null;
        if(pager.getTotalRows() > 0){
            List<ChatRecord> data = pager.getDatas();
            chatVoList = new ArrayList<>(data.size());
            for(ChatRecord record : data){
                chatVoList.add(ChatUtils.acquireChatVo(record, me, meIsCustomerService));
            }
        } else {
            chatVoList = new ArrayList<>(1);
        }
        return ListPageContext.createGenericPager(chatVoList, pager.getPageIndex(), pager.getPageSize(), (int)pager.getTotalRows());
    }
 
    public void setMongoService(MongoService mongoService) {
        this.mongoService = mongoService;
    }
 
    private MongoService mongoService;
}