CommentService.java 9.56 KB
Newer Older
刘基明's avatar
刘基明 committed
1 2 3
package com.tanpu.community.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
张辰's avatar
张辰 committed
4
import com.fasterxml.jackson.core.type.TypeReference;
张辰's avatar
张辰 committed
5 6
import com.tanpu.biz.common.enums.community.CommentTypeEnum;
import com.tanpu.biz.common.enums.community.ReportStatusEnum;
刘基明's avatar
刘基明 committed
7
import com.tanpu.common.exception.BizException;
刘基明's avatar
刘基明 committed
8
import com.tanpu.common.uuid.UuidGenHelper;
刘基明's avatar
刘基明 committed
9
import com.tanpu.community.api.beans.qo.ThemeQo;
10
import com.tanpu.community.api.beans.req.theme.ForwardThemeReq;
刘基明's avatar
刘基明 committed
11
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
刘基明's avatar
刘基明 committed
12
import com.tanpu.community.api.enums.DeleteTagEnum;
张辰's avatar
张辰 committed
13
import com.tanpu.community.cache.RedisCache;
刘基明's avatar
刘基明 committed
14
import com.tanpu.community.dao.entity.community.CommentEntity;
刘基明's avatar
刘基明 committed
15
import com.tanpu.community.dao.entity.community.TimesCountEntity;
刘基明's avatar
刘基明 committed
16
import com.tanpu.community.dao.mapper.community.CommentMapper;
刘基明's avatar
刘基明 committed
17
import com.tanpu.community.util.ConvertUtil;
刘基明's avatar
刘基明 committed
18
import org.apache.commons.collections4.CollectionUtils;
刘基明's avatar
刘基明 committed
19
import org.apache.commons.lang3.StringUtils;
刘基明's avatar
刘基明 committed
20
import org.springframework.stereotype.Service;
刘基明's avatar
刘基明 committed
21
import org.springframework.transaction.annotation.Transactional;
刘基明's avatar
刘基明 committed
22 23

import javax.annotation.Resource;
张辰's avatar
张辰 committed
24
import java.util.*;
刘基明's avatar
刘基明 committed
25
import java.util.stream.Collectors;
刘基明's avatar
刘基明 committed
26

27
import static com.tanpu.community.api.constants.RedisKeyConstant.*;
张辰's avatar
张辰 committed
28

刘基明's avatar
刘基明 committed
29 30 31 32 33 34
@Service
public class CommentService {

    @Resource
    private CommentMapper commentMapper;

刘基明's avatar
刘基明 committed
35
    @Resource
刘基明's avatar
刘基明 committed
36 37
    private UuidGenHelper uuidGenHelper;

刘基明's avatar
刘基明 committed
38
    @Resource
张辰's avatar
张辰 committed
39
    private RedisCache redisCache;
刘基明's avatar
刘基明 committed
40 41
    @Resource
    private FeignService feignService;
张辰's avatar
张辰 committed
42

刘基明's avatar
刘基明 committed
43
    @Transactional
刘基明's avatar
刘基明 committed
44
    public void insertComment(CommentEntity commentEntity) {
刘基明's avatar
刘基明 committed
45
        commentEntity.setCommentId(uuidGenHelper.getUuidStr());
刘基明's avatar
刘基明 committed
46
        commentMapper.insert(commentEntity);
刘基明's avatar
刘基明 committed
47
        //失效缓存
刘基明's avatar
刘基明 committed
48 49
        evictThemeCache(commentEntity.getThemeId());

刘基明's avatar
刘基明 committed
50 51 52
    }


刘基明's avatar
刘基明 committed
53
    public CommentEntity queryByIdIncludeDelete(String commmentId) {
54 55
        return commentMapper.selectOne(new LambdaQueryWrapper<CommentEntity>()
                .eq(CommentEntity::getCommentId, commmentId));
刘基明's avatar
刘基明 committed
56
    }
刘基明's avatar
刘基明 committed
57 58

