zyy
2025-11-26 d53c0d89aeac598ef941cd196fe459f02f699786
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
package com.yami.trading.service.chat.online;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yami.trading.bean.Tip;
import com.yami.trading.bean.chat.domain.MessageUser;
import com.yami.trading.bean.chat.domain.OnlineChatMessage;
import com.yami.trading.bean.model.Customer;
import com.yami.trading.bean.model.User;
import com.yami.trading.bean.model.UserRecom;
import com.yami.trading.bean.syspara.domain.Syspara;
import com.yami.trading.common.constants.TipConstants;
import com.yami.trading.common.exception.BusinessException;
import com.yami.trading.common.util.DateUtils;
import com.yami.trading.common.util.StringUtils;
import com.yami.trading.common.util.UnicodeUtils;
import com.yami.trading.service.chat.MessageUserService;
import com.yami.trading.service.customer.CustomerService;
import com.yami.trading.service.syspara.SysparaService;
import com.yami.trading.service.system.TipService;
import com.yami.trading.service.user.UserRecomService;
import com.yami.trading.service.user.UserService;
import org.apache.commons.collections.Predicate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
 
 
@Service
public class OnlineChatMessageServiceImpl implements OnlineChatMessageService {
 
 
    protected static final Object onlineChatMessage = null;
    private Logger log = LoggerFactory.getLogger(OnlineChatMessageServiceImpl.class);
    private Map<String, List<com.yami.trading.bean.chat.domain.OnlineChatMessage>> cahce_chat = new ConcurrentHashMap<String, List<OnlineChatMessage>>();
 
    private Map<String, MessageUser> cahceUser = new ConcurrentHashMap<String, MessageUser>();
    @Autowired
    private UserService partyService;
    @Autowired
    private UserRecomService userRecomService;
    @Autowired
    private TipService tipService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private DBOnlineChatMessageServiceImpl dbOnlineChatMessageService;
    @Autowired
    private MessageUserService messageUserService;
    @Autowired
    private JdbcTemplate jdbcTemplate;
 
    @Autowired
    private SysparaService sysparaService;
 
 
    /**
     * @param messageId
     * @param pageSize
     * @param partyId
     * @param clicentType 请求的客户端类型,用户端user,客服端 不传
     * @return
     */
    public List<OnlineChatMessage> cacheGetList(String messageId, int pageSize, String partyId, boolean isClient, String... clicentType) {
        List<OnlineChatMessage> cache = cahce_chat.get(partyId);
        if (cache == null) {
            return new LinkedList<OnlineChatMessage>();
        }
        List<OnlineChatMessage> result = new ArrayList<>(cache);
        if ((clicentType.length != 0 && "user".equals(clicentType[0])) || isClient) {
            // 过滤掉已撤回的消息
            result = cache.stream().filter(chat -> chat.getDeleteStatus() == null || chat.getDeleteStatus() == 0).collect(Collectors.toList());
        }
 
        int start = cacheIndex(messageId, result);
//        start = start == 0 ? start : start + 1;// 空消息则表示首页,消息索引的后一条为起始
        int end = start + pageSize;
 
        if (start >= result.size()) {// 起始数据大于总量,返回空
            return new LinkedList<OnlineChatMessage>();
        }
        if (result.size() <= end) {
            end = result.size();
        }
        result = result.subList(start, end);
        if (CollectionUtils.isEmpty(result)) {
            return result;
        }
        result.forEach(msg -> {
            String content = msg.getContent();
            content = UnicodeUtils.decodeUnicode(content);
            msg.setContent(content);
        });
        return result;
    }
 
