TeamService.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package com.izouma.nineth.service.netease;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.izouma.nineth.config.RedisKeys;
  5. import com.izouma.nineth.domain.netease.NeteaseMessage;
  6. import com.izouma.nineth.domain.netease.NeteaseUser;
  7. import com.izouma.nineth.domain.netease.Team;
  8. import com.izouma.nineth.dto.PageQuery;
  9. import com.izouma.nineth.dto.netease.TeamMember;
  10. import com.izouma.nineth.enums.netease.TeamType;
  11. import com.izouma.nineth.exception.BusinessException;
  12. import com.izouma.nineth.repo.AssetRepo;
  13. import com.izouma.nineth.repo.NeteaseUserRepo;
  14. import com.izouma.nineth.repo.netease.NeteaseMessageRepo;
  15. import com.izouma.nineth.repo.netease.TeamRepo;
  16. import com.izouma.nineth.utils.JpaUtils;
  17. import com.izouma.nineth.utils.SecurityUtils;
  18. import lombok.AllArgsConstructor;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.springframework.cache.annotation.Cacheable;
  21. import org.springframework.data.domain.Page;
  22. import org.springframework.data.domain.PageImpl;
  23. import org.springframework.data.redis.core.RedisTemplate;
  24. import org.springframework.stereotype.Service;
  25. import java.time.Instant;
  26. import java.time.LocalDateTime;
  27. import java.time.ZoneId;
  28. import java.time.ZoneOffset;
  29. import java.util.*;
  30. @Service
  31. @AllArgsConstructor
  32. public class TeamService {
  33. private TeamRepo teamRepo;
  34. private NeteaseUserService neteaseUserService;
  35. private NeteaseUserRepo neteaseUserRepo;
  36. private RedisTemplate<String, Object> redisTemplate;
  37. private NeteaseMessageRepo neteaseMessageRepo;
  38. private AssetRepo assetRepo;
  39. public Page<Team> all(Long userId, PageQuery pageQuery) {
  40. Page<Team> teams = teamRepo
  41. .findAll(JpaUtils.toSpecification(pageQuery, Team.class), JpaUtils.toPageRequest(pageQuery));
  42. List<Team> content = teams.getContent();
  43. List<Team> newContent = new ArrayList<>();
  44. content.forEach(team -> {
  45. List<String> members = new ArrayList<>(team.getMembers());
  46. boolean inTeam = false;
  47. if (StringUtils.equals(team.getOwnerid(), userId.toString())) {
  48. inTeam = true;
  49. } else {
  50. for (String member : members) {
  51. String accid = userId.toString();
  52. if (StringUtils.equals(accid, member)) {
  53. inTeam = true;
  54. }
  55. }
  56. }
  57. team.setBan(team.getBanned().stream().anyMatch(member -> StringUtils.equals(member, userId.toString())));
  58. team.setLastMsg(getTeamLastMessage(team.getTid()));
  59. team.setUnread(getUnreadCount(userId.toString(), team.getTid().toString()));
  60. team.setInTeam(inTeam);
  61. newContent.add(team);
  62. });
  63. return new PageImpl<>(newContent, teams.getPageable(), teams.getTotalElements());
  64. }
  65. public Team create(Team team) {
  66. NeteaseUser neteaseUser = neteaseUserRepo.findById(Long.valueOf(team.getOwnerid())).orElse(null);
  67. if (neteaseUser == null) {
  68. neteaseUserService.create(Long.valueOf(team.getOwnerid()));
  69. }
  70. Map<String, Object> params = new HashMap<>();
  71. params.put("tname", team.getName());
  72. params.put("owner", team.getOwnerid());
  73. params.put("members", JSONObject.toJSONString(team.getMembers()));
  74. params.put("joinmode", "0");
  75. team.setCustom(TeamType.PUBLIC);
  76. params.put("custom", team.getCustom().toString());
  77. params.put("msg", "欢迎加入本群!");
  78. String result = neteaseUserService
  79. .httpPost("team/create.action", "application/x-www-form-urlencoded;charset=utf-8", params);
  80. JSONObject jsonObject = JSON.parseObject(result);
  81. Integer code = jsonObject.getInteger("code");
  82. if (code != 200) {
  83. throw new BusinessException("注册出错,请核查后重新注册");
  84. }
  85. Long id = jsonObject.getLong("tid");
  86. team.setTid(id);
  87. return teamRepo.save(team);
  88. }
  89. public Team update(Team team) {
  90. Map<String, Object> params = new HashMap<>();
  91. params.put("tid", team.getTid());
  92. params.put("tname", team.getName());
  93. params.put("owner", team.getOwnerid());
  94. params.put("announcement", team.getAnnouncement());
  95. params.put("intro", team.getIntro());
  96. params.put("teamMemberLimit", 500);
  97. params.put("icon", "0");
  98. String result = neteaseUserService
  99. .httpPost("team/update.action", "application/x-www-form-urlencoded;charset=utf-8", params);
  100. JSONObject jsonObject = JSON.parseObject(result);
  101. Integer code = jsonObject.getInteger("code");
  102. if (code != 200) {
  103. throw new BusinessException("注册出错,请核查后重新注册");
  104. }
  105. return teamRepo.save(team);
  106. }
  107. public void mute(Long tid) {
  108. Map<String, Object> params = new HashMap<>();
  109. Team team = teamRepo.findById(tid).orElseThrow(new BusinessException("暂无"));
  110. params.put("tid", tid);
  111. params.put("owner", team.getOwnerid());
  112. params.put("mute", true);
  113. String result = neteaseUserService
  114. .httpPost("team/muteTlistAll.action", "application/x-www-form-urlencoded;charset=utf-8", params);
  115. JSONObject jsonObject = JSON.parseObject(result);
  116. Integer code = jsonObject.getInteger("code");
  117. if (code != 200) {
  118. throw new BusinessException("禁言操作出错");
  119. }
  120. team.setMute(true);
  121. teamRepo.save(team);
  122. }
  123. public void cancelMute(Long tid) {
  124. Map<String, Object> params = new HashMap<>();
  125. Team team = teamRepo.findById(tid).orElseThrow(new BusinessException("暂无"));
  126. params.put("tid", tid);
  127. params.put("owner", team.getOwnerid());
  128. params.put("mute", false);
  129. String result = neteaseUserService
  130. .httpPost("team/muteTlistAll.action", "application/x-www-form-urlencoded;charset=utf-8", params);
  131. JSONObject jsonObject = JSON.parseObject(result);
  132. Integer code = jsonObject.getInteger("code");
  133. if (code != 200) {
  134. throw new BusinessException("禁言操作出错");
  135. }
  136. team.setMute(false);
  137. teamRepo.save(team);
  138. }
  139. public void muteMember(Long tid, String accid) {
  140. Map<String, Object> params = new HashMap<>();
  141. Team team = teamRepo.findById(tid).orElseThrow(new BusinessException("暂无"));
  142. params.put("tid", tid);
  143. params.put("owner", team.getOwnerid());
  144. params.put("accid", accid);
  145. params.put("mute", 1);
  146. String result = neteaseUserService
  147. .httpPost("team/muteTlist.action", "application/x-www-form-urlencoded;charset=utf-8", params);
  148. JSONObject jsonObject = JSON.parseObject(result);
  149. Integer code = jsonObject.getInteger("code");
  150. if (code != 200) {
  151. throw new BusinessException("禁言操作出错");
  152. }
  153. List<String> banned = new ArrayList<>(team.getBanned());
  154. banned.add(accid);
  155. team.setBanned(banned);
  156. teamRepo.save(team);
  157. }
  158. public void cancelMuteMember(Long tid, String accid) {
  159. Map<String, Object> params = new HashMap<>();
  160. Team team = teamRepo.findById(tid).orElseThrow(new BusinessException("暂无"));
  161. params.put("tid", tid);
  162. params.put("owner", team.getOwnerid());
  163. params.put("accid", accid);
  164. params.put("mute", 0);
  165. String result = neteaseUserService
  166. .httpPost("team/muteTlist.action", "application/x-www-form-urlencoded;charset=utf-8", params);
  167. JSONObject jsonObject = JSON.parseObject(result);
  168. Integer code = jsonObject.getInteger("code");
  169. if (code != 200) {
  170. throw new BusinessException("禁言操作出错");
  171. }
  172. List<String> banned = new ArrayList<>(team.getBanned());
  173. banned.removeIf(id -> StringUtils.equals(id, accid));
  174. team.setBanned(banned);
  175. teamRepo.save(team);
  176. }
  177. public Team invite(String id, String tid) {
  178. List<String> ids = new ArrayList<>();
  179. ids.add(id);
  180. Team team = teamRepo.findById(Long.valueOf(tid)).orElseThrow(new BusinessException("未找到群聊"));
  181. List<String> members = new ArrayList<>(team.getMembers());
  182. if (members.size() > 500) {
  183. throw new BusinessException("群成员已满");
  184. }
  185. List<String> kicked = team.getKicked();
  186. if (kicked.stream().anyMatch(member -> StringUtils.equals(member, id))) {
  187. throw new BusinessException("用户被踢出,无法加入");
  188. }
  189. Map<String, Object> params = new HashMap<>();
  190. params.put("tid", tid);
  191. params.put("owner", team.getOwnerid());
  192. params.put("members", JSONObject.toJSONString(ids));
  193. params.put("magree", "0");
  194. params.put("custom", team.getCustom().toString());
  195. params.put("msg", "欢迎加入大厅测试群~~");
  196. String result = neteaseUserService
  197. .httpPost("team/add.action", "application/x-www-form-urlencoded;charset=utf-8", params);
  198. JSONObject jsonObject = JSON.parseObject(result);
  199. Integer code = jsonObject.getInteger("code");
  200. if (code != 200) {
  201. throw new BusinessException("群成员已满,无法加入");
  202. }
  203. members.add(id);
  204. team.setMembers(members);
  205. return teamRepo.save(team);
  206. }
  207. public void leave(String id, String tid) {
  208. Team team = teamRepo.findById(Long.valueOf(tid)).orElseThrow(new BusinessException("未找到群聊"));
  209. Map<String, Object> params = new HashMap<>();
  210. params.put("tid", tid);
  211. params.put("accid", id);
  212. String result = neteaseUserService
  213. .httpPost("team/leave.action", "application/x-www-form-urlencoded;charset=utf-8", params);
  214. JSONObject jsonObject = JSON.parseObject(result);
  215. Integer code = jsonObject.getInteger("code");
  216. if (code != 200) {
  217. throw new BusinessException("退群出错");
  218. }
  219. List<String> members = new ArrayList<>(team.getMembers());
  220. members.removeIf(member -> StringUtils.equals(member, id));
  221. team.setMembers(members);
  222. teamRepo.save(team);
  223. }
  224. public void kick(String id, String tid) {
  225. Team team = teamRepo.findById(Long.valueOf(tid)).orElseThrow(new BusinessException("未找到群聊"));
  226. Map<String, Object> params = new HashMap<>();
  227. params.put("tid", tid);
  228. params.put("accid", id);
  229. String result = neteaseUserService
  230. .httpPost("team/leave.action", "application/x-www-form-urlencoded;charset=utf-8", params);
  231. JSONObject jsonObject = JSON.parseObject(result);
  232. Integer code = jsonObject.getInteger("code");
  233. if (code != 200) {
  234. throw new BusinessException("退群出错");
  235. }
  236. List<String> members = new ArrayList<>(team.getMembers());
  237. members.removeIf(member -> StringUtils.equals(member, id));
  238. team.setMembers(members);
  239. List<String> kicked = new ArrayList<>(team.getKicked());
  240. kicked.add(id);
  241. team.setKicked(kicked);
  242. teamRepo.save(team);
  243. }
  244. public void checkout(String accid, String tid) {
  245. Long ts = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
  246. redisTemplate.opsForValue().set(RedisKeys.USER_CHECKOUT_TIME + accid + ":" + tid, ts);
  247. }
  248. public Long getUnreadCount(String accid, String tid) {
  249. Object object = redisTemplate.opsForValue().get(RedisKeys.USER_CHECKOUT_TIME + accid + ":" + tid);
  250. Long unreadCount;
  251. if (object != null) {
  252. Long ts = Long.valueOf(object.toString());
  253. Instant instant = Instant.ofEpochMilli(ts);
  254. LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
  255. unreadCount = neteaseMessageRepo.countAllByToIdAndOpeAndCreatedAtAfter(tid, 1, localDateTime);
  256. } else {
  257. unreadCount = neteaseMessageRepo.countAllByToIdAndOpe(tid, 1);
  258. }
  259. return unreadCount;
  260. }
  261. public NeteaseMessage getTeamLastMessage(Long tid) {
  262. return neteaseMessageRepo.findFirstByToIdAndOpeOrderByCreatedAtDesc(tid.toString(), 1);
  263. }
  264. public void getTeamInfo(Long tid) {
  265. Map<String, Object> params = new HashMap<>();
  266. params.put("tid", tid);
  267. String result = neteaseUserService
  268. .httpPost("team/queryDetail.action", "application/x-www-form-urlencoded;charset=utf-8", params);
  269. JSONObject jsonObject = JSON.parseObject(result);
  270. System.out.println(jsonObject);
  271. }
  272. public boolean checkTeamIn(Long tid, String accid) {
  273. Team team = teamRepo.findById(tid).orElseThrow(new BusinessException("暂无群信息"));
  274. Long ownerId = Long.valueOf(accid);
  275. String collectionString = team.getCollection();
  276. if (StringUtils.isNotBlank(collectionString)) {
  277. String[] collectionKeywords = collectionString.split(",");
  278. boolean canJoin = false;
  279. for (String keyword : collectionKeywords) {
  280. if (StringUtils.isNotBlank(keyword)) {
  281. Long count = assetRepo.countNameLikeNotDestroyedAndOwner("%" + keyword + "%", ownerId);
  282. if (count > 0) {
  283. canJoin = true;
  284. }
  285. }
  286. }
  287. return canJoin;
  288. }
  289. return true;
  290. }
  291. public List<TeamMember> getTeamMembers(Long tid) {
  292. Team team = teamRepo.findById(tid).orElseThrow(new BusinessException("未找到群组"));
  293. List<String> strings = team.getMembers();
  294. List<NeteaseUser> neteaseUsers = neteaseUserRepo.findAllByAccIdIn(strings);
  295. List<TeamMember> result = new ArrayList<>();
  296. List<String> banned = new ArrayList<>(team.getBanned());
  297. neteaseUsers.forEach(member -> {
  298. TeamMember teamMember = new TeamMember();
  299. teamMember.setAccid(member.getAccId());
  300. teamMember.setAvatar(member.getIcon());
  301. teamMember.setNickName(member.getName());
  302. teamMember.setBanned(banned.stream()
  303. .anyMatch(bannedOne -> StringUtils.equals(bannedOne, member.getAccId())));
  304. result.add(teamMember);
  305. });
  306. return result;
  307. }
  308. }