package db

import (
	"baishuihu/conf"
	"fmt"

	"go.mongodb.org/mongo-driver/mongo"
	"infish.cn/comm"
)

type MongoDB struct {
	Client   *mongo.Client
	Database *mongo.Database
}

var GMongoDb *MongoDB

func (db *MongoDB) GetCollection(name string) *mongo.Collection {
	return db.Database.Collection(name)
}

// 从config-service中获取配置，创建
func NewConfigMongoDB(bus *comm.NatsBus, config *conf.AppConf) *MongoDB {

	fmt.Println("db-config=>", config.Configer.Mongo)
	MongoInst, err := bus.NewMongoDBFromConfigDev(config.Configer.Mongo)
	if err != nil {
		panic(err)
	}
	GMongoDb = &MongoDB{Client: MongoInst.Client, Database: MongoInst.Database}

	return GMongoDb
}
