TeamService.java 8.3 KB

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