派生自 projectDept/qhighschool

胡仁荣
2022-10-31 fe7381d6e8ec1f427408de0297ac7f41533202f6
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package com.qxueyou.scc.msg.service.impl;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
 
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
import org.springframework.stereotype.Service;
 
import com.qxueyou.scc.base.model.Result;
import com.qxueyou.scc.base.service.ICacheService;
import com.qxueyou.scc.msg.model.MsgChatroomMsg;
import com.qxueyou.scc.msg.service.IChatroomMsgService;
 
@Service
public class ChatroomMessageBean implements IChatroomMsgService {
 
    @Autowired
    ICacheService redis;
 
    private static final int PAGE_SIZE = 20;
 
    private static final String CACHEKEY_MSG_QUEUE = "msg_chatroom_msg_"; // ÊµÊ±ÏûÏ¢¶ÓÁÐ
    private static final String CACHEKEY_MESSAGE_A_COUNT = "msg_chatroom_message_count_"; // AÀàÏûÏ¢×ÜÊý
    private static final String CACHEKEY_MESSAGE_HISTORY = "msg_chatroom_msg_history_"; //ÀúÊ·ÏûÏ¢
    
    private static final String CACHEKEY_ONLINE_COUNT = "msg_chatroom_online_count_"; // ÔÚÏßÈËÊý
    private static final String CACHEKEY_PRAISE_COUNT = "msg_chatroom_praise_count_"; // µãÔÞÊý
    private static final String CACHEKEY_LOTTERY_COUNT = "msg_chatroom_lottery_count_"; // Ç©µ½Êý
    private static final String CACHEKEY_VIEW_COUNT = "msg_chatroom_view_count_"; // ä¯ÀÀÊý
    private static final String CACHEKEY_LOTTERY_RESULT_COUNT = "msg_chatroom_lottery_result_count_"; //Öн±½á¹û
    private static final String CACHEKEY_REWARD_COUNT = "msg_chatroom_reward_count_"; //´òÉÍ´ÎÊý
    private static final String CACHEKEY_CONSULT_COUNT = "msg_chatroom_consult_count_"; //×Éѯ´ÎÊý
    private static final String CACHEKEY_REWARD_AMOUNT = "msg_chatroom_reward_amount_"; //´òÉͽð¶î
 
    /**
     * ·¢ËÍÏûÏ¢
     */
 
    @Override
    public Result doSendMessage(String chatroomId, String senderId,
            String imgPath, String alias, String content, Integer type, String userType) {
 
        MsgChatroomMsg mes = new MsgChatroomMsg();
        mes.setMsgId(redis.incr("ChatroomMsgId"));
        mes.setChatroomId(chatroomId);
        mes.setSenderId(senderId);
        mes.setContent(content);
        mes.setType(type);
        mes.setAlias(alias);
        mes.setUserType(userType);
        mes.setImgPath(imgPath);
        mes.setCreateTime(new Date());
 
        //»º´æÏûÏ¢
        this.msgCache(chatroomId, mes);
        this.msgTypeCountCache(chatroomId, type);
                            
        return new Result(true, null, mes);
 
    }
    
    /**
     * ÁÄÌìÏûÏ¢¼ôÇУ¬
     * 
     * @param chatroomId
     */
    @Deprecated
    private void msgListTrim(String key, String chatroomId){
        List<MsgChatroomMsg> mesList = redis.lstRange(key, 0l, -1l);
        if(mesList != null && !mesList.isEmpty()){
            if(mesList.size() <= PAGE_SIZE){
                return;
            }
            long index = 0;
            long time = System.currentTimeMillis();
            for(MsgChatroomMsg msg : mesList){
                if(time - msg.getCreateTime().getTime()<= 10000){
                    break;
                }
                index ++;
            }
            //±£Áô20Ìõ
            redis.lstTrim(key, mesList.size() - index <= 20?mesList.size()-20:index, -1l);
        }
    }
    
    /**
     * »º´æÏûÏ¢
     * 
     * @param chatroomId
     * @param mes
     */
    private void msgCache(String chatroomId, MsgChatroomMsg mes){
        String key = CACHEKEY_MSG_QUEUE + chatroomId;        
        
        //ÏÈ»ñÈ¡10ÃëÄÚµÄÊý¾Ý
//        this.msgListTrim(key, chatroomId);
//        List<MsgChatroomMsg> mesList = redis.lstRange(key, 0l, -1l);
        
        //»ñÈ¡PAGE_SIZEÌõÒÔÄÚµÄÏûÏ¢
        List<MsgChatroomMsg> mesList= redis.lstRange(key, 0l, PAGE_SIZE);
        
        //×î½ü20ÏûÏ¢¹ýÂËÖØ¸´µÄµãÔÞ£¬½øÈ룬Í˳ö
        if((MsgChatroomMsg.TYPE_PRAISE == mes.getType() || MsgChatroomMsg.TYPE_ENTER == mes.getType() || MsgChatroomMsg.TYPE_EXIT == mes.getType())
                && mesList.contains(mes)){
            return;
        }
        redis.lstRightPush(key, mes);
        redis.expire(key, 360, TimeUnit.DAYS);
    }
    
