package models

import (
	"eta/eta_index_lib/models/data_stat"
	"eta/eta_index_lib/models/edb_refresh"
	"eta/eta_index_lib/models/excel"
	"eta/eta_index_lib/models/future_good"
	"eta/eta_index_lib/models/supply_analysis"
	"eta/eta_index_lib/utils"
	"github.com/beego/beego/v2/client/orm"
	_ "github.com/go-sql-driver/mysql"
	"time"
)

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)

	_ = 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)

	_ = 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)

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

	//注册对象
	orm.RegisterModel(
		new(EdbDataCalculateZjpj),
		new(EdbDataCalculateLjztbpj),
		new(EdbInfo),
		new(EdbInfoCalculateMapping),
		new(EdbPythonCode),
		new(EdbDataPython),
		new(ChartEdbMapping),
		new(PredictEdbConf),
		new(EdbClassify),
		new(PredictEdbConfCalculateMapping),
		new(PredictEdbRuleData),
		new(EdbDataCalculateNhcc),
		new(EdbDataPredictCalculateLjztbpj),
		new(EdbDataPredictCalculateNhcc),
		new(EdbDataPredictCalculateZjpj),
		new(EdbDataInsertConfig),
		new(EdbAdjustConf), // 数据调整的配置
		new(BaseFromMysteelChemicalClassify),
	)

	// 注册期货数据 数据表
	initFutureGood()

	// 供应分析 数据表
	initSupplyAnalysis()

	// 基础指标表
	initBaseIndex()

	// initDataStat 数据源统计管理相关表
	initDataStat()

	// Eta表格相关
	initExcel()

	// 初始化指标刷新
	initEdbRefresh()

	// 自有数据指标
	initBusinessEdb()

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

// initFutureGood 注册期货数据 数据表
func initFutureGood() {
	//注册对象
	orm.RegisterModel(
		new(future_good.FutureGoodEdbInfo),         //期货指标表
		new(future_good.FutureGoodEdbData),         //期货指标数据表
		new(future_good.ChartInfoFutureGoodProfit), //商品期货利润图表相关的信息
	)
}

// initSupplyAnalysis 供应分析
func initSupplyAnalysis() {
	orm.RegisterModel(
		new(supply_analysis.Variety),
		new(supply_analysis.VarietyAdminPermission),
		new(supply_analysis.VarietyEdbInfo),
		new(supply_analysis.VarietyPlant),
		new(supply_analysis.BaseFromStockPlantData),
	)
}

// initBaseIndex 基础指标表
func initBaseIndex() {
	orm.RegisterModel(
		new(BaseFromSciIndex),
		new(BaseFromSciData),
		new(BaseFromBaiinfoIndex),
		new(BaseFromBaiinfoData),
		new(BaseFromSmmIndex),
		new(BaseFromSmmData),
		new(BaseFromMysteelChemicalIndex),
		new(BaseFromMysteelChemicalData),
		new(BaseFromTradeShanghaiIndex),
		new(BaseFromTradeIneIndex),
		new(BaseFromSmmClassify),
		new(BaseFromYongyiIndex),
		new(BaseFromYongyiData),
		new(BaseFromYongyiClassify),
		new(BaseFromCoalmineMapping),
		new(BaseFromCoalmineJsmIndex),
		new(BaseFromCoalmineCoastalIndex),
		new(BaseFromCoalmineInlandIndex),
		new(BaseFromCoalmineCompanyIndex),
		new(BaseFromCoalmineFirmIndex),
		new(BaseFromMtjhMapping),
		new(BaseFromMtjhIndex),
		new(BaseFromFenweiIndex),
		new(BaseFromFenweiData),
		new(BaseFromBloombergIndex),
		new(BaseFromBloombergData),
		new(BaseFromSci99Data),
		new(BaseFromSci99Index),
		new(BaseFromSci99Classify),
	)
}

// initDataStat 数据源统计管理相关表
func initDataStat() {
	orm.RegisterModel(
		new(data_stat.EdbInfoUpdateLog),  // 指标更新/刷新日志列表
		new(data_stat.EdbInfoUpdateStat), // 数据源明细表
		new(data_stat.EdbSourceStat),     // 数据源统计表
	)
}

// initExcel Excel
func initExcel() {
	orm.RegisterModel(
		new(excel.ExcelInfo),
		new(excel.ExcelClassify),
		new(excel.ExcelEdbMapping), //excel与指标的关系表
	)
}

// initEdbRefresh 初始化指标刷新
func initEdbRefresh() {
	orm.RegisterModel(
		new(edb_refresh.EdbRefreshSource),        // 刷新的数据源表
		new(edb_refresh.EdbRefreshDefaultConfig), // 指标的默认刷新时间配置表
		new(edb_refresh.EdbRefreshConfig),        // 指标的刷新时间配置表
		new(edb_refresh.EdbRefreshMapping),       // 指标刷新时间配置关系表
	)
}

// initBusinessEdb 初始化指标刷新
func initBusinessEdb() {
	orm.RegisterModel(
		new(BaseFromBusinessIndex), // 外部指标（商家系统）表
		new(EdbBusinessSource),     // 自有数据（商家）指标来源
	)
}
