TeamService.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.Team;
  7. import com.izouma.nineth.dto.PageQuery;
  8. import com.izouma.nineth.enums.netease.TeamType;
  9. import com.izouma.nineth.exception.BusinessException;
  10. import com.izouma.nineth.repo.AssetRepo;
  11. import com.izouma.nineth.repo.netease.NeteaseMessageRepo;
  12. import com.izouma.nineth.repo.netease.TeamRepo;
  13. import com.izouma.nineth.utils.JpaUtils;
  14. import com.izouma.nineth.utils.SecurityUtils;
  15. import lombok.AllArgsConstructor;
  16. import org.apache.commons.lang3.StringUtils;
  17. import org.springframework.data.domain.Page;
  18. import org.springframework.data.domain.PageImpl;
  19. import org.springframework.data.redis.core.RedisTemplate;
  20. import org.springframework.stereotype.Service;
  21. import java.time.Instant;
  22. import java.time.LocalDateTime;
  23. import java.time.ZoneId;
  24. import java.time.ZoneOffset;
  25. import java.util.*;
  26. @Service
  27. @AllArgsConstructor
  28. public class TeamService {
  29. private TeamRepo teamRepo;
  30. private NeteaseUserService neteaseUserService;
  31. private RedisTemplate<String, Object> redisTemplate;
  32. private NeteaseMessageRepo neteaseMessageRepo;
  33. private AssetRepo assetRepo;
  34. public Page<Team> all(PageQuery pageQuery) {
  35. Long userId = SecurityUtils.getAuthenticatedUser().getId();
  36. Page<Team> teams = teamRepo
  37. .findAll(JpaUtils.toSpecification(pageQuery, Team.class), JpaUtils.toPageRequest(pageQuery));
  38. List<Team> content = teams.getContent();
  39. List<Team> newContent = new ArrayList<>();
  40. content.forEach(team -> {
  41. List<String> members = new ArrayList<>(team.getMembers());
  42. boolean inTeam = false;
  43. if (StringUtils.equals(team.getOwnerid(), userId.toString())) {
  44. inTeam = true;
  45. } else {
  46. for (String member : members) {
  47. String accid = userId.toString();
  48. if (StringUtils.equals(accid, member)) {
  49. inTeam = true;
  50. }
  51. }
  52. }
  53. team.setLastMsg(getTeamLastMessage(team.getTid()));
  54. team.setUnread(getUnreadCount(userId.toString(), team.getTid().toString()));
  55. team.setInTeam(inTeam);
  56. newContent.add(team);
  57. });
  58. return new PageImpl<>(newContent, teams.getPageable(), teams.getTotalElements());
  59. }
  60. public Team create(Team team) {
  61. Map<String, Object> params = new HashMap<>();
  62. params.put("tname", team.getName());
  63. params.put("owner", team.getOwnerid());
  64. params.put("members", JSONObject.toJSONString(team.getMembers()));
  65. params.put("joinmode", "0");
  66. team.setCustom(TeamType.PUBLIC);
  67. params.put("custom", team.getCustom().toString());
  68. params.put("msg", team.getMsg());
  69. String result = neteaseUserService
  70. .httpPost("team/create.action", "application/x-www-form-urlencoded;charset=utf-8", params);
  71. JSONObject jsonObject = JSON.parseObject(result);
  72. Integer code = jsonObject.getInteger("code");
  73. if (code != 200) {
  74. throw new BusinessException("注册出错,请核查后重新注册");
  75. }
  76. Long id = jsonObject.getLong("tid");
  77. team.setTid(id);
  78. return teamRepo.save(team);
  79. }
  80. public Team update(Team team) {
  81. Map<String, Object> params = new HashMap<>();
  82. params.put("tid", team.getTid());
  83. params.put("tname", team.getName());
  84. params.put("owner", team.getOwnerid());
  85. params.put("announcement", team.getAnnouncement());
  86. params.put("intro", team.getIntro());
  87. params.put("icon", "0");
  88. String result = neteaseUserService
  89. .httpPost("team/update.action", "application/x-www-form-urlencoded;charset=utf-8", params);
  90. JSONObject jsonObject = JSON.parseObject(result);
  91. Integer code = jsonObject.getInteger("code");
  92. if (code != 200) {
  93. throw new BusinessException("注册出错,请核查后重新注册");
  94. }
  95. return teamRepo.save(team);
  96. }
  97. public void mute(Long tid) {
  98. Map<String, Object> params = new HashMap<>();
  99. Team team = teamRepo.findById(tid).orElseThrow(new BusinessException("暂无"));
  100. params.put("tid", tid);
  101. params.put("owner", team.getOwnerid());
  102. params.put("mute", true);
  103. String result = neteaseUserService
  104. .httpPost("team/muteTlistAll.action", "application/x-www-form-urlencoded;charset=utf-8", params);
  105. JSONObject jsonObject = JSON.parseObject(result);
  106. Integer code = jsonObject.getInteger("code");
  107. if (code != 200) {
  108. throw new BusinessException("禁言操作出错");
  109. }
  110. team.setMute(true);
  111. teamRepo.save(team);
  112. }
  113. public void cancelMute(Long tid) {
  114. Map<String, Object> params = new HashMap<>();
  115. Team team = teamRepo.findById(tid).orElseThrow(new BusinessException("暂无"));
  116. params.put("tid", tid);
  117. params.put("owner", team.getOwnerid());
  118. params.put("mute", false);
  119. String result = neteaseUserService
  120. .httpPost("team/muteTlistAll.action", "application/x-www-form-urlencoded;charset=utf-8", params);
  121. JSONObject jsonObject = JSON.parseObject(result);
  122. Integer code = jsonObject.getInteger("code");
  123. if (code != 200) {
  124. throw new BusinessException("禁言操作出错");
  125. }
  126. team.setMute(false);
  127. teamRepo.save(team);
  128. }
  129. public Team invite(String id, String tid) {
  130. List<String> ids = new ArrayList<>();
  131. ids.add(id);
  132. Team team = teamRepo.findById(Long.valueOf(tid)).orElseThrow(new BusinessException("未找到群聊"));
  133. Map<String, Object> params = new HashMap<>();
  134. params.put("tid", tid);
  135. params.put("owner", team.getOwnerid());
  136. params.put("members", JSONObject.toJSONString(ids));
  137. params.put("magree", "0");
  138. params.put("custom", team.getCustom().toString());
  139. params.put("msg", "欢迎加入大厅测试群~~");
  140. String result = neteaseUserService
  141. .httpPost("team/add.action", "application/x-www-form-urlencoded;charset=utf-8", params);
  142. JSONObject jsonObject = JSON.parseObject(result);
  143. Integer code = jsonObject.getInteger("code");
  144. if (code != 200) {
  145. throw new BusinessException("注册出错,请核查后重新注册");
  146. }
  147. List<String> members = new ArrayList<>(team.getMembers());
  148. members.add(id);
  149. team.setMembers(members);
  150. return teamRepo.save(team);
  151. }
  152. public void leave(String id, String tid) {
  153. Team team = teamRepo.findById(Long.valueOf(tid)).orElseThrow(new BusinessException("未找到群聊"));
  154. Map<String, Object> params = new HashMap<>();
  155. params.put("tid", tid);
  156. params.put("accid", id);
  157. String result = neteaseUserService
  158. .httpPost("team/leave.action", "application/x-www-form-urlencoded;charset=utf-8", params);
  159. JSONObject jsonObject = JSON.parseObject(result);
  160. Integer code = jsonObject.getInteger("code");
  161. if (code != 200) {
  162. throw new BusinessException("退群出错");
  163. }
  164. List<String> members = new ArrayList<>(team.getMembers());
  165. members.removeIf(member -> StringUtils.equals(member, id));
  166. team.setMembers(members);
  167. teamRepo.save(team);
  168. }
  169. public void checkout(String accid, String tid) {
  170. Long ts = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
  171. redisTemplate.opsForValue().set(RedisKeys.USER_CHECKOUT_TIME + accid + ":" + tid, ts);
  172. }
  173. public Long getUnreadCount(String accid, String tid) {
  174. Object object = redisTemplate.opsForValue().get(RedisKeys.USER_CHECKOUT_TIME + accid + ":" + tid);
  175. Long unreadCount;
  176. if (object != null) {
  177. Long ts = Long.valueOf(object.toString());
  178. Instant instant = Instant.ofEpochMilli(ts);
  179. LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
  180. unreadCount = neteaseMessageRepo.countAllByToIdAndOpeAndCreatedAtAfter(tid, 1, localDateTime);
  181. } else {
  182. unreadCount = neteaseMessageRepo.countAllByToIdAndOpe(tid, 1);
  183. }
  184. return unreadCount;
  185. }
  186. public NeteaseMessage getTeamLastMessage(Long tid) {
  187. return neteaseMessageRepo.findFirstByToIdAndOpeOrderByCreatedAtDesc(tid.toString(), 1);
  188. }
  189. public void getTeamInfo(Long tid) {
  190. Map<String, Object> params = new HashMap<>();
  191. params.put("tid", tid);
  192. String result = neteaseUserService
  193. .httpPost("team/queryDetail.action", "application/x-www-form-urlencoded;charset=utf-8", params);
  194. JSONObject jsonObject = JSON.parseObject(result);
  195. System.out.println(jsonObject);
  196. }
  197. public boolean checkTeamIn(Long tid, String accid) {
  198. Team team = teamRepo.findById(tid).orElseThrow(new BusinessException("暂无群信息"));
  199. Long ownerId = Long.valueOf(accid);
  200. String collectionString = team.getCollection();
  201. if (StringUtils.isNotBlank(collectionString)) {
  202. String[] collectionKeywords = collectionString.split(",");
  203. boolean canJoin = false;
  204. for (String keyword : collectionKeywords) {
  205. if (StringUtils.isNotBlank(keyword)) {
  206. Long count = assetRepo.countNameLikeNotDestroyedAndOwner("%" + collectionString + "%", ownerId);
  207. if (count > 0) {
  208. canJoin = true;
  209. }
  210. }
  211. }
  212. return canJoin;
  213. }
  214. return true;
  215. }
  216. }