|
@@ -0,0 +1,105 @@
|
|
|
+package com.yonge.cooleshow.biz.dal.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.yonge.cooleshow.biz.dal.config.RongCloudConfig;
|
|
|
+import com.yonge.cooleshow.biz.dal.dao.ImGroupDao;
|
|
|
+import com.yonge.cooleshow.biz.dal.dto.ImGroupDto;
|
|
|
+import com.yonge.cooleshow.biz.dal.entity.ImGroup;
|
|
|
+import com.yonge.cooleshow.biz.dal.entity.ImGroupMember;
|
|
|
+import com.yonge.cooleshow.biz.dal.service.ImGroupMemberService;
|
|
|
+import com.yonge.cooleshow.biz.dal.service.ImGroupService;
|
|
|
+import com.yonge.cooleshow.common.exception.BizException;
|
|
|
+import io.rong.models.Result;
|
|
|
+import io.rong.models.group.GroupMember;
|
|
|
+import io.rong.models.group.GroupModel;
|
|
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 即时通讯群组(ImGroup)表服务实现类
|
|
|
+ *
|
|
|
+ * @author zx
|
|
|
+ * @since 2022-03-22 10:45:57
|
|
|
+ */
|
|
|
+@Service("imGroupService")
|
|
|
+public class ImGroupServiceImpl extends ServiceImpl<ImGroupDao, ImGroup> implements ImGroupService {
|
|
|
+
|
|
|
+ private final static Logger log = LoggerFactory.getLogger(ImGroupServiceImpl.class);
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ImGroupMemberService imGroupMemberService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ImGroupDao getDao() {
|
|
|
+ return this.baseMapper;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void create(ImGroupDto imGroup) throws Exception {
|
|
|
+ Set<Long> groupMemberIdList = imGroup.getGroupMemberList();
|
|
|
+ if(CollectionUtils.isNotEmpty(groupMemberIdList)){
|
|
|
+ groupMemberIdList.removeAll(Collections.singleton(null));
|
|
|
+ }
|
|
|
+ if (CollectionUtils.isEmpty(groupMemberIdList)){
|
|
|
+ throw new BizException("群成员不可为空");
|
|
|
+ }
|
|
|
+ //创建本地群聊
|
|
|
+ Date now = new Date();
|
|
|
+ imGroup.setType(ImGroup.ImGroupType.FAN);
|
|
|
+ imGroup.setMemberNum(1);
|
|
|
+ imGroup.setCreateTime(now);
|
|
|
+ imGroup.setUpdateTime(now);
|
|
|
+ this.baseMapper.insert(imGroup);
|
|
|
+ Long imGroupId = imGroup.getId();
|
|
|
+ //创建融云群
|
|
|
+ this.rtcCreate(groupMemberIdList,imGroupId,imGroup.getName());
|
|
|
+ //创建成功,添加群成员
|
|
|
+ imGroupMemberService.addGroupMember(imGroupId,imGroup.getCreateBy(),true,ImGroupMember.ImGroupMemberRoleType.TEACHER);
|
|
|
+ }
|
|
|
+
|
|
|
+ //创建融云群
|
|
|
+ private void rtcCreate(Set<Long> groupMemberIdList,Long imGroupId,String imGroupName) throws Exception {
|
|
|
+ List<GroupMember> groupMemberList = new ArrayList<>();
|
|
|
+ for (Long userId : groupMemberIdList) {
|
|
|
+ groupMemberList.add(new GroupMember(userId.toString(),imGroupId.toString(),null));
|
|
|
+ }
|
|
|
+ //创建融云群
|
|
|
+ GroupMember[] groupMembers = groupMemberList.toArray(new GroupMember[groupMemberList.size()]);
|
|
|
+ GroupModel groupModel = new GroupModel(imGroupId.toString(),0);
|
|
|
+ groupModel.setMembers(groupMembers);
|
|
|
+ groupModel.setName(imGroupName);
|
|
|
+ Result result = RongCloudConfig.rongCloud.group.create(groupModel);
|
|
|
+ if(!result.code.equals(200)){
|
|
|
+ log.error("创建群聊失败:{}",result.errorMessage);
|
|
|
+ throw new BizException("创建群聊失败,请联系管理员");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void dismiss(Long groupId) throws Exception {
|
|
|
+ ImGroup imGroup = this.baseMapper.selectById(groupId);
|
|
|
+ if(Objects.isNull(imGroup)){
|
|
|
+ throw new BizException("操作失败:群组不存在");
|
|
|
+ }
|
|
|
+ //解散融云群
|
|
|
+ GroupModel groupModel = new GroupModel(groupId.toString(),0);
|
|
|
+ Result result = RongCloudConfig.rongCloud.group.dismiss(groupModel);
|
|
|
+ if(!result.code.equals(200)){
|
|
|
+ log.error("解散群聊失败:{}",result.errorMessage);
|
|
|
+ throw new BizException("解散群聊失败,请联系管理员");
|
|
|
+ }
|
|
|
+ //销毁成功,删除群
|
|
|
+ this.baseMapper.deleteById(groupId);
|
|
|
+ //删除群成员
|
|
|
+ imGroupMemberService.delByGroupId(groupId);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|