package serverinit

import (
	"go-admin/config"
	"go-admin/models"
	"go-admin/pkg/httputils"
	"go-admin/services/binanceservice"

	log "github.com/go-admin-team/go-admin-core/logger"
	"github.com/robfig/cron/v3"
)

func FuturesInit() error {
	httputils.InitProxy(config.ExtConfig.ProxyUrl)

	//FutFundingRate()

	log.Info("初始化交易对-开始")
	data := make(map[string]models.TradeSet, 0)
	err := binanceservice.GetAndReloadSymbols(&data)

	if err != nil {
		return err
	}
	log.Info("初始化交易对-结束")

	log.Info("初始化24h行情-开始")
	_, err = binanceservice.InitSymbolsTicker24h(&data)

	if err != nil {
		return err
	}
	log.Info("初始化24h行情-结束")

	//定时任务
	RunJobs()
	return nil
}

/*
缓存合约资金费率
*/
func FutFundingRate() error {
	log.Info("初始化 合约-费率 开始")
	_, err := binanceservice.GetPremiumIndex()

	if err != nil {
		log.Error("初始化 合约-费率 失败：", err)
		return err
	}

	log.Info("初始化 合约-费率 成功")
	return nil
}

// 定义任务
func RunJobs() {
	cronJob := cron.New()

	//添加任务
	//_, err := cronJob.AddFunc("@every 1h", func() {
	//	FutFundingRate()
	//})
	//
	//if err != nil {
	//	fmt.Println("添加定时获取资金费率任务失败")
	//} else {
	//	fmt.Println("添加定时获取资金费率任务成功")
	//}

	//启动
	cronJob.Start()
}
