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

王亚雷's avatar
王亚雷 committed
3
import com.google.common.collect.Sets;
张辰's avatar
张辰 committed
4 5 6
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
7
import com.tanpu.common.api.CommonResp;
8
import com.tanpu.common.constant.ErrorCodeConstant;
刘基明's avatar
刘基明 committed
9
import com.tanpu.common.exception.BizException;
刘基明's avatar
刘基明 committed
10
import com.tanpu.community.api.beans.qo.CommentQo;
刘基明's avatar
刘基明 committed
11
import com.tanpu.community.api.beans.req.comment.CreateCommentReq;
刘基明's avatar
刘基明 committed
12
import com.tanpu.community.api.beans.req.comment.LikeCommentReq;
13
import com.tanpu.community.api.beans.req.comment.ReportCommentReq;
14
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
15
import com.tanpu.community.api.constants.BizConstant;
刘基明's avatar
刘基明 committed
16
import com.tanpu.community.api.enums.NotificationTypeEnum;
刘基明's avatar
刘基明 committed
17
import com.tanpu.community.api.enums.OperationTypeEnum;
张辰's avatar
张辰 committed
18
import com.tanpu.community.cache.RedisCache;
刘基明's avatar
刘基明 committed
19
import com.tanpu.community.dao.entity.community.CommentEntity;
刘基明's avatar
刘基明 committed
20
import com.tanpu.community.dao.entity.community.ThemeEntity;
刘基明's avatar
刘基明 committed
21
import com.tanpu.community.feign.fatools.FeignClientForFatools;
刘基明's avatar
刘基明 committed
22 23
import com.tanpu.community.service.CollectionService;
import com.tanpu.community.service.CommentService;
刘基明's avatar
刘基明 committed
24
import com.tanpu.community.service.FeignService;
刘基明's avatar
刘基明 committed
25
import com.tanpu.community.service.NotificationService;
26
import com.tanpu.community.service.ReportLogService;
刘基明's avatar
刘基明 committed
27
import com.tanpu.community.service.ThemeService;
刘基明's avatar
刘基明 committed
28
import com.tanpu.community.service.TopicService;
刘基明's avatar
刘基明 committed
29
import com.tanpu.community.util.ConvertUtil;
张辰's avatar
张辰 committed
30
import org.apache.commons.lang3.StringUtils;
刘基明's avatar
刘基明 committed
31 32
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
刘基明's avatar
刘基明 committed
33
import org.springframework.transaction.annotation.Transactional;
刘基明's avatar
刘基明 committed
34

刘基明's avatar
刘基明 committed
35
import javax.annotation.Resource;
36
import java.time.LocalDateTime;
刘基明's avatar
刘基明 committed
37
import java.util.Comparator;
刘基明's avatar
刘基明 committed
38 39
import java.util.List;
import java.util.Set;
刘基明's avatar
刘基明 committed
40
import java.util.stream.Collectors;
刘基明's avatar
刘基明 committed
41

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

刘基明's avatar
刘基明 committed
44 45 46 47 48 49 50
@Service
public class CommentManager {

    @Autowired
    private CommentService commentService;

    @Autowired
刘基明's avatar
刘基明 committed
51
    private FeignClientForFatools feignClientForFatools;
刘基明's avatar
刘基明 committed
52 53 54 55

