TeamService.java 9.6 KB

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