| | |
| | | package cn.lili.modules.lmk.service.impl; |
| | | |
| | | import cn.lili.cache.Cache; |
| | | import cn.lili.cache.CachePrefix; |
| | | import cn.lili.common.properties.RocketmqCustomProperties; |
| | | import cn.lili.common.security.AuthUser; |
| | | import cn.lili.common.security.context.UserContext; |
| | | import cn.lili.common.sensitive.SensitiveWordsFilter; |
| | | import cn.lili.modules.lmk.domain.entity.ThumbsUpRecord; |
| | | import cn.lili.modules.lmk.domain.form.ThumbsUpRecordForm; |
| | | import cn.lili.modules.lmk.domain.vo.CollectTypeNumVO; |
| | | import cn.lili.modules.lmk.enums.general.VideoCommentStatusEnum; |
| | | import cn.lili.modules.lmk.service.ThumbsUpRecordService; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import cn.lili.modules.lmk.domain.entity.VideoComment; |
| | | import cn.lili.modules.lmk.mapper.VideoCommentMapper; |
| | | import cn.lili.modules.lmk.service.VideoCommentService; |
| | | import cn.lili.base.Result; |
| | | import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; |
| | | import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import cn.lili.modules.lmk.domain.form.VideoCommentForm; |
| | | import cn.lili.modules.lmk.domain.vo.VideoCommentVO; |
| | | import cn.lili.modules.lmk.domain.query.VideoCommentQuery; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.rocketmq.spring.core.RocketMQTemplate; |
| | | import org.springframework.stereotype.Service; |
| | | import lombok.RequiredArgsConstructor; |
| | | import cn.lili.utils.PageUtil; |
| | |
| | | |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | public class VideoCommentServiceImpl extends ServiceImpl<VideoCommentMapper, VideoComment> implements VideoCommentService { |
| | | |
| | | private final VideoCommentMapper videoCommentMapper; |
| | | private final Cache cache; |
| | | private final ThumbsUpRecordService thumbsUpRecordService; |
| | | private final RocketMQTemplate rocketMQTemplate; |
| | | private final RocketmqCustomProperties rocketmqCustomProperties; |
| | | |
| | | /** |
| | | * 评论点赞数的过期时间 |
| | | */ |
| | | public final static long EXPIRE_TIME = 15l; |
| | | |
| | | /** |
| | | * 添加 |
| | |
| | | } |
| | | } |
| | | VideoComment entity = VideoCommentForm.getEntityByForm(form, null); |
| | | entity.setStatus(VideoCommentStatusEnum.AUDITING.getValue()); |
| | | |
| | | entity.setStatus(VideoCommentStatusEnum.NORMAL.getValue()); |
| | | entity.setUserId(UserContext.getCurrentUserId()); |
| | | AuthUser currentUser = UserContext.getCurrentUser(); |
| | | entity.setUserNickname(currentUser.getNickName()); |
| | | entity.setUserAvatar(currentUser.getFace()); |
| | | |
| | | baseMapper.insert(entity); |
| | | // 初始化redis中评论的点赞数量 |
| | | cache.put(CachePrefix.VIDEO_COMMENT_LIKE_NUM.getPrefixWithId(entity.getId()), 0, EXPIRE_TIME, TimeUnit.DAYS); |
| | | return Result.ok("添加成功").data(this.detail(entity.getId()).get("data")); |
| | | } |
| | | |
| | |
| | | |
| | | @Override |
| | | public Result wxPage(VideoCommentQuery query) { |
| | | query.setUserId(UserContext.getCurrentUserId()); |
| | | IPage<VideoCommentVO> page = PageUtil.getPage(query, VideoCommentVO.class); |
| | | if (StringUtils.isNotBlank(query.getMasterCommentId())) { |
| | | // 加载子评论的情况 |
| | | baseMapper.replyCommentPage(page, query); |
| | | for (VideoCommentVO comment : page.getRecords()) { |
| | | comment.setThumbsUpNum(this.getCommentThumbsUpNum(comment.getId(), comment.getThumbsUpNum())); |
| | | } |
| | | return Result.ok().data(page.getRecords()); |
| | | } else { |
| | | // 加载主评论的情况。主评论id = masterCommentId |
| | | baseMapper.masterCommentPage(page, query); |
| | | for (VideoCommentVO comment : page.getRecords()) { |
| | | comment.setThumbsUpNum(this.getCommentThumbsUpNum(comment.getId(), comment.getThumbsUpNum())); |
| | | } |
| | | } |
| | | return Result.ok().data(page.getRecords()).total(page.getTotal()); |
| | | } |
| | |
| | | public List<CollectTypeNumVO> countNumGroupByVideo() { |
| | | return baseMapper.countNumGroupByVideo(); |
| | | } |
| | | |
| | | @Override |
| | | public Result thumbsUp(ThumbsUpRecordForm form) { |
| | | boolean exists = new LambdaQueryChainWrapper<>(thumbsUpRecordService.getBaseMapper()) |
| | | .eq(ThumbsUpRecord::getRefId, form.getRefId()) |
| | | .eq(ThumbsUpRecord::getUserId, UserContext.getCurrentUserId()) |
| | | .exists(); |
| | | if (exists) { |
| | | return Result.ok(); |
| | | } |
| | | ThumbsUpRecord record = ThumbsUpRecordForm.getEntityByForm(form, null); |
| | | record.setUserId(UserContext.getCurrentUserId()); |
| | | thumbsUpRecordService.save(record); |
| | | if (cache.exist(CachePrefix.VIDEO_COMMENT_LIKE_NUM.getPrefixWithId(record.getRefId()))) { |
| | | cache.incr(CachePrefix.VIDEO_COMMENT_LIKE_NUM.getPrefixWithId(record.getRefId())); |
| | | } else { |
| | | VideoComment comment = baseMapper.selectById(form.getRefId()); |
| | | if (Objects.nonNull(comment)) { |
| | | cache.put(CachePrefix.VIDEO_COMMENT_LIKE_NUM.getPrefixWithId(comment.getId()), comment.getThumbsUpNum() + 1, EXPIRE_TIME, TimeUnit.DAYS); |
| | | } |
| | | } |
| | | // TODO 使用rocketmq异步写入到mysql中 |
| | | // rocketMQTemplate.asyncSend(); |
| | | return Result.ok(); |
| | | } |
| | | |
| | | @Override |
| | | public Result cancelThumbsUp(ThumbsUpRecordForm form) { |
| | | new LambdaUpdateChainWrapper<>(thumbsUpRecordService.getBaseMapper()) |
| | | .eq(ThumbsUpRecord::getRefId, form.getRefId()) |
| | | .eq(ThumbsUpRecord::getThumbsUpType, form.getThumbsUpType()) |
| | | .eq(ThumbsUpRecord::getUserId, UserContext.getCurrentUserId()) |
| | | .remove(); |
| | | // redis数量减一 |
| | | cache.decr(CachePrefix.VIDEO_COMMENT_LIKE_NUM.getPrefixWithId(form.getRefId())); |
| | | // TODO mq异步同步到mysql |
| | | return Result.ok(); |
| | | } |
| | | |
| | | /** |
| | | * 从redis中获取评论数量,如果redis中没有则将mysql中的数量写入到redis |
| | | * |
| | | * @param commentId |
| | | * @param mysqlNum |
| | | * @return |
| | | */ |
| | | private long getCommentThumbsUpNum(String commentId, long mysqlNum) { |
| | | Object redisNum = cache.get(CachePrefix.VIDEO_COMMENT_LIKE_NUM.getPrefixWithId(commentId)); |
| | | if (Objects.isNull(redisNum)) { |
| | | // redis中没有就把数据库的写到redis中 |
| | | cache.put(CachePrefix.VIDEO_COMMENT_LIKE_NUM.getPrefixWithId(commentId), mysqlNum, EXPIRE_TIME, TimeUnit.DAYS); |
| | | return mysqlNum; |
| | | } |
| | | return Long.valueOf((Integer) redisNum); |
| | | } |
| | | } |