CommentService.java 4.29 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
import com.tanpu.community.api.enums.DeleteTagEnum;
刘基明'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.apache.commons.lang3.StringUtils;
刘基明's avatar
刘基明 committed
12
import org.springframework.beans.factory.annotation.Autowired;
刘基明's avatar
刘基明 committed
13
import org.springframework.stereotype.Service;
刘基明's avatar
刘基明 committed
14
import org.springframework.transaction.annotation.Transactional;
刘基明's avatar
刘基明 committed
15 16 17

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

@Service
public class CommentService {

    @Resource
    private CommentMapper commentMapper;

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

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


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

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

刘基明's avatar
刘基明 committed
51 52 53 54 55 56 57
    //统计主题集合的评论量
    public Integer getCommentCountByThemeId(String themeId) {
        return  commentMapper.selectList((new LambdaQueryWrapper<CommentEntity>()
                .eq(CommentEntity::getThemeId, themeId)))
                .size();
    }

刘基明's avatar
刘基明 committed
58
    public Set<String> getCommentUserCount(List<String> themeIds) {
刘基明's avatar
刘基明 committed
59
        return commentMapper.selectList((new LambdaQueryWrapper<CommentEntity>()
刘基明's avatar
刘基明 committed
60
                .in(CommentEntity::getThemeId, themeIds)))
刘基明's avatar
刘基明 committed
61 62
                .stream().map(CommentEntity::getAuthorId).collect(Collectors.toSet());
    }
刘基明's avatar
刘基明 committed
63

刘基明's avatar
刘基明 committed
64 65 66
    public List<CommentEntity> selectByThemeIdAndParentId(String themeId, String parentId) {
        LambdaQueryWrapper<CommentEntity> queryWrapper = new LambdaQueryWrapper<CommentEntity>()
                .eq(CommentEntity::getThemeId, themeId)
刘基明's avatar
刘基明 committed
67
                .eq(CommentEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
刘基明's avatar
刘基明 committed
68
                .orderByDesc(CommentEntity::getCreateTime);
刘基明's avatar
刘基明 committed
69 70 71 72 73 74
        //二级评论,暂未开放,注意“”与null的区别
//        if (parentId == null) {
//            queryWrapper.isNull(CommentEntity::getParentId);
//        } else {
//            queryWrapper.eq(CommentEntity::getParentId, parentId);
//        }
刘基明's avatar
刘基明 committed
75
        return commentMapper.selectList(queryWrapper);
刘基明's avatar
刘基明 committed
76 77 78
    }


刘基明's avatar
刘基明 committed
79 80 81 82 83 84
    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
85 86 87 88 89 90 91 92


    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
93 94
            CommentEntity commentEntity = commentMapper.selectOne(new LambdaQueryWrapper<CommentEntity>()
                    .eq(CommentEntity::getCommentId,lastId));
刘基明's avatar
刘基明 committed
95 96 97 98 99 100 101 102
            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
103
}