CommentManager.java 3.61 KB
Newer Older
刘基明's avatar
刘基明 committed
1 2 3
package com.tanpu.community.manager;

import com.tanpu.community.api.beans.qo.CommentQo;
刘基明's avatar
刘基明 committed
4
import com.tanpu.community.api.beans.req.comment.CreateCommentReq;
刘基明's avatar
刘基明 committed
5
import com.tanpu.community.api.beans.req.comment.LikeCommentReq;
刘基明's avatar
刘基明 committed
6 7 8
import com.tanpu.community.api.enums.CollectionTypeEnum;
import com.tanpu.community.api.enums.CommentTypeEnum;
import com.tanpu.community.dao.entity.community.CommentEntity;
刘基明's avatar
刘基明 committed
9
import com.tanpu.community.dao.entity.user.UserInfoEntity;
刘基明's avatar
刘基明 committed
10 11 12
import com.tanpu.community.service.CollectionService;
import com.tanpu.community.service.CommentService;
import com.tanpu.community.service.HomePageService;
刘基明's avatar
刘基明 committed
13
import com.tanpu.community.service.UserInfoService;
刘基明's avatar
刘基明 committed
14 15 16 17
import com.tanpu.community.util.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

刘基明's avatar
刘基明 committed
18
import javax.annotation.Resource;
刘基明's avatar
刘基明 committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
import java.util.List;
import java.util.Set;

@Service
public class CommentManager {

    @Autowired
    private CommentService commentService;

    @Autowired
    private HomePageService homePageService;

    @Autowired
    private CollectionService collectionService;

刘基明's avatar
刘基明 committed
34 35 36 37

    @Resource
    private UserInfoService userInfoService;

刘基明's avatar
刘基明 committed
38 39 40 41 42 43 44
    // 评论(对主题)
    public void comment(CreateCommentReq req, String userId) {
        CommentEntity commentEntity = CommentEntity.builder()
                .themeId(req.getThemeId())
                .parentId(req.getParentId())
                .replyId(req.getReplyId())
                .authorId(userId)
刘基明's avatar
刘基明 committed
45
                .content(req.getComment())
刘基明's avatar
刘基明 committed
46 47 48 49 50 51
                .commentType(CommentTypeEnum.THEME.getCode())
                .build();

        commentService.insertComment(commentEntity);
    }

刘基明's avatar
刘基明 committed
52
    //查询
刘基明's avatar
刘基明 committed
53 54 55 56 57 58 59 60 61 62
    public List<CommentQo> queryComments(String themeId,String userId) {
        List<CommentEntity> commentEntities = commentService.selectByThemeIdAndParentId(themeId, null);
        List<CommentQo> commentQos = ConvertUtil.commentEntity2Qos(commentEntities);


        Set<String> likeCommentList = collectionService.getListByUser(userId, CollectionTypeEnum.LIKE_COMMENT);

        for (CommentQo commentQo : commentQos) {
            //用户信息
            String authorId = commentQo.getAuthorId();
刘基明's avatar
刘基明 committed
63
            UserInfoEntity userInfo = userInfoService.selectById(authorId);
刘基明's avatar
刘基明 committed
64 65 66 67
            if (userInfo!=null){
                commentQo.setUserImg(userInfo.getUiHeadimg());
                commentQo.setNickName(userInfo.getUiNickname());
            }
刘基明's avatar
刘基明 committed
68 69 70
            //是否点赞及点赞数
            String commentId = commentQo.getCommentId();
            if (likeCommentList.contains(commentId)){
刘基明's avatar
刘基明 committed
71
                commentQo.setHasLiked(true);
刘基明's avatar
刘基明 committed
72
            }else {
刘基明's avatar
刘基明 committed
73
                commentQo.setHasLiked(false);
刘基明's avatar
刘基明 committed
74 75 76 77 78 79 80 81 82 83 84 85
            }
            Integer countByTypeAndId = collectionService.getCountByTypeAndId(commentId,CollectionTypeEnum.LIKE_COMMENT);
            commentQo.setLikeCount(countByTypeAndId);

            //二级评论,本期不上线
//            List<CommentEntity> CommentLv2Entities = commentService.selectByThemeIdAndParentId(themeId, commentId);
//            List<CommentLv2Qo> commentLv2Qos = ConvertUtil.commentLv2Entity2Qos(CommentLv2Entities);
//            commentQo.setCommentLv2Qos(commentLv2Qos);
        }

        return commentQos;
    }
刘基明's avatar
刘基明 committed
86 87 88 89 90 91 92 93 94 95 96

    //点赞评论/取消点赞
    public void likeComment(LikeCommentReq req, String userId) {
        //todo 枚举值
        if (1==req.getType()){
            collectionService.addIfNotExist(req.getCommentId(), userId, CollectionTypeEnum.LIKE_COMMENT);
        }else if (2==req.getType()) {
            collectionService.delete(req.getCommentId(), userId, CollectionTypeEnum.LIKE_COMMENT);
        }

    }
刘基明's avatar
刘基明 committed
97
}