    /**
     * ¼ÆÊýÆ÷
     * 
     * @param chatroomId
     * @param type
     */
    private void msgTypeCountCache(String chatroomId, Integer type){
        if(type == MsgChatroomMsg.TYPE_ENTER){//ÔÚÏßÈËÊý¼ÓÒ»
            redis.incr(CACHEKEY_ONLINE_COUNT + chatroomId);
            redis.expire(CACHEKEY_ONLINE_COUNT + chatroomId, 360, TimeUnit.DAYS);
        }
        if(type == MsgChatroomMsg.TYPE_ENTER || type == MsgChatroomMsg.TYPE_PRAISE || type == MsgChatroomMsg.TYPE_LOTTERY 
                || type == MsgChatroomMsg.TYPE_REWARD || type == MsgChatroomMsg.TYPE_CONSULT || type == MsgChatroomMsg.TYPE_LOTTERY_RESULT){
            redis.incr(this.getKey(chatroomId, type));
            redis.expire(this.getKey(chatroomId, type),  360, TimeUnit.DAYS);
        }else if(type == MsgChatroomMsg.TYPE_EXIT){//ÔÚÏßÈËÊý¼õÒ»
            String key = CACHEKEY_ONLINE_COUNT + chatroomId;
            Long value = redis.getIncrValue(key);
            if(value.compareTo(0l)>0){
                redis.decr(key);
            }
            redis.expire(key,  360, TimeUnit.DAYS);
        }else if(type == MsgChatroomMsg.TYPE_NORMAL || type == MsgChatroomMsg.TYPE_LOTTERY_RESULT){//AÀàÏûÏ¢¼ÆÊýÆ÷
            redis.incr(CACHEKEY_MESSAGE_A_COUNT + chatroomId);
            redis.expire(CACHEKEY_MESSAGE_A_COUNT + chatroomId,  360, TimeUnit.DAYS);
        }
    }
    
    //»ñÈ¡»º´ækeyÖµ
    private String getKey(String chatroomId, Integer type){
        String key = "";
        switch(type){
            case MsgChatroomMsg.TYPE_PRAISE:
                key = CACHEKEY_PRAISE_COUNT + chatroomId;
                break;
            case MsgChatroomMsg.TYPE_LOTTERY:
                key = CACHEKEY_LOTTERY_COUNT + chatroomId;
                break;
            case MsgChatroomMsg.TYPE_ENTER:
                key = CACHEKEY_VIEW_COUNT + chatroomId;
                break;
            case MsgChatroomMsg.TYPE_REWARD:
                key = CACHEKEY_REWARD_COUNT + chatroomId;
                break;
            case MsgChatroomMsg.TYPE_CONSULT:
                key = CACHEKEY_CONSULT_COUNT + chatroomId;
                break;
            case MsgChatroomMsg.TYPE_LOTTERY_RESULT:
                key = CACHEKEY_LOTTERY_RESULT_COUNT + chatroomId;
                break;
            default:
                key = "";
        }
        return key;
    }
 
    /**
     * ÂÖѯ
     * 
     * @param chatroomId
     * @throws Exception
     */
    @Override
    public Result latestMsg(String chatroomId) {
        List<MsgChatroomMsg> mesList = redis.lstRange(CACHEKEY_MSG_QUEUE + chatroomId, 0l, -1l);
        
        //½±½ð×ÜÊý
        Long rewardAmount = redis.getIncrValue(CACHEKEY_REWARD_AMOUNT + chatroomId);
        
        Map<String, Object> data = new HashMap<String, Object>();
        //ͳ¼ÆÊý¾Ý
        Map<String,Long> statistics = this.reStatistics(chatroomId);
        Integer maxPage = this.getMaxPage(chatroomId, String.valueOf(statistics.get("historyMsgCount")));
        data.put("maxPage", maxPage);
        data.put("currentPage", maxPage);
        data.put("onlineCount", statistics.get("onlineCount"));
        data.put("praiseCount", statistics.get("praiseCount"));
        data.put("viewCount", statistics.get("viewCount"));
        data.put("lotteryCount", statistics.get("lotteryCount"));
        data.put("rewardCount", statistics.get("rewardCount"));
        data.put("consultCount", statistics.get("consultCount"));
        data.put("rewardAmount", rewardAmount);
        data.put("dataList", mesList);
        data.put("serverTime", new Date());
        return new Result(true, null, data);
    }
    
    /**
     * »ñÈ¡ä¯ÀÀÈË´Î
     * @param chatroomId
     * @throws Exception
     */
    @Override
    public Long getViewCount(String chatroomId){
        return redis.getIncrValue(CACHEKEY_VIEW_COUNT + chatroomId);
    }
    