    //统计主题集合的评论量
刘基明's avatar
刘基明 committed
59 60
    public Integer getTotalCountByThemeIds(List<String> themeIds) {
        if (CollectionUtils.isEmpty(themeIds)) {
刘基明's avatar
刘基明 committed
61 62
            return 0;
        }
刘基明's avatar
刘基明 committed
63
        return commentMapper.selectList((new LambdaQueryWrapper<CommentEntity>()
刘基明's avatar
刘基明 committed
64 65
                .in(CommentEntity::getThemeId, themeIds))
                .eq(CommentEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED))
刘基明's avatar
刘基明 committed
66 67
                .size();
    }
刘基明's avatar
刘基明 committed
68

刘基明's avatar
刘基明 committed
69 70
    //统计主题集合的评论量
    public Integer getCommentCountByThemeId(String themeId) {
张辰's avatar
张辰 committed
71
        return commentMapper.selectCount((new LambdaQueryWrapper<CommentEntity>()
刘基明's avatar
刘基明 committed
72
                .eq(CommentEntity::getThemeId, themeId))
张辰's avatar
张辰 committed
73 74 75 76 77 78 79 80 81 82 83 84 85
                .eq(CommentEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED));
    }
    public Integer getCommentCountByThemeIds(List<String> themeIds) {
        return commentMapper.selectCount((new LambdaQueryWrapper<CommentEntity>()
                .in(CommentEntity::getThemeId, themeIds))
                .eq(CommentEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED));
    }
    public Integer getCommentCountByThemeIds(List<String> themeIds, Date startDate, Date endDate) {
        return commentMapper.selectCount((new LambdaQueryWrapper<CommentEntity>()
                .in(CommentEntity::getThemeId, themeIds))
                .gt(CommentEntity::getCreateTime, startDate)
                .lt(CommentEntity::getCreateTime, endDate)
                .eq(CommentEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED));
刘基明's avatar
刘基明 committed
86 87
    }

刘基明's avatar
刘基明 committed
88 89 90

