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

import (
	"jyBXBase/entity"
	"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"
	"github.com/zeromicro/go-zero/core/logx"
)

var (
	MainMysql        *mysql.Mysql
	BaseServiceMysql *mysql.Mysql
	Mgo              mongodb.MongodbSim
	MgoEnt           mongodb.MongodbSim
	MgoBidding       mongodb.MongodbSim
)

//
func MongoDBInit(em *entity.Mongo) {
	//初始化 mongodb
	if em.Main.Address != "" {
		logx.Info("--初始化 mongodb--")
		Mgo = mongodb.MongodbSim{
			MongodbAddr: em.Main.Address,
			Size:        em.Main.Size,
			DbName:      em.Main.DbName,
		}
		Mgo.InitPool()
	}
	//
	if em.Bidding.Address != "" {
		logx.Info("--初始化 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()
	}
	if em.Ent.Address != "" {
		logx.Info("--初始化 mongodb ent--")
		MgoEnt = mongodb.MongodbSim{
			MongodbAddr: em.Ent.Address,
			Size:        em.Ent.Size,
			DbName:      em.Ent.DbName,
			UserName:    em.Ent.UserName,
			Password:    em.Ent.Password,
		}
		MgoEnt.InitPool()
	}
}

//
func MysqlInit(em *entity.Mysql) {
	//初始化 mysql-main
	if em.Main.Address != "" {
		logx.Info("--初始化 main mysql--")
		MainMysql = &mysql.Mysql{
			Address:      em.Main.Address,
			UserName:     em.Main.UserName,
			PassWord:     em.Main.Password,
			DBName:       em.Main.DbName,
			MaxOpenConns: em.Main.MaxOpenConns,
			MaxIdleConns: em.Main.MaxIdleConns,
		}
		MainMysql.Init()
	}
	//初始化 mysql-BaseService

	//初始化 mysql-main
	if em.Main.Address != "" {
		logx.Info("--初始化 main mysql--")
		MainMysql = &mysql.Mysql{
			Address:      em.Main.Address,
			UserName:     em.Main.UserName,
			PassWord:     em.Main.Password,
			DBName:       em.Main.DbName,
			MaxOpenConns: em.Main.MaxOpenConns,
			MaxIdleConns: em.Main.MaxIdleConns,
		}
		MainMysql.Init()
	}
	//初始化 mysql-main
	if em.Main.Address != "" {
		logx.Info("--初始化 main mysql--")
		MainMysql = &mysql.Mysql{
			Address:      em.Main.Address,
			UserName:     em.Main.UserName,
			PassWord:     em.Main.Password,
			DBName:       em.Main.DbName,
			MaxOpenConns: em.Main.MaxOpenConns,
			MaxIdleConns: em.Main.MaxIdleConns,
		}
		MainMysql.Init()
	}
	if em.BaseService.Address != "" {
		logx.Info("--初始化 BaseServiceMysql tidb--")
		BaseServiceMysql = &mysql.Mysql{
			Address:      em.BaseService.Address,
			UserName:     em.BaseService.UserName,
			PassWord:     em.BaseService.Password,
			DBName:       em.BaseService.DbName,
			MaxOpenConns: em.BaseService.MaxOpenConns,
			MaxIdleConns: em.BaseService.MaxIdleConns,
		}
		BaseServiceMysql.Init()
	}
}

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

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