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

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

@Service
public class CommentService {

    @Resource
    private CommentMapper commentMapper;

刘基明's avatar
刘基明 committed
28 29 30
    @Autowired
    private UuidGenHelper uuidGenHelper;

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


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

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

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

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


刘基明's avatar
刘基明 committed
73 74 75 76 77 78
    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
79 80 81 82 83 84 85 86


    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)) {
刘基明's avatar
刘基明 committed
87 88
            CommentEntity commentEntity = commentMapper.selectOne(new LambdaQueryWrapper<CommentEntity>()
                    .eq(CommentEntity::getCommentId,lastId));
刘基明's avatar
刘基明 committed
89 90 91 92 93 94 95 96
            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);
    }
刘基明's avatar
刘基明 committed
97
}