Commit 10f5ad2d by 罗长华

完成GroupService 创建群组、解散群组、加入群组、离开群组功能

parent f9784c21
...@@ -4,6 +4,7 @@ import io.geekidea.springbootplus.framework.common.api.ApiResult; ...@@ -4,6 +4,7 @@ import io.geekidea.springbootplus.framework.common.api.ApiResult;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
...@@ -42,7 +43,9 @@ public class ImGroupController { ...@@ -42,7 +43,9 @@ public class ImGroupController {
@PostMapping("/createGroup") @PostMapping("/createGroup")
public ApiResult<Long> createGroup(@RequestBody CreateGroupParam param) { public ApiResult<Long> createGroup(@RequestBody CreateGroupParam param) {
log.info("创建群组请求 参数: {}", JSON.toJSONString(param)); log.info("创建群组请求 参数: {}", JSON.toJSONString(param));
return null; List<String> memberClientIds = Arrays.asList(param.getMemberIds().split(","));
Long conversationId = groupService.createGroup(param.getUserId(), param.getGroupName(), memberClientIds);
return ApiResult.ok(conversationId);
} }
/** /**
...@@ -54,7 +57,7 @@ public class ImGroupController { ...@@ -54,7 +57,7 @@ public class ImGroupController {
@PostMapping("/dismissGroup") @PostMapping("/dismissGroup")
public ApiResult<Boolean> dismissGroup(@RequestBody DismissGroupParam param) { public ApiResult<Boolean> dismissGroup(@RequestBody DismissGroupParam param) {
log.info("解散群组请求 参数: {}", JSON.toJSONString(param)); log.info("解散群组请求 参数: {}", JSON.toJSONString(param));
return null; return ApiResult.ok(groupService.dismissGroup(param.getUserId(), param.getGroupId()));
} }
/** /**
...@@ -64,8 +67,10 @@ public class ImGroupController { ...@@ -64,8 +67,10 @@ public class ImGroupController {
* @Return * @Return
*/ */
@PostMapping("/joinGroup") @PostMapping("/joinGroup")
public ApiResult<Long> joinGroup(@RequestBody JoinGroupParam param) { public ApiResult<Integer> joinGroup(@RequestBody JoinGroupParam param) {
return null; log.info("加入群组请求 参数: {}", JSON.toJSONString(param));
List<String> memberIds = Arrays.asList(param.getUserIds().split(","));
return ApiResult.ok(groupService.joinGroup(param.getGroupId(), memberIds));
} }
/** /**
...@@ -75,9 +80,10 @@ public class ImGroupController { ...@@ -75,9 +80,10 @@ public class ImGroupController {
* @Return * @Return
*/ */
@PostMapping("/leaveGroup") @PostMapping("/leaveGroup")
public ApiResult<Long> leaveGroup(@RequestBody LeaveGroupParam param) { public ApiResult<Integer> leaveGroup(@RequestBody LeaveGroupParam param) {
log.info("离开群组请求 参数: {}", JSON.toJSONString(param)); log.info("离开群组请求 参数: {}", JSON.toJSONString(param));
return null; List<String> memberIds = Arrays.asList(param.getUserIds().split(","));
return ApiResult.ok(groupService.leaveGroup(param.getGroupId(), memberIds));
} }
/** /**
......
package com.wecloud.im.param.group; package com.wecloud.im.param.group;
import lombok.Getter;
import lombok.Setter;
/** /**
* 加入群 * 加入群
* @Author luozh * @Author luozh
* @Date 2022年05月10日 09:37 * @Date 2022年05月10日 09:37
* @Version 1.0 * @Version 1.0
*/ */
@Getter
@Setter
public class JoinGroupParam { public class JoinGroupParam {
/** /**
......
...@@ -36,20 +36,20 @@ public interface ImGroupService { ...@@ -36,20 +36,20 @@ public interface ImGroupService {
* @Author luozh * @Author luozh
* @Date 2022年05月10日 03:22:35 * @Date 2022年05月10日 03:22:35
* @param groupId * @param groupId
* @param userIds * @param memberClientIds
* @Return * @Return
*/ */
Boolean joinGroup(String groupId, List<String> memberClientIds); Integer joinGroup(String groupId, List<String> memberClientIds);
/** /**
* 离开群组 * 离开群组
* @Author luozh * @Author luozh
* @Date 2022年05月10日 03:22:44 * @Date 2022年05月10日 03:22:44
* @param groupId * @param groupId
* @param userIds * @param memberClientIds
* @Return * @Return
*/ */
Boolean leaveGroup(String groupId, List<String> memberClientIds); Integer leaveGroup(String groupId, List<String> memberClientIds);
/** /**
* 获取群成员列表 * 获取群成员列表
......
...@@ -329,6 +329,7 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap ...@@ -329,6 +329,7 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap
return imConversationCreateVo; return imConversationCreateVo;
} }
@Transactional(rollbackFor = Exception.class)
@Override @Override
public ImConversation serverCreateImConversation(ServerImConversationCreate imConversationCreate) { public ImConversation serverCreateImConversation(ServerImConversationCreate imConversationCreate) {
if (ChatTypeEnum.CHAT_ROOM.equals(imConversationCreate.getChatType())) { if (ChatTypeEnum.CHAT_ROOM.equals(imConversationCreate.getChatType())) {
......
...@@ -6,7 +6,9 @@ import lombok.AllArgsConstructor; ...@@ -6,7 +6,9 @@ import lombok.AllArgsConstructor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -17,6 +19,7 @@ import com.wecloud.im.entity.ImApplication; ...@@ -17,6 +19,7 @@ import com.wecloud.im.entity.ImApplication;
import com.wecloud.im.entity.ImClient; import com.wecloud.im.entity.ImClient;
import com.wecloud.im.entity.ImConversation; import com.wecloud.im.entity.ImConversation;
import com.wecloud.im.entity.ImConversationMembers; import com.wecloud.im.entity.ImConversationMembers;
import com.wecloud.im.entity.ImMessage;
import com.wecloud.im.mapper.ImConversationMapper; import com.wecloud.im.mapper.ImConversationMapper;
import com.wecloud.im.param.add.ServerImConversationCreate; import com.wecloud.im.param.add.ServerImConversationCreate;
import com.wecloud.im.sdk.enums.ChatTypeEnum; import com.wecloud.im.sdk.enums.ChatTypeEnum;
...@@ -27,7 +30,10 @@ import com.wecloud.im.service.ImClientService; ...@@ -27,7 +30,10 @@ import com.wecloud.im.service.ImClientService;
import com.wecloud.im.service.ImConversationMembersService; import com.wecloud.im.service.ImConversationMembersService;
import com.wecloud.im.service.ImConversationService; import com.wecloud.im.service.ImConversationService;
import com.wecloud.im.service.ImGroupService; import com.wecloud.im.service.ImGroupService;
import com.wecloud.im.service.ImMessageService;
import com.wecloud.im.ws.enums.MsgTypeEnum;
import com.wecloud.imserver.client.model.enums.DeviceTypeEnum; import com.wecloud.imserver.client.model.enums.DeviceTypeEnum;
import com.wecloud.utils.JsonUtils;
import com.wecloud.utils.SnowflakeUtil; import com.wecloud.utils.SnowflakeUtil;
/** /**
...@@ -65,6 +71,11 @@ public class ImGroupServiceImpl implements ImGroupService { ...@@ -65,6 +71,11 @@ public class ImGroupServiceImpl implements ImGroupService {
*/ */
private final ImConversationMapper imConversationMapper; private final ImConversationMapper imConversationMapper;
/**
* 消息服务
*/
private final ImMessageService imMessageService;
@Override @Override
public Long createGroup(String creatorClientId, String groupName, List<String> memberIds) { public Long createGroup(String creatorClientId, String groupName, List<String> memberIds) {
// 获取应用 // 获取应用
...@@ -78,7 +89,8 @@ public class ImGroupServiceImpl implements ImGroupService { ...@@ -78,7 +89,8 @@ public class ImGroupServiceImpl implements ImGroupService {
} }
// 获取群成员信息 // 获取群成员信息
List<ImClient> members = clientService.listByIds(memberIds); List<ImClient> members =
clientService.list(Wrappers.<ImClient>lambdaQuery().eq(ImClient::getFkAppid, appId).in(ImClient::getClientId, memberIds));
if (members.isEmpty()) { if (members.isEmpty()) {
throw new BusinessException("群成员列表为空"); throw new BusinessException("群成员列表为空");
} }
...@@ -128,12 +140,33 @@ public class ImGroupServiceImpl implements ImGroupService { ...@@ -128,12 +140,33 @@ public class ImGroupServiceImpl implements ImGroupService {
// 删除所有成员 // 删除所有成员
conversationMembersService.deleteByConversationId(conversation.getId()); conversationMembersService.deleteByConversationId(conversation.getId());
// 保存事件消息
ImMessage imMessage = new ImMessage();
Map<String, Object> content = new HashMap<>();
content.put("operator", operator.getClientId());
imMessage.setContent(JsonUtils.encodeJson(content));
// 保存消息至消息表
imMessage.setId(SnowflakeUtil.getId());
imMessage.setMsgType(MsgTypeEnum.CONVERSATION_DISBAND.getUriCode());
imMessage.setCreateTime(new Date());
imMessage.setFkAppid(operator.getFkAppid());
imMessage.setSender(operator.getId());
imMessage.setWithdraw(false);
imMessage.setEvent(true);
imMessage.setSystemFlag(false);
imMessage.setSendStatus(2);
imMessage.setFkConversationId(conversation.getId());
imMessageService.save(imMessage);
conversationService.sendMsgToMembers(conversation.getId(), membersList, appId, operator.getClientId(), content,
imMessage);
return true; return true;
} }
@Override @Override
public Boolean joinGroup(String groupId, List<String> memberClientIds) { public Integer joinGroup(String groupId, List<String> memberClientIds) {
Long appId = SecurityUtils.getCurrentAppId(); Long appId = SecurityUtils.getCurrentAppId();
ImApplication imApplication = applicationService.getCacheById(appId); ImApplication imApplication = applicationService.getCacheById(appId);
// 查询会话 // 查询会话
...@@ -180,11 +213,12 @@ public class ImGroupServiceImpl implements ImGroupService { ...@@ -180,11 +213,12 @@ public class ImGroupServiceImpl implements ImGroupService {
conversationMembersService.saveBatch(newMemberList); conversationMembersService.saveBatch(newMemberList);
// 将群成员数量增加 // 将群成员数量增加
imConversationMapper.addMemberCount(imApplication.getId(), conversation.getId(), clientList.size()); imConversationMapper.addMemberCount(imApplication.getId(), conversation.getId(), clientList.size());
return true;
return clientList.size();
} }
@Override @Override
public Boolean leaveGroup(String groupId, List<String> memberClientIds) { public Integer leaveGroup(String groupId, List<String> memberClientIds) {
Long appId = SecurityUtils.getCurrentAppId(); Long appId = SecurityUtils.getCurrentAppId();
ImApplication imApplication = applicationService.getCacheById(appId); ImApplication imApplication = applicationService.getCacheById(appId);
// 查询会话 // 查询会话
...@@ -204,11 +238,10 @@ public class ImGroupServiceImpl implements ImGroupService { ...@@ -204,11 +238,10 @@ public class ImGroupServiceImpl implements ImGroupService {
List<Long> conversationMemberIds = List<Long> conversationMemberIds =
existMemberList.stream().map(ImConversationMembers::getId).collect(Collectors.toList()); existMemberList.stream().map(ImConversationMembers::getId).collect(Collectors.toList());
conversationMembersService.removeByIds(conversationMemberIds); conversationMembersService.removeByIds(conversationMemberIds);
// 将群成员数量减
imConversationMapper.addMemberCount(imApplication.getId(), conversation.getId(), -existMemberList.size());
} }
// 将群成员数量减 return existMemberList.size();
imConversationMapper.addMemberCount(imApplication.getId(), conversation.getId(), existMemberList.size());
return true;
} }
@Override @Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment