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

import com.apobates.forum.core.dao.BoardConfigDao;
import com.apobates.forum.core.entity.BoardConfig;
import com.apobates.forum.core.service.BoardConfigService;
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 BoardConfigServiceImpl implements BoardConfigService{
    @Autowired
    private BoardConfigDao boardConfigDao;
    private final static Logger logger = LoggerFactory.getLogger(BoardConfigServiceImpl.class);
    
    @Cacheable(value="boardCache", key="'board_config_'+#boardId", unless="#result == null")
    @Override
    public Optional<BoardConfig> getByBoardId(long boardId) {
        return boardConfigDao.findOneByBoard(boardId);
    }
    
    @Override
    public Optional<Boolean> create(long boardId, BoardConfig config) {
        try {
            config.setBoardId(boardId);
            boardConfigDao.save(config);
            return config.getId() > 0 ? Optional.of(true) : Optional.empty(); //("版块配置文件创建失败");
        } catch (Exception e) {
            if(logger.isDebugEnabled()){
                logger.debug("版块配置文件创建失败", e);
            }
        }
        return Optional.empty();
    }
}