Search in sources :

Example 1 with CommentZanBO

use of com.ch999.haha.admin.document.redis.CommentZanBO in project haha by hahafreeasair666.

the class NewsCommentServiceImpl method addZan.

@Override
public Boolean addZan(String id, Integer userId) {
    NewsCommentBO newsCommentBO = newsCommentRepository.findOne(id);
    newsCommentBO = newsCommentBO != null ? newsCommentBO : newsCommentRepository.findByHotReplyReplyId(id);
    if (newsCommentBO == null) {
        return null;
    }
    CommentZanBO one = commentZanRepository.findOne(id);
    if (one != null) {
        if (one.getZanUserList().parallelStream().anyMatch(li -> li.equals(userId))) {
            return false;
        } else {
            one.getZanUserList().add(userId);
        }
    } else {
        one = new CommentZanBO();
        one.setCommentId(id);
        List<Integer> zanList = new ArrayList<>();
        zanList.add(userId);
        one.setZanUserList(zanList);
    }
    NewsCommentBO.updateZanCount(newsCommentBO, id, one.getZanUserList().size());
    newsCommentRepository.save(newsCommentBO);
    commentZanRepository.save(one);
    return true;
}
Also used : NewsCommentBO(com.ch999.haha.admin.document.mongo.NewsCommentBO) CommentZanBO(com.ch999.haha.admin.document.redis.CommentZanBO)

Example 2 with CommentZanBO

use of com.ch999.haha.admin.document.redis.CommentZanBO in project haha by hahafreeasair666.

the class NewsCommentServiceImpl method getNewsCommentList.

@Override
public PageVO<NewsCommentBO> getNewsCommentList(Integer newsId, Pageable pageable, Integer userId) {
    Page<NewsCommentBO> allByNewsIdAndIsDel = newsCommentRepository.findAllByNewsIdAndIsDel(newsId, false, pageable);
    List<NewsCommentBO> commentList = allByNewsIdAndIsDel.getContent();
    commentList.forEach(li -> {
        // 评论中评论的回复只显示未删除的点赞最多的三条
        if (li.getReplies().size() > 3) {
            li.setReplies(li.getReplies().stream().filter(de -> !de.getIsDel()).sorted(Comparator.comparing(NewsCommentBO.Reply::getZan).reversed()).collect(Collectors.toList()).subList(0, 3));
            li.setIsNeedOpen(true);
        } else {
            li.setIsNeedOpen(false);
        }
        // 评论字段赋值
        UserInfo userInfo = userInfoService.selectById(li.getUserId());
        li.setUserName(userInfo.getUsername());
        li.setAvatar(userInfo.getPicPath());
        if (li.getZan() > 0 && userId != null) {
            CommentZanBO zanInfo = commentZanRepository.findOne(li.getId());
            if (zanInfo != null && zanInfo.getZanUserList().parallelStream().anyMatch(zan -> zan.equals(userId))) {
                li.setIsPraised(true);
            } else {
                li.setIsPraised(false);
            }
        } else {
            li.setIsPraised(false);
        }
        // 设置能否删除该评论字段
        if (li.getUserId().equals(userId)) {
            li.setIsCanDel(true);
        } else {
            li.setIsCanDel(false);
        }
        // 回复字段赋值
        li.getReplies().forEach(re -> handleReplies(re, userId));
    });
    return new PageVO(allByNewsIdAndIsDel, pageable.getPageNumber(), commentList);
}
Also used : java.util(java.util) CommentZanRepository(com.ch999.haha.admin.repository.redis.CommentZanRepository) PageVO(com.ch999.haha.admin.vo.PageVO) Resource(javax.annotation.Resource) NewsCommentService(com.ch999.haha.admin.service.NewsCommentService) StopWatch(org.springframework.util.StopWatch) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) NewsCommentBO(com.ch999.haha.admin.document.mongo.NewsCommentBO) UserInfoService(com.ch999.haha.admin.service.UserInfoService) CommentReplyVO(com.ch999.haha.admin.vo.CommentReplyVO) CollectionUtils(org.apache.commons.collections.CollectionUtils) PageableVo(com.ch999.haha.common.PageableVo) Service(org.springframework.stereotype.Service) UserInfo(com.ch999.haha.admin.entity.UserInfo) CommentZanBO(com.ch999.haha.admin.document.redis.CommentZanBO) NewsCommentRepository(com.ch999.haha.admin.repository.mongo.NewsCommentRepository) Pageable(org.springframework.data.domain.Pageable) PageVO(com.ch999.haha.admin.vo.PageVO) UserInfo(com.ch999.haha.admin.entity.UserInfo) NewsCommentBO(com.ch999.haha.admin.document.mongo.NewsCommentBO) CommentZanBO(com.ch999.haha.admin.document.redis.CommentZanBO)

Example 3 with CommentZanBO