    @Autowired
    private CollectionService collectionService;

刘基明's avatar
刘基明 committed
56
    @Resource
57
    private ReportLogService reportLogService;
刘基明's avatar
刘基明 committed
58

张辰's avatar
张辰 committed
59 60 61
    @Autowired
    private RedisCache redisCache;

刘基明's avatar
刘基明 committed
62 63
    @Autowired
    private NotificationService notificationService;
刘基明's avatar
刘基明 committed
64

刘基明's avatar
刘基明 committed
65 66
    @Autowired
    private ThemeService themeService;
刘基明's avatar
刘基明 committed
67 68
    @Resource
    private TopicService topicService;
刘基明's avatar
刘基明 committed
69 70
    @Resource
    private FeignService feignService;
刘基明's avatar
刘基明 committed
71

刘基明's avatar
刘基明 committed
72
    // 评论(对主题)
刘基明's avatar
刘基明 committed
73
    // 发表评论(对主题)
刘基明's avatar
刘基明 committed
74
    public CommentQo comment(CreateCommentReq req, String userId) {
75

76 77 78
        if (StringUtils.isEmpty(req.getComment())) {
            throw new IllegalArgumentException("评论内容不能为空");
        }
刘基明's avatar
刘基明 committed
79
        if (req.getComment().length() > 500) {
80 81 82
            throw new IllegalArgumentException("评论内容不能超过500字");
        }

83
        LocalDateTime now = LocalDateTime.now();
刘基明's avatar
刘基明 committed
84 85 86 87
        CommentEntity commentEntity = CommentEntity.builder()
                .themeId(req.getThemeId())
                .parentId(req.getParentId())
                .replyId(req.getReplyId())
刘基明's avatar
刘基明 committed
88
                .replyUserId(req.getReplyUserId())
刘基明's avatar
刘基明 committed
89
                .authorId(userId)
刘基明's avatar
刘基明 committed
90
                .content(req.getComment())
刘基明's avatar
刘基明 committed
91
                .commentType(CommentTypeEnum.THEME.getCode())
92 93
                .createTime(now)
                .updateTime(now)
刘基明's avatar
刘基明 committed
94 95 96
                .build();

        commentService.insertComment(commentEntity);
刘基明's avatar
刘基明 committed
97 98
        CommentQo commentQo = ConvertUtil.commentEntity2Qo(commentEntity);
        buildUserInfo(commentQo);
刘基明's avatar
刘基明 committed
99 100
        // 消息通知
        ThemeEntity themeEntity = themeService.queryByThemeId(req.getThemeId());
刘基明's avatar
刘基明 committed
101 102
        if (themeEntity == null) {
            throw new BizException(ErrorCodeConstant.UNREACHABLE);
103
        }
104

刘基明's avatar
刘基明 committed
105 106

        // 一级回复通知发帖人,二级回复通知一级评论人
刘基明's avatar
刘基明 committed
107 108 109
        if (StringUtils.isNotBlank(req.getReplyUserId())){
            notificationService.insert(userId, req.getReplyUserId(), NotificationTypeEnum.COMMENT_REPLY, commentEntity.getCommentId(), req.getComment());
            notificationService.putNotifyCache(req.getReplyUserId(), userId, NotificationTypeEnum.COMMENT_REPLY);
刘基明's avatar
刘基明 committed
110 111 112
        }else {
            notificationService.insert(userId, themeEntity.getAuthorId(), NotificationTypeEnum.COMMENT, commentEntity.getCommentId(), req.getComment());
            notificationService.putNotifyCache(themeEntity.getAuthorId(), userId, NotificationTypeEnum.COMMENT);
刘基明's avatar
刘基明 committed
113 114 115 116

        }


117 118 119 120 121
        // 评论同步转发并消息通知
        if (req.getSyncForward() == BizConstant.SyncForwardType.SYNC_FORWARD) {
            String themeId = themeService.commentSyncForward(req, userId);

            // 消息通知
刘基明's avatar
刘基明 committed
122 123 124
            ThemeEntity commentTheme = themeService.queryByThemeId(req.getThemeId());
            notificationService.insertForward(userId, commentTheme.getAuthorId(), commentTheme.getThemeId(), "", req.getComment(), themeId);
            notificationService.putNotifyCache(commentTheme.getAuthorId(), userId, NotificationTypeEnum.FORWARD);
125 126 127
        }


刘基明's avatar
刘基明 committed
128
        return commentQo;
刘基明's avatar
刘基明 committed
129 130
    }

刘基明's avatar
刘基明 committed
131
    // 查询评论
刘基明's avatar
刘基明 committed
132
    public List<CommentQo> queryComments(String themeId, String userId) {
刘基明's avatar
刘基明 committed
133 134
        // 因为需要排序,所以从库中查询所有评论
        List<CommentEntity> commentEntities = commentService.selectByThemeId(themeId);
刘基明's avatar
刘基明 committed
135 136
        List<CommentQo> commentQos = ConvertUtil.commentEntity2Qos(commentEntities);

王亚雷's avatar
王亚雷 committed
137 138 139 140 141 142
        Set<String> likeCommentList;
        if (StringUtils.isNotEmpty(userId)) {
            likeCommentList = collectionService.getSetByUser(userId, CollectionTypeEnum.LIKE_COMMENT);
        } else {
            likeCommentList = Sets.newHashSetWithExpectedSize(0);
        }
刘基明's avatar
刘基明 committed
143 144
        // 查询管理员
        ThemeEntity themeEntity = themeService.queryByThemeId(themeId);
刘基明's avatar
刘基明 committed
145
        Set<String> managerId = topicService.getManagerId(themeEntity.getTopicId());
刘基明's avatar
刘基明 committed
146 147

        for (CommentQo commentQo : commentQos) {
刘基明's avatar
刘基明 committed
148 149 150
            // 封装用户信息
            buildUserInfo(commentQo);
            // 是否点赞及点赞数
刘基明's avatar
刘基明 committed
151
            String commentId = commentQo.getCommentId();
刘基明's avatar
刘基明 committed
152 153
            Integer likeCount = collectionService.getCountByTypeAndId(commentId, CollectionTypeEnum.LIKE_COMMENT);
            commentQo.setLikeCount(likeCount);
刘基明's avatar
刘基明 committed
154
            commentQo.setHasLiked(likeCommentList.contains(commentId));
刘基明's avatar
刘基明 committed
155
            // 是否管理员
刘基明's avatar
刘基明 committed
156
            if (managerId.contains(commentQo.getAuthorId())) {
刘基明's avatar
刘基明 committed
157 158 159 160 161
                commentQo.setManager(true);
            } else {
                commentQo.setManager(false);
            }

刘基明's avatar
刘基明 committed
162 163

        }
刘基明's avatar
刘基明 committed
164
        //排序:点赞降序+时间降序
刘基明's avatar
刘基明 committed
165 166
        return commentQos.stream().sorted(Comparator.comparing(CommentQo::getLikeCount, Comparator.reverseOrder()).
                thenComparing(CommentQo::getUpdateTime, Comparator.reverseOrder()))
刘基明's avatar
刘基明 committed
167
                .collect(Collectors.toList());
刘基明's avatar
刘基明 committed
168
    }
刘基明's avatar
刘基明 committed
169

刘基明's avatar
刘基明 committed
170 171 172 173