    /**
     * ÖØÐÂͳ¼ÆÊý¾Ý£¬¶ÁÈ¡Êý¾Ý¿â
     * 
     * @param type
     * @param statistics
     * @return
     */
    private Map<String,Long> reStatistics(String chatroomId){
        Long msgCount = nul2Zero(redis.getIncrValue(CACHEKEY_MESSAGE_A_COUNT + chatroomId));
        Long onlineCount = nul2Zero(redis.getIncrValue(CACHEKEY_ONLINE_COUNT + chatroomId));
        Long praiseCount = nul2Zero(redis.getIncrValue(CACHEKEY_PRAISE_COUNT + chatroomId));
        Long lotteryCount = nul2Zero(redis.getIncrValue(CACHEKEY_LOTTERY_COUNT + chatroomId));
        Long viewCount = nul2Zero(redis.getIncrValue(CACHEKEY_VIEW_COUNT + chatroomId));
        Long rewardCount = nul2Zero(redis.getIncrValue(CACHEKEY_REWARD_COUNT + chatroomId));
        Long lotteryResultCount = nul2Zero(redis.getIncrValue(CACHEKEY_LOTTERY_RESULT_COUNT + chatroomId));
        Long consultCount = nul2Zero(redis.getIncrValue(CACHEKEY_CONSULT_COUNT + chatroomId));
        
        Map<String, Long> result = new HashMap<>();
        result.put("msgCount", msgCount);
        //ÀúÊ·ÏûÏ¢ÊýÁ¿£¬Õý³£ÏûÏ¢¡¢Öн±ÏûÏ¢£¬´òÉÍÏûÏ¢
        result.put("historyMsgCount", msgCount+lotteryResultCount+rewardCount);
        result.put("onlineCount", onlineCount);
        result.put("praiseCount", praiseCount);
        result.put("lotteryCount", lotteryCount);
        result.put("viewCount", viewCount);
        result.put("rewardCount", rewardCount);
        result.put("consultCount", consultCount);
        return result;
    }
    
    private Long nul2Zero(Long value) {
        if(value==null) {
            return  0l;
        }
        return value;
    }
    
    /**
     * °´Ò³»ñÈ¡ÀúÊ·ÏûÏ¢
     */
    @Override
    public Result getHistoryMes(String chatroomId, Integer pageNo)
            {
        //ͳ¼ÆÊý¾Ý
        List<MsgChatroomMsg> mesList = redis.lstRange(CACHEKEY_MSG_QUEUE + chatroomId, 0l, -1l);
        Map<String,Long> statistics = this.reStatistics(chatroomId);
        Integer maxPage = getMaxPage(chatroomId, String.valueOf(statistics.get("historyMsgCount")+mesList.size()));
        String key = CACHEKEY_MESSAGE_HISTORY + chatroomId+ "_" + pageNo;
        if (StringUtils.isEmpty(chatroomId) || pageNo == null
                || pageNo > maxPage) {
            return new Result(false, "²ÎÊý´íÎó");
        }
        List<MsgChatroomMsg> msgList = redis.lstAll(key);
        msgList = this.distinct(msgList, chatroomId);
        Map<String, Object> data = new HashMap<>();
        data.put("currentPage", pageNo);
        data.put("maxPage", maxPage);
        data.put("dataList", msgList);
        return new Result(true, null, data);
    }
    
    /**
     * È¥³ýÖØ¸´ÏûÏ¢
     * 
     * @return
     */
    private List<MsgChatroomMsg> distinct(List<MsgChatroomMsg> historyMsgList, String chatroomId){
        List<MsgChatroomMsg> lastMsgList = redis.lstRange(CACHEKEY_MSG_QUEUE + chatroomId, 0l, -1l);
        if(historyMsgList != null && !historyMsgList.isEmpty() && lastMsgList != null && !lastMsgList.isEmpty()){
            for(int i=historyMsgList.size()-1;i>= 0;i--){
                MsgChatroomMsg historyMsgChatroomMsg = historyMsgList.get(i);
                for(MsgChatroomMsg lastMsgChatroomMsg : lastMsgList){
                    if(historyMsgChatroomMsg.getMsgId().equals(lastMsgChatroomMsg.getMsgId())){
                        historyMsgList.remove(i);
                        break;
                    }
                }
            }
        }
        return historyMsgList;
    }
 
 
    /**
     * »ñÈ¡AÀàÏûÏ¢×î´óÒ³Âë
     * 
     * @param chatroomId
     * @return
     */
    public Integer getMaxPage(String chatroomId, String msgCount) {
        Integer count = Integer.valueOf(msgCount);
        int maxPage = count % PAGE_SIZE == 0 ? count / PAGE_SIZE : count
                / PAGE_SIZE + 1;
        return maxPage;
    }
 
}