SpringBoot+WebSocket实现即时通讯(三)

前言

紧接着上文《SpringBoot+WebSocket实现即时通讯(二)》

本博客姊妹篇

一、功能描述

  • 用户管理:业务自己实现,暂从数据库添加
  • 好友管理:添加好友、删除好友、修改备注、好友列表等
  • 群组管理:新建群、解散群、编辑群、变更群主、拉人进群、踢出群等
  • 聊天模式:私聊、群聊
  • 消息类型:系统、文本、语音、图片、视频
  • 聊天管理:删除聊天、置顶聊天、查看聊天记录等

二、好友、群组功能实现

2.1 好友

mapper

java 复制代码
package com.qiangesoft.im.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qiangesoft.im.entity.ImFriend;

/**
 * <p>
 * 好友 Mapper 接口
 * </p>
 *
 * @author qiangesoft
 * @date 2024-02-07
 */
public interface ImFriendMapper extends BaseMapper<ImFriend> {

}

service

java 复制代码
package com.qiangesoft.im.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.qiangesoft.im.entity.ImFriend;
import com.qiangesoft.im.pojo.dto.ImFriendDTO;
import com.qiangesoft.im.pojo.vo.ImFriendVO;

import java.util.List;

/**
 * <p>
 * 好友 服务类
 * </p>
 *
 * @author qiangesoft
 * @date 2024-02-07
 */
public interface IImFriendService extends IService<ImFriend> {

    /**
     * 添加好友
     *
     * @param friendDTO
     */
    void addFriend(ImFriendDTO friendDTO);

    /**
     * 删除好友
     *
     * @param id
     */
    void removeFriend(Long id);

    /**
     * 编辑好友信息
     *
     * @param id
     * @param friendDTO
     */
    void updateFriend(Long id, ImFriendDTO friendDTO);

    /**
     * 好友列表
     *
     * @param keyword
     * @return
     */
    List<ImFriendVO> listFriend(String keyword);

    /**
     * 好友详情
     *
     * @param id
     * @return
     */
    ImFriendVO getFriendInfo(Long id);
}
java 复制代码
package com.qiangesoft.im.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiangesoft.im.auth.UserUtil;
import com.qiangesoft.im.entity.ImFriend;
import com.qiangesoft.im.exception.ServiceException;
import com.qiangesoft.im.mapper.ImFriendMapper;
import com.qiangesoft.im.pojo.dto.ImFriendDTO;
import com.qiangesoft.im.pojo.vo.ImFriendVO;
import com.qiangesoft.im.pojo.vo.SysUserVo;
import com.qiangesoft.im.service.IImFriendService;
import com.qiangesoft.im.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 好友 服务实现类
 * </p>
 *
 * @author qiangesoft
 * @date 2024-02-07
 */
@RequiredArgsConstructor
@Service
public class ImFriendServiceImpl extends ServiceImpl<ImFriendMapper, ImFriend> implements IImFriendService {

