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

后续内容见下章

相关推荐
小池先生10 分钟前
springboot启动不了 因一个spring-boot-starter-web底下的tomcat-embed-core依赖丢失
java·spring boot·后端
苹果醋31 小时前
2020重新出发,MySql基础,MySql表数据操作
java·运维·spring boot·mysql·nginx
小蜗牛慢慢爬行1 小时前
如何在 Spring Boot 微服务中设置和管理多个数据库
java·数据库·spring boot·后端·微服务·架构·hibernate
azhou的代码园1 小时前
基于JAVA+SpringBoot+Vue的制造装备物联及生产管理ERP系统
java·spring boot·制造
wm10432 小时前
java web springboot
java·spring boot·后端
ZoeLandia4 小时前
WebSocket | 背景 概念 原理 使用 优缺点及适用场景
网络·websocket·网络协议
路在脚下@10 小时前
spring boot的配置文件属性注入到类的静态属性
java·spring boot·sql
啦啦右一10 小时前
Spring Boot | (一)Spring开发环境构建
spring boot·后端·spring
森屿Serien10 小时前
Spring Boot常用注解
java·spring boot·后端
苹果醋311 小时前
React源码02 - 基础知识 React API 一览
java·运维·spring boot·mysql·nginx