package cn.com.capcloud.core.system.configuration;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class MongoDBConfig {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(MongoDBConfig.class);
    @Value("${rzth.mongodb.isCreateMongoDBConnect:N}")
    private  String isCreateMongoDBConnect;

    @Resource
    private MongoTemplate mongoTemplate;

    @PostConstruct
    public void testMongoDbClient() {
        if(!isCreateMongoDBConnect.equals("Y")){
            logger.info("不启用MongoDB配置");
        }else{
            logger.info("启用MongoDB配置");
            try {
                // 查询所有的集合
                List<String> collectionNames = new ArrayList<>(mongoTemplate.getCollectionNames());
                logger.info("MongoDB 连接成功！");
            } catch (Exception e) {
                logger.warn("MongoDB 连接失败！（如果不启用MongoDB，请在配置文件配置mongodb.isCreateConnect:N）");
                logger.error("MongoDB 连接失败，原因是：" + e.getMessage());
            }
        }
    }

}