    private final ISysUserService sysUserService;

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void addFriend(ImFriendDTO friendDTO) {
        Long userId = UserUtil.getUserId();
        Long friendUserId = friendDTO.getFriendUserId();
        if (userId.equals(friendUserId)) {
            throw new ServiceException("不能添加自己!");
        }

        LambdaQueryWrapper<ImFriend> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImFriend::getUserId, userId)
                .eq(ImFriend::getFriendUserId, friendUserId)
                .eq(ImFriend::getDelFlag, false);
        Long count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new ServiceException("已添加过好友!");
        }

        ImFriend friend = new ImFriend();
        friend.setUserId(userId);
        friend.setFriendUserId(friendDTO.getFriendUserId());
        friend.setRemark(friendDTO.getRemark());
        friend.setDelFlag(false);
        baseMapper.insert(friend);
    }

    @Override
    public void removeFriend(Long id) {
        LambdaUpdateWrapper<ImFriend> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ImFriend::getId, id)
                .set(ImFriend::getDelFlag, true);
        baseMapper.update(null, updateWrapper);
    }

    @Override
    public void updateFriend(Long id, ImFriendDTO friendDTO) {
        LambdaUpdateWrapper<ImFriend> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ImFriend::getId, id)
                .set(ImFriend::getRemark, friendDTO.getRemark());
        baseMapper.update(null, updateWrapper);
    }

    @Override
    public List<ImFriendVO> listFriend(String keyword) {
        LambdaQueryWrapper<ImFriend> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(keyword), ImFriend::getRemark, keyword)
                .eq(ImFriend::getDelFlag, false);
        List<ImFriend> friendList = baseMapper.selectList(queryWrapper);
        List<ImFriendVO> friendVOList = new ArrayList<>();
        if (CollectionUtils.isEmpty(friendList)) {
            return friendVOList;
        }

        List<Long> userIdList = friendList.stream().map(ImFriend::getFriendUserId).collect(Collectors.toList());
        Map<Long, SysUserVo> sysUserVoMap = sysUserService.listByIds(userIdList)
                .stream().map(SysUserVo::fromEntity)
                .collect(Collectors.toMap(SysUserVo::getId, sysUserVo -> sysUserVo));
        for (ImFriend friend : friendList) {
            ImFriendVO friendVO = new ImFriendVO();
            friendVO.setId(friend.getId());
            friendVO.setFriendUser(sysUserVoMap.get(friend.getFriendUserId()));
            friendVO.setRemark(friend.getRemark());
            friendVOList.add(friendVO);
        }
        return friendVOList;
    }

    @Override
    public ImFriendVO getFriendInfo(Long id) {
        LambdaQueryWrapper<ImFriend> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImFriend::getId, id)
                .eq(ImFriend::getDelFlag, false);
        ImFriend friend = baseMapper.selectOne(queryWrapper);
        if (friend == null) {
            throw new ServiceException("未添加为好友!");
        }

        SysUserVo sysUserVo = SysUserVo.fromEntity(sysUserService.getById(friend.getFriendUserId()));
        ImFriendVO friendVO = new ImFriendVO();
        friendVO.setId(friend.getId());
        friendVO.setFriendUser(sysUserVo);
        friendVO.setRemark(friend.getRemark());
        return friendVO;
    }
}

controller

java 复制代码
package com.qiangesoft.im.controller;

import com.qiangesoft.im.pojo.dto.ImFriendDTO;
import com.qiangesoft.im.pojo.vo.ImFriendVO;
import com.qiangesoft.im.pojo.vo.ResultInfo;
import com.qiangesoft.im.service.IImFriendService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 好友 前端控制器
 * </p>
 *
 * @author qiangesoft
 * @date 2024-02-07
 */
@Api(tags = "好友")
@RequiredArgsConstructor
@RestController
@RequestMapping("/im/friend")
public class ImFriendController {

    private final IImFriendService friendService;

    @PostMapping
    @ApiOperation(value = "添加好友")
    public ResultInfo<Void> addFriend(@RequestBody ImFriendDTO friendDTO) {
        friendService.addFriend(friendDTO);
        return ResultInfo.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除好友")
    public ResultInfo<Void> removeFriend(@PathVariable Long id) {
        friendService.removeFriend(id);
        return ResultInfo.ok();
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "编辑好友")
    public ResultInfo<Void> updateFriend(@PathVariable Long id, @RequestBody ImFriendDTO friendDTO) {
        friendService.updateFriend(id, friendDTO);
        return ResultInfo.ok();
    }

    @GetMapping
    @ApiOperation(value = "好友列表")
    public ResultInfo<List<ImFriendVO>> listFriend(String keyword) {
        return ResultInfo.ok(friendService.listFriend(keyword));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "好友详情")
    public ResultInfo<ImFriendVO> getFriendInfo(@PathVariable Long id) {
        return ResultInfo.ok(friendService.getFriendInfo(id));
    }

}

2.2 群组

mapper

java 复制代码
package com.qiangesoft.im.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qiangesoft.im.entity.ImGroup;

/**
 * <p>
 * 群组 Mapper 接口
 * </p>
 *
 * @author qiangesoft
 * @date 2024-02-07
 */
public interface ImGroupMapper extends BaseMapper<ImGroup> {

}
java 复制代码
package com.qiangesoft.im.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qiangesoft.im.entity.ImGroupUser;

/**
 * <p>
 * 群成员 Mapper 接口
 * </p>
 *
 * @author qiangesoft
 * @date 2024-02-07
 */
public interface ImGroupUserMapper extends BaseMapper<ImGroupUser> {

}
java 复制代码
package com.qiangesoft.im.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.qiangesoft.im.entity.ImGroup;
import com.qiangesoft.im.pojo.dto.ImGroupDTO;
import com.qiangesoft.im.pojo.dto.ImGroupMasterDTO;
import com.qiangesoft.im.pojo.vo.ImGroupInfoVO;
import com.qiangesoft.im.pojo.vo.ImGroupVO;

import java.util.List;

/**
 * <p>
 * 群组 服务类
 * </p>
 *
 * @author qiangesoft
 * @date 2024-02-07
 */
public interface IImGroupService extends IService<ImGroup> {

