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 redisTemplate; private NeteaseMessageRepo neteaseMessageRepo; private AssetRepo assetRepo; public Page all(Long userId, PageQuery pageQuery) { Page teams = teamRepo .findAll(JpaUtils.toSpecification(pageQuery, Team.class), JpaUtils.toPageRequest(pageQuery)); List content = teams.getContent(); List newContent = new ArrayList<>(); content.forEach(team -> { List 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 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 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 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 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 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 banned = new ArrayList<>(team.getBanned()); banned.add(accid); team.setBanned(banned); teamRepo.save(team); } public void cancelMuteMember(Long tid, String accid) { Map 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 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 ids = new ArrayList<>(); ids.add(id); Team team = teamRepo.findById(Long.valueOf(tid)).orElseThrow(new BusinessException("未找到群聊")); List members = new ArrayList<>(team.getMembers()); if (members.size() > 500) { throw new BusinessException("群成员已满"); } List kicked = team.getKicked(); if (kicked.stream().anyMatch(member -> StringUtils.equals(member, id))) { throw new BusinessException("用户被踢出,无法加入"); } Map 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 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 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 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 members = new ArrayList<>(team.getMembers()); members.removeIf(member -> StringUtils.equals(member, id)); team.setMembers(members); List 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 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 getTeamMembers(Long tid) { Team team = teamRepo.findById(tid).orElseThrow(new BusinessException("未找到群组")); List strings = team.getMembers(); List neteaseUsers = neteaseUserRepo.findAllByAccIdIn(strings); List result = new ArrayList<>(); List 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; } }