package com.apobates.forum.core.impl.service;

import com.apobates.forum.core.dao.TopicConfigDao;
import com.apobates.forum.core.entity.TopicConfig;
import com.apobates.forum.core.service.TopicConfigService;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 *
 * @author xiaofanku
 * @since 20200513
 */
@Service
public class TopicConfigServiceImpl implements TopicConfigService{
    @Autowired
    private TopicConfigDao topicConfigDao;
    private final static Logger logger = LoggerFactory.getLogger(TopicConfigServiceImpl.class);
    
    @Cacheable(value="topicCache", key="'topic_config_'+#topicId", unless="#result == null")
    @Override
    public Optional<TopicConfig> getByTopicId(long topicId) {
        return topicConfigDao.findOneByTopic(topicId);
    }
    
    @Override
    public Optional<Boolean> create(long topicId, TopicConfig config) {
        try {
            config.setTopicId(topicId);
            topicConfigDao.save(config);
            return config.getId() > 0 ? Optional.of(true) : Optional.empty();
        } catch (Exception e) {
            if(logger.isDebugEnabled()){
                logger.debug("创建话题配置文件失败", e);
            }
        }
        return Optional.empty();
    }
}