    /**
     * 建群
     *
     * @param groupDTO
     */
    void addGroup(ImGroupDTO groupDTO);

    /**
     * 解散群
     *
     * @param id
     */
    void removeGroup(Long id);

    /**
     * 编辑群
     *
     * @param id
     * @param groupDTO
     */
    void updateGroup(Long id, ImGroupDTO groupDTO);

    /**
     * 群列表
     *
     * @param keyword
     * @return
     */
    List<ImGroupVO> listGroup(String keyword);

    /**
     * 群详情
     *
     * @param id
     * @return
     */
    ImGroupInfoVO getGroupInfo(Long id);

    /**
     * 变更群主
     *
     * @param groupMasterDTO
     */
    void updateMaster(ImGroupMasterDTO groupMasterDTO);

    /**
     * 校验群组
     *
     * @param id
     * @return
     */
    ImGroup validateGroup(Long id);
}
java 复制代码
package com.qiangesoft.im.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.qiangesoft.im.entity.ImGroupUser;
import com.qiangesoft.im.pojo.dto.ImGroupUserDTO;
import com.qiangesoft.im.pojo.vo.SysUserVo;

import java.util.List;

/**
 * <p>
 * 群成员 服务类
 * </p>
 *
 * @author qiangesoft
 * @date 2024-02-07
 */
public interface IImGroupUserService extends IService<ImGroupUser> {

    /**
     * 添加群成员
     *
     * @param groupUserDTO
     */
    void addGroupUser(ImGroupUserDTO groupUserDTO);

    /**
     * 删除群成员
     *
     * @param groupUserDTO
     */
    void removeGroupUser(ImGroupUserDTO groupUserDTO);

    /**
     * 群成员列表
     *
     * @param id
     * @return
     */
    List<SysUserVo> listGroupUser(Long id);

}
java 复制代码
package com.qiangesoft.im.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiangesoft.im.auth.UserUtil;
import com.qiangesoft.im.entity.ImGroup;
import com.qiangesoft.im.entity.ImGroupUser;
import com.qiangesoft.im.entity.SysUser;
import com.qiangesoft.im.exception.ServiceException;
import com.qiangesoft.im.mapper.ImGroupMapper;
import com.qiangesoft.im.pojo.dto.ImGroupDTO;
import com.qiangesoft.im.pojo.dto.ImGroupMasterDTO;
import com.qiangesoft.im.pojo.vo.ImGroupInfoVO;
import com.qiangesoft.im.pojo.vo.ImGroupVO;
import com.qiangesoft.im.pojo.vo.SysUserVo;
import com.qiangesoft.im.service.IImGroupService;
import com.qiangesoft.im.service.IImGroupUserService;
import com.qiangesoft.im.service.ISysUserService;
import com.qiangesoft.im.util.IdSplitUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 群组 服务实现类
 * </p>
 *
 * @author qiangesoft
 * @date 2024-02-07
 */
@Service
public class ImGroupServiceImpl extends ServiceImpl<ImGroupMapper, ImGroup> implements IImGroupService {

