• 张辰's avatar
    cache · 9a58c57f
    张辰 authored
    9a58c57f
CommentService.java 5.11 KB
package com.tanpu.community.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.tanpu.common.exception.BizException;
import com.tanpu.common.redis.RedisKeyHelper;
import com.tanpu.common.uuid.UuidGenHelper;
import com.tanpu.community.api.enums.CommentTypeEnum;
import com.tanpu.community.api.enums.DeleteTagEnum;
import com.tanpu.community.api.enums.ReportStatusEnum;
import com.tanpu.community.cache.RedisCache;
import com.tanpu.community.dao.entity.community.CommentEntity;
import com.tanpu.community.dao.mapper.community.CommentMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.tanpu.community.api.constants.RedisKeyConstant.CACHE_COMMENT_THEMEID;
import static com.tanpu.community.api.constants.RedisKeyConstant.CAHCE_COMMENT_ID;

@Service
public class CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Autowired
    private UuidGenHelper uuidGenHelper;

    @Autowired
    private RedisCache redisCache;

    @Transactional
    public void insertComment(CommentEntity commentEntity) {
        commentEntity.setCommentId(uuidGenHelper.getUuidStr());
        commentMapper.insert(commentEntity);
    }


    public CommentEntity queryByCommentId(String commmentId) {
        return commentMapper.selectOne(new LambdaQueryWrapper<CommentEntity>()
                .eq(CommentEntity::getCommentId, commmentId));
    }

    //统计主题集合的评论量
    public Integer getCommentCountByThemeIds(List<String> themeIds) {
        if (CollectionUtils.isEmpty(themeIds)){
            return 0;
        }
        return  commentMapper.selectList((new LambdaQueryWrapper<CommentEntity>()
                .in(CommentEntity::getThemeId, themeIds)))
                .size();
    }

    //统计主题集合的评论量
    public Integer getCommentCountByThemeId(String themeId) {
        return  commentMapper.selectList((new LambdaQueryWrapper<CommentEntity>()
                .eq(CommentEntity::getThemeId, themeId)))
                .size();
    }

    public Set<String> getCommentUserCount(List<String> themeIds) {
        return commentMapper.selectList((new LambdaQueryWrapper<CommentEntity>()
                .in(CommentEntity::getThemeId, themeIds)))
                .stream().map(CommentEntity::getAuthorId).collect(Collectors.toSet());
    }

    public List<CommentEntity> selectByThemeIdAndParentId(String themeId, String parentId) {
        return redisCache.getList(StringUtils.joinWith("_", CACHE_COMMENT_THEMEID, themeId, parentId),
                60, () -> {
            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>>() {});
    }


    public List<CommentEntity> queryThemesByUserId(String userId) {
        return commentMapper.selectList(new LambdaQueryWrapper<CommentEntity>()
                .eq(CommentEntity::getAuthorId,userId)
                .eq(CommentEntity::getCommentType, CommentTypeEnum.THEME.getCode())
                .eq(CommentEntity::getDeleteTag,DeleteTagEnum.NOT_DELETED.getCode()));
    }


    public List<CommentEntity> queryThemesByUserId(String userId, String lastId,Integer pageSize) {
        LambdaQueryWrapper<CommentEntity> queryWrapper = new LambdaQueryWrapper<CommentEntity>()
                .eq(CommentEntity::getAuthorId, userId)
                .eq(CommentEntity::getCommentType, CommentTypeEnum.THEME.getCode())
                .eq(CommentEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
        if (StringUtils.isNotEmpty(lastId)) {
            CommentEntity commentEntity = commentMapper.selectOne(new LambdaQueryWrapper<CommentEntity>()
                    .eq(CommentEntity::getCommentId,lastId));
            if (commentEntity==null) throw new BizException("主题未找到,id:"+lastId);
            queryWrapper.lt(CommentEntity::getUpdateTime, commentEntity.getCreateTime());
        }
        if (pageSize!=null){
            queryWrapper.last("limit "+pageSize);
        }
        return commentMapper.selectList(queryWrapper);
    }

    public void updateReportStatus(String commentId) {
        CommentEntity commentEntity = queryByCommentId(commentId);
        if (commentEntity==null){
            throw new BizException("评论未找到,id:"+commentId);
        }
        commentEntity.setReportStatus(ReportStatusEnum.REPORTED.getCode());
        commentMapper.updateById(commentEntity);
        redisCache.evict(StringUtils.joinWith("_", CAHCE_COMMENT_ID, commentId));
    }
}