156 lines
5.4 KiB
Java
156 lines
5.4 KiB
Java
package myplayer.service;
|
||
|
||
|
||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||
import myplayer.entity.Groupchat;
|
||
import myplayer.entity.Grouprelation;
|
||
import myplayer.entity.User;
|
||
import myplayer.entity.request.Member;
|
||
import myplayer.mapper.GroupMapper;
|
||
import myplayer.mapper.GrouprelationMapper;
|
||
import myplayer.mapper.UserMapper;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||
import org.springframework.stereotype.Service;
|
||
import org.springframework.transaction.annotation.Transactional;
|
||
|
||
import java.time.LocalDateTime;
|
||
import java.util.ArrayList;
|
||
import java.util.HashMap;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
|
||
@Service
|
||
public class GroupService {
|
||
|
||
private final String DB = "myplayer:";
|
||
|
||
@Autowired
|
||
private GroupMapper groupMapper;
|
||
|
||
@Autowired
|
||
private GrouprelationMapper grouprelationMapper;
|
||
|
||
@Autowired
|
||
private StringRedisTemplate stringRedisTemplate;
|
||
|
||
@Autowired
|
||
private UserMapper userMapper;
|
||
|
||
public int groupIsExist(String gId) {
|
||
return groupMapper.groupIsExist(gId);
|
||
}
|
||
|
||
@Transactional
|
||
public void createGroup(Groupchat groupchat, String uId) {
|
||
groupMapper.insert(groupchat);
|
||
grouprelationMapper.insert(new Grouprelation(groupchat.getG_id(), uId, 0));
|
||
|
||
stringRedisTemplate.opsForSet().add(DB+groupchat.getG_id(), uId);
|
||
|
||
}
|
||
|
||
public List<Groupchat> searchGroups(String uId, String gName) {
|
||
QueryWrapper<Grouprelation> qw1 = new QueryWrapper<>();
|
||
qw1.select("g_id")
|
||
.eq("u_id", uId);
|
||
List<String> allreadyin = grouprelationMapper.selectObjs(qw1);
|
||
QueryWrapper<Groupchat> qw2 = new QueryWrapper<>();
|
||
if (!allreadyin.isEmpty()) {
|
||
qw2.notIn("g_id", allreadyin)
|
||
.like("g_name", gName)
|
||
.last("limit 5");
|
||
} else {
|
||
System.out.println("无已加入群聊,无需排除");
|
||
qw2.like("g_name", gName)
|
||
.last("limit 5");
|
||
}
|
||
return groupMapper.selectList(qw2);
|
||
}
|
||
|
||
@Transactional
|
||
public void joinGroup(String uId, String gId) {
|
||
grouprelationMapper.insert(new Grouprelation(gId, uId, 2));
|
||
|
||
stringRedisTemplate.opsForSet().add(DB+gId, uId);
|
||
}
|
||
|
||
public List<Groupchat> getGroups(String uId) {
|
||
QueryWrapper<Grouprelation> qw1 = new QueryWrapper<>();
|
||
qw1.select("g_id").eq("u_id", uId);
|
||
List<String> allreadyin = grouprelationMapper.selectObjs(qw1);
|
||
QueryWrapper<Groupchat> qw2 = new QueryWrapper<>();
|
||
if (!allreadyin.isEmpty()) {
|
||
qw2.in("g_id", allreadyin);
|
||
return groupMapper.selectList(qw2);
|
||
} else {
|
||
System.out.println(LocalDateTime.now() + " 无已加入群聊,返回null");
|
||
return null;
|
||
}
|
||
}
|
||
|
||
@Transactional
|
||
public void leave(String uId, String gId) {
|
||
QueryWrapper<Grouprelation> qw1 = new QueryWrapper<>();
|
||
qw1.eq("g_id", gId).eq("u_id", uId);
|
||
grouprelationMapper.delete(qw1);
|
||
stringRedisTemplate.opsForSet().remove(DB+gId, uId);
|
||
}
|
||
|
||
public boolean setNote(String uId, Groupchat groupchat) {
|
||
//判断权限
|
||
Integer role = grouprelationMapper.getRole(uId, groupchat.getG_id());
|
||
if (role != 0 && role != 1) return false;
|
||
if (groupchat.getG_note() == null) groupMapper.setNote(groupchat.getG_id(), null);
|
||
groupMapper.setNote(groupchat.getG_id(), groupchat.getG_note());
|
||
return true;
|
||
|
||
}
|
||
|
||
public void setName(String g_id, String g_name) {
|
||
groupMapper.setName(g_id, g_name);
|
||
}
|
||
|
||
public List<Member> getMembers(String gId) {
|
||
QueryWrapper<Grouprelation> qw1 = new QueryWrapper<>();
|
||
qw1.eq("g_id", gId);
|
||
List<Grouprelation> grouprelations = grouprelationMapper.selectList(qw1);
|
||
Map<String, Integer> roles = new HashMap<>();
|
||
List<String> ids = new ArrayList<>();
|
||
for (Grouprelation g : grouprelations) {
|
||
roles.put(g.getU_id(), g.getRole());
|
||
ids.add(g.getU_id());
|
||
}
|
||
QueryWrapper<User> qw2 = new QueryWrapper<>();
|
||
qw2.in("u_id", ids);
|
||
List<User> users = userMapper.selectList(qw2);
|
||
List<Member> members = new ArrayList<>();
|
||
for (User u : users) {
|
||
members.add(new Member(u, roles.get(u.getU_id())));
|
||
}
|
||
return members;
|
||
}
|
||
|
||
public boolean setRole(String uId, Grouprelation grouprelation) {
|
||
//判断权限
|
||
Integer role = grouprelationMapper.getRole(uId, grouprelation.getG_id());
|
||
if (role != 0) return false;
|
||
//判断数量
|
||
if(grouprelation.getRole()==1){
|
||
QueryWrapper<Grouprelation> qw1 = new QueryWrapper<>();
|
||
qw1.eq("g_id", grouprelation.getG_id())
|
||
.eq("role",1);
|
||
if(grouprelationMapper.selectCount(qw1)>=4) return false;
|
||
}
|
||
grouprelationMapper.update(grouprelation, new UpdateWrapper<Grouprelation>()
|
||
.eq("g_id",grouprelation.getG_id())
|
||
.eq("u_id",grouprelation.getU_id()));
|
||
return true;
|
||
}
|
||
|
||
public int verifyRole(String uId, String gId) {
|
||
return grouprelationMapper.getRole(uId, gId);
|
||
}
|
||
}
|