    @Autowired
    private IImGroupUserService groupUserService;
    @Autowired
    private ISysUserService sysUserService;

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void addGroup(ImGroupDTO groupDTO) {
        Long userId = UserUtil.getUserId();

        ImGroup group = new ImGroup();
        group.setName(groupDTO.getName());
        group.setAvatar(groupDTO.getAvatar());
        group.setMaster(userId);
        group.setManager(IdSplitUtil.listToStr(groupDTO.getManager()));
        group.setNotice(groupDTO.getNotice());
        group.setRemark(groupDTO.getRemark());
        group.setDelFlag(false);
        baseMapper.insert(group);

        // 群成员
        List<ImGroupUser> groupUserList = new ArrayList<>();
        List<Long> memberIdList = groupDTO.getMemberIdList();
        if (!memberIdList.contains(userId)) {
            memberIdList.add(userId);
        }
        for (Long memberId : memberIdList) {
            ImGroupUser groupUser = new ImGroupUser();
            groupUser.setGroupId(group.getId());
            groupUser.setUserId(memberId);
            groupUser.setDelFlag(false);
            groupUserList.add(groupUser);
        }
        groupUserService.saveBatch(groupUserList);
    }

    @Override
    public void removeGroup(Long id) {
        LambdaUpdateWrapper<ImGroup> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ImGroup::getId, id)
                .set(ImGroup::getDelFlag, true);
        baseMapper.update(null, updateWrapper);
    }

    @Override
    public void updateGroup(Long id, ImGroupDTO groupDTO) {
        ImGroup group = this.validateGroup(id);
        if (group.getDelFlag()) {
            throw new ServiceException("群组已解散!");
        }

        group.setName(groupDTO.getName());
        group.setAvatar(groupDTO.getAvatar());
        group.setManager(IdSplitUtil.listToStr(groupDTO.getManager()));
        group.setNotice(groupDTO.getNotice());
        group.setRemark(groupDTO.getRemark());
        baseMapper.updateById(group);
    }

    @Override
    public List<ImGroupVO> listGroup(String keyword) {
        LambdaQueryWrapper<ImGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(keyword), ImGroup::getRemark, keyword)
                .eq(ImGroup::getDelFlag, false);
        List<ImGroup> groupList = baseMapper.selectList(queryWrapper);
        List<ImGroupVO> groupVOList = new ArrayList<>();
        if (CollectionUtils.isEmpty(groupList)) {
            return groupVOList;
        }