use of com.ch999.haha.admin.document.redis.CommentZanBO in project haha by hahafreeasair666.

the class NewsCommentServiceImpl method handleReplies.

private void handleReplies(NewsCommentBO.Reply re, Integer userId) {
    UserInfo replyUserInfo = userInfoService.selectById(re.getReplyUserId());
    UserInfo toUserInfo = userInfoService.selectById(re.getToUserId());
    re.setReplyUserAvatar(replyUserInfo.getPicPath());
    re.setReplyUserName(replyUserInfo.getUsername());
    re.setToUserName(toUserInfo.getUsername());
    if (re.getZan() > 0 && userId != null) {
        CommentZanBO toZanInfo = commentZanRepository.findOne(re.getReplyId());
        if (toZanInfo != null) {
            re.setIsPraised(toZanInfo.getZanUserList().parallelStream().anyMatch(z -> z.equals(userId)));
        } else {
            re.setIsPraised(false);
        }
    } else {
        re.setIsPraised(false);
    }
    // 设置能否删除该评论字段
    if (re.getReplyUserId().equals(userId)) {
        re.setIsCanDel(true);
    } else {
        re.setIsCanDel(false);
    }
}
Also used : java.util(java.util) CommentZanRepository(com.ch999.haha.admin.repository.redis.CommentZanRepository) PageVO(com.ch999.haha.admin.vo.PageVO) Resource(javax.annotation.Resource) NewsCommentService(com.ch999.haha.admin.service.NewsCommentService) StopWatch(org.springframework.util.StopWatch) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) NewsCommentBO(com.ch999.haha.admin.document.mongo.NewsCommentBO) UserInfoService(com.ch999.haha.admin.service.UserInfoService) CommentReplyVO(com.ch999.haha.admin.vo.CommentReplyVO) CollectionUtils(org.apache.commons.collections.CollectionUtils) PageableVo(com.ch999.haha.common.PageableVo) Service(org.springframework.stereotype.Service) UserInfo(com.ch999.haha.admin.entity.UserInfo) CommentZanBO(com.ch999.haha.admin.document.redis.CommentZanBO) NewsCommentRepository(com.ch999.haha.admin.repository.mongo.NewsCommentRepository) Pageable(org.springframework.data.domain.Pageable) UserInfo(com.ch999.haha.admin.entity.UserInfo) CommentZanBO(com.ch999.haha.admin.document.redis.CommentZanBO)

Example 4 with CommentZanBO

use of com.ch999.haha.admin.document.redis.CommentZanBO in project haha by hahafreeasair666.

the class NewsCommentServiceImpl method getCommentReplies.