    /**
     * 获取消息的索引
     *
     * @param messageId
     * @param list
     * @return
     */
    private int cacheIndex(String messageId, List<OnlineChatMessage> list) {
        if (StringUtils.isEmptyString(messageId)) {
            return 0;
        }
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            OnlineChatMessage message = list.get(i);
            if (messageId.equals(message.getUuid().toString())) {
                index = i;
            }
        }
        if (index == -1) {
            throw new BusinessException("参数异常,消息获取失败");
        }
        return index + 1;
    }
 
    // @Override
    public List<MessageUser> cacheGetMessageUserPage1(int pageNo, int pageSize, String username) {
        List<MessageUser> list = new ArrayList<MessageUser>(cahceUser.values());
 
        List<MessageUser> result = new ArrayList<MessageUser>();
        for (MessageUser user : list) {
            if (user.getDeleteStatus() == -1) {
                // System.out.println("DeleteStatus:" + user.getIp());
                continue;
            }
            // 没有指定客服,客服不匹配
            if (StringUtils.isEmptyString(user.getTargetUsername()) || !username.equals(user.getTargetUsername())) {
                // System.out.println("TargetUsername:" + user.getIp());
                continue;
            }
            result.add(user);
        }
        Collections.sort(result);
        return result;
        // 只获取前50个,用户有消息会实时变化,不做翻页
//        int start = 0;
//        int end = start + pageSize;
//
//        if (result.size() <= end)
//            end = result.size();
//
//        List<MessageUser> resultUser = new ArrayList<MessageUser>();
//        resultUser.addAll(result);
//        List<MessageUser> subList = resultUser.subList(start, end);
//
//        return subList;
    }
 
    @Override
    public List<MessageUser> cacheGetMessageUserPage(int pageNo, int pageSize, String username, List<String> roleNames) {
        Syspara register_image_code_button = sysparaService.find("admin_all_message");
        List<MessageUser> list = new ArrayList<MessageUser>(cahceUser.values());
        List<MessageUser> result = new ArrayList<MessageUser>();
        if (register_image_code_button != null && register_image_code_button.getBoolean()) {
            List<String> excludesRoles = Arrays.asList("ROOT", "ADMIN");
 
 
//            Set<Role> roles = sec.getRoles();
//
//            List<String> intersection = roles.stream().map(Role::getRoleName)
//                    .filter(excludesRoles::contains)
//                    .collect(Collectors.toList());
            if (CollectionUtils.isEmpty(roleNames)){
                roleNames=new ArrayList<>();
            }
            boolean flag = "admin".equals(username) || "root".equals(username) || roleNames.contains("管理员");
 
            for (MessageUser user : list) {
                boolean flag1 = (StringUtils.isEmptyString(user.getTargetUsername()) || (!username.equals(user.getTargetUsername()))) && !flag;
                if (user.getDeleteStatus() == -1) continue;
                if (flag1) continue;
                result.add(user);
            }
        } else {
            for (MessageUser user : list) {
                if (user.getDeleteStatus() == -1) continue;
                if (StringUtils.isEmptyString(user.getTargetUsername()) || !username.equals(user.getTargetUsername()))
                    continue;
                result.add(user);
            }
        }
 
        Collections.sort(result);
        return result;
    }
 
 
    @Override
    public OnlineChatMessage saveSend(String partyId, String type, String send_receive, String content,
                                      String username, boolean isAutoAnswer) {
        OnlineChatMessage onlineChatMessage = new OnlineChatMessage();
        onlineChatMessage.setPartyId(partyId);
        onlineChatMessage.setContentType(type);
        onlineChatMessage.setSendReceive(send_receive);
        content = UnicodeUtils.gbEncoding(content);
        onlineChatMessage.setContent(content);
        onlineChatMessage.setCreateTime(new Date());
        onlineChatMessage.setUsername(username);
        onlineChatMessage.setDeleteStatus(0);
 
        dbOnlineChatMessageService.save(onlineChatMessage);
 
        List<OnlineChatMessage> list = cacheMessage(partyId);
        if (list == null) {
            list = new LinkedList<OnlineChatMessage>();
        }
        list.add(onlineChatMessage);
        Collections.sort(list);
        Collections.reverse(list);// 添加完后,时间倒叙排序加回
        putMessage(partyId, list);
        if (!cahceUser.containsKey(partyId)) {// 不存在则添加用户
            saveCreateByPartyId(partyId);
        }
        switch (send_receive) {
            case "receive":// 客服发送
                if (!isAutoAnswer) {
                    updateUnread(partyId, "user", "write");
                }
                break;
            case "send":// 用户发送
                updateUnread(partyId, "customer", "write");
//            tipService.saveTip(onlineChatMessage.getUuid().toString(), TipConstants.ONLINECHAT);
                break;
        }
        return onlineChatMessage;
    }
 
    public String userSendTarget(String partyId, Date sendTime, String targetUsername) {
        if (StringUtils.isNotEmpty(targetUsername)) {
            Customer customer = customerService.cacheByUsername(targetUsername);
            // 表示该用户被有客服权限的系统用户接手
            if (customer == null) {
                return targetUsername;
            }
            // 当前在聊的客服是否在线
            if (customer.getOnlineState() == 1) {
                return customer.getUserName();
            }
        }
 
        // 不在线则重新分配
        Customer customer = this.customerService.cacheOnlineOne();
        if (null == customer) {
            return null;
        }
        while (true) {
            customer.setLastMessageUser(partyId);
            customer.setLastCustomerTime(sendTime);
            boolean update = customerService.update(customer, true);
            if (update) {// 更新成功,退出
                break;
            } else {// 未成功,说明已下线,重新分配新客服
                customer = this.customerService.cacheOnlineOne();
                if (null == customer) {
                    return null;
                }
            }
        }
        return customer.getUserName();
    }
 
    /**
     * 更新未读数
     *
     * @param partyId
     * @param user_customer 更新对象,用户,客服
     * @param type          read:读,write:写
     */
    public void updateUnread(final String partyId, String user_customer, String type) {
        MessageUser messageUser = cahceUser.get(partyId);
        if (messageUser == null) {
            saveCreateByPartyId(partyId);
            messageUser = cahceUser.get(partyId);
        }
        int removeTipNum = 0;
        switch (user_customer) {
            case "user":
                if ("read".equals(type)) {
                    messageUser.setUserUnreadmsg(0);
                } else if ("write".equals(type)) {
                    messageUser.setUserUnreadmsg(messageUser.getUserUnreadmsg() + 1);
                    messageUser.setDeleteStatus(0);
                }
                break;
            case "customer":
                if ("read".equals(type)) {
                    removeTipNum = messageUser.getCustomerUnreadmsg();
                    messageUser.setCustomerUnreadmsg(0);
                } else if ("write".equals(type)) {
                    messageUser.setCustomerUnreadmsg(messageUser.getCustomerUnreadmsg() + 1);
                    messageUser.setDeleteStatus(0);
 
                    final String targetUsername = this.userSendTarget(partyId, new Date(),
                            messageUser.getTargetUsername());
                    if (StringUtils.isNotEmpty(targetUsername)
                            && !targetUsername.equals(messageUser.getTargetUsername())) {
                        final Customer customer = customerService.cacheByUsername(targetUsername);
                        // 客服不存在或者回复内容无效则不回复
                        if (customer != null && customer.getAutoAnswer() != null
                                && !StringUtils.isEmptyString(customer.getAutoAnswer().trim())) {
                            // 客服自动回复一条
                            messageUser.setCustomerUnreadmsg(messageUser.getCustomerUnreadmsg() - 1);
                            saveSend(partyId, "text", "receive", customer.getAutoAnswer(),
                                    targetUsername + "SYSTEM", true);
//                        Thread t = new Thread(new Runnable() {
//                            @Override
//                            public void run() {
//                                // TODO Auto-generated method stub
//                                // 异步,延迟200毫秒发送
//                                ThreadUtils.sleep(200);
//                                // 客服自动回复一条
//                                saveSend(partyId, "text", "receive", customer.getAutoAnswer(),
//                                        targetUsername + "SYSTEM");
//                            }
//                        });
//                        t.start();
                        }
                    }
                    messageUser.setTargetUsername(targetUsername);
                    if (StringUtils.isNotEmpty(targetUsername)) {// 指定的在线客服存在,则发起通知
                        Tip tip = new Tip();
                        tip.setBusinessId(this.cahce_chat.get(partyId).get(0).getUuid().toString());
                        tip.setModel(TipConstants.ONLINECHAT);
                        tip.setTargetUsername(targetUsername);
                        tipService.saveTip(tip);
                    }
                }
                break;
        }
        updateMessageUser(messageUser);
        if (removeTipNum > 0) {
            removeTips(messageUser.getPartyId(), removeTipNum);
        }
    }
 
    /**
     * 移除通知
     *
     * @param partyId
     * @param removeTipNum
     */
    public void removeTips(String partyId, int removeTipNum) {
        List<OnlineChatMessage> list = this.cacheGetList(null, removeTipNum, partyId, false);
        List<String> ids = new ArrayList<String>();
        for (OnlineChatMessage m : list) {
            ids.add(m.getUuid().toString());
        }
        tipService.deleteTip(ids);
    }
 
    public void updateMessageUser(MessageUser messageUser) {
        messageUserService.saveOrUpdate(messageUser);
        cahceUser.put(messageUser.getPartyId(), messageUser);
    }
 
 
    public void saveCreateByPartyId(String partyId) {
        User party = partyService.getById(partyId);
        if (party == null) {
            throw new BusinessException("无效的UID");
        }
        MessageUser messageUser = cahceUser.get(party.getUserId().toString());
        if (messageUser == null) {
            messageUser = new MessageUser();
            messageUser.setUserUnreadmsg(0);
            messageUser.setCustomerUnreadmsg(1);
            messageUser.setPartyId(party.getUserId().toString());
        }
        messageUser.setUpdatetime(new Date());
 
        messageUserService.saveOrUpdate(messageUser);
        cahceUser.put(party.getUserId().toString(), messageUser);
 
    }
 
    @Override
    public MessageUser saveCreate(String uid, String username) {
        User party = partyService.findUserByUserCode(uid);
        if (party == null) {
            party = partyService.findByUserName(uid);
            if (party == null) {
                throw new BusinessException("用户不存在");
            }
        }
        MessageUser messageUser = cahceUser.get(party.getUserId().toString());
        if (messageUser == null) {
            messageUser = new MessageUser();
            messageUser.setUserUnreadmsg(0);
            messageUser.setPartyId(party.getUserId().toString());
        }
        messageUser.setUpdatetime(new Date());
        messageUser.setDeleteStatus(0);
        messageUser.setTargetUsername(username);
        messageUserService.saveOrUpdate(messageUser);
        cahceUser.put(party.getUserId().toString(), messageUser);
        return messageUser;
    }
 
    @Override
    public void delete(String partyId) {
        MessageUser messageUser = cahceUser.get(partyId);
        if (messageUser != null) {
            messageUser.setDeleteStatus(-1);
            messageUser.setTargetUsername(null);
            this.updateMessageUser(messageUser);
        }
 
    }
 
    @Override
    public int unreadMsg(String partyId, String type, String targetUsername) {
        int unreadmsg = 0;
        if (!StringUtils.isNullOrEmpty(partyId)) {
            MessageUser messageUser = cahceUser.get(partyId);
            if (messageUser != null) {
                switch (type) {
                    case "user":
                        unreadmsg = messageUser.getUserUnreadmsg();
                        break;
                    case "customer":
                        unreadmsg = messageUser.getCustomerUnreadmsg();
                        break;
                }
            }
        } else {
            Iterator<Entry<String, MessageUser>> it = cahceUser.entrySet().iterator();
            while (it.hasNext()) {
                Entry<String, MessageUser> entry = it.next();
                MessageUser messageUser = entry.getValue();
                if (StringUtils.isEmptyString(targetUsername)
                        || !targetUsername.equals(messageUser.getTargetUsername())) {
                    continue;
                }
                switch (type) {
                    case "user":
                        unreadmsg += messageUser.getCustomerUnreadmsg();
                        break;
                    case "customer":
                        unreadmsg += messageUser.getCustomerUnreadmsg();
                        break;
                }
//                unreadmsg = unreadmsg + entry.getValue().getUnreadmsg();
            }
        }
 
        return unreadmsg;
    }
 
    /**
     * 设置备注
     *
     * @param partyId
     * @param remarks
     */
    public String updateResetRemarks(String partyId, String remarks) throws Exception {
        if (StringUtils.isEmptyString(remarks) || StringUtils.isEmptyString(remarks.trim())) {
            return null;
        }
        MessageUser messageUser = this.cacheMessageUser(partyId);
        if (messageUser == null) {
            throw new BusinessException("用户不存在");
        }
        messageUser.setRemarks(URLDecoder.decode(remarks, "utf-8"));
        this.updateMessageUser(messageUser);
        return remarks;
    }
 
    /**
     * 获取用户信息
     *
     * @param partyId
     * @return
     */
    public Map<String, Object> getUserInfo(String partyId) {
        User party = partyService.getById(partyId);
        if (party == null) {
            throw new BusinessException("用户不存在");
        }
        MessageUser messageUser = this.cacheMessageUser(partyId);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("partyId", partyId);
        result.put("remarks", messageUser.getRemarks());
        result.put("username", party.getUserName());
        result.put("usercode", party.getUserCode());
        result.put("last_login_time", DateUtils.format(party.getUserLasttime(), DateUtils.DF_yyyyMMddHHmmss));
        result.put("create_time", DateUtils.format(party.getCreateTime(), DateUtils.DF_yyyyMMddHHmmss));
        result.put("role_name", party.getRoleName());
        result.put("loginIp", party.getUserLastip());
//        result.put("online", userService.isOnline(partyId));
//        List<UserRecom> parents = userRecomService.getParents(party.getUserId());
        if (StringUtils.isNotEmpty(party.getUserRecom())) {
            User parentParty = partyService.getById(party.getUserRecom());
            result.put("recom_parent_name", parentParty == null ? null : parentParty.getUserName());
        } else {
            result.put("recom_parent_name", null);
        }
        return result;
    }
 
    @EventListener
    public void onApplicationEvent(ContextRefreshedEvent event) {
        init();
    }
 
    public void init() {
 
        List<MessageUser> listUser = messageUserService.list();
 
        for (int i = 0; i < listUser.size(); i++) {
            MessageUser item = listUser.get(i);
            if (StringUtils.isEmptyString(item.getPartyId())) {
                this.cahceUser.put(item.getIp(), item);
            } else {
                this.cahceUser.put(item.getPartyId(), item);
            }
        }
 
 
        QueryWrapper<OnlineChatMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        List<OnlineChatMessage> list_chat = dbOnlineChatMessageService.list(queryWrapper);
 
        for (int i = 0; i < list_chat.size(); i++) {
 
            OnlineChatMessage item = list_chat.get(i);
            List<OnlineChatMessage> list = null;
            if (StringUtils.isEmptyString(item.getPartyId())) {
                list = cahce_chat.get(item.getIp());
            } else {
                list = cahce_chat.get(item.getPartyId());
            }
            if (list == null) {
                list = new LinkedList<OnlineChatMessage>();
            }
            list.add(item);
            if (StringUtils.isEmptyString(item.getPartyId())) {
                this.cahce_chat.put(item.getIp(), list);
            } else {
                this.cahce_chat.put(item.getPartyId(), list);
            }
//            this.cahce_chat.put(item.getPartyId(), list);
        }
    }
 
    public Map<String, List<OnlineChatMessage>> cacheMessageAll() {
        return cahce_chat;
    }
 
    public Map<String, MessageUser> cacheMessageUserAll() {
        return cahceUser;
    }
 
    public MessageUser cacheMessageUser(String key) {
        return cahceUser.get(key);
    }
 
    public List<OnlineChatMessage> cacheMessage(String key) {
        List<OnlineChatMessage> onlineChatMessages = cahce_chat.get(key);
        if (CollectionUtils.isEmpty(onlineChatMessages)) {
            return onlineChatMessages;
        }
        onlineChatMessages.forEach(msg -> {
            String content = msg.getContent();
            content = UnicodeUtils.decodeUnicode(content);
            msg.setContent(content);
        });
        return onlineChatMessages;
    }
 
    public void putMessage(String key, List<OnlineChatMessage> value) {
        cahce_chat.put(key, value);
    }
 
    public void putMessageUser(String key, MessageUser value) {
        cahceUser.put(key, value);
    }
 
    public void updateMessageUserByIp(MessageUser messageUser) {
        messageUserService.saveOrUpdate(messageUser);
        cahceUser.put(messageUser.getIp(), messageUser);
    }
 
    public void deleteByIp(String ip) {
        MessageUser messageUser = cahceUser.get(ip);
        if (messageUser != null) {
            messageUser.setDeleteStatus(-1);
            messageUser.setTargetUsername(null);
            this.updateMessageUserByIp(messageUser);
        }
    }
 
    @Override
    public void removeTips(String partyId, long removeTipNum) {
 
    }
 
    /**
     * 未分配到客服的用户,分配客服
     *
     * @return
     */
    public void updateNoAnwserUser(String username) {
        List<MessageUser> users = new ArrayList<MessageUser>(this.cacheMessageUserAll().values());
        org.apache.commons.collections.CollectionUtils.filter(users, new Predicate() {
 
            @Override
            public boolean evaluate(Object arg0) {
                // TODO Auto-generated method stub
                return ((MessageUser) arg0).getCustomerUnreadmsg() > 0
                        && StringUtils.isEmptyString(((MessageUser) arg0).getTargetUsername());
            }
        });
        if (CollectionUtils.isEmpty(users)) {
            return;
        }
        for (MessageUser user : users) {
            user.setTargetUsername(username);
            if (StringUtils.isEmptyString(user.getPartyId())) {
                this.updateMessageUserByIp(user);
            } else {
                this.updateMessageUser(user);
            }
        }
    }
 
    public OnlineChatMessage getMessageById(String messageId) {
        return dbOnlineChatMessageService.getById(messageId);
    }
 
    public void updateMessageDelete(String messageId, String targetUserName) {
        OnlineChatMessage onlineChatMessage = getMessageById(messageId);
        if (onlineChatMessage.getDeleteStatus() == -1) {
            throw new BusinessException("该消息已撤回");
        }
        //游客或者登录用户
        String userKey = StringUtils.isEmptyString(onlineChatMessage.getPartyId()) ? onlineChatMessage.getIp() : onlineChatMessage.getPartyId();
        MessageUser messageUser = cahceUser.get(userKey);
        if (StringUtils.isEmptyString(messageUser.getTargetUsername())
                || !targetUserName.equals(messageUser.getTargetUsername())) {
            throw new BusinessException("并非当前客服接手的用户,无法撤回");
        }
//        if (!"receive".equals(onlineChatMessage.getSendReceive())) {
//            throw new BusinessException("只能撤回客服发送消息");
//        }
 
        List<OnlineChatMessage> list = cahce_chat.get(userKey);
        int indexOf = -1;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getUuid().equals(onlineChatMessage.getUuid())) {
                indexOf = i;
                break;
            }
        }
        if (indexOf == -1) {
            throw new BusinessException("撤回失败");
        }
        onlineChatMessage.setDeleteStatus(-1);
        dbOnlineChatMessageService.saveOrUpdate(onlineChatMessage);
        list.remove(indexOf);
        list.add(indexOf, onlineChatMessage);
        cahce_chat.put(userKey, list);
    }
 
    @Override
    public void updateMessageRead(String messageId, String messageType) {
        OnlineChatMessage onlineChatMessage = getMessageById(messageId);
 
        if (null != onlineChatMessage && messageType.equals(onlineChatMessage.getSendReceive())) { // 只更新未读消息
            String partyId = onlineChatMessage.getPartyId();
            String userKey = StringUtils.isEmptyString(partyId) ? onlineChatMessage.getIp() : partyId;
 
 
//            ApplicationUtil.executeUpdate(onlineChatMessage);
 
//            System.out.println("-----onlineChatMessage -333-" + messageType);
//            System.out.println("-----onlineChatMessage -333- " + onlineChatMessage.getUuid());
//            System.out.println("-----onlineChatMessage -333- " + onlineChatMessage.getIsRead());
//            System.out.println("-----onlineChatMessage -333-");
 
            if (StringUtils.isNotEmpty(userKey)) {
//                System.out.println("-----onlineChatMessage -111- " + onlineChatMessage.getUuid());
//                System.out.println("-----onlineChatMessage -111- " + onlineChatMessage.getIsRead());
                List<OnlineChatMessage> list = cahce_chat.get(userKey);
                if (null != list && !list.isEmpty()) {
                    int indexOf = findOnlineChatMessage(onlineChatMessage, list);
 
//                    System.out.println("-----onlineChatMessage -222- " + onlineChatMessage.getUuid());
//                    System.out.println("-----onlineChatMessage -222- " + onlineChatMessage.getIsRead());
 
                    if (indexOf > -1) {
                        onlineChatMessage.setIsRead(1);
//                        System.out.println("-----onlineChatMessage -- " + onlineChatMessage.getUuid());
//                        System.out.println("-----onlineChatMessage -- " + onlineChatMessage.getIsRead());
//                        System.out.println("-----onlineChatMessage");
                        list.set(indexOf, onlineChatMessage);
                    }
                }
                cahce_chat.put(userKey, list);
            }
            updateMessageReadData(onlineChatMessage);
        }
 
    }
 
    private int findOnlineChatMessage(OnlineChatMessage onlineChatMessage, List<OnlineChatMessage> list) {
        for (int i = 0; i < list.size(); i++) {
            if (Objects.equals(list.get(i).getUuid(), onlineChatMessage.getUuid())) {
                return i;
            }
        }
        return -1;
    }
 
    private void updateMessageReadData(OnlineChatMessage onlineChatMessage) {
        int count = jdbcTemplate.update("UPDATE t_onlinechat_message SET IS_READ=? WHERE UUID=?"
                , onlineChatMessage.getIsRead()
                , onlineChatMessage.getUuid());
 
    }
 
}