    private void buildUserInfo(CommentQo commentQo) {
        String authorId = commentQo.getAuthorId();
        UserInfoResp userInfo = redisCache.getObject(StringUtils.joinWith("_", CACHE_FEIGN_USER_INFO, authorId),
刘基明's avatar
刘基明 committed
174
                60, () -> this.getUserInfo(authorId), UserInfoResp.class);
刘基明's avatar
刘基明 committed
175 176 177 178 179 180 181 182 183
        if (userInfo != null) {
            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
184
        // 回复用户名
刘基明's avatar
刘基明 committed
185
        if (StringUtils.isNotBlank(commentQo.getReplyUserId())) {
刘基明's avatar
刘基明 committed
186 187 188
            UserInfoResp userResp = feignService.getUserInfoById(commentQo.getReplyUserId());
            if (userResp != null) {
                commentQo.setReplyUserName(userResp.getNickName());
刘基明's avatar
刘基明 committed
189
            }
刘基明's avatar
刘基明 committed
190
        }
刘基明's avatar
刘基明 committed
191

刘基明's avatar
刘基明 committed
192 193 194 195
        commentQo.setHasLiked(false);
        commentQo.setLikeCount(0);
    }

刘基明's avatar
刘基明 committed
196
    private UserInfoResp getUserInfo(String authorId) {
197
        CommonResp<UserInfoResp> userInfoNewCommonResp = feignClientForFatools.queryUserInfoNew(authorId);
刘基明's avatar
刘基明 committed
198 199 200 201 202 203
        if (userInfoNewCommonResp.isNotSuccess()) {
            throw new BizException("内部接口调用失败");
        }
        return userInfoNewCommonResp.getData();
    }

刘基明's avatar
刘基明 committed
204 205
    //点赞评论/取消点赞
    public void likeComment(LikeCommentReq req, String userId) {
刘基明's avatar
刘基明 committed
206
        if (OperationTypeEnum.CONFIRM.getCode().equals(req.getType())) {
207
            boolean b = collectionService.saveOrUpdate(req.getCommentId(), userId, CollectionTypeEnum.LIKE_COMMENT);
刘基明's avatar
刘基明 committed
208
            if (b) {
209
                CommentEntity commentEntity = commentService.queryByCommentId(req.getCommentId());
刘基明's avatar
刘基明 committed
210
                notificationService.insertLikeComment(userId, commentEntity.getAuthorId(), req.getCommentId());
211 212
                notificationService.putNotifyCache(commentEntity.getAuthorId(), userId, NotificationTypeEnum.COMMENT_LIKE);
            }
刘基明's avatar
刘基明 committed
213

刘基明's avatar
刘基明 committed
214
        } else if (OperationTypeEnum.CANCEL.getCode().equals(req.getType())) {
刘基明's avatar
刘基明 committed
215 216 217 218
            collectionService.delete(req.getCommentId(), userId, CollectionTypeEnum.LIKE_COMMENT);
        }

    }
219 220

    //举报评论
刘基明's avatar
刘基明 committed
221
    @Transactional
222 223 224
    public void report(ReportCommentReq req, String userId) {
        //更改举报状态
        commentService.updateReportStatus(req.getCommentId());
刘基明's avatar
刘基明 committed
225
        //写入举报记录表
刘基明's avatar
刘基明 committed
226
        CommentEntity commentEntity = commentService.queryByIdIncludeDelete(req.getCommentId());
刘基明's avatar
刘基明 committed
227
        reportLogService.insert(ReportTypeEnum.COMMENT, userId, req.getCommentId(), commentEntity.getAuthorId(), req.getReason());
228
    }
刘基明's avatar
刘基明 committed
229 230 231 232


    //删除评论
    public void delete(String commentId, String userId) {
刘基明's avatar
刘基明 committed
233
        if (StringUtils.isBlank(commentId)) {
234 235
            throw new BizException("commentId不能为空");
        }
刘基明's avatar
刘基明 committed
236
        commentService.delete(commentId, userId);
237 238
        CommentEntity commentEntity = commentService.queryByIdIncludeDelete(commentId);
        ThemeEntity themeEntity = themeService.queryByThemeId(commentEntity.getThemeId());
刘基明's avatar
刘基明 committed
239
        notificationService.deleteCommentNotify(themeEntity.getAuthorId(), userId, commentId, commentEntity.getCreateTime());
240

刘基明's avatar
刘基明 committed
241
    }
刘基明's avatar
刘基明 committed
242

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