CommentService.java 3.02 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.tanpu.common.uuid.UuidGenHelper;
刘基明's avatar
刘基明 committed
5
import com.tanpu.community.api.enums.CommentTypeEnum;
刘基明's avatar
刘基明 committed
6 7
import com.tanpu.community.api.enums.DeleteTagEnum;
import com.tanpu.community.api.enums.TopicStatusEnum;
刘基明's avatar
刘基明 committed
8 9
import com.tanpu.community.dao.entity.community.CommentEntity;
import com.tanpu.community.dao.mapper.community.CommentMapper;
刘基明's avatar
刘基明 committed
10
import org.apache.commons.collections4.CollectionUtils;
刘基明's avatar
刘基明 committed
11
import org.springframework.beans.factory.annotation.Autowired;
刘基明's avatar
刘基明 committed
12
import org.springframework.stereotype.Service;
刘基明's avatar
刘基明 committed
13
import org.springframework.transaction.annotation.Transactional;
刘基明's avatar
刘基明 committed
14 15 16

import javax.annotation.Resource;
import java.util.List;
刘基明's avatar
刘基明 committed
17 18
import java.util.Set;
import java.util.stream.Collectors;
刘基明's avatar
刘基明 committed
19 20 21 22 23 24 25

@Service
public class CommentService {

    @Resource
    private CommentMapper commentMapper;

刘基明's avatar
刘基明 committed
26 27 28
    @Autowired
    private UuidGenHelper uuidGenHelper;

刘基明's avatar
刘基明 committed
29
    @Transactional
刘基明's avatar
刘基明 committed
30
    public void insertComment(CommentEntity commentEntity) {
刘基明's avatar
刘基明 committed
31
        commentEntity.setCommentId(uuidGenHelper.getUuidStr());
刘基明's avatar
刘基明 committed
32 33 34 35
        commentMapper.insert(commentEntity);
    }


刘基明's avatar
刘基明 committed
36 37
    public List<CommentEntity> selectByUserId(String userId) {
        return commentMapper.selectList(new LambdaQueryWrapper<CommentEntity>().eq(CommentEntity::getAuthorId, userId));
刘基明's avatar
刘基明 committed
38
    }
刘基明's avatar
刘基明 committed
39 40

    //统计主题集合的评论量
刘基明's avatar
刘基明 committed
41
    public Integer getCommentCountByThemeIds(List<String> themeIds) {
刘基明's avatar
刘基明 committed
42 43 44
        if (CollectionUtils.isEmpty(themeIds)){
            return 0;
        }
刘基明's avatar
刘基明 committed
45
        return  commentMapper.selectList((new LambdaQueryWrapper<CommentEntity>()
刘基明's avatar
刘基明 committed
46
                .in(CommentEntity::getThemeId, themeIds)))
刘基明's avatar
刘基明 committed
47 48
                .size();
    }
刘基明's avatar
刘基明 committed
49

刘基明's avatar
刘基明 committed
50
    public Set<String> getCommentUserCount(List<String> themeIds) {
刘基明's avatar
刘基明 committed
51
        return commentMapper.selectList((new LambdaQueryWrapper<CommentEntity>()
刘基明's avatar
刘基明 committed
52
                .in(CommentEntity::getThemeId, themeIds)))
刘基明's avatar
刘基明 committed
53 54
                .stream().map(CommentEntity::getAuthorId).collect(Collectors.toSet());
    }
刘基明's avatar
刘基明 committed
55

刘基明's avatar
刘基明 committed
56 57 58
    public List<CommentEntity> selectByThemeIdAndParentId(String themeId, String parentId) {
        LambdaQueryWrapper<CommentEntity> queryWrapper = new LambdaQueryWrapper<CommentEntity>()
                .eq(CommentEntity::getThemeId, themeId)
刘基明's avatar
刘基明 committed
59 60
                .eq(CommentEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
                .eq(CommentEntity::getIsBlock, TopicStatusEnum.FALSE.getCode())
刘基明's avatar
刘基明 committed
61 62 63 64 65 66 67
                .orderByDesc(CommentEntity::getCreateTime);
        if (parentId == null) {
            queryWrapper.isNull(CommentEntity::getParentId);
        } else {
            queryWrapper.eq(CommentEntity::getParentId, parentId);
        }
        return commentMapper.selectList(queryWrapper);
刘基明's avatar
刘基明 committed
68 69 70
    }


刘基明's avatar
刘基明 committed
71 72 73 74 75 76
    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()));
    }
刘基明's avatar
刘基明 committed
77
}