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

张辰's avatar
张辰 committed
3 4 5
import com.tanpu.biz.common.enums.community.CollectionTypeEnum;
import com.tanpu.biz.common.enums.community.CommentTypeEnum;
import com.tanpu.biz.common.enums.community.ReportTypeEnum;
刘基明's avatar
刘基明 committed
6 7
import com.tanpu.common.api.CommonResp;
import com.tanpu.common.exception.BizException;
刘基明's avatar
刘基明 committed
8
import com.tanpu.community.api.beans.qo.CommentQo;
刘基明's avatar
刘基明 committed
9
import com.tanpu.community.api.beans.req.comment.CreateCommentReq;
刘基明's avatar
刘基明 committed
10
import com.tanpu.community.api.beans.req.comment.LikeCommentReq;
11
import com.tanpu.community.api.beans.req.comment.ReportCommentReq;
12
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
刘基明's avatar
刘基明 committed
13
import com.tanpu.community.api.enums.OperationTypeEnum;
张辰's avatar
张辰 committed
14
import com.tanpu.community.cache.RedisCache;
刘基明's avatar
刘基明 committed
15
import com.tanpu.community.dao.entity.community.CommentEntity;
刘基明's avatar
刘基明 committed
16
import com.tanpu.community.feign.fatools.FeignClientForFatools;
刘基明's avatar
刘基明 committed
17 18
import com.tanpu.community.service.CollectionService;
import com.tanpu.community.service.CommentService;
19
import com.tanpu.community.service.ReportLogService;
刘基明's avatar
刘基明 committed
20
import com.tanpu.community.util.ConvertUtil;
张辰's avatar
张辰 committed
21
import org.apache.commons.lang3.StringUtils;
刘基明's avatar
刘基明 committed
22 23
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
刘基明's avatar
刘基明 committed
24
import org.springframework.transaction.annotation.Transactional;
刘基明's avatar
刘基明 committed
25

刘基明's avatar
刘基明 committed
26
import javax.annotation.Resource;
刘基明's avatar
刘基明 committed
27
import java.util.Comparator;
刘基明's avatar
刘基明 committed
28 29
import java.util.List;
import java.util.Set;
刘基明's avatar
刘基明 committed
30
import java.util.stream.Collectors;
刘基明's avatar
刘基明 committed
31

刘基明's avatar
刘基明 committed
32
import static com.tanpu.community.api.constants.RedisKeyConstant.CACHE_FEIGN_USER_INFO;
张辰's avatar
张辰 committed
33

刘基明's avatar
刘基明 committed
34 35 36 37 38 39 40
@Service
public class CommentManager {

    @Autowired
    private CommentService commentService;

    @Autowired
刘基明's avatar
刘基明 committed
41
    private FeignClientForFatools feignClientForFatools;
刘基明's avatar
刘基明 committed
42 43 44 45

