package models

import (
	"eta/eta_task/models/data_manage"
	"eta/eta_task/models/data_manage/future_good"
	"eta/eta_task/utils"
	_ "github.com/go-sql-driver/mysql"
	"time"

	"github.com/beego/beego/v2/client/orm"
)

func init() {

	_ = orm.RegisterDataBase("default", "mysql", utils.MYSQL_URL)
	orm.SetMaxIdleConns("default", 50)
	orm.SetMaxOpenConns("default", 100)

	db, _ := orm.GetDB("default")
	db.SetConnMaxLifetime(10 * time.Minute)

	if utils.MYSQL_URL_RDDP != "" {
		_ = orm.RegisterDataBase("rddp", "mysql", utils.MYSQL_URL_RDDP)
		orm.SetMaxIdleConns("rddp", 50)
		orm.SetMaxOpenConns("rddp", 100)

		report_db, _ := orm.GetDB("rddp")
		report_db.SetConnMaxLifetime(10 * time.Minute)
	}

	if utils.MYSQL_URL_EDB != "" {
		_ = orm.RegisterDataBase("edb", "mysql", utils.MYSQL_URL_EDB)
		orm.SetMaxIdleConns("edb", 50)
		orm.SetMaxOpenConns("edb", 100)

		edb_db, _ := orm.GetDB("edb")
		edb_db.SetConnMaxLifetime(10 * time.Minute)
	}

	if utils.MYSQL_URL_DATA != "" {
		_ = orm.RegisterDataBase("data", "mysql", utils.MYSQL_URL_DATA)
		orm.SetMaxIdleConns("data", 50)
		orm.SetMaxOpenConns("data", 100)

		data_db, _ := orm.GetDB("data")
		data_db.SetConnMaxLifetime(10 * time.Minute)

	}

	if utils.MYSQL_URL_GL != "" {
		_ = orm.RegisterDataBase("gl", "mysql", utils.MYSQL_URL_GL)
		orm.SetMaxIdleConns("gl", 50)
		orm.SetMaxOpenConns("gl", 100)

		gl, _ := orm.GetDB("gl")
		gl.SetConnMaxLifetime(10 * time.Minute)
	}

	if utils.MYSQL_URL_ETA != "" {
		_ = orm.RegisterDataBase("eta", "mysql", utils.MYSQL_URL_ETA)
		orm.SetMaxIdleConns("eta", 50)
		orm.SetMaxOpenConns("eta", 100)

		etaDb, _ := orm.GetDB("eta")
		etaDb.SetConnMaxLifetime(10 * time.Minute)
	}

	orm.Debug = true
	orm.DebugLog = orm.NewLog(utils.Binlog)

	// 注册Edb指标 数据表
	initEdbDataTable()

	// 注册商品期货 相关数据表
	initFutureGoodEdbDataTable()

	//注册持仓分析 数据表
	initTradePositionTop()

	// 智能研报数据表
	initSmartReport()

	// 初始化部分数据表变量(直接init会有顺序问题=_=!)
	data_manage.InitEdbSourceVar()
}

// initEdbDataTable 注册Edb指标 数据表
func initEdbDataTable() {
	//注册对象
	orm.RegisterModel(
		new(data_manage.EdbDataWind),
		new(data_manage.EdbDataThs),
		new(data_manage.EdbDataPb),
		new(data_manage.BaseFromSmm),
		new(data_manage.BaseFromSmmIndex),
		new(data_manage.BaseFromSmmData),
		new(data_manage.EdbDataYs),
		new(data_manage.EdbInfoCalculateMapping),
		new(data_manage.EdbInfo),                 //hz_data库的edb_info表
		new(data_manage.EdbDataCalculateZjpj),    //直接拼接
		new(data_manage.EdbDataCalculateLjztbpj), //累计同比值拼接
		new(data_manage.BaseFromTradeZhengzhouIndex),
		new(data_manage.BaseFromTradeDalianIndex),
		new(data_manage.BaseFromTradeShanghaiIndex),
		new(data_manage.BaseFromTradeCffexIndex),
		new(data_manage.BaseFromTradeIneIndex),
		new(data_manage.BaseFromChangesVisitorsCovid),
		new(data_manage.BaseFromEiaSteoIndex),
		new(data_manage.BaseFromEiaSteoData),
		new(data_manage.ComTradeIndex),        // 联合国指标表
		new(data_manage.ComTradeCodeMapping),  // 联合国指标关系表
		new(data_manage.ComTradeData),         // 联合国数据表
		new(data_manage.MeetingProbabilities), // 联储加息概率

		//广期所
		new(data_manage.BaseFromTradeGuangzhouClassify),
		new(data_manage.BaseFromTradeGuangzhouContract),
		new(data_manage.BaseFromTradeGuangzhouIndex),
		new(data_manage.BaseFromTradeGuangzhouData),
		//ICPI
		new(data_manage.BaseFromIcpiIndex),
		new(data_manage.BaseFromIcpiClassify),
		new(data_manage.BaseFromIcpiData),
	)
}

// initFutureGoodEdbDataTable 注册商品期货Edb指标 数据表
func initFutureGoodEdbDataTable() {
	//注册对象
	orm.RegisterModel(
		new(future_good.FutureGoodEdbInfo),
	)
}

// initTradePositionTop 注册持仓分析 数据表
func initTradePositionTop() {
	//注册对象
	orm.RegisterModel(
		new(data_manage.TradePositionDalianTop),
		new(data_manage.TradePositionZhengzhouTop),
		new(data_manage.TradePositionShanghaiTop),
		new(data_manage.TradePositionIneTop),
		new(data_manage.TradePositionCffexTop),
		new(data_manage.BaseFromTradeClassify), // 交易所分类
	)
}

// initSmartReport 注册智能研报数据表
func initSmartReport() {
	orm.RegisterModel(
		new(SmartReport),
		new(ReportStateRecord),
	)
}