TopicService.java 3.78 KB
package com.tanpu.community.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.tanpu.common.uuid.UuidGenHelper;
import com.tanpu.community.api.enums.DeleteTagEnum;
import com.tanpu.community.api.enums.StatusEnum;
import com.tanpu.community.dao.entity.community.TopicEntity;
import com.tanpu.community.dao.mapper.community.TopicMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;


@Service
@EnableCaching
public class TopicService {

    @Resource
    private TopicMapper topicMapper;

    @Autowired
    private UuidGenHelper uuidGenHelper;

    public List<TopicEntity> queryAll() {
        return topicMapper.selectList(new LambdaQueryWrapper<TopicEntity>()
                .eq(TopicEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
                .orderByDesc(TopicEntity::getCreateTime));
    }

    public List<TopicEntity> queryByKeyword(String keyword) {
        return topicMapper.selectList(new LambdaQueryWrapper<TopicEntity>()
                .like(TopicEntity::getTopicTitle, keyword)
                .eq(TopicEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
                .orderByDesc(TopicEntity::getCreateTime));
    }

    @Transactional
    public void addTopic(String topicTitle, String userId) {
        TopicEntity entity = TopicEntity.builder()
                .topicId(uuidGenHelper.getUuidStr())
                .topicTitle(topicTitle)
                .isTop(StatusEnum.FALSE.getCode())
                .isConceal(StatusEnum.FALSE.getCode())
                .build();

        topicMapper.insert(entity);
    }


    public void updateTopicToTop(String topicId) {
        TopicEntity topicEntity = new TopicEntity();
        topicEntity.setIsTop(StatusEnum.TRUE.getCode());
        topicMapper.update(topicEntity, new LambdaUpdateWrapper<TopicEntity>()
                .eq(TopicEntity::getId, topicId));
    }

    public void updateTopicNotTop(String topicId) {
        TopicEntity topicEntity = new TopicEntity();
        topicEntity.setIsTop(StatusEnum.FALSE.getCode());
        topicMapper.update(topicEntity, new LambdaUpdateWrapper<TopicEntity>()
                .eq(TopicEntity::getId, topicId));
    }

    public void updateTopicToConceal(String topicId) {
        TopicEntity topicEntity = new TopicEntity();
        topicEntity.setIsConceal(StatusEnum.TRUE.getCode());
        topicMapper.update(topicEntity, new LambdaUpdateWrapper<TopicEntity>()
                .eq(TopicEntity::getId, topicId));
    }

    public void updateTopicNotConceal(String topicId) {
        TopicEntity topicEntity = new TopicEntity();
        topicEntity.setIsConceal(StatusEnum.FALSE.getCode());
        topicMapper.update(topicEntity, new LambdaUpdateWrapper<TopicEntity>()
                .eq(TopicEntity::getId, topicId));
    }

    public TopicEntity queryById(String topicId) {
        return topicMapper.selectOne(new LambdaQueryWrapper<TopicEntity>().eq(TopicEntity::getTopicId, topicId));
    }

    public List<TopicEntity> queryByIds(List<String> topicIds) {
        if (CollectionUtils.isEmpty(topicIds)) {
            return Collections.emptyList();
        }
        return topicMapper.selectList(new LambdaQueryWrapper<TopicEntity>().in(TopicEntity::getTopicId, topicIds));
    }

    public TopicEntity queryByTitile(String topicTitle) {
        return topicMapper.selectOne(new LambdaQueryWrapper<TopicEntity>().eq(TopicEntity::getTopicTitle, topicTitle));
    }
}