78 行
2.9 KiB
Java
78 行
2.9 KiB
Java
|
|
package com.xuqm.im.service;
|
||
|
|
|
||
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||
|
|
import com.xuqm.common.exception.BusinessException;
|
||
|
|
import com.xuqm.im.entity.ImGroupEntity;
|
||
|
|
import com.xuqm.im.repository.ImGroupRepository;
|
||
|
|
import org.springframework.stereotype.Service;
|
||
|
|
|
||
|
|
import java.time.LocalDateTime;
|
||
|
|
import java.util.ArrayList;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.UUID;
|
||
|
|
|
||
|
|
@Service
|
||
|
|
public class ImGroupService {
|
||
|
|
|
||
|
|
private final ImGroupRepository groupRepository;
|
||
|
|
private final ObjectMapper objectMapper;
|
||
|
|
|
||
|
|
public ImGroupService(ImGroupRepository groupRepository, ObjectMapper objectMapper) {
|
||
|
|
this.groupRepository = groupRepository;
|
||
|
|
this.objectMapper = objectMapper;
|
||
|
|
}
|
||
|
|
|
||
|
|
public ImGroupEntity create(String appId, String name, String creatorId, List<String> memberIds) {
|
||
|
|
List<String> members = new ArrayList<>(memberIds);
|
||
|
|
if (!members.contains(creatorId)) members.add(creatorId);
|
||
|
|
|
||
|
|
ImGroupEntity group = new ImGroupEntity();
|
||
|
|
group.setId(UUID.randomUUID().toString());
|
||
|
|
group.setAppId(appId);
|
||
|
|
group.setName(name);
|
||
|
|
group.setCreatorId(creatorId);
|
||
|
|
group.setMemberIds(toJson(members));
|
||
|
|
group.setAdminIds(toJson(List.of(creatorId)));
|
||
|
|
group.setCreatedAt(LocalDateTime.now());
|
||
|
|
return groupRepository.save(group);
|
||
|
|
}
|
||
|
|
|
||
|
|
public ImGroupEntity addMember(String groupId, String userId) {
|
||
|
|
ImGroupEntity group = groupRepository.findById(groupId)
|
||
|
|
.orElseThrow(() -> new BusinessException(404, "群组不存在"));
|
||
|
|
List<String> members = fromJson(group.getMemberIds());
|
||
|
|
if (!members.contains(userId)) {
|
||
|
|
members.add(userId);
|
||
|
|
group.setMemberIds(toJson(members));
|
||
|
|
groupRepository.save(group);
|
||
|
|
}
|
||
|
|
return group;
|
||
|
|
}
|
||
|
|
|
||
|
|
public ImGroupEntity removeMember(String groupId, String userId, String operatorId) {
|
||
|
|
ImGroupEntity group = groupRepository.findById(groupId)
|
||
|
|
.orElseThrow(() -> new BusinessException(404, "群组不存在"));
|
||
|
|
List<String> admins = fromJson(group.getAdminIds());
|
||
|
|
if (!admins.contains(operatorId) && !group.getCreatorId().equals(operatorId)) {
|
||
|
|
throw new BusinessException(403, "无权操作");
|
||
|
|
}
|
||
|
|
List<String> members = new ArrayList<>(fromJson(group.getMemberIds()));
|
||
|
|
members.remove(userId);
|
||
|
|
group.setMemberIds(toJson(members));
|
||
|
|
return groupRepository.save(group);
|
||
|
|
}
|
||
|
|
|
||
|
|
public List<ImGroupEntity> listByApp(String appId) {
|
||
|
|
return groupRepository.findByAppId(appId);
|
||
|
|
}
|
||
|
|
|
||
|
|
private String toJson(List<String> list) {
|
||
|
|
try { return objectMapper.writeValueAsString(list); } catch (Exception e) { return "[]"; }
|
||
|
|
}
|
||
|
|
|
||
|
|
private List<String> fromJson(String json) {
|
||
|
|
try { return objectMapper.readValue(json, new TypeReference<>() {}); } catch (Exception e) { return new ArrayList<>(); }
|
||
|
|
}
|
||
|
|
}
|