package db

import (
	"database/sql"
	"log"

	"app.yhyue.com/moapp/jybase/mongodb"
	"app.yhyue.com/moapp/jybase/mysql"
	"app.yhyue.com/moapp/jybase/redis"
	"app.yhyue.com/moapp/jypkg/ent/util"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gcfg"
	"github.com/gogf/gf/v2/os/gctx"
)

type Mysql struct {
	Address      string  //数据库地址:端口
	UserName     string  //用户名
	PassWord     string  //密码
	DBName       string  //数据库名
	DB           *sql.DB //数据库连接池对象
	MaxOpenConns int     //用于设置最大打开的连接数，默认值为0表示不限制。
	MaxIdleConns int     //用于设置闲置的连接数。
}
type MgoConf struct {
	Address         string
	Size            int
	DbName          string
	ReplSet         string
	UserName        string
	Password        string
	Collection      string
	Collection_back string
}

var GateWatMySql *mysql.Mysql
var JyMysql *mysql.Mysql
var MgoLog mongodb.MongodbSim
var MgoJy mongodb.MongodbSim

// InitDatabases 初始化数据库
func InitDatabases() {
	initDbCtx := gctx.New()
	// 初始化redis数据库
	redisAddr := g.Cfg().MustGet(initDbCtx, "databases.redis", "session=127.0.0.1:6379,other=127.0.0.1:6379").String()
	redis.InitRedis(redisAddr)
	log.Printf("初始化redis完成 %s\n", redisAddr)
	// 初始化mysql数据库
	var mysqlConf Mysql
	err := gcfg.Instance().MustGet(initDbCtx, "databases.mysql").Scan(&mysqlConf)
	if err != nil {
		log.Printf("mysql配置异常 %v\n", err)
		log.Fatal(err)
	}
	GateWatMySql = &mysql.Mysql{
		Address:      mysqlConf.Address,
		UserName:     mysqlConf.UserName,
		PassWord:     mysqlConf.PassWord,
		DBName:       mysqlConf.DBName,
		MaxOpenConns: mysqlConf.MaxOpenConns,
		MaxIdleConns: mysqlConf.MaxIdleConns,
	}
	GateWatMySql.Init()
	log.Printf("初始化Mysql完成 %+v\n", mysqlConf)
	//初始化jymysql数据库
	var jyMysqlConf Mysql
	err2 := gcfg.Instance().MustGet(initDbCtx, "databases.mysqlJy").Scan(&jyMysqlConf)
	if err2 != nil {
		log.Printf("jy mysql配置异常 %v\n", err)
		log.Fatal(err)
	}
	JyMysql = &mysql.Mysql{
		Address:      jyMysqlConf.Address,
		UserName:     jyMysqlConf.UserName,
		PassWord:     jyMysqlConf.PassWord,
		DBName:       jyMysqlConf.DBName,
		MaxOpenConns: jyMysqlConf.MaxOpenConns,
		MaxIdleConns: jyMysqlConf.MaxIdleConns,
	}
	JyMysql.Init()
	util.Mysql = JyMysql
	log.Printf("初始化jy Mysql完成 %+v\n", mysqlConf)
	// 初始化mongodb
	var mgoConf MgoConf
	if err := gcfg.Instance().MustGet(initDbCtx, "databases.mogLog").Scan(&mgoConf); err == nil {
		MgoLog = mongodb.MongodbSim{
			MongodbAddr: mgoConf.Address,
			Size:        mgoConf.Size,
			DbName:      mgoConf.DbName,
			ReplSet:     mgoConf.ReplSet,
			UserName:    mgoConf.UserName,
			Password:    mgoConf.Password,
		}
		MgoLog.InitPool()
		log.Printf("初始化 mongodb log 完成 %+v\n", mgoConf)
	}
	//初始化 jy mongodb
	var jyMgoConf MgoConf
	if err := gcfg.Instance().MustGet(initDbCtx, "databases.mogJy").Scan(&jyMgoConf); err == nil {
		MgoJy = mongodb.MongodbSim{
			MongodbAddr: jyMgoConf.Address,
			Size:        jyMgoConf.Size,
			DbName:      jyMgoConf.DbName,
			ReplSet:     jyMgoConf.ReplSet,
		}
		MgoJy.InitPool()
		log.Printf("初始化 mongodb log 完成 %+v\n", jyMgoConf)
	}
}
