当前位置:   article > 正文

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

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

前言

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

本博客姊妹篇

一、功能描述

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

二、好友、群组功能实现

2.1 好友

mapper

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> {

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

service

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);
}
  • 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
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;
    }
}

  • 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

controller

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));
    }

}


  • 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

2.2 群组

mapper

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> {

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
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> {

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
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);
}

  • 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
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);

}

  • 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
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;
    }
}

  • 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
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
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;
    }
}

  • 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

controller

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();
    }
}


  • 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
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));
    }

}


  • 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

三、源码地址

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

后续内容见下章

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/154896
推荐阅读
相关标签
  

闽ICP备14008679号