| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- package com.izouma.nineth.service.netease;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.izouma.nineth.config.RedisKeys;
- import com.izouma.nineth.domain.netease.NeteaseMessage;
- import com.izouma.nineth.domain.netease.NeteaseUser;
- import com.izouma.nineth.domain.netease.Team;
- import com.izouma.nineth.dto.PageQuery;
- import com.izouma.nineth.dto.netease.TeamMember;
- import com.izouma.nineth.enums.netease.TeamType;
- import com.izouma.nineth.exception.BusinessException;
- import com.izouma.nineth.repo.AssetRepo;
- import com.izouma.nineth.repo.NeteaseUserRepo;
- import com.izouma.nineth.repo.netease.NeteaseMessageRepo;
- import com.izouma.nineth.repo.netease.TeamRepo;
- import com.izouma.nineth.utils.JpaUtils;
- import com.izouma.nineth.utils.SecurityUtils;
- import lombok.AllArgsConstructor;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.cache.annotation.Cacheable;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.PageImpl;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.stereotype.Service;
- import java.time.Instant;
- import java.time.LocalDateTime;
- import java.time.ZoneId;
- import java.time.ZoneOffset;
- import java.util.*;
- @Service
- @AllArgsConstructor
- public class TeamService {
- private TeamRepo teamRepo;
- private NeteaseUserService neteaseUserService;
- private NeteaseUserRepo neteaseUserRepo;
- private RedisTemplate<String, Object> redisTemplate;
- private NeteaseMessageRepo neteaseMessageRepo;
- private AssetRepo assetRepo;
- public Page<Team> all(Long userId, PageQuery pageQuery) {
- Page<Team> teams = teamRepo
- .findAll(JpaUtils.toSpecification(pageQuery, Team.class), JpaUtils.toPageRequest(pageQuery));
- List<Team> content = teams.getContent();
- List<Team> newContent = new ArrayList<>();
- content.forEach(team -> {
- List<String> members = new ArrayList<>(team.getMembers());
- boolean inTeam = false;
- if (StringUtils.equals(team.getOwnerid(), userId.toString())) {
- inTeam = true;
- } else {
- for (String member : members) {
- String accid = userId.toString();
- if (StringUtils.equals(accid, member)) {
- inTeam = true;
- }
- }
- }
- team.setBan(team.getBanned().stream().anyMatch(member -> StringUtils.equals(member, userId.toString())));
- team.setLastMsg(getTeamLastMessage(team.getTid()));
- team.setUnread(getUnreadCount(userId.toString(), team.getTid().toString()));
- team.setInTeam(inTeam);
- newContent.add(team);
- });
- return new PageImpl<>(newContent, teams.getPageable(), teams.getTotalElements());
- }
- public Team create(Team team) {
- NeteaseUser neteaseUser = neteaseUserRepo.findById(Long.valueOf(team.getOwnerid())).orElse(null);
- if (neteaseUser == null) {
- neteaseUserService.create(Long.valueOf(team.getOwnerid()));
- }
- Map<String, Object> params = new HashMap<>();
- params.put("tname", team.getName());
- params.put("owner", team.getOwnerid());
- params.put("members", JSONObject.toJSONString(team.getMembers()));
- params.put("joinmode", "0");
- team.setCustom(TeamType.PUBLIC);
- params.put("custom", team.getCustom().toString());
- params.put("msg", "欢迎加入本群!");
- String result = neteaseUserService
- .httpPost("team/create.action", "application/x-www-form-urlencoded;charset=utf-8", params);
- JSONObject jsonObject = JSON.parseObject(result);
- Integer code = jsonObject.getInteger("code");
- if (code != 200) {
- throw new BusinessException("注册出错,请核查后重新注册");
- }
- Long id = jsonObject.getLong("tid");
- team.setTid(id);
- return teamRepo.save(team);
- }
- public Team update(Team team) {
- Map<String, Object> params = new HashMap<>();
- params.put("tid", team.getTid());
- params.put("tname", team.getName());
- params.put("owner", team.getOwnerid());
- params.put("announcement", team.getAnnouncement());
- params.put("intro", team.getIntro());
- params.put("teamMemberLimit", 500);
- params.put("icon", "0");
- String result = neteaseUserService
- .httpPost("team/update.action", "application/x-www-form-urlencoded;charset=utf-8", params);
- JSONObject jsonObject = JSON.parseObject(result);
- Integer code = jsonObject.getInteger("code");
- if (code != 200) {
- throw new BusinessException("注册出错,请核查后重新注册");
- }
- return teamRepo.save(team);
- }
- public void mute(Long tid) {
- Map<String, Object> params = new HashMap<>();
- Team team = teamRepo.findById(tid).orElseThrow(new BusinessException("暂无"));
- params.put("tid", tid);
- params.put("owner", team.getOwnerid());
- params.put("mute", true);
- String result = neteaseUserService
- .httpPost("team/muteTlistAll.action", "application/x-www-form-urlencoded;charset=utf-8", params);
- JSONObject jsonObject = JSON.parseObject(result);
- Integer code = jsonObject.getInteger("code");
- if (code != 200) {
- throw new BusinessException("禁言操作出错");
- }
- team.setMute(true);
- teamRepo.save(team);
- }
- public void cancelMute(Long tid) {
- Map<String, Object> params = new HashMap<>();
- Team team = teamRepo.findById(tid).orElseThrow(new BusinessException("暂无"));
- params.put("tid", tid);
- params.put("owner", team.getOwnerid());
- params.put("mute", false);
- String result = neteaseUserService
- .httpPost("team/muteTlistAll.action", "application/x-www-form-urlencoded;charset=utf-8", params);
- JSONObject jsonObject = JSON.parseObject(result);
- Integer code = jsonObject.getInteger("code");
- if (code != 200) {
- throw new BusinessException("禁言操作出错");
- }
- team.setMute(false);
- teamRepo.save(team);
- }
- public void muteMember(Long tid, String accid) {
- Map<String, Object> params = new HashMap<>();
- Team team = teamRepo.findById(tid).orElseThrow(new BusinessException("暂无"));
- params.put("tid", tid);
- params.put("owner", team.getOwnerid());
- params.put("accid", accid);
- params.put("mute", 1);
- String result = neteaseUserService
- .httpPost("team/muteTlist.action", "application/x-www-form-urlencoded;charset=utf-8", params);
- JSONObject jsonObject = JSON.parseObject(result);
- Integer code = jsonObject.getInteger("code");
- if (code != 200) {
- throw new BusinessException("禁言操作出错");
- }
- List<String> banned = new ArrayList<>(team.getBanned());
- banned.add(accid);
- team.setBanned(banned);
- teamRepo.save(team);
- }
- public void cancelMuteMember(Long tid, String accid) {
- Map<String, Object> params = new HashMap<>();
- Team team = teamRepo.findById(tid).orElseThrow(new BusinessException("暂无"));
- params.put("tid", tid);
- params.put("owner", team.getOwnerid());
- params.put("accid", accid);
- params.put("mute", 0);
- String result = neteaseUserService
- .httpPost("team/muteTlist.action", "application/x-www-form-urlencoded;charset=utf-8", params);
- JSONObject jsonObject = JSON.parseObject(result);
- Integer code = jsonObject.getInteger("code");
- if (code != 200) {
- throw new BusinessException("禁言操作出错");
- }
- List<String> banned = new ArrayList<>(team.getBanned());
- banned.removeIf(id -> StringUtils.equals(id, accid));
- team.setBanned(banned);
- teamRepo.save(team);
- }
- public Team invite(String id, String tid) {
- List<String> ids = new ArrayList<>();
- ids.add(id);
- Team team = teamRepo.findById(Long.valueOf(tid)).orElseThrow(new BusinessException("未找到群聊"));
- List<String> members = new ArrayList<>(team.getMembers());
- if (members.size() > 500) {
- throw new BusinessException("群成员已满");
- }
- List<String> kicked = team.getKicked();
- if (kicked.stream().anyMatch(member -> StringUtils.equals(member, id))) {
- throw new BusinessException("用户被踢出,无法加入");
- }
- Map<String, Object> params = new HashMap<>();
- params.put("tid", tid);
- params.put("owner", team.getOwnerid());
- params.put("members", JSONObject.toJSONString(ids));
- params.put("magree", "0");
- params.put("custom", team.getCustom().toString());
- params.put("msg", "欢迎加入大厅测试群~~");
- String result = neteaseUserService
- .httpPost("team/add.action", "application/x-www-form-urlencoded;charset=utf-8", params);
- JSONObject jsonObject = JSON.parseObject(result);
- Integer code = jsonObject.getInteger("code");
- if (code != 200) {
- throw new BusinessException("群成员已满,无法加入");
- }
- members.add(id);
- team.setMembers(members);
- return teamRepo.save(team);
- }
- public void leave(String id, String tid) {
- Team team = teamRepo.findById(Long.valueOf(tid)).orElseThrow(new BusinessException("未找到群聊"));
- Map<String, Object> params = new HashMap<>();
- params.put("tid", tid);
- params.put("accid", id);
- String result = neteaseUserService
- .httpPost("team/leave.action", "application/x-www-form-urlencoded;charset=utf-8", params);
- JSONObject jsonObject = JSON.parseObject(result);
- Integer code = jsonObject.getInteger("code");
- if (code != 200) {
- throw new BusinessException("退群出错");
- }
- List<String> members = new ArrayList<>(team.getMembers());
- members.removeIf(member -> StringUtils.equals(member, id));
- team.setMembers(members);
- teamRepo.save(team);
- }
- public void kick(String id, String tid) {
- Team team = teamRepo.findById(Long.valueOf(tid)).orElseThrow(new BusinessException("未找到群聊"));
- Map<String, Object> params = new HashMap<>();
- params.put("tid", tid);
- params.put("accid", id);
- String result = neteaseUserService
- .httpPost("team/leave.action", "application/x-www-form-urlencoded;charset=utf-8", params);
- JSONObject jsonObject = JSON.parseObject(result);
- Integer code = jsonObject.getInteger("code");
- if (code != 200) {
- throw new BusinessException("退群出错");
- }
- List<String> members = new ArrayList<>(team.getMembers());
- members.removeIf(member -> StringUtils.equals(member, id));
- team.setMembers(members);
- List<String> kicked = new ArrayList<>(team.getKicked());
- kicked.add(id);
- team.setKicked(kicked);
- teamRepo.save(team);
- }
- public void checkout(String accid, String tid) {
- Long ts = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
- redisTemplate.opsForValue().set(RedisKeys.USER_CHECKOUT_TIME + accid + ":" + tid, ts);
- }
- public Long getUnreadCount(String accid, String tid) {
- Object object = redisTemplate.opsForValue().get(RedisKeys.USER_CHECKOUT_TIME + accid + ":" + tid);
- Long unreadCount;
- if (object != null) {
- Long ts = Long.valueOf(object.toString());
- Instant instant = Instant.ofEpochMilli(ts);
- LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
- unreadCount = neteaseMessageRepo.countAllByToIdAndOpeAndCreatedAtAfter(tid, 1, localDateTime);
- } else {
- unreadCount = neteaseMessageRepo.countAllByToIdAndOpe(tid, 1);
- }
- return unreadCount;
- }
- public NeteaseMessage getTeamLastMessage(Long tid) {
- return neteaseMessageRepo.findFirstByToIdAndOpeOrderByCreatedAtDesc(tid.toString(), 1);
- }
- public void getTeamInfo(Long tid) {
- Map<String, Object> params = new HashMap<>();
- params.put("tid", tid);
- String result = neteaseUserService
- .httpPost("team/queryDetail.action", "application/x-www-form-urlencoded;charset=utf-8", params);
- JSONObject jsonObject = JSON.parseObject(result);
- System.out.println(jsonObject);
- }
- public boolean checkTeamIn(Long tid, String accid) {
- Team team = teamRepo.findById(tid).orElseThrow(new BusinessException("暂无群信息"));
- Long ownerId = Long.valueOf(accid);
- String collectionString = team.getCollection();
- if (StringUtils.isNotBlank(collectionString)) {
- String[] collectionKeywords = collectionString.split(",");
- boolean canJoin = false;
- for (String keyword : collectionKeywords) {
- if (StringUtils.isNotBlank(keyword)) {
- Long count = assetRepo.countNameLikeNotDestroyedAndOwner("%" + keyword + "%", ownerId);
- if (count > 0) {
- canJoin = true;
- }
- }
- }
- return canJoin;
- }
- return true;
- }
- public List<TeamMember> getTeamMembers(Long tid) {
- Team team = teamRepo.findById(tid).orElseThrow(new BusinessException("未找到群组"));
- List<String> strings = team.getMembers();
- List<NeteaseUser> neteaseUsers = neteaseUserRepo.findAllByAccIdIn(strings);
- List<TeamMember> result = new ArrayList<>();
- List<String> banned = new ArrayList<>(team.getBanned());
- neteaseUsers.forEach(member -> {
- TeamMember teamMember = new TeamMember();
- teamMember.setAccid(member.getAccId());
- teamMember.setAvatar(member.getIcon());
- teamMember.setNickName(member.getName());
- teamMember.setBanned(banned.stream()
- .anyMatch(bannedOne -> StringUtils.equals(bannedOne, member.getAccId())));
- result.add(teamMember);
- });
- return result;
- }
- }
|