ThemeService.java 4.71 KB
package com.tanpu.community.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanpu.common.exception.BizException;
import com.tanpu.common.uuid.UuidGenHelper;
import com.tanpu.community.api.enums.DeleteTagEnum;
import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.dao.mapper.community.ThemeMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class ThemeService {

    @Resource
    private ThemeMapper themeMapper;

    @Autowired
    private UuidGenHelper uuidGenHelper;

    public void insertTheme(ThemeEntity themeEntity) {
        themeEntity.setThemeId(uuidGenHelper.getUuidStr());
        themeMapper.insert(themeEntity);
    }

    //根据id返回主题详情
    public ThemeEntity queryByThemeId(String themeId) {
        return themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>().eq(ThemeEntity::getThemeId,themeId));
    }

    //分页倒叙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);
        queryWrapper.orderByDesc(ThemeEntity::getUpdateTime);
        return themeMapper.selectList(queryWrapper);
    }


    /**
     * 根据条件查询主题
     * @param topidId 话题Id
     * @param lastId 查询此主题Id之前的主题
     * @param pageSize 查询数量
     * @return
     */
    public List<ThemeEntity> queryByTopic(String topidId, String lastId,Integer pageSize) {
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getTopicId, topidId);
        if (StringUtils.isNotEmpty(lastId)) {
            ThemeEntity lastEntity = queryByThemeId(lastId);
            if (lastEntity==null) throw new BizException("主题未找到,id:"+lastId);
            queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getUpdateTime());
        }
        if (pageSize!=null){
            queryWrapper.last("limit "+pageSize);
        }
        return themeMapper.selectList(queryWrapper);
    }

    //根据话题查询所有的主题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());
    }


    //关注的主题列表
    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)) {
            ThemeEntity lastEntity = queryByThemeId(lastId);
            if (lastEntity==null) throw new BizException("主题未找到,id:"+lastId);
            queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getUpdateTime());
        }
        queryWrapper.last("limit "+pageSize);

        return themeMapper.selectList(queryWrapper);

    }


    //查询对应话题的发表用户集合
    public Set<String> getPostUserCount(List<String> themeIds) {
        return themeMapper.selectBatchIds(themeIds).stream()
                .map(ThemeEntity::getAuthorId)
                .collect(Collectors.toSet());
    }

    public Integer getForwardCountById(String themeId) {
        return themeMapper.selectCount(new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getThemeId, themeId)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED));
    }

    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));
    }
}