/*
初始数据库应用
*/
package init

import (
	"jyBXCore/entity"
	"log"
	"strings"

	. "app.yhyue.com/moapp/jybase/es"
	"app.yhyue.com/moapp/jybase/mongodb"
	"app.yhyue.com/moapp/jybase/mysql"
	"app.yhyue.com/moapp/jybase/redis"
)

var (
	MainMysql  *mysql.Mysql
	BaseMysql  *mysql.Mysql
	Mgo        mongodb.MongodbSim
	MgoBidding mongodb.MongodbSim //标讯详情等（第一版没用）
)

func MongoDBInit(em *entity.Mongo) {
	//初始化 mongodb
	if em.Main.Address != "" {
		log.Println("--初始化 mongodb--")
		Mgo = mongodb.MongodbSim{
			MongodbAddr: em.Main.Address,
			Size:        em.Main.Size,
			DbName:      em.Main.DbName,
		}
		Mgo.InitPool()
	}
	//
	if em.Bidding.Address != "" {
		log.Println("--初始化 mongodb bidding--")
		MgoBidding = mongodb.MongodbSim{
			MongodbAddr: em.Bidding.Address,
			Size:        em.Bidding.Size,
			DbName:      em.Bidding.DbName,
			UserName:    em.Bidding.UserName,
			Password:    em.Bidding.Password,
		}
		MgoBidding.InitPool()
	}
}

func MysqlInit(mm *entity.Mysql) {
	//初始化 mysql-main
	if mm.Main.Address != "" {
		log.Println("--初始化 mysql--")
		MainMysql = &mysql.Mysql{
			Address:      mm.Main.Address,
			UserName:     mm.Main.UserName,
			PassWord:     mm.Main.Password,
			DBName:       mm.Main.DbName,
			MaxOpenConns: mm.Main.MaxOpenConns,
			MaxIdleConns: mm.Main.MaxIdleConns,
		}
		MainMysql.Init()
	}
	//初始化 mysql-base
	if mm.Base.Address != "" {
		log.Println("--初始化 tidb--")
		BaseMysql = &mysql.Mysql{
			Address:      mm.Base.Address,
			UserName:     mm.Base.UserName,
			PassWord:     mm.Base.Password,
			DBName:       mm.Base.DbName,
			MaxOpenConns: mm.Base.MaxOpenConns,
			MaxIdleConns: mm.Base.MaxIdleConns,
		}
		BaseMysql.Init()
	}
}

func RedisInit(rm *entity.RedisStuct) {
	//初始化 redis
	if len(rm.Addr) > 0 {
		log.Println("--初始化 redis--")
		redis.InitRedisBySize(strings.Join(rm.Addr, ","), 100, 30, 300)
	}
}

func EsInit(es *entity.EsStruct) {
	//初始化 elasticsearch
	if es.Addr != "" {
		log.Println("--初始化 elasticsearch--")
		NewEs(es.Version, es.Addr, es.Size, es.UserName, es.Password)
	}
}