@Override
public CommentReplyVO getCommentReplies(String commentId, PageableVo pageable, Integer userId) {
    StopWatch stopWatch = new StopWatch("mongo");
    stopWatch.start("查询");
    CommentReplyVO commentReplyVO = new CommentReplyVO(newsCommentRepository.findOne(commentId), pageable);
    stopWatch.stop();
    if (commentReplyVO.getNewsCommentAndReply() != null) {
        stopWatch.start("评论处理");
        UserInfo userInfo = userInfoService.selectById(commentReplyVO.getNewsCommentAndReply().getUserId());
        commentReplyVO.getNewsCommentAndReply().setAvatar(userInfo.getPicPath());
        commentReplyVO.getNewsCommentAndReply().setUserName(userInfo.getUsername());
        if (commentReplyVO.getNewsCommentAndReply().getZan() > 0 && userId != null) {
            CommentZanBO one = commentZanRepository.findOne(commentReplyVO.getNewsCommentAndReply().getId());
            if (one != null) {
                commentReplyVO.getNewsCommentAndReply().setIsPraised(one.getZanUserList().parallelStream().anyMatch(li -> li.equals(userId)));
            } else {
                commentReplyVO.getNewsCommentAndReply().setIsPraised(false);
            }
        } else {
            commentReplyVO.getNewsCommentAndReply().setIsPraised(false);
        }
        // 设置能否删除该评论字段
        if (commentReplyVO.getNewsCommentAndReply().getUserId().equals(userId)) {
            commentReplyVO.getNewsCommentAndReply().setIsCanDel(true);
        } else {
            commentReplyVO.getNewsCommentAndReply().setIsCanDel(false);
        }
        stopWatch.stop();
        if (CollectionUtils.isNotEmpty(commentReplyVO.getNewsCommentAndReply().getReplies())) {
            stopWatch.start("回复分页处理");
            commentReplyVO.getNewsCommentAndReply().setReplies(commentReplyVO.getNewsCommentAndReply().getReplies().stream().filter(li -> !li.getIsDel()).sorted(Comparator.comparing(NewsCommentBO.Reply::getZan).reversed()).collect(Collectors.toList()));
            Integer replySize = commentReplyVO.getNewsCommentAndReply().getReplies().size();
            if (replySize >= pageable.getPage() * pageable.getSize() - pageable.getSize()) {
                if (pageable.getPage() == 1) {
                    commentReplyVO.getNewsCommentAndReply().setReplies(commentReplyVO.getNewsCommentAndReply().getReplies().subList(0, pageable.getSize() > replySize ? replySize : pageable.getSize()));
                } else {
                    commentReplyVO.getNewsCommentAndReply().setReplies(commentReplyVO.getNewsCommentAndReply().getReplies().subList(pageable.getPage() * pageable.getSize() - pageable.getSize(), replySize > pageable.getPage() * pageable.getSize() ? pageable.getPage() * pageable.getSize() : replySize));
                }
            } else {
                commentReplyVO.getNewsCommentAndReply().setReplies(new ArrayList<>());
            }
            stopWatch.stop();
            stopWatch.start("数据库查询操作");
            commentReplyVO.getNewsCommentAndReply().getReplies().forEach(li -> handleReplies(li, userId));
            stopWatch.stop();
        }
    }
    System.out.println(stopWatch.prettyPrint());
    return commentReplyVO;
}
Also used : java.util(java.util) CommentZanRepository(com.ch999.haha.admin.repository.redis.CommentZanRepository) PageVO(com.ch999.haha.admin.vo.PageVO) Resource(javax.annotation.Resource) NewsCommentService(com.ch999.haha.admin.service.NewsCommentService) StopWatch(org.springframework.util.StopWatch) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) NewsCommentBO(com.ch999.haha.admin.document.mongo.NewsCommentBO) UserInfoService(com.ch999.haha.admin.service.UserInfoService) CommentReplyVO(com.ch999.haha.admin.vo.CommentReplyVO) CollectionUtils(org.apache.commons.collections.CollectionUtils) PageableVo(com.ch999.haha.common.PageableVo) Service(org.springframework.stereotype.Service) UserInfo(com.ch999.haha.admin.entity.UserInfo) CommentZanBO(com.ch999.haha.admin.document.redis.CommentZanBO) NewsCommentRepository(com.ch999.haha.admin.repository.mongo.NewsCommentRepository) Pageable(org.springframework.data.domain.Pageable) CommentReplyVO(com.ch999.haha.admin.vo.CommentReplyVO) UserInfo(com.ch999.haha.admin.entity.UserInfo) CommentZanBO(com.ch999.haha.admin.document.redis.CommentZanBO) StopWatch(org.springframework.util.StopWatch)

Example 5 with CommentZanBO

use of com.ch999.haha.admin.document.redis.CommentZanBO in project haha by hahafreeasair666.

the class NewsServiceImpl method addNewsZan.

@Override
public Boolean addNewsZan(Integer id, Integer userId) {
    News news = this.selectById(id);
    if (news == null) {
        return null;
    }
    CommentZanBO one = commentZanRepository.findOne(id.toString());
    if (one != null) {
        if (one.getZanUserList().stream().anyMatch(li -> li.equals(userId))) {
            return false;
        }
        one.getZanUserList().add(userId);
    } else {
        one = new CommentZanBO();
        one.setCommentId(id.toString());
        List<Integer> list = new ArrayList<>();
        list.add(userId);
        one.setZanUserList(list);
    }
    news.setZan(one.getZanUserList().size());
    this.updateById(news);
    commentZanRepository.save(one);
    return true;
}
Also used : CommentZanBO(com.ch999.haha.admin.document.redis.CommentZanBO)

Aggregations

CommentZanBO (com.ch999.haha.admin.document.redis.CommentZanBO)6 NewsCommentBO (com.ch999.haha.admin.document.mongo.NewsCommentBO)4 UserInfo (com.ch999.haha.admin.entity.UserInfo)3 NewsCommentRepository (com.ch999.haha.admin.repository.mongo.NewsCommentRepository)3 CommentZanRepository (com.ch999.haha.admin.repository.redis.CommentZanRepository)3 NewsCommentService (com.ch999.haha.admin.service.NewsCommentService)3 UserInfoService (com.ch999.haha.admin.service.UserInfoService)3 CommentReplyVO (com.ch999.haha.admin.vo.CommentReplyVO)3 PageVO (com.ch999.haha.admin.vo.PageVO)3 PageableVo (com.ch999.haha.common.PageableVo)3 java.util (java.util)3 Collectors (java.util.stream.Collectors)3 Resource (javax.annotation.Resource)3 CollectionUtils (org.apache.commons.collections.CollectionUtils)3 Page (org.springframework.data.domain.Page)3 Pageable (org.springframework.data.domain.Pageable)3 Service (org.springframework.stereotype.Service)3 StopWatch (org.springframework.util.StopWatch)3 JSONObject (com.alibaba.fastjson.JSONObject)1 EntityWrapper (com.baomidou.mybatisplus.mapper.EntityWrapper)1