|
@@ -17,13 +17,17 @@ import org.jim.common.cache.redis.JedisTemplate;
|
|
|
import org.jim.common.packets.*;
|
|
|
import org.jim.server.command.handler.processor.login.LoginCmdProcessor;
|
|
|
import org.jim.server.util.SnowflakeIdUtils;
|
|
|
+import org.jim.server.util.TransactionKit;
|
|
|
import org.redisson.api.RLock;
|
|
|
import org.redisson.api.RedissonClient;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.tio.core.ChannelContext;
|
|
|
import org.tio.utils.lock.SetWithLock;
|
|
|
+import redis.clients.jedis.Jedis;
|
|
|
+import redis.clients.jedis.Transaction;
|
|
|
|
|
|
+import java.sql.SQLException;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
@@ -72,45 +76,40 @@ public class IMChatLoginServiceProcessor implements LoginCmdProcessor {
|
|
|
RLock rLock = redisson.getLock(LOGIN);
|
|
|
LoginRespBody loginRespBody;
|
|
|
boolean res = false;
|
|
|
+ Transaction transaction = null;
|
|
|
try {
|
|
|
res = rLock.tryLock(500, 5000, TimeUnit.MILLISECONDS);
|
|
|
if (res) {
|
|
|
+ Jedis jedis = jedisTemplate.getSingletonJedis();
|
|
|
+ //开启redis事务
|
|
|
+ transaction = jedis.multi();
|
|
|
+ //开启sql事务,关闭自动提交,改为手动提交
|
|
|
+ TransactionKit.beginTransation();
|
|
|
//获取token
|
|
|
String token = loginReqBody.getToken();
|
|
|
User user = null;
|
|
|
if(StringUtils.isNotBlank(token)){
|
|
|
user = this.getUser(token);
|
|
|
}
|
|
|
- if(user == null){
|
|
|
- loginRespBody = new LoginRespBody(Command.COMMAND_LOGIN_RESP,ImStatus.C10008);
|
|
|
- }else{
|
|
|
- Boolean noOnlineService = user.getExtras().getBoolean("noOnlineService");
|
|
|
- if(noOnlineService != null && noOnlineService){
|
|
|
- loginRespBody = new LoginRespBody(Command.COMMAND_LOGIN_RESP,ImStatus.C10022,user);
|
|
|
- }else{
|
|
|
- loginRespBody = new LoginRespBody(Command.COMMAND_LOGIN_RESP,ImStatus.C10007,user);
|
|
|
- String userJson = JsonKit.toJSONString(user);
|
|
|
- Map<String,String> users = jedisTemplate.hashGetAll(CURRENT_SYSTEM_ALL_USER_ID);
|
|
|
- if(!CollectionUtils.isEmpty(users)){
|
|
|
- boolean flag = true;
|
|
|
- for(Map.Entry<String,String> entry : users.entrySet()){
|
|
|
- if(entry.getKey() == user.getId() ){
|
|
|
- flag = false;
|
|
|
- }
|
|
|
- }
|
|
|
- if(flag){
|
|
|
- //存储所有登录的用户 的 userId
|
|
|
- jedisTemplate.hashSet(CURRENT_SYSTEM_ALL_USER_ID, user.getId(), userJson);
|
|
|
- }
|
|
|
- }else{
|
|
|
- jedisTemplate.hashSet(CURRENT_SYSTEM_ALL_USER_ID, user.getId(), userJson);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ loginRespBody = this.disposeLoginData(user);
|
|
|
+ int a = 4/0;
|
|
|
+ //提交sql事务,关闭手动提交,开启自动提交
|
|
|
+ TransactionKit.commit();
|
|
|
+ //提交redis事务
|
|
|
+ transaction.exec();
|
|
|
}else{
|
|
|
loginRespBody = new LoginRespBody(Command.COMMAND_LOGIN_RESP,ImStatus.C10028);
|
|
|
}
|
|
|
- } catch (InterruptedException e) {
|
|
|
+ } catch (Exception e) {
|
|
|
+ if(transaction != null){
|
|
|
+ transaction.discard();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ //sql事务回滚
|
|
|
+ TransactionKit.rollback();
|
|
|
+ } catch (SQLException ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
loginRespBody = new LoginRespBody(Command.COMMAND_LOGIN_RESP,ImStatus.C10029);
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
@@ -122,6 +121,42 @@ public class IMChatLoginServiceProcessor implements LoginCmdProcessor {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 处理登录数据
|
|
|
+ * @param user
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private LoginRespBody disposeLoginData(User user){
|
|
|
+ LoginRespBody loginRespBody;
|
|
|
+ if(user == null){
|
|
|
+ loginRespBody = new LoginRespBody(Command.COMMAND_LOGIN_RESP,ImStatus.C10008);
|
|
|
+ }else{
|
|
|
+ Boolean noOnlineService = user.getExtras().getBoolean("noOnlineService");
|
|
|
+ if(noOnlineService != null && noOnlineService){
|
|
|
+ loginRespBody = new LoginRespBody(Command.COMMAND_LOGIN_RESP,ImStatus.C10022,user);
|
|
|
+ }else{
|
|
|
+ loginRespBody = new LoginRespBody(Command.COMMAND_LOGIN_RESP,ImStatus.C10007,user);
|
|
|
+ String userJson = JsonKit.toJSONString(user);
|
|
|
+ Map<String,String> users = jedisTemplate.hashGetAll(CURRENT_SYSTEM_ALL_USER_ID);
|
|
|
+ if(!CollectionUtils.isEmpty(users)){
|
|
|
+ boolean flag = true;
|
|
|
+ for(Map.Entry<String,String> entry : users.entrySet()){
|
|
|
+ if(entry.getKey() == user.getId() ){
|
|
|
+ flag = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(flag){
|
|
|
+ //存储所有登录的用户 的 userId
|
|
|
+ jedisTemplate.hashSet(CURRENT_SYSTEM_ALL_USER_ID, user.getId(), userJson);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ jedisTemplate.hashSet(CURRENT_SYSTEM_ALL_USER_ID, user.getId(), userJson);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return loginRespBody;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 通过token 获取用户信息
|
|
|
* @param token
|
|
|
* @return {@link {@link User}}
|
|
@@ -147,9 +182,11 @@ public class IMChatLoginServiceProcessor implements LoginCmdProcessor {
|
|
|
if( VisitTypeEnum.VISITOR.getKey().equals(visitType) ){
|
|
|
//当前访问人为游客
|
|
|
user = this.createVisitorData(map);
|
|
|
- //修改游客在线状态
|
|
|
+ //判断 是否有在线客服
|
|
|
Boolean noOnlineService = user.getExtras().getBoolean("noOnlineService");
|
|
|
if(!noOnlineService){
|
|
|
+ //存在在线客服
|
|
|
+ //修改游客在线状态
|
|
|
VisitorDepartmentMiddle.dao.findById(user.getId()).set("online",0).update();
|
|
|
}
|
|
|
}
|
|
@@ -239,6 +276,7 @@ public class IMChatLoginServiceProcessor implements LoginCmdProcessor {
|
|
|
//游客对应客服存在 且 在线; isOnline=true;
|
|
|
isOnline = serviceAccountContainer.containsKey(accountRoleDepartmentId);
|
|
|
}
|
|
|
+ //有没有在线客服
|
|
|
user.getExtras().put("noOnlineService",false);
|
|
|
if(!isOnline){
|
|
|
// 从当前在线的客服中 获取 连接客户数最少的 客服
|
|
@@ -638,7 +676,7 @@ public class IMChatLoginServiceProcessor implements LoginCmdProcessor {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 初始化 由客服主动发起聊天的群组
|
|
|
+ * 初始化 由 客服 向客户/游客 主动发起聊天的群组
|
|
|
* @param user
|
|
|
* @return {@link {@link List< Group>}}
|
|
|
* @author Darren
|
|
@@ -702,7 +740,7 @@ public class IMChatLoginServiceProcessor implements LoginCmdProcessor {
|
|
|
.append(" order by create_time desc");
|
|
|
ChatGroup chatGroup = ChatGroup.dao.findFirst(sql.toString());
|
|
|
//customer 是否在线 如果不在线就是 留言群组
|
|
|
- boolean isOnline = isOnline(customerDepartmentId);
|
|
|
+ boolean isOnline = this.isOnline(customerDepartmentId);
|
|
|
if(null != chatGroup){
|
|
|
//说明 当前存在 群组
|
|
|
if(
|
|
@@ -967,7 +1005,7 @@ public class IMChatLoginServiceProcessor implements LoginCmdProcessor {
|
|
|
//修改 sw_group 表中的 message_state 状态; 群组留言状态 1:留言 0:非留言
|
|
|
//修改 sw_group 表中的 service_account_type 状态; 客服离线状态 1: 离线 0: 在线
|
|
|
for (ChatGroup chatGroup : messageChatGroups) {
|
|
|
- chatGroup .set("message_state", MessageStateEnum.NO.getKey())
|
|
|
+ chatGroup.set("message_state", MessageStateEnum.NO.getKey())
|
|
|
.set("service_account_type",ServiceAccountOfflineTypeEnum.ON_LINE.getKey())
|
|
|
.update();
|
|
|
}
|