CommentLikesServiceImpl.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. package com.ruoyi.system.service.impl;
  2. import com.ruoyi.common.core.domain.AjaxResult;
  3. import com.ruoyi.common.core.redis.RedisCache;
  4. import com.ruoyi.common.utils.DateUtils;
  5. import com.ruoyi.common.utils.StringUtils;
  6. import com.ruoyi.system.domain.communityNews.CommentLikes;
  7. import com.ruoyi.system.mapper.CommentLikesMapper;
  8. import com.ruoyi.system.service.ICommentLikesService;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Service;
  11. import org.springframework.transaction.annotation.Transactional;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.concurrent.TimeUnit;
  15. import static com.ruoyi.common.constant.Constants.*;
  16. /**
  17. * 社区资讯点赞Service业务层处理
  18. *
  19. * @author boman
  20. * @date 2025-02-25
  21. */
  22. @Service
  23. public class CommentLikesServiceImpl implements ICommentLikesService {
  24. @Autowired
  25. private CommentLikesMapper commentLikesMapper;
  26. @Autowired
  27. private RedisCache redisCache;
  28. /**
  29. * 查询社区资讯点赞
  30. *
  31. * @param likesId 社区资讯点赞主键
  32. * @return 社区资讯点赞
  33. */
  34. @Override
  35. public CommentLikes selectCommentLikesByLikesId(Long likesId) {
  36. return commentLikesMapper.selectCommentLikesByLikesId(likesId);
  37. }
  38. /**
  39. * 查询社区资讯点赞列表
  40. *
  41. * @param commentLikes 社区资讯点赞
  42. * @return 社区资讯点赞
  43. */
  44. @Override
  45. public List<CommentLikes> selectCommentLikesList(CommentLikes commentLikes) {
  46. return commentLikesMapper.selectCommentLikesList(commentLikes);
  47. }
  48. /**
  49. * 新增社区资讯点赞
  50. *
  51. * @param commentLikes 社区资讯点赞
  52. * @return 结果
  53. */
  54. @Override
  55. public int insertCommentLikes(CommentLikes commentLikes) {
  56. commentLikes.setCreateTime(DateUtils.getNowDate());
  57. return commentLikesMapper.insertCommentLikes(commentLikes);
  58. }
  59. /**
  60. * 修改社区资讯点赞
  61. *
  62. * @param commentLikes 社区资讯点赞
  63. * @return 结果
  64. */
  65. @Override
  66. public int updateCommentLikes(CommentLikes commentLikes) {
  67. commentLikes.setUpdateTime(DateUtils.getNowDate());
  68. return commentLikesMapper.updateCommentLikes(commentLikes);
  69. }
  70. /**
  71. * 批量删除社区资讯点赞
  72. *
  73. * @param likesIds 需要删除的社区资讯点赞主键
  74. * @return 结果
  75. */
  76. @Override
  77. public int deleteCommentLikesByLikesIds(Long[] likesIds) {
  78. return commentLikesMapper.deleteCommentLikesByLikesIds(likesIds);
  79. }
  80. /**
  81. * 删除社区资讯点赞信息
  82. *
  83. * @param likesId 社区资讯点赞主键
  84. * @return 结果
  85. */
  86. @Override
  87. public int deleteCommentLikesByLikesId(Long likesId) {
  88. return commentLikesMapper.deleteCommentLikesByLikesId(likesId);
  89. }
  90. /**
  91. * 进行点赞操作
  92. * //点赞数据存储到redis,设置redis过期key监听,继承onMessage类把数据更新到数据库中
  93. * //点赞目标id + 目标类型 + 用户信息组装成CommentLikes写入数据库
  94. * //文章需要统计点赞的数量和当前登录用户是否已经点赞
  95. * //点赞之前应该去查一遍该文章或者回复的点赞key,value应该是点赞的人员id判断用户是否已经点赞过了,点赞过了就取消,没点赞过就加入
  96. * //还需要同步到数据库,需要一个完整的点赞key 一个设置过期时间的监听key
  97. */
  98. @Override
  99. @Transactional(rollbackFor = Exception.class)
  100. public AjaxResult giveTheThumbs(CommentLikes commentLikes) {
  101. String targetType = commentLikes.getTargetType();
  102. Long targetId = commentLikes.getTargetId();
  103. Long userId = commentLikes.getUserId();
  104. //保存点赞信息
  105. //根据点赞目标id' 点赞目标类型(1:资讯 2:回复) 用户id查询是否已经点赞过
  106. CommentLikes commentLikesOld = commentLikesMapper.selectCommentLikes(commentLikes);
  107. if (commentLikesOld != null){
  108. commentLikesOld.setDelFlag("Y");
  109. commentLikesMapper.updateCommentLikes(commentLikesOld);
  110. }else {
  111. commentLikesMapper.insertCommentLikes(commentLikes);
  112. }
  113. if (StringUtils.isNotBlank(targetType)) {
  114. if (ONE.equals(targetType)) {
  115. // 社区资讯文章点赞的人员集合的key key = LIKE_ONE:{targetId} value = [userId,userId];
  116. String likeOneKey = ONE_LIKE + targetId;
  117. //社区资讯文章点赞的数量数据库同步的key
  118. String likeOneCountTimeKey = ONE_LIKE_COUNT_TIME + targetId;
  119. //文章永久点赞的数量key
  120. String likeOneCountKey = ONE_LIKE_COUNT + targetId;
  121. //先去查看key对应的value集合 value = 点赞人员的id集合
  122. List<Long> userIdListValue = redisCache.getCacheList(likeOneKey);
  123. //该用户点赞文章的key
  124. String myLikeOneKey = ONE_MY_LIKE + userId;
  125. List<Long> communityIdList = redisCache.getCacheList(myLikeOneKey);
  126. if (userIdListValue != null && userIdListValue.size() > 0) {
  127. //说明该文章被点过赞
  128. //判断该用户是否点过赞
  129. if (userIdListValue.contains(userId)) {
  130. //进行取消点赞
  131. return unLike(commentLikes);
  132. } else {
  133. //新增点赞信息
  134. return isLike(commentLikes);
  135. }
  136. } else {
  137. //该文章没有被点过赞直接新增
  138. userIdListValue = new ArrayList<>();
  139. userIdListValue.add(userId);
  140. redisCache.setCacheList(likeOneKey, userIdListValue);
  141. //给数据库同步数据在 redis中增加一个有过期时间的key 1分钟过期时间 key=like_one_count_time:{文章id}#{数量}
  142. redisCache.setCacheObject(likeOneCountTimeKey+"#"+1, 1, 1, TimeUnit.MINUTES);
  143. //给该用户点赞文章的key添加值
  144. communityIdList.add(targetId);
  145. redisCache.setCacheList(myLikeOneKey, communityIdList);
  146. //给该文章永久点赞数量的key添加值
  147. redisCache.setCacheObject(likeOneCountKey, 1);
  148. return AjaxResult.success();
  149. }
  150. } else {
  151. //对回复进行点赞
  152. // 社区资讯回复点赞的人员集合的key key = LIKE_ONE:{targetId} value = [userId,userId];
  153. String likeTwoKey = TWO_LIKE + targetId;
  154. //社区资讯回复点赞的数量数据库同步的key
  155. String likeTwoCountTimeKey = TWO_LIKE_COUNT_TIME + targetId;
  156. //回复永久点赞的数量key
  157. String likeTwoCountKey = TWO_LIKE_COUNT + targetId;
  158. //先去查看key对应的value集合 value = 点赞人员的id集合
  159. List<Long> userIdListValue = redisCache.getCacheList(likeTwoKey);
  160. //该用户点赞回复的key
  161. String myLikeTwoKey = TWO_MY_LIKE + userId;
  162. List<Long> communityIdList = redisCache.getCacheList(myLikeTwoKey);
  163. if (userIdListValue != null && userIdListValue.size() > 0) {
  164. //说明该回复被点过赞
  165. //判断该用户是否点过赞
  166. if (userIdListValue.contains(userId)) {
  167. //进行取消点赞
  168. return unLike(commentLikes);
  169. } else {
  170. //新增点赞信息
  171. return isLike(commentLikes);
  172. }
  173. } else {
  174. //该回复没有被点过赞直接新增
  175. userIdListValue = new ArrayList<>();
  176. userIdListValue.add(userId);
  177. redisCache.setCacheList(likeTwoKey, userIdListValue);
  178. //给数据库同步数据在 redis中增加一个有过期时间的key 1分钟过期时间
  179. redisCache.setCacheObject(likeTwoCountTimeKey+"#"+1, 1, 1, TimeUnit.MINUTES);
  180. //给该用户点赞回复的key添加值
  181. communityIdList.add(targetId);
  182. redisCache.setCacheList(myLikeTwoKey, communityIdList);
  183. //给该回复永久点赞数量的key添加值
  184. redisCache.setCacheObject(likeTwoCountKey, 1);
  185. return AjaxResult.success();
  186. }
  187. }
  188. }
  189. return AjaxResult.error("点赞失败");
  190. }
  191. /**
  192. * 用户点赞,但不是第一次点赞的方法
  193. *
  194. * @param commentLikes
  195. * @return
  196. */
  197. public AjaxResult isLike(CommentLikes commentLikes) {
  198. String targetType = commentLikes.getTargetType();
  199. Long targetId = commentLikes.getTargetId();
  200. Long userId = commentLikes.getUserId();
  201. if (ONE.equals(targetType)) {
  202. // 社区资讯文章点赞的人员集合的key key = ONE_LIKE:{targetId} value = [userId,userId];
  203. String likeOneKey = ONE_LIKE + targetId;
  204. //社区资讯文章点赞的数量数据库同步的key
  205. String likeOneCountTimeKey = ONE_LIKE_COUNT + targetId;
  206. //文章永久点赞的数量key
  207. String likeOneCountKey = ONE_LIKE_COUNT + targetId;
  208. //先去查看key对应的value集合 value = 点赞人员的id集合
  209. List<Long> userIdListValue = redisCache.getCacheList(likeOneKey);
  210. //该用户点赞文章的key
  211. String myLikeOneKey = ONE_MY_LIKE + userId;
  212. List<Long> communityIdList = redisCache.getCacheList(myLikeOneKey);
  213. //新增点赞信息
  214. // 新增社区资讯文章点赞的人员集合
  215. userIdListValue.add(userId);
  216. redisCache.setCacheList(likeOneKey, userIdListValue);
  217. //新增该用户点赞文章
  218. communityIdList.add(targetId);
  219. redisCache.setCacheList(myLikeOneKey, communityIdList);
  220. //新增文章永久点赞数量的key更新值
  221. Integer likeOneCount = redisCache.getCacheObject(likeOneCountKey);
  222. likeOneCount = likeOneCount + 1;
  223. redisCache.setCacheObject(likeOneCountKey, likeOneCount);
  224. //新增数据库同步数据在 redis中增加一个有过期时间的key 1分钟过期时间
  225. redisCache.setCacheObject(likeOneCountTimeKey+"#"+likeOneCount, likeOneCount, 1, TimeUnit.MINUTES);
  226. return AjaxResult.success();
  227. } else {
  228. // 社区资讯文章回复的人员集合的key key = TWO_LIKE:{targetId} value = [userId,userId];
  229. String likeTwoKey = TWO_LIKE + targetId;
  230. //社区资讯文章回复的数量数据库同步的key
  231. String likeTwoCountTimeKey = TWO_LIKE_COUNT_TIME + targetId;
  232. //文章永久回复的数量key
  233. String likeTwoCountKey = TWO_LIKE_COUNT + targetId;
  234. //先去查看key对应的value集合 value = 点赞人员的id集合
  235. List<Long> userIdListValue = redisCache.getCacheList(likeTwoKey);
  236. //该用户点赞回复的key
  237. String myLikeTwoKey = TWO_MY_LIKE + userId;
  238. List<Long> communityIdList = redisCache.getCacheList(myLikeTwoKey);
  239. //新增点赞信息
  240. // 新增社区资讯回复点赞的人员集合
  241. userIdListValue.add(userId);
  242. redisCache.setCacheList(likeTwoKey, userIdListValue);
  243. //新增该用户点赞回复
  244. communityIdList.add(targetId);
  245. redisCache.setCacheList(myLikeTwoKey, communityIdList);
  246. //新增回复永久点赞数量的key更新值
  247. Integer likeTwoCount = redisCache.getCacheObject(likeTwoCountKey);
  248. likeTwoCount = likeTwoCount + 1;
  249. redisCache.setCacheObject(likeTwoCountKey, likeTwoCount);
  250. //新增数据库同步数据在 redis中增加一个有过期时间的key 1分钟过期时间
  251. redisCache.setCacheObject(likeTwoCountTimeKey+"#"+likeTwoCount, likeTwoCount, 1, TimeUnit.MINUTES);
  252. return AjaxResult.success();
  253. }
  254. }
  255. /**
  256. * 用户取消点赞
  257. *
  258. * @param commentLikes
  259. * @return
  260. */
  261. public AjaxResult unLike(CommentLikes commentLikes) {
  262. //用户点过赞 进行取消点赞
  263. String targetType = commentLikes.getTargetType();
  264. Long targetId = commentLikes.getTargetId();
  265. Long userId = commentLikes.getUserId();
  266. if (ONE.equals(targetType)) {
  267. //文章永久点赞的数量key
  268. String likeOneCountKey = ONE_LIKE_COUNT + targetId;
  269. //社区资讯文章点赞的数量数据库同步的key
  270. String likeOneCountTimeKey = ONE_LIKE_COUNT_TIME + targetId;
  271. //-------------------------删除 社区资讯文章点赞的人员集合value中的userId
  272. // key = ONE_LIKE:{targetId} value = [userId,userId];
  273. String likeOneKey = ONE_LIKE + targetId;
  274. //先去查看key对应的value集合
  275. List<Long> userIdListValue = redisCache.getCacheList(likeOneKey);
  276. userIdListValue.remove(userId);
  277. if (userIdListValue.size() == 0){
  278. redisCache.deleteObject(likeOneKey);
  279. }else {
  280. // 社区资讯文章点赞的人员集合中删除该用户
  281. redisCache.setCacheList(likeOneKey, userIdListValue);
  282. }
  283. //-------------------------删除 该用户点赞文章value中的文章id
  284. String myLikeOneKey = ONE_MY_LIKE + userId;
  285. List<Long> communityIdListValue = redisCache.getCacheList(myLikeOneKey);
  286. communityIdListValue.remove(targetId);
  287. if (communityIdListValue.size() == 0){
  288. redisCache.deleteObject(myLikeOneKey);
  289. }else {
  290. redisCache.setCacheList(myLikeOneKey, communityIdListValue);
  291. }
  292. // ---------点赞数量操作------------------
  293. //先获取永久点赞数量的值
  294. Integer likeCount = redisCache.getCacheObject(likeOneCountKey);
  295. likeCount = likeCount - 1;
  296. if (likeCount < 0) {
  297. likeCount = 0;
  298. }
  299. //更新永久点赞数量
  300. redisCache.setCacheObject(likeOneCountKey, likeCount);
  301. //给数据库同步数据在 redis中增加一个有过期时间的key 1分钟过期时间
  302. redisCache.setCacheObject(likeOneCountTimeKey+"#"+likeCount, likeCount, 1, TimeUnit.MINUTES);
  303. return AjaxResult.success();
  304. } else {
  305. //评论的删除
  306. //评论永久点赞的数量key
  307. String likeTwoCountKey = TWO_LIKE_COUNT + targetId;
  308. //社区资讯评论点赞的数量数据库同步的key
  309. String likeTwoCountTimeKey = TWO_LIKE_COUNT_TIME + targetId;
  310. //-------------------------删除 社区资讯文章评论的人员集合value中的userId
  311. // key = LIKE_ONE:{targetId} value = [userId,userId];
  312. String likeTwoKey = TWO_LIKE + targetId;
  313. //先去查看key对应的value集合
  314. List<Long> userIdListValue = redisCache.getCacheList(likeTwoKey);
  315. userIdListValue.remove(userId);
  316. if (userIdListValue.size() == 0){
  317. redisCache.deleteObject(likeTwoKey);
  318. }else {
  319. // 社区资讯文章评论的人员集合中删除该用户
  320. redisCache.setCacheList(likeTwoKey, userIdListValue);
  321. }
  322. //-------------------------删除 该用户点赞评论value中的文章id
  323. String myLikeTwoKey = TWO_MY_LIKE + userId;
  324. List<Long> communityIdListValue = redisCache.getCacheList(myLikeTwoKey);
  325. communityIdListValue.remove(targetId);
  326. if (communityIdListValue.size() == 0){
  327. redisCache.deleteObject(myLikeTwoKey);
  328. }else {
  329. redisCache.setCacheList(myLikeTwoKey, communityIdListValue);
  330. }
  331. // ---------点赞数量操作------------------
  332. //先获取永久点赞数量的值
  333. Integer likeCount = redisCache.getCacheObject(likeTwoCountKey);
  334. likeCount = likeCount - 1;
  335. if (likeCount < 0) {
  336. likeCount = 0;
  337. }
  338. //更新永久点赞数量
  339. redisCache.setCacheObject(likeTwoCountKey, likeCount);
  340. //给数据库同步数据在 redis中增加一个有过期时间的key 1分钟过期时间
  341. redisCache.setCacheObject(likeTwoCountTimeKey+"#"+likeCount, likeCount, 1, TimeUnit.MINUTES);
  342. return AjaxResult.success();
  343. }
  344. }
  345. }