        for (ImGroup group : groupList) {
            ImGroupVO groupVO = new ImGroupVO();
            groupVO.setId(group.getId());
            groupVO.setName(group.getName());
            groupVO.setAvatar(group.getAvatar());
            groupVO.setRemark(group.getRemark());
            groupVO.setCreateTime(group.getCreateTime().getTime());
            groupVOList.add(groupVO);
        }
        return groupVOList;
    }

    @Override
    public ImGroupInfoVO getGroupInfo(Long id) {
        ImGroup group = this.validateGroup(id);

        ImGroupInfoVO vo = new ImGroupInfoVO();
        vo.setId(group.getId());
        vo.setName(group.getName());
        vo.setAvatar(group.getAvatar());
        vo.setNotice(group.getNotice());
        vo.setRemark(group.getRemark());
        vo.setCreateTime(group.getCreateTime().getTime());

        // 群主
        Long masterId = group.getMaster();
        if (masterId != null) {
            SysUser master = sysUserService.getById(masterId);
            if (master != null) {
                SysUserVo masterVo = SysUserVo.fromEntity(master);
                vo.setMaster(masterVo);
            }
        }

        // 群管理
        String manager = group.getManager();
        List<Long> managerIdList = IdSplitUtil.strToList(manager);
        if (!CollectionUtils.isEmpty(managerIdList)) {
            List<SysUserVo> managerUserList = sysUserService.listByIds(managerIdList).stream().map(SysUserVo::fromEntity).collect(Collectors.toList());
            vo.setManagerList(managerUserList);
        }

        // 群成员
        List<SysUserVo> userVoList = new ArrayList<>();
        LambdaQueryWrapper<ImGroupUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImGroupUser::getGroupId, id)
                .eq(ImGroupUser::getDelFlag, false);
        List<ImGroupUser> groupUserList = groupUserService.list(queryWrapper);
        if (CollectionUtils.isEmpty(groupUserList)) {
            vo.setMemberList(userVoList);
            return vo;
        }

        List<Long> userIdList = groupUserList.stream().map(ImGroupUser::getUserId).collect(Collectors.toList());
        Map<Long, SysUserVo> sysUserVOMap = sysUserService.listByIds(userIdList).stream().map(SysUserVo::fromEntity).collect(Collectors.toMap(SysUserVo::getId, sysUserVo -> sysUserVo));
        for (ImGroupUser groupUser : groupUserList) {
            SysUserVo sysUserVo = sysUserVOMap.get(groupUser.getUserId());
            if (sysUserVo != null) {
                userVoList.add(sysUserVo);
            }
        }
        vo.setMemberList(userVoList);
        return vo;
    }

    @Override
    public void updateMaster(ImGroupMasterDTO groupMasterDTO) {
        Long id = groupMasterDTO.getId();
        ImGroup group = this.validateGroup(id);
        if (group.getDelFlag()) {
            throw new ServiceException("群组已解散!");
        }

        LambdaUpdateWrapper<ImGroup> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ImGroup::getId, id)
                .set(ImGroup::getMaster, groupMasterDTO.getMaster());
        baseMapper.update(null, updateWrapper);
    }

    @Override
    public ImGroup validateGroup(Long id) {
        ImGroup group = baseMapper.selectById(id);
        if (group == null) {
            throw new ServiceException("群组不存在");
        }
        return group;
    }
}
java 复制代码
package com.qiangesoft.im.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiangesoft.im.auth.UserUtil;
import com.qiangesoft.im.entity.ImGroup;
import com.qiangesoft.im.entity.ImGroupUser;
import com.qiangesoft.im.exception.ServiceException;
import com.qiangesoft.im.mapper.ImGroupUserMapper;
import com.qiangesoft.im.pojo.dto.ImGroupUserDTO;
import com.qiangesoft.im.pojo.vo.SysUserVo;
import com.qiangesoft.im.service.IImGroupService;
import com.qiangesoft.im.service.IImGroupUserService;
import com.qiangesoft.im.service.ISysUserService;
import com.qiangesoft.im.util.IdSplitUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 群成员 服务实现类
 * </p>
 *
 * @author qiangesoft
 * @date 2024-02-07
 */
@Service
public class ImGroupUserServiceImpl extends ServiceImpl<ImGroupUserMapper, ImGroupUser> implements IImGroupUserService {
    @Autowired
    private ISysUserService sysUserService;
    @Lazy
    @Autowired
    private IImGroupService groupService;

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void addGroupUser(ImGroupUserDTO groupUserDTO) {
        Long groupId = groupUserDTO.getGroupId();
        ImGroup group = groupService.validateGroup(groupId);
        if (group.getDelFlag()) {
            throw new ServiceException("群组已解散!");
        }

        Long userId = UserUtil.getUserId();
        List<Long> managerIdList = IdSplitUtil.strToList(group.getManager());
        if (group.getMaster().equals(userId)) {
            throw new ServiceException("无添加好友权限!");
        } else {
            if (!managerIdList.contains(userId)) {
                throw new ServiceException("无添加好友权限!");
            }
        }
        List<Long> memberIdList = groupUserDTO.getMemberIdList();
        if (memberIdList.contains(userId)) {
            throw new ServiceException("你是群主,不可添加!");
        }

        LambdaQueryWrapper<ImGroupUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImGroupUser::getGroupId, groupId)
                .eq(ImGroupUser::getDelFlag, false);
        List<Long> existMemberIdList = baseMapper.selectList(queryWrapper).stream().map(ImGroupUser::getUserId).collect(Collectors.toList());

