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

刘基明's avatar
刘基明 committed
3
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.DeleteTagEnum;
刘基明's avatar
刘基明 committed
7
import com.tanpu.community.dao.entity.community.ThemeEntity;
刘基明's avatar
刘基明 committed
8
import com.tanpu.community.dao.mapper.community.ThemeMapper;
刘基明's avatar
刘基明 committed
9
import org.apache.commons.lang3.StringUtils;
刘基明's avatar
刘基明 committed
10
import org.springframework.beans.factory.annotation.Autowired;
刘基明's avatar
刘基明 committed
11 12
import org.springframework.stereotype.Service;

刘基明's avatar
刘基明 committed
13 14
import javax.annotation.Resource;
import java.util.List;
刘基明's avatar
刘基明 committed
15 16
import java.util.Set;
import java.util.stream.Collectors;
刘基明's avatar
刘基明 committed
17

刘基明's avatar
刘基明 committed
18 19 20
@Service
public class ThemeService {

刘基明's avatar
刘基明 committed
21
    @Resource
刘基明's avatar
刘基明 committed
22
    private ThemeMapper themeMapper;
刘基明's avatar
刘基明 committed
23

刘基明's avatar
刘基明 committed
24 25 26
    @Autowired
    private UuidGenHelper uuidGenHelper;

刘基明's avatar
刘基明 committed
27
    public void insertTheme(ThemeEntity themeEntity) {
刘基明's avatar
刘基明 committed
28
        themeEntity.setThemeId(uuidGenHelper.getUuidStr());
刘基明's avatar
刘基明 committed
29 30 31 32
        themeMapper.insert(themeEntity);
    }

    //根据id返回主题详情
刘基明's avatar
刘基明 committed
33 34
    public ThemeEntity queryByThemeId(String themeId) {
        return themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>().eq(ThemeEntity::getThemeId,themeId));
刘基明's avatar
刘基明 committed
35 36
    }

刘基明's avatar
刘基明 committed
37 38 39 40 41 42 43 44 45 46
    //分页倒叙lastId之前的主题
    public List<ThemeEntity> selectAll(String lastId,Integer pageSize) {
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .orderByDesc(ThemeEntity::getUpdateTime);
        if (StringUtils.isNotEmpty(lastId)) {
            ThemeEntity lastEntity = queryByThemeId(lastId);
            if (lastEntity==null) throw new BizException("主题未找到,id:"+lastId);
            queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getUpdateTime());
        }
        queryWrapper.last("limit "+pageSize);
刘基明's avatar
刘基明 committed
47
        queryWrapper.orderByDesc(ThemeEntity::getId);
刘基明's avatar
刘基明 committed
48 49 50 51
        return themeMapper.selectList(queryWrapper);
    }


刘基明's avatar
刘基明 committed
52 53 54 55 56 57 58
    /**
     * 根据条件查询主题
     * @param topidId 话题Id
     * @param lastId 查询此主题Id之前的主题
     * @param pageSize 查询数量
     * @return
     */
刘基明's avatar
刘基明 committed
59 60 61 62
    public List<ThemeEntity> queryByTopic(String topidId, String lastId,Integer pageSize) {
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getTopicId, topidId);
        if (StringUtils.isNotEmpty(lastId)) {
刘基明's avatar
刘基明 committed
63
            ThemeEntity lastEntity = queryByThemeId(lastId);
刘基明's avatar
刘基明 committed
64 65 66 67 68 69
            if (lastEntity==null) throw new BizException("主题未找到,id:"+lastId);
            queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getUpdateTime());
        }
        if (pageSize!=null){
            queryWrapper.last("limit "+pageSize);
        }
刘基明's avatar
刘基明 committed
70
        queryWrapper.orderByDesc(ThemeEntity::getId);
刘基明's avatar
刘基明 committed
71
        return themeMapper.selectList(queryWrapper);
刘基明's avatar
刘基明 committed
72
    }
刘基明's avatar
刘基明 committed
73

刘基明's avatar
刘基明 committed
74 75 76 77 78 79 80 81
    //根据话题查询所有的主题Id
    public List<String> queryThemeIdsByTopic(String topidId) {
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getTopicId, topidId);
        queryWrapper.select(ThemeEntity::getThemeId);
        return themeMapper.selectList(queryWrapper).stream().map(ThemeEntity::getThemeId).collect(Collectors.toList());
    }

刘基明's avatar
刘基明 committed
82 83 84 85 86 87 88

    //关注的主题列表
    public List<ThemeEntity> queryByFans(List<String> fansList, String lastId,Integer pageSize) {
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .in(ThemeEntity::getAuthorId, fansList)
                .orderByDesc(ThemeEntity::getUpdateTime);
        if (StringUtils.isNotEmpty(lastId)) {
刘基明's avatar
刘基明 committed
89
            ThemeEntity lastEntity = queryByThemeId(lastId);
刘基明's avatar
刘基明 committed
90 91 92 93 94 95
            if (lastEntity==null) throw new BizException("主题未找到,id:"+lastId);
            queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getUpdateTime());
        }
        queryWrapper.last("limit "+pageSize);

        return themeMapper.selectList(queryWrapper);
刘基明's avatar
刘基明 committed
96 97 98

    }

刘基明's avatar
刘基明 committed
99 100

    //查询对应话题的发表用户集合
刘基明's avatar
刘基明 committed
101
    public Set<String> getPostUserCount(List<String> themeIds) {
刘基明's avatar
刘基明 committed
102 103 104 105
        return themeMapper.selectBatchIds(themeIds).stream()
                .map(ThemeEntity::getAuthorId)
                .collect(Collectors.toSet());
    }
刘基明's avatar
刘基明 committed
106 107 108

    public Integer getForwardCountById(String themeId) {
        return themeMapper.selectCount(new LambdaQueryWrapper<ThemeEntity>()
刘基明's avatar
刘基明 committed
109
                .eq(ThemeEntity::getThemeId, themeId)
刘基明's avatar
刘基明 committed
110 111
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED));
    }
刘基明's avatar
刘基明 committed
112 113 114 115 116 117 118

    public Integer getForwardCountByUser(String themeId,String userId) {
        return themeMapper.selectCount(new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getFormerThemeId, themeId)
                .eq(ThemeEntity::getAuthorId, userId)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED));
    }
刘基明's avatar
刘基明 committed
119 120 121 122 123 124 125 126 127

    public void deleteById(String themeId) {
        ThemeEntity themeEntity = themeMapper.selectById(themeId);
        if (themeEntity==null){
            throw new BizException("主题未找到,id:"+themeId);
        }
        themeEntity.setDeleteTag(DeleteTagEnum.DELETED.getCode());
        themeMapper.updateById(themeEntity);
    }
刘基明's avatar
刘基明 committed
128
}