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

import (
	"strings"

	"github.com/zeromicro/go-zero/core/logx"

	"jyBXBuyer/entity"

	. "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
	Mgo       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()
	}
}

//
func MysqlInit(mm *entity.Mysql) {
	//初始化 mysql-main
	if mm.Main.Address != "" {
		logx.Info("--初始化 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()
	}
}

//
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)
	}
}
