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

import (
	"strings"

	elastic "app.yhyue.com/moapp/jybase/es"
	P "app.yhyue.com/moapp/jybase/mapping"
	"app.yhyue.com/moapp/jypkg/compatible"
	"app.yhyue.com/moapp/jypkg/middleground"
	"github.com/zeromicro/go-zero/core/logx"

	"jyBXSubscribe/entity"

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

var (
	MainMysql        *mysql.Mysql
	BaseServiceMysql *mysql.Mysql
	Mgo              mongodb.MongodbSim
	MgoBidding       mongodb.MongodbSim
	GlobalCommonData *mysql.Mysql
)
var Middleground *middleground.Middleground
var Compatible *compatible.Compatible

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

//
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()
	}
	//初始化 mysql-BaseService
	if mm.BaseService.Address != "" {
		logx.Info("--初始化 推送 mysql--")
		BaseServiceMysql = &mysql.Mysql{
			Address:      mm.BaseService.Address,
			UserName:     mm.BaseService.UserName,
			PassWord:     mm.BaseService.Password,
			DBName:       mm.BaseService.DbName,
			MaxOpenConns: mm.BaseService.MaxOpenConns,
			MaxIdleConns: mm.BaseService.MaxIdleConns,
		}
		BaseServiceMysql.Init()
	}
	//初始化 mysql-GlobalCommonData
	if mm.GlobalCommonData.Address != "" {
		logx.Info("--初始化 推送 mysql--")
		GlobalCommonData = &mysql.Mysql{
			Address:      mm.GlobalCommonData.Address,
			UserName:     mm.GlobalCommonData.UserName,
			PassWord:     mm.GlobalCommonData.Password,
			DBName:       mm.GlobalCommonData.DbName,
			MaxOpenConns: mm.GlobalCommonData.MaxOpenConns,
			MaxIdleConns: mm.GlobalCommonData.MaxIdleConns,
		}
		GlobalCommonData.Init()
	}
	P.BidCodeMapping.Init(BaseServiceMysql)
}

//
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--")
		elastic.NewEs(es.Version, es.Addr, es.Size, es.UserName, es.Password)
	}
}
