package utils

import (
	"fmt"
	"github.com/beego/beego/v2/core/logs"
	"github.com/beego/beego/v2/server/web"
	"github.com/rdlucklib/rdluck_tools/cache"
	"github.com/rdlucklib/rdluck_tools/log"
)

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

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

var ApiLog *log.Log

var (
	Bucketname           string = "hongze"
	Endpoint             string
	Imghost              string = "https://hongze.oss-accelerate.aliyuncs.com/"
	Upload_dir           string = "static/images/"
	Upload_Audio_Dir     string = "static/audio/"
	AccessKeyId          string = "LTAIFMZYQhS2BTvW"
	AccessKeySecret      string = "12kk1ptCHoGWedhBnKRVW5hRJzq9Fq"
	ResearchReportUrl           = "" //旧版报告的跳转地址
	NewResearchReportUrl        = "" //新版报告的跳转地址
	ReportAppid          string
	ReportSecret         string
	YIDONG_HONGZE_KEY    string //易董加密的盐值
)

//全时会议账号信息
var (
	QsAppID     string
	QsSecretKey string
	QsUserId    int64
	QsUserName  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()
		fmt.Println("localIp:", localIp)
		if localIp == "10.0.0.123" {
			RunMode = "debug"
		} else {
			RunMode = "release"
		}
		fmt.Println("RunMode:", RunMode)
		configPath := `/home/code/config/hongze_open_api/conf/app.conf`
		fmt.Println("configPath:", configPath)
		err = web.LoadAppConfig("ini", configPath)
		if err != nil {
			fmt.Println("web.LoadAppConfig Err:" + err.Error())
		}
	}
	fmt.Println(RunMode, "模式")
	config, err := web.AppConfig.GetSection(RunMode)
	if err != nil {
		panic("配置文件读取错误 " + err.Error())
	}
	logs.Info(RunMode + " 模式")
	MYSQL_URL = config["mysql_url"]
	MYSQL_URL_RDDP = config["mysql_url_rddp"]

	// 初始化redis
	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" {
		logDir := `/data/rdlucklog/hongze_open_api`
		ApiLog = log.Init("20060102.api", logDir)
		ResearchReportUrl = "https://details.hzinsights.com/"
		NewResearchReportUrl = "https://details.hzinsights.com/"
		ReportAppid = "CQWx3EqDLNk7bVHo"                  //Appid
		ReportSecret = "vRVFCk182Hpv8CyrHsxaHhA3CgGhh4Uu" //AppSecret
		YIDONG_HONGZE_KEY = "YIDONG20220816VshIcFonYccA"  //易董加密的盐值
	} else {
		ApiLog = log.Init("20060102.api")
		ResearchReportUrl = "http://advisoryadmin.brilliantstart.cn/xcx_h5/"
		NewResearchReportUrl = "http://xcxh5test.hzinsights.com/xcx_h5/"
		ReportAppid = "VH2ou4EKlYZz58Uj"                  //Appid
		ReportSecret = "Q6Y5vwmx1IAA9OPkobl8qWvrnV7dGJ4k" //AppSecret
		YIDONG_HONGZE_KEY = "YIDONG20220816QxbNHiYNnnhT"  //易董加密的盐值
	}

	//初始化系统用户角色map集合
	initRoleType()

	//初始化全时开放api配置
	initQsOpenApiConfig()
}

type RoleType struct {
	RoleId         int    `json:"role_id"`
	RoleName       string `json:"role_name"`
	RoleType       string `json:"role_type"`
	RoleTypeCode   string `json:"role_type_code"`
	Role           string `json:"role"`
	DepartmentId   int    `json:"department_id"`
	DepartmentName string `json:"department_name"`
	GroupId        int    `json:"group_id"`
	GroupName      string `json:"group_name"`
}

var RoleTypeMap = map[int]RoleType{}

func initRoleType() {
	if RunMode == "release" {
		RoleTypeMap[1] = RoleType{
			RoleId:         6,
			RoleName:       "ficc研究员",
			RoleType:       "ficc研究员",
			RoleTypeCode:   "researcher",
			Role:           "researcher",
			DepartmentId:   1,
			DepartmentName: "FICC研究部",
			GroupId:        8,
			GroupName:      "无",
		}
		RoleTypeMap[2] = RoleType{
			RoleId:         10,
			RoleName:       "权益研究员",
			RoleType:       "权益研究员",
			RoleTypeCode:   "rai_researcher",
			Role:           "researcher",
			DepartmentId:   5,
			DepartmentName: "权益销售部",
			GroupId:        17,
			GroupName:      "无",
		}
		RoleTypeMap[3] = RoleType{
			RoleId:         4,
			RoleName:       "ficc销售",
			RoleType:       "ficc销售",
			RoleTypeCode:   "ficc_seller",
			Role:           "sales",
			DepartmentId:   2,
			DepartmentName: "ficc销售部",
			GroupId:        9,
			GroupName:      "无",
		}
		RoleTypeMap[4] = RoleType{
			RoleId:         5,
			RoleName:       "权益销售",
			RoleType:       "权益销售",
			RoleTypeCode:   "rai_seller",
			Role:           "sales",
			DepartmentId:   5,
			DepartmentName: "权益销售部",
			GroupId:        17,
			GroupName:      "无",
		}
	} else {
		RoleTypeMap[1] = RoleType{
			RoleId:         6,
			RoleName:       "ficc研究员",
			RoleType:       "ficc研究员",
			RoleTypeCode:   "researcher",
			Role:           "researcher",
			DepartmentId:   1,
			DepartmentName: "FICC研究部",
			GroupId:        8,
			GroupName:      "无",
		}
		RoleTypeMap[2] = RoleType{
			RoleId:         10,
			RoleName:       "权益研究员",
			RoleType:       "权益研究员",
			RoleTypeCode:   "rai_researcher",
			Role:           "researcher",
			DepartmentId:   5,
			DepartmentName: "权益销售部-王芳",
			GroupId:        17,
			GroupName:      "无",
		}
		RoleTypeMap[3] = RoleType{
			RoleId:         4,
			RoleName:       "ficc销售",
			RoleType:       "ficc销售",
			RoleTypeCode:   "ficc_seller",
			Role:           "sales",
			DepartmentId:   2,
			DepartmentName: "ficc销售部",
			GroupId:        9,
			GroupName:      "无",
		}
		RoleTypeMap[4] = RoleType{
			RoleId:         5,
			RoleName:       "权益销售",
			RoleType:       "权益销售",
			RoleTypeCode:   "rai_seller",
			Role:           "sales",
			DepartmentId:   5,
			DepartmentName: "权益销售部-王芳",
			GroupId:        17,
			GroupName:      "无",
		}
	}
}

// initQsOpenApiConfig 全时开放api配置
func initQsOpenApiConfig() {
	QsAppID = "ed1cc7c87c97089263fc899fbab193b0"
	QsSecretKey = "d92b91265dbbc5e3af44edfb82503635"
	if RunMode == "release" {
		QsUserId = 22330940
		QsUserName = "1406228535@qq.com"
	} else {
		QsUserId = 19896481
		QsUserName = "pyan@hzinsights.com"
	}
}
