Selaa lähdekoodia

修改 群组逻辑 (修改成不是临时删除的)

779513719 5 vuotta sitten
vanhempi
commit
5c5ec8cc4d

+ 11 - 11
jim-server/src/main/java/org/jim/server/command/command.properties

@@ -1,22 +1,22 @@
 #########################################
 # CommandHandler configuration
-# [cmd命令码] = 全局cmd处理理器类路径(必须处在第一位),该cmd命令不同业务处理器
+# [cmd\u547D\u4EE4\u7801] = \u5168\u5C40cmd\u5904\u7406\u7406\u5668\u7C7B\u8DEF\u5F84(\u5FC5\u987B\u5904\u5728\u7B2C\u4E00\u4F4D),\u8BE5cmd\u547D\u4EE4\u4E0D\u540C\u4E1A\u52A1\u5904\u7406\u5668
 #########################################
-#鉴权请求处理器
+#\u9274\u6743\u8BF7\u6C42\u5904\u7406\u5668
 3 = org.jim.server.command.handler.AuthReqHandler
-#聊天请求处理器
-11 = org.jim.server.command.handler.ChatReqHandler,org.jim.server.command.handler.processor.chat.DefaultAsyncChatMessageProcessor
-#关闭、退出请求处理器
+#\u804A\u5929\u8BF7\u6C42\u5904\u7406\u5668
+11 = org.jim.server.command.handler.ChatReqHandler,org.jim.server.command.handler.processor.chat.DefaultAsyncChatMessageProcessor,com.cn.processor.IMChatAsyncChatMessageProcessor
+#\u5173\u95ED\u3001\u9000\u51FA\u8BF7\u6C42\u5904\u7406\u5668
 14 = org.jim.server.command.handler.CloseReqHandler
-#握手请求处理器(TCP协议握手处理器,WS协议握手处理器)
+#\u63E1\u624B\u8BF7\u6C42\u5904\u7406\u5668(TCP\u534F\u8BAE\u63E1\u624B\u5904\u7406\u5668,WS\u534F\u8BAE\u63E1\u624B\u5904\u7406\u5668)
 1 = org.jim.server.command.handler.HandshakeReqHandler,org.jim.server.command.handler.processor.handshake.TcpHandshakeProcessor,org.jim.server.command.handler.processor.handshake.WsHandshakeProcessor
-#心跳请求处理器
+#\u5FC3\u8DF3\u8BF7\u6C42\u5904\u7406\u5668
 13 = org.jim.server.command.handler.HeartbeatReqHandler
-#加入群组请求处理器
+#\u52A0\u5165\u7FA4\u7EC4\u8BF7\u6C42\u5904\u7406\u5668
 7 = org.jim.server.command.handler.JoinGroupReqHandler
-#登录请求处理器
+#\u767B\u5F55\u8BF7\u6C42\u5904\u7406\u5668
 5 = org.jim.server.command.handler.LoginReqHandler
-#获取用户信息处理器
+#\u83B7\u53D6\u7528\u6237\u4FE1\u606F\u5904\u7406\u5668
 17 = org.jim.server.command.handler.UserReqHandler
-#获取用户消息处理器
+#\u83B7\u53D6\u7528\u6237\u6D88\u606F\u5904\u7406\u5668
 19 = org.jim.server.command.handler.MessageReqHandler

+ 22 - 19
jim-server/src/main/java/org/jim/server/command/handler/LoginReqHandler.java

@@ -99,7 +99,7 @@ public class LoginReqHandler extends AbstractCmdHandler {
 		//判断当前用户 是否具有 查看所有客服权限 如果有 绑定群组
 		hasViewAllServiceAccountAuthority(channelContext,user);
 		loginServiceHandler.onSuccess(channelContext);
-		loginRespBody.clear();
+//		loginRespBody.clear();
 		ImPacket loginRespPacket = new ImPacket(Command.COMMAND_LOGIN_RESP, loginRespBody.toByte());
 		return loginRespPacket;
 	}