        List<Long> notExistMemberIdList = memberIdList.stream().filter(e -> !existMemberIdList.contains(e)).collect(Collectors.toList());
        List<ImGroupUser> groupUserList = new ArrayList<>();
        for (Long memberId : notExistMemberIdList) {
            ImGroupUser groupUser = new ImGroupUser();
            groupUser.setGroupId(groupId);
            groupUser.setUserId(memberId);
            groupUser.setDelFlag(false);
            groupUserList.add(groupUser);
        }
        this.saveBatch(groupUserList);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void removeGroupUser(ImGroupUserDTO groupUserDTO) {
        Long groupId = groupUserDTO.getGroupId();
        ImGroup group = groupService.validateGroup(groupId);
        if (group.getDelFlag()) {
            throw new ServiceException("群组已解散!");
        }

        Long userId = UserUtil.getUserId();
        List<Long> managerIdList = IdSplitUtil.strToList(group.getManager());
        if (group.getMaster().equals(userId)) {
            throw new ServiceException("无移除好友权限!");
        } else {
            if (!managerIdList.contains(userId)) {
                throw new ServiceException("无移除好友权限!");
            }
        }
        List<Long> memberIdList = groupUserDTO.getMemberIdList();
        if (memberIdList.contains(userId)) {
            throw new ServiceException("不能移除自己!");
        }

        LambdaUpdateWrapper<ImGroupUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ImGroupUser::getGroupId, groupId)
                .in(ImGroupUser::getUserId, memberIdList)
                .set(ImGroupUser::getDelFlag, true);
        baseMapper.update(null, updateWrapper);

        // 去除管理员权限
        List<Long> collect = managerIdList.stream().filter(e -> !memberIdList.contains(e)).collect(Collectors.toList());
        LambdaUpdateWrapper<ImGroup> updateWrapper1 = new LambdaUpdateWrapper<>();
        updateWrapper1.eq(ImGroup::getId, groupId)
                .set(ImGroup::getManager, IdSplitUtil.listToStr(collect));
        groupService.update(updateWrapper1);
    }

    @Override
    public List<SysUserVo> listGroupUser(Long id) {
        groupService.validateGroup(id);

        List<SysUserVo> userVoList = new ArrayList<>();
        LambdaQueryWrapper<ImGroupUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImGroupUser::getGroupId, id)
                .eq(ImGroupUser::getDelFlag, false);
        List<ImGroupUser> groupUserList = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(groupUserList)) {
            return userVoList;
        }

        List<Long> userIdList = groupUserList.stream().map(ImGroupUser::getUserId).collect(Collectors.toList());
        Map<Long, SysUserVo> sysUserVOMap = sysUserService.listByIds(userIdList).stream().map(SysUserVo::fromEntity).collect(Collectors.toMap(SysUserVo::getId, sysUserVo -> sysUserVo));
        for (ImGroupUser groupUser : groupUserList) {
            SysUserVo sysUserVo = sysUserVOMap.get(groupUser.getUserId());
            if (sysUserVo != null) {
                userVoList.add(sysUserVo);
            }
        }
        return userVoList;
    }
}

controller

java 复制代码
package com.qiangesoft.im.controller;

import com.qiangesoft.im.pojo.dto.ImGroupDTO;
import com.qiangesoft.im.pojo.dto.ImGroupMasterDTO;
import com.qiangesoft.im.pojo.vo.ImGroupInfoVO;
import com.qiangesoft.im.pojo.vo.ImGroupVO;
import com.qiangesoft.im.pojo.vo.ResultInfo;
import com.qiangesoft.im.service.IImGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 群组 前端控制器
 * </p>
 *
 * @author qiangesoft
 * @date 2024-02-07
 */
@Api(tags = "群组")
@RestController
@RequestMapping("/im/group")
public class ImGroupController {

    @Autowired
    private IImGroupService groupService;

