Skip to content

Commit

Permalink
feat: 添加显示管理员功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Kkuil committed Oct 29, 2023
1 parent 378d767 commit 34162f1
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.abin.mallchat.common.chat.constant;

/**
* @Author Kkuil
* @Date 2023/10/24 16:06
* @Description 群常量
*/
public class GroupConst {

/**
* 最大群管理员数量
*/
public static final int MAX_MANAGE_COUNT = 3;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.abin.mallchat.common.chat.domain.vo.request.*;
import com.abin.mallchat.common.chat.domain.vo.response.ChatMemberListResp;
import com.abin.mallchat.common.chat.domain.vo.response.MemberResp;
import com.abin.mallchat.common.chat.service.IGroupMemberService;
import com.abin.mallchat.common.chat.service.RoomAppService;
import com.abin.mallchat.common.common.domain.vo.request.IdReqVO;
import com.abin.mallchat.common.common.domain.vo.response.ApiResult;
Expand Down Expand Up @@ -35,6 +36,8 @@
public class RoomController {
@Autowired
private RoomAppService roomService;
@Autowired
private IGroupMemberService groupMemberService;

@GetMapping("/public/group")
@ApiOperation("群组详情")
Expand Down Expand Up @@ -78,5 +81,12 @@ public ApiResult<Void> addMember(@Valid @RequestBody MemberAddReq request) {
roomService.addMember(uid, request);
return ApiResult.success();
}
}

@PutMapping("/group/admin")
@ApiOperation("添加管理员")
public ApiResult<Boolean> addAdmin(@Valid @RequestBody AdminAddReq request) {
Long uid = RequestHolder.get().getUid();
groupMemberService.addAdmin(uid, request);
return ApiResult.success();
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package com.abin.mallchat.common.chat.dao;

import cn.hutool.core.util.ObjectUtil;
import com.abin.mallchat.common.chat.domain.entity.GroupMember;
import com.abin.mallchat.common.chat.domain.enums.GroupRoleEnum;
import com.abin.mallchat.common.chat.mapper.GroupMemberMapper;
import com.abin.mallchat.common.chat.service.cache.GroupMemberCache;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

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

import static com.abin.mallchat.common.chat.domain.enums.GroupRoleEnum.ADMIN_LIST;

/**
* <p>
* 群成员表 服务实现类
Expand All @@ -21,6 +29,10 @@
@Service
public class GroupMemberDao extends ServiceImpl<GroupMemberMapper, GroupMember> {

@Autowired
@Lazy
private GroupMemberCache groupMemberCache;

public List<Long> getMemberUidList(Long groupId) {
List<GroupMember> list = lambdaQuery()
.eq(GroupMember::getGroupId, groupId)
Expand All @@ -38,6 +50,23 @@ public List<Long> getMemberBatch(Long groupId, List<Long> uidList) {
return list.stream().map(GroupMember::getUid).collect(Collectors.toList());
}

/**
* 批量获取成员群角色
*
* @param groupId 群ID
* @param uidList 用户列表
* @return 成员群角色列表
*/
public Map<Long, Integer> getMemberMapRole(Long groupId, List<Long> uidList) {
List<GroupMember> list = lambdaQuery()
.eq(GroupMember::getGroupId, groupId)
.in(GroupMember::getUid, uidList)
.in(GroupMember::getRole, ADMIN_LIST)
.select(GroupMember::getUid, GroupMember::getRole)
.list();
return list.stream().collect(Collectors.toMap(GroupMember::getUid, GroupMember::getRole));
}

public GroupMember getMember(Long groupId, Long uid) {
return lambdaQuery()
.eq(GroupMember::getGroupId, groupId)
Expand All @@ -53,18 +82,59 @@ public List<GroupMember> getSelfGroup(Long uid) {
}

/**
* 批量获取成员群角色
* 判断用户是否在房间中
*
* @param groupId 群ID
* @param roomId 房间ID
* @param uidList 用户ID
* @return 是否在群聊中
*/
public Boolean isGroupShip(Long roomId, List<Long> uidList) {
List<Long> memberUidList = groupMemberCache.getMemberUidList(roomId);
return memberUidList.containsAll(uidList);
}

/**
* 是否是群主
*
* @param id 群组ID
* @param uid 用户ID
* @return 是否是群主
*/
public Boolean isLord(Long id, Long uid) {
GroupMember groupMember = this.lambdaQuery()
.eq(GroupMember::getGroupId, id)
.eq(GroupMember::getUid, uid)
.one();
return ObjectUtil.isNotNull(groupMember);
}

/**
* 获取管理员uid列表
*
* @param id 群主ID
* @return 管理员uid列表
*/
public List<Long> getManageUidList(Long id) {
return this.lambdaQuery()
.eq(GroupMember::getGroupId, id)
.eq(GroupMember::getRole, GroupRoleEnum.MANAGER.getType())
.list()
.stream()
.map(GroupMember::getUid)
.collect(Collectors.toList());
}

/**
* 增加管理员
*
* @param id 群主ID
* @param uidList 用户列表
* @return 成员群角色列表
*/
public Map<Long, Integer> getMemberMapRole(Long groupId, List<Long> uidList) {
List<GroupMember> list = lambdaQuery()
.eq(GroupMember::getGroupId, groupId)
public void addAdmin(Long id, List<Long> uidList) {
LambdaUpdateWrapper<GroupMember> wrapper = new UpdateWrapper<GroupMember>().lambda()
.eq(GroupMember::getGroupId, id)
.in(GroupMember::getUid, uidList)
.select(GroupMember::getUid, GroupMember::getRole)
.list();
return list.stream().collect(Collectors.toMap(GroupMember::getUid, GroupMember::getRole));
.set(GroupMember::getRole, GroupRoleEnum.MANAGER.getType());
this.update(wrapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand All @@ -16,6 +17,9 @@
@AllArgsConstructor
@Getter
public enum GroupRoleEnum {
/**
*
*/
LEADER(1, "群主"),
MANAGER(2, "管理"),
MEMBER(3, "普通成员"),
Expand All @@ -24,6 +28,8 @@ public enum GroupRoleEnum {
private final Integer type;
private final String desc;

public static final List<Integer> ADMIN_LIST = Arrays.asList(GroupRoleEnum.LEADER.getType(), GroupRoleEnum.MANAGER.getType());

private static Map<Integer, GroupRoleEnum> cache;

static {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.abin.mallchat.common.chat.domain.vo.request;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

/**
* @Author Kkuil
* @Date 2023/10/24 12:46
* @Description 添加管理员请求信息
*/
@Data
public class AdminAddReq {
@NotNull
@ApiModelProperty("房间号")
private Long roomId;

@NotNull
@Size(min = 1, max = 3)
@ApiModelProperty("需要添加管理的列表")
private List<Long> uidList;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.abin.mallchat.common.chat.service;

import com.abin.mallchat.common.chat.domain.entity.GroupMember;
import com.abin.mallchat.common.chat.domain.vo.request.AdminAddReq;
import com.baomidou.mybatisplus.extension.service.IService;

/**
Expand All @@ -11,6 +12,12 @@
* @author <a href="https://github.com/zongzibinbin">abin</a>
* @since 2023-07-16
*/
public interface IGroupMemberService extends IService<GroupMember> {

public interface IGroupMemberService {
/**
* 增加管理员
*
* @param uid 用户ID
* @param request 请求信息
*/
void addAdmin(Long uid, AdminAddReq request);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.abin.mallchat.common.chat.service.impl;

import com.abin.mallchat.common.chat.dao.GroupMemberDao;
import com.abin.mallchat.common.chat.dao.RoomGroupDao;
import com.abin.mallchat.common.chat.domain.entity.RoomGroup;
import com.abin.mallchat.common.chat.domain.vo.request.AdminAddReq;
import com.abin.mallchat.common.chat.service.IGroupMemberService;
import com.abin.mallchat.common.common.exception.GroupErrorEnum;
import com.abin.mallchat.common.common.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;

import static com.abin.mallchat.common.chat.constant.GroupConst.MAX_MANAGE_COUNT;

/**
* @Author Kkuil
* @Date 2023/10/24 15:45
* @Description 群成员服务类
*/
@Service
public class GroupMemberServiceImpl implements IGroupMemberService {

@Autowired
private GroupMemberDao groupMemberDao;

@Autowired
private RoomGroupDao roomGroupDao;

/**
* 增加管理员
*
* @param uid 用户ID
* @param request 请求信息
*/
@Override
public void addAdmin(Long uid, AdminAddReq request) {
// 1. 判断群聊是否存在
RoomGroup roomGroup = roomGroupDao.getByRoomId(request.getRoomId());
AssertUtil.isNotEmpty(roomGroup, GroupErrorEnum.GROUP_NOT_EXIST);

// 2. 判断该用户是否是群主
Boolean isLord = groupMemberDao.isLord(roomGroup.getId(), uid);
AssertUtil.isTrue(isLord, GroupErrorEnum.NOT_ALLOWED_OPERATION);

// 3. 判断群成员是否在群中
Boolean isGroupShip = groupMemberDao.isGroupShip(roomGroup.getRoomId(), request.getUidList());
AssertUtil.isTrue(isGroupShip, GroupErrorEnum.USER_NOT_IN_GROUP);

// 4. 判断管理员数量是否达到上限
// 4.1 查询现有管理员数量
List<Long> manageUidList = groupMemberDao.getManageUidList(roomGroup.getId());
// 4.2 去重
HashSet<Long> manageUidSet = new HashSet<>(manageUidList);
manageUidSet.addAll(request.getUidList());
AssertUtil.isFalse(manageUidSet.size() > MAX_MANAGE_COUNT, GroupErrorEnum.MANAGE_COUNT_EXCEED);

// 5. 增加管理员
groupMemberDao.addAdmin(roomGroup.getId(), request.getUidList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.abin.mallchat.common.common.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
* @Author Kkuil
* @Date 2023/10/24 15:50
* @Description 群异常码
*/
@AllArgsConstructor
@Getter
public enum GroupErrorEnum implements ErrorEnum {
/**
*
*/
GROUP_NOT_EXIST(9001, "该群不存在~"),
NOT_ALLOWED_OPERATION(9002, "您无权操作~"),
MANAGE_COUNT_EXCEED(9003, "群管理员数量达到上限,请先删除后再操作~"),
USER_NOT_IN_GROUP(9004, "非法操作,用户不存在群聊中~"),
;
private final Integer code;
private final String msg;

@Override
public Integer getErrorCode() {
return this.code;
}

@Override
public String getErrorMsg() {
return this.msg;
}
}

0 comments on commit 34162f1

Please sign in to comment.