@@ -123,27 +123,30 @@ public class LoginReqHandler extends AbstractCmdHandler {
 			for(String serviceAccountRoleDepartmentId : adminContainer){
 				//获取 客服 所在 的通道
 				SetWithLock<ChannelContext> channelContexts = ImAio.getChannelContextsByUserId(serviceAccountRoleDepartmentId);
-				//获取读锁
-				ReadLock readLock = channelContexts.getLock().readLock();
-				//加锁
-				readLock.lock();
-				try {
-					Set<ChannelContext> channels = channelContexts.getObj();
-					for (ChannelContext channelContext : channels) {
-						//判断 通道 是否已经加入了 群组
-						if( !Aio.isInGroup(group.getGroup_id(),channelContext) ){
-							//该通道未加入该群组则 加入
-							//获取加入群组 处理器
-							JoinGroupReqHandler joinGroupReqHandler = CommandManager.getCommand(Command.COMMAND_JOIN_GROUP_REQ, JoinGroupReqHandler.class);
-							joinGroupReqHandler.bindGroup(groupPacket, channelContext);
+				if( null != channelContexts && channelContexts.size() > 0 ){
+					//获取读锁
+					ReadLock readLock = channelContexts.getLock().readLock();
+					//加锁
+					readLock.lock();
+					try {
+						Set<ChannelContext> channels = channelContexts.getObj();
+						for (ChannelContext channelContext : channels) {
+							//判断 通道 是否已经加入了 群组
+							if( !Aio.isInGroup(group.getGroup_id(),channelContext) ){
+								//该通道未加入该群组则 加入
+								//获取加入群组 处理器
+								JoinGroupReqHandler joinGroupReqHandler = CommandManager.getCommand(Command.COMMAND_JOIN_GROUP_REQ, JoinGroupReqHandler.class);
+								joinGroupReqHandler.bindGroup(groupPacket, channelContext);
+							}
 						}
+					} catch (Exception e) {
+						log.error(e.toString(),e);
+					}finally {
+						//解锁
+						readLock.unlock();
 					}
-				} catch (Exception e) {
-					log.error(e.toString(),e);
-				}finally {
-					//解锁
-					readLock.unlock();
 				}
+
 			}
 		}
 	}

+ 1 - 3
jim-server/src/main/java/org/jim/server/command/handler/processor/chat/DefaultAsyncChatMessageProcessor.java

@@ -1,6 +1,7 @@
 package org.jim.server.command.handler.processor.chat;
 
 import org.jim.common.packets.ChatBody;
+import org.jim.common.packets.ChatType;
 import org.tio.core.ChannelContext;
 /**
  * 默认异步聊天消息处理器
@@ -11,8 +12,5 @@ public class DefaultAsyncChatMessageProcessor extends BaseAsyncChatMessageProces
 
 	@Override
 	public void doHandler(ChatBody chatBody, ChannelContext channelContext){
-
-		System.out.println("-----------------------------用来消息持久话的--------------------------");
-
 	}
 }

+ 29 - 0
jim-server/src/main/java/org/jim/server/enums/GroupStateEnum.java

@@ -0,0 +1,29 @@
+package org.jim.server.enums;
+
+/**
+ * 群组状态 1:在线 0:离线
+ * @author Darren
+ * @date 2020/2/7 19:21
+ */
+public enum GroupStateEnum {
+
+    ON_LINE(1, "在线状态"),
+    OFF_LINE(0, "离线状态");
+
+    private int key;
+    private String desc;
+
+    private GroupStateEnum(int key, String desc) {
+        this.key = key;
+        this.desc = desc;
+    }
+
+    public int getKey() {
+        return key;
+    }
+
+    public String getDesc() {
+        return desc;
+    }
+
+}

+ 36 - 5
server-chat/src/main/java/com/cn/listener/IMChatGroupListener.java

@@ -2,20 +2,39 @@ package com.cn.listener;
 
 import com.cn.model.ChatGroup;
 import org.jim.common.ImAio;
+import org.jim.common.ImConst;
 import org.jim.common.ImPacket;
 import org.jim.common.ImSessionContext;
+import org.jim.common.cache.redis.JedisTemplate;
 import org.jim.common.packets.*;
+import org.jim.server.enums.GroupStateEnum;
 import org.jim.server.enums.ServiceAccountOfflineTypeEnum;
 import org.jim.server.enums.VisitTypeEnum;
 import org.jim.server.listener.ImGroupListener;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.tio.core.ChannelContext;
 
+import java.util.Map;
+
 /**
  * @author Darren
  * @date 2020/2/6 15:53
  */
 public class IMChatGroupListener extends ImGroupListener {
 
+    private static Logger logger = LoggerFactory.getLogger(IMChatGroupListener.class);
+
+    private static JedisTemplate jedisTemplate = null;
+
+    static {
+        try {
+            jedisTemplate = JedisTemplate.me();
+        } catch (Exception e) {
+            logger.info("JedisTemplate初始化失败!");
+            e.printStackTrace();
+        }
+    }
 
     @Override
     public void onAfterUnbind(ChannelContext channelContext, String group) throws Exception {
@@ -34,13 +53,25 @@ public class IMChatGroupListener extends ImGroupListener {
         //访问类型 (SERVICEACCOUNT 客服 / CUSTOMER 客户 / VISITOR 游客)
         String visitType = clientUser.getVisitType();
         if( !VisitTypeEnum.SERVICEACCOUNT.equals(visitType) ){
-            //客户 / 游客 关闭连接 退出群组 后 删除临时群组
             ChatGroup chatGroup = ChatGroup.dao.findById(group);
-            Integer serviceAccountType = chatGroup.getInt("service_account_type");
-            if( ServiceAccountOfflineTypeEnum.NO.getKey() == serviceAccountType ){
-                //在线群组 才可以删除
-                ChatGroup.dao.deleteById(group);
+            if( null != chatGroup ){
+                Integer serviceAccountType = chatGroup.getInt("service_account_type");
+                // service_account_type  客服是否是离线状态 1: 是 0: 否
+                if( ServiceAccountOfflineTypeEnum.NO.getKey() == serviceAccountType ){
+                    //只能更改 客服 是在线时 的群组状态;  group_state 群组状态 1:在线 0:离线
+                    chatGroup.set("group_state", GroupStateEnum.OFF_LINE.getKey()).update();
+                }
             }
+
+        }else{
+            //移除客服容器中的 客服
+            String serviceAccountContainerKey
+                    =  clientUser.getExtras().getString("companyId") + ImConst.SEPARATOR
+                    + clientUser.getExtras().getString("departmentId") + ImConst.SEPARATOR
+                    + VisitTypeEnum.SERVICEACCOUNT.getKey() ;
+            String userId = clientUser.getId();
+            String[] userIdArray = {userId};
+            jedisTemplate.hashDel(serviceAccountContainerKey,userIdArray);
         }
 
         User notifyUser = new User(clientUser.getId(),clientUser.getNick());

+ 82 - 0
server-chat/src/main/java/com/cn/processor/IMChatAsyncChatMessageProcessor.java

@@ -0,0 +1,82 @@
+package com.cn.processor;
+
+import com.cn.model.ChatGroup;
+import com.cn.model.Conversation;
+import com.cn.model.ConversationRecord;
+import org.jim.common.ImAio;
+import org.jim.common.packets.ChatBody;
+import org.jim.common.packets.ChatType;
+import org.jim.common.packets.User;
+import org.jim.server.command.handler.processor.chat.DefaultAsyncChatMessageProcessor;
+import org.jim.server.enums.VisitTypeEnum;
+import org.jim.server.util.SnowflakeIdUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.tio.core.ChannelContext;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author Darren
+ * @date 2020/2/6 19:42
+ */
+public class IMChatAsyncChatMessageProcessor extends DefaultAsyncChatMessageProcessor {
+
+    private static Logger log = LoggerFactory.getLogger(IMChatAsyncChatMessageProcessor.class);
+
+    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+
+    @Override
+    public void doHandler(ChatBody chatBody, ChannelContext channelContext) {
+
+        log.info("聊天记录持久化... 开始:" + sdf.format(new Date()));
+
+        //聊天类型(如 0:未知 1:公聊、2:私聊)
+        Integer chatType = chatBody.getChatType();
+        //消息类型;(如:0:text、1:image、2:voice、3:vedio、4:music、5:news)
+        Integer msgType = chatBody.getMsgType();
+        if( ChatType.CHAT_TYPE_PUBLIC.getNumber() == chatType ){
+            //公聊
+            // group_id 与 conversation_id一致
+            Conversation conversation = Conversation.dao.findById(chatBody.getGroup_id());
+
+            Map<String, Object> attrs = new HashMap<>();
+            attrs.put("id", SnowflakeIdUtils.getInstance().nextId());
+            attrs.put("chat_body_id",chatBody.getId());
+            attrs.put("content",chatBody.getContent());
+            attrs.put("is_recall",0);
+
+            String fromId = chatBody.getFrom();
+            // customerDepartmentId  visitorDepartmentId  serviceAccountRoleDepartmentId
+            User user = ImAio.getUser(fromId);
+            String visitType = user.getVisitType();
+            if(VisitTypeEnum.CUSTOMER.getKey().equals(visitType)){
+                // CUSTOMER 客户  发送的消息
+                String companyId = user.getExtras().getString("customerId");
+                attrs.put("from_id",Long.valueOf(companyId));
+                attrs.put("to_id",conversation.getLong("service_account_id"));
+                attrs.put("type",0);
+            }else if(VisitTypeEnum.VISITOR.getKey().equals(visitType)){
+                // VISITOR 游客  发送的消息
+                String visitorId = user.getExtras().getString("visitorId");
+                attrs.put("from_id",Long.valueOf(visitorId));
+                attrs.put("to_id",conversation.getLong("service_account_id"));
+                attrs.put("type",0);
+            }else{
+                //SERVICEACCOUNT 客服  发送的消息
+                String serviceAccountId = user.getExtras().getString("serviceAccountId");
+                attrs.put("from_id",Long.valueOf(serviceAccountId));
+                attrs.put("to_id",conversation.getLong("user_id"));
+                attrs.put("type",1);
+            }
+            attrs.put("conversation_id",user.getExtras().getLong("conversationId"));
+            ConversationRecord.dao._setAttrs(attrs).save();
+        }
+
+        log.info("聊天记录持久化... 结束:" + sdf.format(new Date()));
+
+    }
+}

+ 10 - 1
server-chat/src/main/java/com/cn/processor/IMChatWsHandshakeProcessor.java

@@ -10,8 +10,12 @@ import org.jim.common.utils.JsonKit;
 import org.jim.server.command.CommandManager;
 import org.jim.server.command.handler.LoginReqHandler;
 import org.jim.server.command.handler.processor.handshake.WsHandshakeProcessor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.tio.core.ChannelContext;
 
+import java.text.SimpleDateFormat;
+import java.util.Date;
 import java.util.Map;
 
 /**
@@ -21,6 +25,10 @@ import java.util.Map;
  */
 public class IMChatWsHandshakeProcessor extends WsHandshakeProcessor {
 
+    private static Logger log = LoggerFactory.getLogger(IMChatWsHandshakeProcessor.class);
+
+    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+
     /**
      * 三次握手之后处理的操作
      * @param packet
@@ -31,7 +39,7 @@ public class IMChatWsHandshakeProcessor extends WsHandshakeProcessor {
      */
     @Override
     public void onAfterHandshaked(ImPacket packet, ChannelContext channelContext) throws Exception {
-
+        log.info("握手成功后,执行方法 onAfterHandshaked() .. 开始时间:" + sdf.format(new Date()));
         LoginReqHandler loginReqHandler = (LoginReqHandler)CommandManager.getCommand(Command.COMMAND_LOGIN_REQ);
         //获取参数
         HttpRequest request = (HttpRequest)packet;
@@ -48,5 +56,6 @@ public class IMChatWsHandshakeProcessor extends WsHandshakeProcessor {
             //通知对应客户端 发送响应结果
             ImAio.send(channelContext,loginRespPacket);
         }
+        log.info("握手成功后,执行方法 onAfterHandshaked() .. 结束时间:" + sdf.format(new Date()));
     }
 }

+ 47 - 48
server-chat/src/main/java/com/cn/service/IMChatLoginServiceProcessor.java

@@ -2,6 +2,7 @@ package com.cn.service;
 
 import com.alibaba.fastjson.JSONObject;
 import com.cn.model.ChatGroup;
+import com.cn.model.Conversation;
 import com.cn.model.ServiceAccountRoleDepartmentMiddle;
 import nl.basjes.shaded.org.springframework.util.CollectionUtils;
 import org.jim.server.enums.*;
@@ -29,7 +30,7 @@ public class IMChatLoginServiceProcessor implements LoginCmdProcessor {
 
     private static JedisTemplate jedisTemplate = null;
 
-    private static final String GROUP_PROPERTY = " group_id,name,avatar,service_account_role_department_middle_id,consumerId,group_type,company_id,department_id ";
+    private static final String GROUP_PROPERTY = " group_id,name,avatar,service_account_role_department_middle_id,consumer_id,group_type,company_id,department_id ";
 
     static {
         try {
@@ -49,6 +50,7 @@ public class IMChatLoginServiceProcessor implements LoginCmdProcessor {
      */
     @Override
     public LoginRespBody doLogin(LoginReqBody loginReqBody, ChannelContext channelContext) {
+        logger.info("执行登录处理器doLogin()方法...");
         //获取token
         String token = loginReqBody.getToken();
         LoginRespBody loginRespBody;
@@ -137,7 +139,9 @@ public class IMChatLoginServiceProcessor implements LoginCmdProcessor {
      * @date 2020/2/5 15:26
      */
     private List<Group> initVisitorGroups(User user) {
-        return disposeVisitorGroupData(user);
+        List<Group> groups = disposeVisitorGroupData(user);
+        this.createConversation(groups.get(0),user);
+        return groups;
     }
 
     /**
@@ -239,47 +243,46 @@ public class IMChatLoginServiceProcessor implements LoginCmdProcessor {
                             serviceAccountContainerKey
                     , user.getExtras().getString("serviceAccountRoleDepartmentId")
             );
-            //判断当前 认领这个 客户 的客服 是否在线
-            if( StringUtils.isNotBlank(receptionNum) ){
-                //在线
-                //创建临时在线群组 当客户 断开连接时 删除群组
+            String sql = this.createSelectGroupSql(user);
+            ChatGroup chatGroup = ChatGroup.dao.findFirst(sql);
+            if( null != chatGroup){
+                //之前 客服 和 客户 之间 有群组
+                Group group = disposeChatGroupToGroup(chatGroup);
+                groups.add(group);
+
+                // 更改 sw_group 表中 group_state 字段为在线    (group_state 1:在线 0:离线0
+                chatGroup.set("group_state",GroupStateEnum.ON_LINE.getKey()).update();
+            }else{
+                // 创建新的群组
                 Group group = this.createGroup(user);
                 //把 Group 转成 Map 用于 jfianl框架的 添加使用
                 Map<String, Object> attrs = groupToMap(group);
-                //客服是否是离线状态 1: 是 0: 否
-                attrs.put("service_account_type", ServiceAccountOfflineTypeEnum.NO.getKey());
-                //jfianl框架的 保存
-                ChatGroup.dao._setAttrs(attrs).save();
-                groups.add(group);
-                Integer num = Integer.valueOf(receptionNum);
-                //更改客服的服务数量
-                jedisTemplate.hashSet(serviceAccountContainerKey,user.getExtras().getString("serviceAccountRoleDepartmentId"),String.valueOf(++num));
-            }else{
-                //离线
-                //查询客户 是否有 上一次 留言的离线群组
-                String sql = createSelectGroupSql(user);
-                ChatGroup chatGroup = ChatGroup.dao.findFirst(sql);
-                if( null != chatGroup ) {
-                    // 客户 上一次 留言的离线群组
-                    Group group = this.disposeChatGroupToGroup(chatGroup);
-                    groups.add(group);
+                attrs.put("group_state", GroupStateEnum.ON_LINE.getKey());
+                //判断当前 认领这个 客户 的客服 是否在线
+                if( StringUtils.isNotBlank(receptionNum) ){
+                    //在线
+                    //更改客服的服务数量
+                    Integer num = Integer.valueOf(receptionNum);
+                    jedisTemplate.hashSet(serviceAccountContainerKey,user.getExtras().getString("serviceAccountRoleDepartmentId"),String.valueOf(++num));
+                    //客服是否是离线状态 1: 是 0: 否
+                    attrs.put("service_account_type", ServiceAccountOfflineTypeEnum.NO.getKey());
                 }else{
-                    //创建临时离线群组 当客户 断开连接时 删除群组
-                    Group group = this.createGroup(user);
-                    //把 Group 转成 Map 用于 jfianl框架的 添加使用
-                    Map<String, Object> attrs = groupToMap(group);
+                    //离线
                     //客服是否是离线状态 1: 是 0: 否
                     attrs.put("service_account_type", ServiceAccountOfflineTypeEnum.YES.getKey());
-                    //jfianl框架的 保存
-                    ChatGroup.dao._setAttrs(attrs).save();
-                    groups.add(group);
                 }
+
+                //jfianl框架的 保存
+                ChatGroup.dao._setAttrs(attrs).save();
+
+                groups.add(group);
             }
 
         }else{
             //按照游客 方式 处理 找到 在线客服 连接数 最少的
             groups.addAll(this.disposeVisitorGroupData(user));
         }
+        this.createConversation(groups.get(0),user);
         return groups;
     }
 
@@ -292,9 +295,9 @@ public class IMChatLoginServiceProcessor implements LoginCmdProcessor {
      * @date 2020/2/6 10:16
      */
     private void createConversation(Group group,User user) {
-        ServiceAccountRoleDepartmentMiddle dao = ServiceAccountRoleDepartmentMiddle.dao;
         Map<String, Object> attrs = new HashMap<>();
-        attrs.put("id", Long.valueOf(group.getGroup_id()));
+        Long conversationId = SnowflakeIdUtils.getInstance().nextId();
+        attrs.put("id", conversationId );
         //会话状态 0:游客会话 1:客户会话  群组类型 0:游客-客服 类型 1:客户-客服 类型
         attrs.put("state", group.getGroupType());
         if(VisitTypeEnum.CUSTOMER.getKey().equals(user.getVisitType())){
@@ -308,9 +311,11 @@ public class IMChatLoginServiceProcessor implements LoginCmdProcessor {
         attrs.put("company_id", Long.valueOf(group.getCompanyId()));
         attrs.put("department_id", Long.valueOf(group.getDepartmentId()));
         ServiceAccountRoleDepartmentMiddle pojo
-                = dao.findById(group.getServiceAccountRoleDepartmentId());
-        attrs.put("service_account_id", Long.valueOf(pojo.get("service_account_id")));
-        dao._setAttrs(attrs).save();
+                = ServiceAccountRoleDepartmentMiddle.dao.findById(group.getServiceAccountRoleDepartmentId());
+        attrs.put("service_account_id", pojo.getLong("service_account_id"));
+        Conversation.dao._setAttrs(attrs).save();
+        //保存 会话id 存入user中
+        user.getExtras().put("conversationId",conversationId);
     }
 
     /**
@@ -324,7 +329,7 @@ public class IMChatLoginServiceProcessor implements LoginCmdProcessor {
         StringBuilder sql = new StringBuilder();
         sql.append(" select ")
                 .append(GROUP_PROPERTY)
-                .append(" from sw_group where service_account_type = 1 ")
+                .append(" from sw_group where 1 = 1 ")
                 .append(" and company_id = " + user.getExtras().getString("companyId"))
                 .append(" and department_id = " + user.getExtras().getString("departmentId"))
                 .append(
@@ -397,10 +402,6 @@ public class IMChatLoginServiceProcessor implements LoginCmdProcessor {
         }
         group.setCompanyId(user.getExtras().getString("companyId"));
         group.setDepartmentId(user.getExtras().getString("departmentId"));
-
-        //创建会话
-        this.createConversation(group,user);
-
         return group;
     }
 
@@ -493,7 +494,8 @@ public class IMChatLoginServiceProcessor implements LoginCmdProcessor {
                 sql
                         .append(" select ")
                         .append(GROUP_PROPERTY)
-                        .append(" from sw_group where 1 = 1 ")
+                        //查看当前平台下 部门下 的所有在线群组
+                        .append(" from sw_group where group_state = 1 ")
                         .append(" and company_id = "+ user.getExtras().getString("companyId") + " ")
                         .append(" and department_id = " + user.getExtras().getString("departmentId") + " ");
                 List<ChatGroup> chatGroups = ChatGroup.dao.find(sql.toString());
@@ -540,10 +542,8 @@ public class IMChatLoginServiceProcessor implements LoginCmdProcessor {
         sql
                 .append(" select ")
                 .append(GROUP_PROPERTY)
-                //  service_account_type = 1 判断 当前的 是否是离线状态下的
-                //有可能 客服 下线 客户没下线 所以 这个地方的过滤 不用 过滤 service_account_type = 1
-                //群组 的删除 是依 客户/游客的下线 为准(并且 是在线群组 才会删除,离线群组不会删除)
-                .append(" from sw_group where 1 = 1 and group_type = 1 ")
+                //  service_account_type = 1 判断 当前的 是否是离线状态下的; group_state 群组状态 1:在线 0:离线
+                .append(" from sw_group where group_state = 1 and service_account_type = 1 and group_type = 1 ")
                 .append(" and company_id = "+ user.getExtras().getString("companyId") + " ")
                 .append(" and department_id = " + user.getExtras().getString("departmentId") + " ")
                 .append(
@@ -552,11 +552,10 @@ public class IMChatLoginServiceProcessor implements LoginCmdProcessor {
         List<ChatGroup> chatGroups = ChatGroup.dao.find(sql.toString());
         List<Group> groups = this.disposeChatGroupsToGroups(chatGroups);
 
-        //修改 sw_group 字段service_account_type 状态
+        //修改 sw_group 字段service_account_type 状态   客服是否是离线状态 1: 是 0: 否
         if( !CollectionUtils.isEmpty(chatGroups) ){
             for (ChatGroup chatGroup : chatGroups) {
-                ChatGroup.dao.findById(chatGroup.getStr("group_id"))
-                        .set("service_account_type", ServiceAccountOfflineTypeEnum.NO.getKey())
+                chatGroup .set("service_account_type", ServiceAccountOfflineTypeEnum.NO.getKey())
                         .update();
             }
         }