    @PostMapping
    @ApiOperation(value = "建群")
    public ResultInfo<Void> addGroup(@RequestBody ImGroupDTO groupDTO) {
        groupService.addGroup(groupDTO);
        return ResultInfo.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "解散群")
    public ResultInfo<Void> removeGroup(@PathVariable Long id) {
        groupService.removeGroup(id);
        return ResultInfo.ok();
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "编辑群")
    public ResultInfo<Void> updateGroup(@PathVariable Long id, @RequestBody ImGroupDTO groupDTO) {
        groupService.updateGroup(id, groupDTO);
        return ResultInfo.ok();
    }

    @GetMapping
    @ApiOperation(value = "群列表")
    public ResultInfo<List<ImGroupVO>> listGroup(String keyword) {
        return ResultInfo.ok(groupService.listGroup(keyword));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "群详情")
    public ResultInfo<ImGroupInfoVO> getGroupInfo(@PathVariable Long id) {
        return ResultInfo.ok(groupService.getGroupInfo(id));
    }

    @GetMapping("/updateMaster")
    @ApiOperation(value = "变更群主")
    public ResultInfo<Void> updateMaster(@RequestBody ImGroupMasterDTO groupMasterDTO) {
        groupService.updateMaster(groupMasterDTO);
        return ResultInfo.ok();
    }
}
java 复制代码
package com.qiangesoft.im.controller;

import com.qiangesoft.im.pojo.dto.ImGroupUserDTO;
import com.qiangesoft.im.pojo.vo.ResultInfo;
import com.qiangesoft.im.pojo.vo.SysUserVo;
import com.qiangesoft.im.service.IImGroupUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 群成员 前端控制器
 * </p>
 *
 * @author qiangesoft
 * @date 2024-02-07
 */
@Api(tags = "群成员")
@RestController
@RequestMapping("/im/groupUser")
public class ImGroupUserController {

    @Autowired
    private IImGroupUserService groupUserService;

    @PostMapping
    @ApiOperation(value = "添加群成员")
    public ResultInfo<Void> addGroupUser(@RequestBody ImGroupUserDTO groupUserDTO) {
        groupUserService.addGroupUser(groupUserDTO);
        return ResultInfo.ok();
    }

    @DeleteMapping
    @ApiOperation(value = "删除群成员")
    public ResultInfo<Void> removeGroupUser(@RequestBody ImGroupUserDTO groupUserDTO) {
        groupUserService.removeGroupUser(groupUserDTO);
        return ResultInfo.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "群成员列表")
    public ResultInfo<List<SysUserVo>> listGroupUser(@PathVariable Long id) {
        return ResultInfo.ok(groupUserService.listGroupUser(id));
    }

}

三、源码地址

源码地址:https://gitee.com/qiangesoft/boot-business/tree/master/boot-business-im

后续内容见下章

相关推荐
世间万物皆对象5 小时前
Spring Boot核心概念:日志管理
java·spring boot·单元测试
qq_17448285756 小时前
springboot基于微信小程序的旧衣回收系统的设计与实现
spring boot·后端·微信小程序
代码小鑫8 小时前
A043-基于Spring Boot的秒杀系统设计与实现
java·开发语言·数据库·spring boot·后端·spring·毕业设计
真心喜欢你吖8 小时前
SpringBoot与MongoDB深度整合及应用案例
java·spring boot·后端·mongodb·spring
周全全9 小时前
Spring Boot + Vue 基于 RSA 的用户身份认证加密机制实现
java·vue.js·spring boot·安全·php
飞升不如收破烂~10 小时前
Spring boot常用注解和作用
java·spring boot·后端
计算机毕设源码qq-383653104110 小时前
(附项目源码)Java开发语言,215 springboot 大学生爱心互助代购网站,计算机毕设程序开发+文案(LW+PPT)
java·开发语言·spring boot·mysql·课程设计
岁岁岁平安10 小时前
springboot实战(15)(注解@JsonFormat(pattern=“?“)、@JsonIgnore)
java·spring boot·后端·idea
潜洋14 小时前
Spring Boot教程之五:在 IntelliJ IDEA 中运行第一个 Spring Boot 应用程序
java·spring boot·后端
灯雾️15 小时前
Spring Boot、Spring MVC和Spring间的区别
spring boot