    //统计主题集合的评论量
    public Map<String, Integer> getCountMapByThemeIds(List<String> themeIds) {
刘基明's avatar
刘基明 committed
91
        if (CollectionUtils.isEmpty(themeIds)) {
刘基明's avatar
刘基明 committed
92 93
            return new HashMap<>();
        }
刘基明's avatar
刘基明 committed
94 95 96 97 98 99
        LambdaQueryWrapper<CommentEntity> wrapper = (new LambdaQueryWrapper<CommentEntity>()
                .in(CommentEntity::getThemeId, themeIds))
                .eq(CommentEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED)
                .groupBy(CommentEntity::getThemeId);
        return commentMapper.selectCountByThemeIds(wrapper).stream()
                .collect(Collectors.toMap(TimesCountEntity::getId, TimesCountEntity::getTimes));
刘基明's avatar
刘基明 committed
100
    }
刘基明's avatar
刘基明 committed
101

刘基明's avatar
刘基明 committed
102 103 104 105 106 107
    /**
     * 获取单个帖子的所有评论
     *
     * @param themeId
     * @return
     */
刘基明's avatar
刘基明 committed
108 109
    public List<CommentEntity> selectByThemeId(String themeId) {
        return redisCache.getList(StringUtils.joinWith("_", CACHE_COMMENT_THEMEID, themeId),
张辰's avatar
张辰 committed
110
                60, () -> {
刘基明's avatar
刘基明 committed
111 112 113 114 115 116 117
                    LambdaQueryWrapper<CommentEntity> queryWrapper = new LambdaQueryWrapper<CommentEntity>()
                            .eq(CommentEntity::getThemeId, themeId)
                            .eq(CommentEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
                            .orderByDesc(CommentEntity::getCreateTime);
                    return commentMapper.selectList(queryWrapper);
                }, new TypeReference<List<CommentEntity>>() {
                });
刘基明's avatar
刘基明 committed
118 119 120
    }


刘基明's avatar
刘基明 committed
121
    public List<CommentEntity> queryCommentsByUserId(String userId, String lastId, Integer pageSize) {
刘基明's avatar
刘基明 committed
122 123 124
        LambdaQueryWrapper<CommentEntity> queryWrapper = new LambdaQueryWrapper<CommentEntity>()
                .eq(CommentEntity::getAuthorId, userId)
                .eq(CommentEntity::getCommentType, CommentTypeEnum.THEME.getCode())
刘基明's avatar
刘基明 committed
125 126
                .eq(CommentEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
                .orderByDesc(CommentEntity::getCreateTime);
刘基明's avatar
刘基明 committed
127
        if (StringUtils.isNotEmpty(lastId)) {
刘基明's avatar
刘基明 committed
128
            CommentEntity commentEntity = commentMapper.selectOne(new LambdaQueryWrapper<CommentEntity>()
刘基明's avatar
刘基明 committed
129 130
                    .eq(CommentEntity::getCommentId, lastId));
            if (commentEntity == null) throw new BizException("评论未找到,id:" + lastId);
131
            queryWrapper.lt(CommentEntity::getCreateTime, commentEntity.getCreateTime());
刘基明's avatar
刘基明 committed
132
        }
刘基明's avatar
刘基明 committed
133 134
        if (pageSize != null) {
            queryWrapper.last("limit " + pageSize);
刘基明's avatar
刘基明 committed
135 136 137
        }
        return commentMapper.selectList(queryWrapper);
    }
138

刘基明's avatar
刘基明 committed
139
    //修改举报状态,可修改已删除
140
    public void updateReportStatus(String commentId) {
刘基明's avatar
刘基明 committed
141
        CommentEntity commentEntity = queryByIdIncludeDelete(commentId);
刘基明's avatar
刘基明 committed
142 143
        if (commentEntity == null) {
            throw new BizException("评论未找到,id:" + commentId);
144 145
        }
        commentEntity.setReportStatus(ReportStatusEnum.REPORTED.getCode());
刘基明's avatar
刘基明 committed
146 147 148 149
        commentMapper.updateById(commentEntity);
        //失效缓存
        evictThemeCache(commentEntity.getThemeId());

150
    }
刘基明's avatar
刘基明 committed
151 152 153 154

    //删除评论
    @Transactional
    public void delete(String commentId, String userId) {
刘基明's avatar
刘基明 committed
155
        CommentEntity commentEntity = this.queryByIdIncludeDelete(commentId);
刘基明's avatar
刘基明 committed
156 157
        if (commentEntity == null || !commentEntity.getAuthorId().equals(userId)) {
            throw new BizException("删除评论与用户不匹配,commentId:" + commentId + ",userId:" + userId);
刘基明's avatar
刘基明 committed
158 159 160
        }
        commentEntity.setDeleteTag(DeleteTagEnum.DELETED.getCode());
        commentMapper.updateById(commentEntity);
刘基明's avatar
刘基明 committed
161
        //失效缓存
刘基明's avatar
刘基明 committed
162 163 164 165
        evictThemeCache(commentEntity.getThemeId());
    }

    // 失效关联主题缓存
刘基明's avatar
刘基明 committed
166
    private void evictThemeCache(String themeId) {
167
        // 评论内容
刘基明's avatar
刘基明 committed
168
        redisCache.evict(StringUtils.joinWith("_", CACHE_COMMENT_THEMEID, themeId));
169
        // 主题内容
刘基明's avatar
刘基明 committed
170
        redisCache.evict(StringUtils.joinWith("_", CACHE_THEME_ID, themeId));
171
        // 评论数
刘基明's avatar
刘基明 committed
172
        redisCache.evict(StringUtils.joinWith("_", THEME_COMMENT_COUNT, themeId));
刘基明's avatar
刘基明 committed
173 174
    }

刘基明's avatar
刘基明 committed
175 176
    public List<CommentEntity> queryAll() {
        return commentMapper.selectList(new LambdaQueryWrapper<CommentEntity>()
刘基明's avatar
刘基明 committed
177
                .eq(CommentEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
刘基明's avatar
刘基明 committed
178 179 180
                .orderByAsc(CommentEntity::getCreateTime));
    }

181
    public CommentEntity queryByCommentId(String commentId) {
刘基明's avatar
刘基明 committed
182
        return commentMapper.selectOne(new LambdaQueryWrapper<CommentEntity>().eq(CommentEntity::getCommentId, commentId));
183
    }
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

    public String forwardSyncComment(ForwardThemeReq req, String userId) {
        CommentEntity commentEntity = CommentEntity.builder()
                .themeId(req.getFormerThemeId())
                .authorId(userId)
                .content(req.getContent().get(0).getValue())
                .commentType(CommentTypeEnum.THEME.getCode())
                .build();

        this.insertComment(commentEntity);

        return commentEntity.getCommentId();


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

刘基明's avatar
刘基明 committed
200
    public void queryRecentComments(List<ThemeQo> themes) {
刘基明's avatar
刘基明 committed
201 202
        if (CollectionUtils.isEmpty(themes)) return;

刘基明's avatar
刘基明 committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
        List<String> themeIds = themes.stream().map(ThemeQo::getThemeId).collect(Collectors.toList());

        List<CommentEntity> commentEntities = commentMapper.selectList(new LambdaQueryWrapper<CommentEntity>()
                .in(CommentEntity::getThemeId, themeIds)
                .orderByDesc(CommentEntity::getCreateTime));

        LinkedHashMap<String, List<CommentEntity>> collect = commentEntities.stream().collect(Collectors.groupingBy(CommentEntity::getThemeId, LinkedHashMap::new, Collectors.toList()));
        // 查询用户信息
        List<String> authorIds = new ArrayList<>(commentEntities.stream().map(CommentEntity::getAuthorId).collect(Collectors.toSet()));
        List<UserInfoResp> queryUsersListNew = feignService.getUserList(authorIds);
        Map<String, String> nameMap = queryUsersListNew.stream().collect(Collectors.toMap(UserInfoResp::getUserId, UserInfoResp::getNickName));

        for (ThemeQo theme : themes) {
            if (collect.containsKey(theme.getThemeId())) {
                List<CommentEntity> comments = collect.get(theme.getThemeId());
刘基明's avatar
刘基明 committed
218
                theme.setRecentComments(ConvertUtil.comment2Simple(comments, nameMap));
刘基明's avatar
刘基明 committed
219 220 221 222
            }
        }

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