|
|
@ -1,28 +1,30 @@ |
|
|
package com.bx.implatform.service.impl; |
|
|
package com.bx.implatform.service.impl; |
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil; |
|
|
import cn.hutool.core.util.StrUtil; |
|
|
import cn.hutool.core.util.StrUtil; |
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
import com.bx.imclient.IMClient; |
|
|
import com.bx.imclient.IMClient; |
|
|
|
|
|
import com.bx.imcommon.model.IMGroupMessage; |
|
|
|
|
|
import com.bx.imcommon.model.IMUserInfo; |
|
|
|
|
|
import com.bx.imcommon.util.CommaTextUtils; |
|
|
import com.bx.implatform.contant.Constant; |
|
|
import com.bx.implatform.contant.Constant; |
|
|
import com.bx.implatform.contant.RedisKey; |
|
|
import com.bx.implatform.contant.RedisKey; |
|
|
import com.bx.implatform.entity.Friend; |
|
|
import com.bx.implatform.entity.*; |
|
|
import com.bx.implatform.entity.Group; |
|
|
import com.bx.implatform.enums.MessageStatus; |
|
|
import com.bx.implatform.entity.GroupMember; |
|
|
import com.bx.implatform.enums.MessageType; |
|
|
import com.bx.implatform.entity.User; |
|
|
|
|
|
import com.bx.implatform.enums.ResultCode; |
|
|
import com.bx.implatform.enums.ResultCode; |
|
|
import com.bx.implatform.exception.GlobalException; |
|
|
import com.bx.implatform.exception.GlobalException; |
|
|
import com.bx.implatform.mapper.GroupMapper; |
|
|
import com.bx.implatform.mapper.GroupMapper; |
|
|
import com.bx.implatform.service.IFriendService; |
|
|
import com.bx.implatform.mapper.GroupMessageMapper; |
|
|
import com.bx.implatform.service.IGroupMemberService; |
|
|
import com.bx.implatform.service.*; |
|
|
import com.bx.implatform.service.IGroupService; |
|
|
|
|
|
import com.bx.implatform.service.IUserService; |
|
|
|
|
|
import com.bx.implatform.session.SessionContext; |
|
|
import com.bx.implatform.session.SessionContext; |
|
|
import com.bx.implatform.session.UserSession; |
|
|
import com.bx.implatform.session.UserSession; |
|
|
import com.bx.implatform.util.BeanUtils; |
|
|
import com.bx.implatform.util.BeanUtils; |
|
|
import com.bx.implatform.vo.GroupInviteVO; |
|
|
import com.bx.implatform.vo.GroupInviteVO; |
|
|
import com.bx.implatform.vo.GroupMemberVO; |
|
|
import com.bx.implatform.vo.GroupMemberVO; |
|
|
|
|
|
import com.bx.implatform.vo.GroupMessageVO; |
|
|
import com.bx.implatform.vo.GroupVO; |
|
|
import com.bx.implatform.vo.GroupVO; |
|
|
import lombok.RequiredArgsConstructor; |
|
|
import lombok.RequiredArgsConstructor; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
@ -44,6 +46,7 @@ import java.util.stream.Collectors; |
|
|
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements IGroupService { |
|
|
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements IGroupService { |
|
|
private final IUserService userService; |
|
|
private final IUserService userService; |
|
|
private final IGroupMemberService groupMemberService; |
|
|
private final IGroupMemberService groupMemberService; |
|
|
|
|
|
private final GroupMessageMapper groupMessageMapper; |
|
|
private final IFriendService friendsService; |
|
|
private final IFriendService friendsService; |
|
|
private final IMClient imClient; |
|
|
private final IMClient imClient; |
|
|
private final RedisTemplate<String, Object> redisTemplate; |
|
|
private final RedisTemplate<String, Object> redisTemplate; |
|
|
@ -85,7 +88,7 @@ public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements |
|
|
} |
|
|
} |
|
|
// 更新成员信息
|
|
|
// 更新成员信息
|
|
|
GroupMember member = groupMemberService.findByGroupAndUserId(vo.getId(), session.getUserId()); |
|
|
GroupMember member = groupMemberService.findByGroupAndUserId(vo.getId(), session.getUserId()); |
|
|
if (member == null) { |
|
|
if (Objects.isNull(member) || member.getQuit()) { |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR, "您不是群聊的成员"); |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR, "您不是群聊的成员"); |
|
|
} |
|
|
} |
|
|
member.setAliasName(StringUtils.isEmpty(vo.getAliasName()) ? session.getNickName() : vo.getAliasName()); |
|
|
member.setAliasName(StringUtils.isEmpty(vo.getAliasName()) ? session.getNickName() : vo.getAliasName()); |
|
|
@ -104,6 +107,8 @@ public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements |
|
|
if (!group.getOwnerId().equals(session.getUserId())) { |
|
|
if (!group.getOwnerId().equals(session.getUserId())) { |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR, "只有群主才有权限解除群聊"); |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR, "只有群主才有权限解除群聊"); |
|
|
} |
|
|
} |
|
|
|
|
|
// 群聊用户id
|
|
|
|
|
|
List<Long> userIds = groupMemberService.findUserIdsByGroupId(groupId); |
|
|
// 逻辑删除群数据
|
|
|
// 逻辑删除群数据
|
|
|
group.setDeleted(true); |
|
|
group.setDeleted(true); |
|
|
this.updateById(group); |
|
|
this.updateById(group); |
|
|
@ -112,6 +117,8 @@ public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements |
|
|
// 清理已读缓存
|
|
|
// 清理已读缓存
|
|
|
String key = StrUtil.join(":", RedisKey.IM_GROUP_READED_POSITION, groupId); |
|
|
String key = StrUtil.join(":", RedisKey.IM_GROUP_READED_POSITION, groupId); |
|
|
redisTemplate.delete(key); |
|
|
redisTemplate.delete(key); |
|
|
|
|
|
// 推送解散群聊提示
|
|
|
|
|
|
this.sendTipMessage(groupId,userIds,String.format("'%s'解散了群聊",session.getNickName())); |
|
|
log.info("删除群聊,群聊id:{},群聊名称:{}", group.getId(), group.getName()); |
|
|
log.info("删除群聊,群聊id:{},群聊名称:{}", group.getId(), group.getName()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -127,6 +134,8 @@ public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements |
|
|
// 清理已读缓存
|
|
|
// 清理已读缓存
|
|
|
String key = StrUtil.join(":", RedisKey.IM_GROUP_READED_POSITION, groupId); |
|
|
String key = StrUtil.join(":", RedisKey.IM_GROUP_READED_POSITION, groupId); |
|
|
redisTemplate.opsForHash().delete(key,userId.toString()); |
|
|
redisTemplate.opsForHash().delete(key,userId.toString()); |
|
|
|
|
|
// 推送退出群聊提示
|
|
|
|
|
|
this.sendTipMessage(groupId,Arrays.asList(userId),"您已退出群聊"); |
|
|
log.info("退出群聊,群聊id:{},群聊名称:{},用户id:{}", group.getId(), group.getName(), userId); |
|
|
log.info("退出群聊,群聊id:{},群聊名称:{},用户id:{}", group.getId(), group.getName(), userId); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -138,27 +147,33 @@ public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR, "您不是群主,没有权限踢人"); |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR, "您不是群主,没有权限踢人"); |
|
|
} |
|
|
} |
|
|
if (userId.equals(session.getUserId())) { |
|
|
if (userId.equals(session.getUserId())) { |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR, "亲,不能自己踢自己哟"); |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR, "亲,不能移除自己哟"); |
|
|
} |
|
|
} |
|
|
// 删除群聊成员
|
|
|
// 删除群聊成员
|
|
|
groupMemberService.removeByGroupAndUserId(groupId, userId); |
|
|
groupMemberService.removeByGroupAndUserId(groupId, userId); |
|
|
// 清理已读缓存
|
|
|
// 清理已读缓存
|
|
|
String key = StrUtil.join(":", RedisKey.IM_GROUP_READED_POSITION, groupId); |
|
|
String key = StrUtil.join(":", RedisKey.IM_GROUP_READED_POSITION, groupId); |
|
|
redisTemplate.opsForHash().delete(key,userId.toString()); |
|
|
redisTemplate.opsForHash().delete(key,userId.toString()); |
|
|
|
|
|
// 推送踢出群聊提示
|
|
|
|
|
|
this.sendTipMessage(groupId,Arrays.asList(userId),"您已被移出群聊"); |
|
|
log.info("踢出群聊,群聊id:{},群聊名称:{},用户id:{}", group.getId(), group.getName(), userId); |
|
|
log.info("踢出群聊,群聊id:{},群聊名称:{},用户id:{}", group.getId(), group.getName(), userId); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public GroupVO findById(Long groupId) { |
|
|
public GroupVO findById(Long groupId) { |
|
|
UserSession session = SessionContext.getSession(); |
|
|
UserSession session = SessionContext.getSession(); |
|
|
Group group = this.getById(groupId); |
|
|
Group group = super.getById(groupId); |
|
|
|
|
|
if (Objects.isNull(group)) { |
|
|
|
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR, "群组不存在"); |
|
|
|
|
|
} |
|
|
GroupMember member = groupMemberService.findByGroupAndUserId(groupId, session.getUserId()); |
|
|
GroupMember member = groupMemberService.findByGroupAndUserId(groupId, session.getUserId()); |
|
|
if (member == null) { |
|
|
if (Objects.isNull(member)) { |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR, "您未加入群聊"); |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR, "您未加入群聊"); |
|
|
} |
|
|
} |
|
|
GroupVO vo = BeanUtils.copyProperties(group, GroupVO.class); |
|
|
GroupVO vo = BeanUtils.copyProperties(group, GroupVO.class); |
|
|
vo.setAliasName(member.getAliasName()); |
|
|
vo.setAliasName(member.getAliasName()); |
|
|
vo.setRemark(member.getRemark()); |
|
|
vo.setRemark(member.getRemark()); |
|
|
|
|
|
vo.setQuit(member.getQuit()); |
|
|
return vo; |
|
|
return vo; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -166,7 +181,7 @@ public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements |
|
|
@Override |
|
|
@Override |
|
|
public Group getById(Long groupId) { |
|
|
public Group getById(Long groupId) { |
|
|
Group group = super.getById(groupId); |
|
|
Group group = super.getById(groupId); |
|
|
if (group == null) { |
|
|
if (Objects.isNull(group)) { |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR, "群组不存在"); |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR, "群组不存在"); |
|
|
} |
|
|
} |
|
|
if (group.getDeleted()) { |
|
|
if (group.getDeleted()) { |
|
|
@ -180,6 +195,8 @@ public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements |
|
|
UserSession session = SessionContext.getSession(); |
|
|
UserSession session = SessionContext.getSession(); |
|
|
// 查询当前用户的群id列表
|
|
|
// 查询当前用户的群id列表
|
|
|
List<GroupMember> groupMembers = groupMemberService.findByUserId(session.getUserId()); |
|
|
List<GroupMember> groupMembers = groupMemberService.findByUserId(session.getUserId()); |
|
|
|
|
|
// 一个月内退的群可能存在退群前的离线消息,一并返回作为前端缓存
|
|
|
|
|
|
groupMembers.addAll(groupMemberService.findQuitInMonth(session.getUserId())); |
|
|
if (groupMembers.isEmpty()) { |
|
|
if (groupMembers.isEmpty()) { |
|
|
return new LinkedList<>(); |
|
|
return new LinkedList<>(); |
|
|
} |
|
|
} |
|
|
@ -194,6 +211,7 @@ public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements |
|
|
GroupMember member = groupMembers.stream().filter(m -> g.getId().equals(m.getGroupId())).findFirst().get(); |
|
|
GroupMember member = groupMembers.stream().filter(m -> g.getId().equals(m.getGroupId())).findFirst().get(); |
|
|
vo.setAliasName(member.getAliasName()); |
|
|
vo.setAliasName(member.getAliasName()); |
|
|
vo.setRemark(member.getRemark()); |
|
|
vo.setRemark(member.getRemark()); |
|
|
|
|
|
vo.setQuit(member.getQuit()); |
|
|
return vo; |
|
|
return vo; |
|
|
}).collect(Collectors.toList()); |
|
|
}).collect(Collectors.toList()); |
|
|
} |
|
|
} |
|
|
@ -202,9 +220,13 @@ public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements |
|
|
public void invite(GroupInviteVO vo) { |
|
|
public void invite(GroupInviteVO vo) { |
|
|
UserSession session = SessionContext.getSession(); |
|
|
UserSession session = SessionContext.getSession(); |
|
|
Group group = this.getById(vo.getGroupId()); |
|
|
Group group = this.getById(vo.getGroupId()); |
|
|
if (group == null) { |
|
|
if (Objects.isNull(group)) { |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR, "群聊不存在"); |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR, "群聊不存在"); |
|
|
} |
|
|
} |
|
|
|
|
|
GroupMember member = groupMemberService.findByGroupAndUserId(vo.getGroupId(), session.getUserId()); |
|
|
|
|
|
if (Objects.isNull(group) || member.getQuit()) { |
|
|
|
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR, "您不在群聊中,邀请失败"); |
|
|
|
|
|
} |
|
|
// 群聊人数校验
|
|
|
// 群聊人数校验
|
|
|
List<GroupMember> members = groupMemberService.findByGroupId(vo.getGroupId()); |
|
|
List<GroupMember> members = groupMemberService.findByGroupId(vo.getGroupId()); |
|
|
long size = members.stream().filter(m -> !m.getQuit()).count(); |
|
|
long size = members.stream().filter(m -> !m.getQuit()).count(); |
|
|
@ -234,6 +256,11 @@ public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements |
|
|
if (!groupMembers.isEmpty()) { |
|
|
if (!groupMembers.isEmpty()) { |
|
|
groupMemberService.saveOrUpdateBatch(group.getId(), groupMembers); |
|
|
groupMemberService.saveOrUpdateBatch(group.getId(), groupMembers); |
|
|
} |
|
|
} |
|
|
|
|
|
// 推送进入群聊消息
|
|
|
|
|
|
List<Long> userIds = groupMemberService.findUserIdsByGroupId(vo.getGroupId()); |
|
|
|
|
|
String memberNames = groupMembers.stream().map(GroupMember::getAliasName).collect(Collectors.joining(",")); |
|
|
|
|
|
String content = String.format("'%s'邀请'%s'加入了群聊",session.getNickName(), memberNames); |
|
|
|
|
|
this.sendTipMessage(vo.getGroupId(),userIds,content); |
|
|
log.info("邀请进入群聊,群聊id:{},群聊名称:{},被邀请用户id:{}", group.getId(), group.getName(), vo.getFriendIds()); |
|
|
log.info("邀请进入群聊,群聊id:{},群聊名称:{},被邀请用户id:{}", group.getId(), group.getName(), vo.getFriendIds()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -249,4 +276,33 @@ public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements |
|
|
}).sorted((m1, m2) -> m2.getOnline().compareTo(m1.getOnline())).collect(Collectors.toList()); |
|
|
}).sorted((m1, m2) -> m2.getOnline().compareTo(m1.getOnline())).collect(Collectors.toList()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void sendTipMessage(Long groupId,List<Long> recvIds,String content){ |
|
|
|
|
|
UserSession session = SessionContext.getSession(); |
|
|
|
|
|
// 消息入库
|
|
|
|
|
|
GroupMessage message = new GroupMessage(); |
|
|
|
|
|
message.setContent(content); |
|
|
|
|
|
message.setType(MessageType.TIP_TEXT.code()); |
|
|
|
|
|
message.setStatus(MessageStatus.UNSEND.code()); |
|
|
|
|
|
message.setSendTime(new Date()); |
|
|
|
|
|
message.setSendNickName(session.getNickName()); |
|
|
|
|
|
message.setGroupId(groupId); |
|
|
|
|
|
message.setSendId(session.getUserId()); |
|
|
|
|
|
message.setRecvIds(CommaTextUtils.asText(recvIds)); |
|
|
|
|
|
groupMessageMapper.insert(message); |
|
|
|
|
|
// 推送
|
|
|
|
|
|
GroupMessageVO msgInfo = BeanUtils.copyProperties(message,GroupMessageVO.class); |
|
|
|
|
|
IMGroupMessage<GroupMessageVO> sendMessage = new IMGroupMessage<>(); |
|
|
|
|
|
sendMessage.setSender(new IMUserInfo(session.getUserId(), session.getTerminal())); |
|
|
|
|
|
if(CollUtil.isEmpty(recvIds)){ |
|
|
|
|
|
// 为空表示向全体发送
|
|
|
|
|
|
List<Long> userIds = groupMemberService.findUserIdsByGroupId(groupId); |
|
|
|
|
|
sendMessage.setRecvIds(userIds); |
|
|
|
|
|
}else{ |
|
|
|
|
|
sendMessage.setRecvIds(recvIds); |
|
|
|
|
|
} |
|
|
|
|
|
sendMessage.setData(msgInfo); |
|
|
|
|
|
sendMessage.setSendResult(false); |
|
|
|
|
|
sendMessage.setSendToSelf(false); |
|
|
|
|
|
imClient.sendGroupMessage(sendMessage); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|