    @Autowired
    private CollectionService collectionService;

刘基明's avatar
刘基明 committed
46
    @Resource
47
    private ReportLogService reportLogService;
刘基明's avatar
刘基明 committed
48

张辰's avatar
张辰 committed
49 50 51
    @Autowired
    private RedisCache redisCache;

刘基明's avatar
刘基明 committed
52
    // 评论(对主题)
刘基明's avatar
刘基明 committed
53
    // 发表评论(对主题)
刘基明's avatar
刘基明 committed
54
    public void comment(CreateCommentReq req, String userId) {
55

56 57 58
        if (StringUtils.isEmpty(req.getComment())) {
            throw new IllegalArgumentException("评论内容不能为空");
        }
59 60 61 62
        if (req.getComment().length()>500){
            throw new IllegalArgumentException("评论内容不能超过500字");
        }

刘基明's avatar
刘基明 committed
63 64 65 66 67
        CommentEntity commentEntity = CommentEntity.builder()
                .themeId(req.getThemeId())
                .parentId(req.getParentId())
                .replyId(req.getReplyId())
                .authorId(userId)
刘基明's avatar
刘基明 committed
68
                .content(req.getComment())
刘基明's avatar
刘基明 committed
69 70 71 72 73 74
                .commentType(CommentTypeEnum.THEME.getCode())
                .build();

        commentService.insertComment(commentEntity);
    }

刘基明's avatar
刘基明 committed
75
    // 查询评论
刘基明's avatar
刘基明 committed
76
    public List<CommentQo> queryComments(String themeId, String userId) {
刘基明's avatar
刘基明 committed
77 78
        // 因为需要排序,所以从库中查询所有评论
        List<CommentEntity> commentEntities = commentService.selectByThemeId(themeId);
刘基明's avatar
刘基明 committed
79 80
        List<CommentQo> commentQos = ConvertUtil.commentEntity2Qos(commentEntities);

81
        Set<String> likeCommentList = collectionService.getSetByUser(userId, CollectionTypeEnum.LIKE_COMMENT);
刘基明's avatar
刘基明 committed
82 83

        for (CommentQo commentQo : commentQos) {
刘基明's avatar
刘基明 committed
84
            //查询用户信息
刘基明's avatar
刘基明 committed
85
            String authorId = commentQo.getAuthorId();
86
            UserInfoResp userInfo = redisCache.getObject(StringUtils.joinWith("_", CACHE_FEIGN_USER_INFO, authorId),
87
                    60, () ->this.getUserInfo(authorId) , UserInfoResp.class);
刘基明's avatar
刘基明 committed
88
            if (userInfo != null) {
刘基明's avatar
刘基明 committed
89 90 91 92 93 94 95
                commentQo.setUserImg(userInfo.getHeadImageUrl());
                commentQo.setNickName(userInfo.getNickName());
                commentQo.setUserType(userInfo.getUserType());
                commentQo.setLevelGrade(userInfo.getLevelGrade());
                commentQo.setUserInvestorType(userInfo.getUserInvestorType());
                commentQo.setBelongUserOrgId(userInfo.getBelongUserOrgId());
                commentQo.setBelongUserOrgName(userInfo.getBelongUserOrgName());
刘基明's avatar
刘基明 committed
96
            }
刘基明's avatar
刘基明 committed
97 98
            //是否点赞及点赞数
            String commentId = commentQo.getCommentId();
刘基明's avatar
刘基明 committed
99
            commentQo.setHasLiked(likeCommentList.contains(commentId));
刘基明's avatar
刘基明 committed
100
            Integer countByTypeAndId = collectionService.getCountByTypeAndId(commentId, CollectionTypeEnum.LIKE_COMMENT);
刘基明's avatar
刘基明 committed
101 102 103
            commentQo.setLikeCount(countByTypeAndId);

        }
刘基明's avatar
刘基明 committed
104
        //排序:点赞降序+时间降序
刘基明's avatar
刘基明 committed
105 106
        return commentQos.stream().sorted(Comparator.comparing(CommentQo::getLikeCount, Comparator.reverseOrder()).
                thenComparing(CommentQo::getUpdateTime, Comparator.reverseOrder()))
刘基明's avatar
刘基明 committed
107
                .collect(Collectors.toList());
刘基明's avatar
刘基明 committed
108
    }
刘基明's avatar
刘基明 committed
109

110 111
    private UserInfoResp getUserInfo(String authorId){
        CommonResp<UserInfoResp> userInfoNewCommonResp = feignClientForFatools.queryUsersListNew(authorId);
刘基明's avatar
刘基明 committed
112 113 114 115 116 117
        if (userInfoNewCommonResp.isNotSuccess()) {
            throw new BizException("内部接口调用失败");
        }
        return userInfoNewCommonResp.getData();
    }

刘基明's avatar
刘基明 committed
118 119
    //点赞评论/取消点赞
    public void likeComment(LikeCommentReq req, String userId) {
刘基明's avatar
刘基明 committed
120
        if (OperationTypeEnum.CONFIRM.getCode().equals(req.getType())) {
刘基明's avatar
刘基明 committed
121
            collectionService.saveOrUpdate(req.getCommentId(), userId, CollectionTypeEnum.LIKE_COMMENT);
刘基明's avatar
刘基明 committed
122
        } else if (OperationTypeEnum.CANCEL.getCode().equals(req.getType())) {
刘基明's avatar
刘基明 committed
123 124 125 126
            collectionService.delete(req.getCommentId(), userId, CollectionTypeEnum.LIKE_COMMENT);
        }

    }
127 128

    //举报评论
刘基明's avatar
刘基明 committed
129
    @Transactional
130 131 132
    public void report(ReportCommentReq req, String userId) {
        //更改举报状态
        commentService.updateReportStatus(req.getCommentId());
刘基明's avatar
刘基明 committed
133
        //写入举报记录表
刘基明's avatar
刘基明 committed
134
        CommentEntity commentEntity = commentService.queryByIdIncludeDelete(req.getCommentId());
刘基明's avatar
刘基明 committed
135
        reportLogService.insert(ReportTypeEnum.COMMENT, userId, req.getCommentId(), commentEntity.getAuthorId(), req.getReason());
136
    }
刘基明's avatar
刘基明 committed
137 138 139 140 141 142


    //删除评论
    public void delete(String commentId, String userId) {
        commentService.delete(commentId,userId);
    }
刘基明's avatar
刘基明 committed
143
}