package binanceservice

import (
	"fmt"
	"go-admin/common/const/rediskey"
	"go-admin/common/global"
	"go-admin/common/helper"
	"go-admin/models"
	"go-admin/models/positiondto"
	"go-admin/services/cacheservice"
	"testing"

	"github.com/bytedance/sonic"
	"github.com/go-admin-team/go-admin-core/logger"
	"github.com/go-admin-team/go-admin-core/sdk"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func TestSpotJudge(t *testing.T) {
	dsn := "root:123456@tcp(127.0.0.1:3306)/go_exchange_single?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms"
	db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	helper.InitDefaultRedis("127.0.0.1:6379", "", 2)
	helper.InitLockRedisConn("127.0.0.1:6379", "", "2")
	// tradeSet := models.TradeSet{
	// 	Coin:      "ADA",
	// 	Currency:  "USDT",
	// 	LastPrice: "0.516",
	// }

	key := fmt.Sprintf(rediskey.SpotReduceList, global.EXCHANGE_BINANCE)
	item := `{"id":66,"apiId":49,"mainId":63,"pid":63,"symbol":"ADAUSDT","price":"0.5912","side":"SELL","num":"12.6","orderSn":"397919643961917440"}`
	reduceOrder := positiondto.ReduceListItem{}
	spotApi := SpotRestApi{}
	setting, err := cacheservice.GetSystemSetting(db)

	if err != nil {
		logger.Error("获取系统设置失败")
		return
	}
	if err := sonic.Unmarshal([]byte(item), &reduceOrder); err != nil {
		logger.Error("反序列化失败")
		return
	}
	// JudgeFuturesReduce(tradeSet)
	SpotReduceTrigger(db, reduceOrder, spotApi, setting, key, item, false, 0)
}

// 测试减仓后减仓触发
func TestSpotReduceReduce(t *testing.T) {
	dsn := "root:123456@tcp(127.0.0.1:3306)/go_exchange_single?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms"
	db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	sdk.Runtime.SetDb("default", db)
	helper.InitDefaultRedis("127.0.0.1:6379", "", 2)
	helper.InitLockRedisConn("127.0.0.1:6379", "", "2")
	tradeSet := models.TradeSet{
		Coin:       "ADA",
		Currency:   "USDT",
		LastPrice:  "0.5793",
		PriceDigit: 4,
	}

	// JudgeFuturesReduce(tradeSet)
	JudgeSpotReduce(tradeSet)
}
