Repository navigation
Expand file tree
/
Copy pathTenActiveService.java
More file actions
168 lines (158 loc) · 7.7 KB
/
Copy pathTenActiveService.java
File metadata and controls
168 lines (158 loc) · 7.7 KB
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
package com.zero.barrageserver.activeapi.service;
import com.zero.barrageserver.activeapi.dao.TenActiveDao;
import com.zero.barrageserver.common.constant.ConfigConstant;
import com.zero.barrageserver.common.constant.Constant;
import com.zero.barrageserver.common.constant.ErrorConstant;
import com.zero.barrageserver.common.entity.TenGroupEntity;
import com.zero.barrageserver.common.entity.activeresponse.TenGroupJoinResponse;
import com.zero.barrageserver.common.entity.activeresponse.TenGroupMembersResponse;
import com.zero.barrageserver.common.entity.activeresponse.TenMembersStatusResponse;
import com.zero.barrageserver.common.entity.response.BaseResponseEntity;
import com.zero.barrageserver.common.entity.activeresponse.TenResponseEntity;
import com.zero.barrageserver.common.entity.response.MembersOnLineResponse;
import com.zero.barrageserver.common.manager.ExecutorManager;
import com.zero.barrageserver.common.utils.StringUtil;
import com.alibaba.fastjson.JSONObject;
import com.zero.barrageserver.common.entity.TenGroupEntity;
import com.zero.barrageserver.common.entity.activeresponse.TenGroupJoinResponse;
import com.zero.barrageserver.common.entity.activeresponse.TenResponseEntity;
import com.zero.barrageserver.common.entity.response.BaseResponseEntity;
import com.zero.barrageserver.common.entity.response.MembersOnLineResponse;
import com.zero.barrageserver.common.utils.StringUtil;
import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.schedulers.Schedulers;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by zero大神 on 2017/12/1.
*/
@Log4j2
@Service
public class TenActiveService {
@Autowired
private TenActiveDao tenActiveDao;
@Autowired
private ConfigConstant configConstant;
/**
* 创建或者从redis中获取缓存的群ID
* @param shopId
* @return
*/
public BaseResponseEntity<TenResponseEntity> checkAndReturnGroupId(String shopId){
if(StringUtil.isEmpty(shopId)){
log.error("in checkAndReturnGroupId,shop name or shop id is null.");
return new BaseResponseEntity<>(ErrorConstant.ERROR_CODE_PARAM_ERROR);
}
//保存Redis
TenResponseEntity tenGroupResponse = tenActiveDao.createTenGroup(getBarrageGroupName(shopId),getBarrageGroupId(shopId));
if(null == tenGroupResponse){
log.error("create ten group can not get response error.shopId:" + shopId);
return new BaseResponseEntity<>(ErrorConstant.ERROR_CODE_RESPONSE_NULL);
}
if(!StringUtil.isEmpty(tenGroupResponse.getGroupId())){
return new BaseResponseEntity<>(ErrorConstant.ERROR_CODE_OK,tenGroupResponse);
}
//未获取到GroupId,可能已经创建过了
if(tenGroupResponse.getErrorCode() == ErrorConstant.TEN_IM_ERROR_CODE_GROUP_ID_CREATED){
tenGroupResponse.setGroupId(getBarrageGroupId(shopId));
return new BaseResponseEntity<>(ErrorConstant.ERROR_CODE_OK,tenGroupResponse);
}
return new BaseResponseEntity<>(ErrorConstant.ERROR_CODE_OTHER);
}
/**
* 同步发送消息
* @param shopId
* @param obj
* @param <T>
* @return
*/
public <T>int sendMsg(String shopId,T obj){
if(StringUtil.isEmpty(shopId) || null == obj){
log.error("in send msg shop id is empty or obj is null");
return ErrorConstant.ERROR_CODE_PARAM_ERROR;
}
String text = JSONObject.toJSONString(obj);
String groupId = getBarrageGroupId(shopId);
TenResponseEntity tenResponse = tenActiveDao.sendMsg(groupId,text);
if(null == tenResponse){
log.error("send ten Msg can not get response error.groupId:" + groupId + ";text:" + text);
return ErrorConstant.ERROR_CODE_RESPONSE_NULL;
}
if(tenResponse.getErrorCode() != ErrorConstant.TEN_IM_ERROR_CODE_OK){
log.error("send ten Msg error.groupId:" + groupId + ";text:" + text + ";error code:" + tenResponse.getErrorCode() + ";error msg:" + tenResponse.getErrorInfo());
return tenResponse.getErrorCode();
}
return tenResponse.getErrorCode();
}
/**
* 异步发送消息
* @param shopId
* @param obj
* @param needSingleExecutor 如果是需要顺序发送的消息必须为true
* @param <T>
*/
public <T>void sendMsgAsync(final String shopId, final T obj,final boolean needSingleExecutor,final String threadKey,int retryCount){
//lamda格式,请注意
Observable.create((ObservableOnSubscribe<Integer>) observableEmitter -> {
log.info("on subscribe:" + ";thread:" + Thread.currentThread().getId());
int errorCode = sendMsg(shopId,obj);
if(errorCode != ErrorConstant.TEN_IM_ERROR_CODE_OK && retryCount < Constant.IM_MSG_RETRY_MAX_COUNT){
//执行异步重试
log.error("执行异步重试errorCode:" + errorCode + ";shopId:" + shopId + ";obj:" + obj.toString());
sendMsgAsync(shopId,obj,needSingleExecutor,threadKey,retryCount + 1);
}
})
//单线程使用key去获取相对应的单线程池,如果不需要则使用并发线程池
.subscribeOn(Schedulers.from(needSingleExecutor ? ExecutorManager.getSingleExecutorByKey(threadKey) : ExecutorManager.getNormalMsgExecutor()))
.subscribe();
}
public MembersOnLineResponse getOnLineMembers(String shopId){
MembersOnLineResponse membersOnLineResponse = new MembersOnLineResponse();
if(StringUtil.isEmpty(shopId)){
return membersOnLineResponse;
}
String groupId = getBarrageGroupId(shopId);
TenGroupMembersResponse groupMembersResponse = tenActiveDao.getGroupMembers(groupId);
if(null == groupMembersResponse || null == groupMembersResponse.getMemberList() || groupMembersResponse.getMemberList().size() == 0){
return membersOnLineResponse;
}
TenMembersStatusResponse membersStatusResponse = tenActiveDao.getMembersStatus(groupMembersResponse.getMemberList());
if(null == membersStatusResponse || membersStatusResponse.getQueryResult() == null || membersStatusResponse.getQueryResult().size() == 0){
return membersOnLineResponse;
}
membersOnLineResponse.setOnlineMembers(membersStatusResponse.getQueryResult());
return membersOnLineResponse;
}
/**
* 删除出给出的GroupId之外的所有用户已加入群组
* @param groupId
* @param userId
* @return
*/
public BaseResponseEntity leaveOtherGroup(String groupId,String userId){
if(StringUtil.isEmpty(userId)){
return new BaseResponseEntity(ErrorConstant.ERROR_CODE_PARAM_ERROR);
}
TenGroupJoinResponse groupJoinResponse = tenActiveDao.getGroupJoinInfo(userId);
if(null == groupJoinResponse || groupJoinResponse.getGroupIdList() == null){
return new BaseResponseEntity(ErrorConstant.ERROR_CODE_OK);
}
for(TenGroupEntity groupEntity:groupJoinResponse.getGroupIdList()){
if(groupEntity.getGroupId().equals(groupId)){
//除了传入的GroupId,其他全部退出
continue;
}
//执行退出操作
tenActiveDao.delGroupMember(groupEntity.getGroupId(),userId);
}
return new BaseResponseEntity(ErrorConstant.ERROR_CODE_OK);
}
private String getBarrageGroupId(String shopId){
return "Bg" + configConstant.getBuildType() + "" + shopId;
}
private String getBarrageGroupName(String shopId){
return "Bg" + configConstant.getBuildType() + "" + shopId;
}
}