package utils

import (
	"fmt"
	beeLogger "github.com/beego/bee/v2/logger"
	"github.com/beego/beego/v2/server/web"
	"github.com/rdlucklib/rdluck_tools/cache"
)

var (
	RunMode       string //运行模式
	MYSQL_URL     string //数据库连接
	MYSQL_URL_EDB string
	MYSQL_URL_GL  string

	PYTHON_MYSQL_HOST   string //python数据库链接主机地址
	PYTHON_MYSQL_USER   string //python数据库链接账号
	PYTHON_MYSQL_PASSWD string //python数据库链接密码
	PYTHON_MYSQL_DB     string //python数据库链接数据库名
	PYTHON_PATH         string //python可执行文件地址

	REDIS_CACHE string       //缓存地址
	Rc          *cache.Cache //redis缓存
	Re          error        //redis错误
)
var (
	STATIC_DIR string
)

var (
	DATA_INDEX_NAME string //数据指标库索引
)

func init() {
	tmpRunMode, err := web.AppConfig.String("run_mode")
	if err != nil {
		panic("配置文件读取run_mode错误 " + err.Error())
	}
	RunMode = tmpRunMode
	fmt.Println("RunMode:", RunMode)
	if RunMode == "" {
		localIp, err := GetLocalIP()
		if localIp == "10.0.0.123" {
			RunMode = "debug"
		} else {
			RunMode = "release"
		}
		configPath := `/home/code/config/hongze_edb_lib/conf/app.conf`
		err = web.LoadAppConfig("ini", configPath)
		if err != nil {
			fmt.Println("web.LoadAppConfig Err:" + err.Error())
		}
	}

	config, err := web.AppConfig.GetSection(RunMode)
	if err != nil {
		panic("配置文件读取错误 " + err.Error())
	}
	beeLogger.Log.Info(RunMode + " 模式")
	MYSQL_URL = config["mysql_url"]
	MYSQL_URL_EDB = config["mysql_url_edb"]
	MYSQL_URL_GL = config["mysql_url_gl"]

	//python链接
	PYTHON_MYSQL_HOST = config["python_mysql_host"]
	PYTHON_MYSQL_USER = config["python_mysql_user"]
	PYTHON_MYSQL_PASSWD = config["python_mysql_passwd"]
	PYTHON_MYSQL_DB = config["python_mysql_db"]
	PYTHON_PATH = config["python_path"]

	REDIS_CACHE = config["beego_cache"]
	if len(REDIS_CACHE) <= 0 {
		panic("redis链接参数没有配置")
	}
	Rc, Re = cache.NewCache(REDIS_CACHE) //初始化缓存
	if Re != nil {
		fmt.Println(Re)
		panic(Re)
	}

	if RunMode == "release" {
		Hz_Ths_Data_Url = "http://172.19.173.231:7000/"
		STATIC_DIR = "/home/static/imgs/"
		DATA_INDEX_NAME = "hz_data_lib_v20220214"
	} else {
		Hz_Ths_Data_Url = "http://139.196.136.213:7000/"

		STATIC_DIR = "static/imgs/"
		DATA_INDEX_NAME = "test_hz_data_lib_v20220120"
	}

}

//修改接口文档
//http://8.136.199.33:8300/swagger/
