package binanceservice

import (
	"context"
	"errors"
	"fmt"
	"go-admin/app/admin/models"
	DbModels "go-admin/app/admin/models"
	"go-admin/app/admin/service/dto"
	"go-admin/common/const/rediskey"
	"go-admin/common/global"
	"go-admin/common/helper"
	models2 "go-admin/models"
	"go-admin/models/positiondto"
	"go-admin/pkg/utility"
	"go-admin/services/cacheservice"
	"go-admin/services/commonservice"
	"go-admin/services/positionservice"
	"strconv"
	"strings"
	"time"

	"github.com/bytedance/sonic"
	"github.com/go-admin-team/go-admin-core/logger"
	log "github.com/go-admin-team/go-admin-core/logger"
	"github.com/shopspring/decimal"
	"gorm.io/gorm"
)

/*
订单回调
*/
func ChangeSpotOrder(mapData map[string]interface{}) {
	// 检查订单号是否存在
	orderSn, ok := mapData["c"]
	originOrderSn := mapData["C"] //取消操作 代表原始订单号

	if !ok {
		logger.Error("订单回调失败, 没有订单号", mapData)
		return
	}

	if originOrderSn != nil && originOrderSn != "" {
		orderSn = originOrderSn
	}

	// 获取数据库连接
	db := commonservice.GetDBConnection()
	if db == nil {
		logger.Error("订单回调失败, 无法获取数据库连接")
		return
	}

	// 获取 Redis 锁
	lockKey := fmt.Sprintf(rediskey.SpotCallBack, orderSn)
	lock := helper.NewRedisLock(lockKey, 200, 5, 100*time.Millisecond)
	if err := acquireLock(lock, orderSn); err != nil {
		return
	}
	defer lock.Release()

	// 查询订单
	preOrder, err := getPreOrder(db, orderSn)
	if err != nil {
		logger.Error("订单回调失败, 查询订单失败:", orderSn, " err:", err)
		return
	}

	// 解析订单状态
	status, ok := mapData["X"]
	if !ok {
		logMapData(mapData)
		return
	}

	// 更新订单状态
	orderStatus, reason := parseOrderStatus(status, mapData)

	if orderStatus == 0 {
		logger.Error("订单回调失败,状态错误:", orderSn, " status:", status, " reason:", reason)
		return
	}

	if err := updateOrderStatus(db, preOrder, orderStatus, reason, true, mapData); err != nil {
		logger.Error("修改订单状态失败:", orderSn, " err:", err)
		return
	}

	// 根据订单类型和状态处理逻辑
	handleOrderByType(db, preOrder, orderStatus)
}

// 获取 Redis 锁
func acquireLock(lock *helper.RedisLock, orderSn interface{}) error {
	acquired, err := lock.AcquireWait(context.Background())
	if err != nil {
		logger.Error("订单回调失败, 获取锁失败:", orderSn, " err:", err)
		return err
	}
	if !acquired {
		logger.Error("订单回调失败, 获取锁失败:", orderSn)
		return fmt.Errorf("failed to acquire lock")
	}
	return nil
}

// 记录 mapData 数据
func logMapData(mapData map[string]interface{}) {
	mapStr, _ := sonic.Marshal(&mapData)
	logger.Error("订单回调失败, 没有状态:", string(mapStr))
}

// 根据订单类型和状态处理逻辑
func handleOrderByType(db *gorm.DB, preOrder *DbModels.LinePreOrder, orderStatus int) {
	switch {
	// 主单成交
	case preOrder.OrderType == 0 && orderStatus == 6:
		handleMainOrderFilled(db, preOrder)

	//主单减仓完毕
	case preOrder.OrderType == 4 && orderStatus == 6:
		handleMainReduceFilled(db, preOrder)
	//主单取消
	case preOrder.OrderType == 0 && preOrder.Pid == 0 && orderStatus == 4:
		handleMainOrderCancel(db, preOrder, 1)
	// 止盈成交
	case preOrder.OrderType == 1 && orderStatus == 6:
		handleSpotTakeProfitFilled(db, preOrder)
	//平仓单
	case preOrder.OrderType == 3 && orderStatus == 6:
		handleMainOrderClosePosition(db, preOrder)

	//主单止损回调
	case preOrder.OrderType == 2 && orderStatus == 6:
		removeSpotLossAndAddPosition(preOrder.MainId, preOrder.OrderSn)
		removePosition(db, preOrder)

		if err := db.Model(&DbModels.LinePreOrder{}).Where("id =?", preOrder.MainId).Update("status", 9).Error; err != nil {
			logger.Errorf("主单止损回调 订单号:%s 修改主单状态失败:%v", preOrder.OrderSn, err)
		}

		if err := db.Model(&DbModels.LinePreOrder{}).Where("main_id =? AND status =0", preOrder.MainId).Update("status", 4).Error; err != nil {
			logger.Errorf("主单止损回调 订单号:%s 修改主单状态失败:%v", preOrder.OrderSn, err)
		}

		spotApi := SpotRestApi{}
		apiUserInfo, _ := GetApiInfo(preOrder.ApiId)

		if apiUserInfo.Id > 0 {
			req := CancelOpenOrdersReq{
				Symbol: preOrder.Symbol,
				ApiId:  preOrder.ApiId,
			}
			if err := spotApi.CancelOpenOrders(db, req); err != nil {
				logger.Errorf("止盈单成功 取消其它订单失败 订单号：%s:", err)
			}
		}
	}
}

// 主单减仓完毕
func handleMainReduceFilled(db *gorm.DB, preOrder *DbModels.LinePreOrder) {
	apiUserInfo, _ := GetApiInfo(preOrder.ApiId)

	if apiUserInfo.Id == 0 {
		logger.Errorf("handleMainReduceFilled 获取api信息失败,订单号:%s", preOrder.OrderSn)
		return
	}

	tradeSet, err := cacheservice.GetTradeSet(global.EXCHANGE_BINANCE, preOrder.Symbol, 1)

	if err != nil {
		logger.Errorf("handleMainReduceFilled 获取交易对设置失败,订单号:%s", preOrder.OrderSn)
		return
	}

	//修改减仓单减仓策略状态
	ReduceCallBack(db, preOrder)

	orderExt := models.LinePreOrderExt{}
	positionData := savePosition(db, preOrder)

	if err := db.Model(&DbModels.LinePreOrderStatus{}).Where("order_id =? ", preOrder.MainId).Update("reduce_status", 1).Error; err != nil {
		logger.Errorf("handleMainReduceFilled 更新主单减仓状态失败,订单号:%s", preOrder.OrderSn)
	}

	//策略减仓单 获取主减仓单拓展信息
	if preOrder.ReduceOrderId > 0 {
		db.Model(&orderExt).Where("order_id =?", preOrder.ReduceOrderId).Find(&orderExt)
	} else {
		db.Model(&orderExt).Where("order_id =?", preOrder.Id).Find(&orderExt)
	}

	lock := helper.NewRedisLock(fmt.Sprintf(rediskey.SpotReduceCallback, preOrder.ApiId, preOrder.Symbol), 120, 20, 100*time.Millisecond)

	if ok, err := lock.AcquireWait(context.Background()); err != nil {
		log.Error("获取锁失败", err)
		return
	} else if ok {
		defer lock.Release()
		// 100%减仓 终止流程
		if orderExt.AddPositionVal.Cmp(decimal.NewFromInt(100)) >= 0 {
			//缓存
			removeSpotLossAndAddPosition(preOrder.MainId, preOrder.OrderSn)
			removePosition(db, preOrder)

			ids := []int{preOrder.MainId, preOrder.Pid}
			if err := db.Model(&DbModels.LinePreOrder{}).Where("id IN ? AND status =6", ids).Update("status", 9).Error; err != nil {
				logger.Info("100%减仓完毕，终结流程")
			}
			return
		}

		//市价单跳出
		if preOrder.MainOrderType == "MARKET" {
			return
		}

		totalNum := getSpotPositionAvailableQuantity(db, apiUserInfo, preOrder, tradeSet) //getSpotTotalNum(apiUserInfo, preOrder, tradeSet)
		totalNum = totalNum.Mul(decimal.NewFromFloat(0.998)).Truncate(int32(tradeSet.AmountDigit))
		//亏损大于0 重新计算比例
		SpotTakeProfit(db, preOrder, totalNum, positionData, orderExt, tradeSet, decimal.Zero, decimal.Zero)

		mainId := preOrder.Id

		if preOrder.MainId > 0 {
			mainId = preOrder.MainId
		}

		nextSpotReduceTrigger(db, mainId, totalNum, tradeSet)
	}
}

func SpotTakeProfit(db *gorm.DB, preOrder *DbModels.LinePreOrder, totalNum decimal.Decimal,
	positionData positiondto.PositionDto, orderExt DbModels.LinePreOrderExt, tradeSet models2.TradeSet, manualTakeRatio, manualStopRatio decimal.Decimal) bool {
	price := utility.StrToDecimal(preOrder.Price)
	orders := make([]models.LinePreOrder, 0)

	if err := db.Model(&models.LinePreOrder{}).Where("pid =? AND order_type IN (1,2) AND status=0", preOrder.Id).Find(&orders).Error; err != nil {
		logger.Errorf("获取减仓单止盈止损失败 err:%v", err)
		return true
	}

	spotApi := SpotRestApi{}

	for index := range orders {
		orders[index].Num = totalNum.String()

		if orders[index].OrderType == 1 {
			//手动设置百分比
			if manualTakeRatio.Cmp(decimal.Zero) > 0 {
				orders[index].Rate = manualTakeRatio.String()
				percentag := manualTakeRatio.Div(decimal.NewFromInt(100))

				if positionData.PositionSide == "LONG" {
					orders[index].Price = price.Mul(decimal.NewFromInt(1).Add(percentag)).Truncate(int32(tradeSet.PriceDigit)).String()
				} else {
					orders[index].Price = price.Mul(decimal.NewFromInt(1).Sub(percentag)).Truncate(int32(tradeSet.PriceDigit)).String()
				}
			} else if positionData.TotalLoss.Cmp(decimal.Zero) > 0 && orderExt.Id > 0 {
				percentag := positionData.TotalLoss.Div(totalNum).Div(price).Mul(decimal.NewFromInt(100))
				percentag = percentag.Add(orderExt.TakeProfitRatio).Truncate(2)
				orders[index].Rate = percentag.String()
				percentag = percentag.Div(decimal.NewFromInt(100))
				orders[index].Price = price.Mul(decimal.NewFromInt(1).Add(percentag)).Truncate(int32(tradeSet.PriceDigit)).String()
			}

			processTakeProfitOrder(db, spotApi, orders[index])
		} else if orders[index].OrderType == 2 {
			if manualStopRatio.Cmp(decimal.Zero) > 0 {
				orders[index].Rate = manualStopRatio.String()
				percentag := manualStopRatio.Div(decimal.NewFromInt(100))

				if positionData.PositionSide == "LONG" {
					orders[index].Price = price.Mul(decimal.NewFromInt(1).Sub(percentag)).Truncate(int32(tradeSet.PriceDigit)).String()
				} else {
					orders[index].Price = price.Mul(decimal.NewFromInt(1).Add(percentag)).Truncate(int32(tradeSet.PriceDigit)).String()
				}
			}
			processStopLossOrder(orders[index])
		}
	}
	return false
}

// 缓存下一个减仓单
func nextSpotReduceTrigger(db *gorm.DB, mainId int, totalNum decimal.Decimal, tradeSet models2.TradeSet) bool {
	nextOrder := DbModels.LinePreOrder{}
	nextExt := DbModels.LinePreOrderExt{}
	// var percentag decimal.Decimal

	if err := db.Model(&models.LinePreOrder{}).Where("main_id =? AND order_type =4 AND status=0 AND reduce_order_id =0", mainId).Order("rate asc").First(&nextOrder).Error; err != nil {
		logger.Errorf("获取下一个单失败 err:%v", err)
		return true
	}

	if err := db.Model(&models.LinePreOrderExt{}).Where("order_id =? and add_type =2", nextOrder.Id).First(&nextExt).Error; err != nil {
		logger.Errorf("获取下一个单失败 err:%v", err)
	}

	//移除缓存
	key := fmt.Sprintf(rediskey.SpotReduceList, global.EXCHANGE_BINANCE)
	vals, _ := helper.DefaultRedis.GetAllList(key)
	item := positiondto.ReduceListItem{}

	for _, val := range vals {
		sonic.Unmarshal([]byte(val), &item)
		if item.MainId == mainId {
			if _, err := helper.DefaultRedis.LRem(key, val); err != nil {
				logger.Errorf("减仓单 redis删除失败 main_id:%v err:%v", mainId, err)
			}
		}
	}

	//减仓配置 且减仓比例大于0小于100
	if nextExt.Id > 0 && nextExt.AddType == 2 && nextExt.AddPositionVal.Cmp(decimal.Zero) > 0 && nextExt.AddPositionVal.Cmp(decimal.Zero) < 100 {
		num := totalNum.Mul(nextExt.AddPositionVal.Div(decimal.NewFromInt(100))).Truncate(int32(tradeSet.AmountDigit))
		nextOrder.Num = num.String()

		if err := db.Model(&nextOrder).Update("num", nextOrder.Num).Error; err != nil {
			logger.Errorf("更新减仓单数量失败 err:%v", err)
		}
	}

	//减仓待触发
	processSpotReduceOrder(nextOrder)
	return false
}

// 获取主单可用数量
func getSpotPositionAvailableQuantity(db *gorm.DB, apiUserInfo DbModels.LineApiUser, preOrder *DbModels.LinePreOrder, tradeSet models2.TradeSet) decimal.Decimal {
	totalNum := getSpotTotalNum(apiUserInfo, preOrder, tradeSet)

	//biance查询失败 查系统内数量
	if totalNum.Cmp(decimal.Zero) <= 0 {
		var mainId int

		if preOrder.MainId > 0 {
			mainId = preOrder.MainId
		} else {
			mainId = preOrder.Id
		}

		totalNum = getInternalNum(db, mainId)
	}
	return totalNum
}

// 获取现货剩余数量
func getSpotTotalNum(apiUserInfo DbModels.LineApiUser, preOrder *DbModels.LinePreOrder, tradeSet models2.TradeSet) decimal.Decimal {
	var totalNum decimal.Decimal
	var err error
	for x := 1; x < 4; x++ {
		totalNum, err = getSpotPositionNum(apiUserInfo, preOrder, tradeSet)

		if err == nil {
			break
		}

		time.Sleep(time.Millisecond * 150)
	}
	if err != nil {
		totalNum = utility.StrToDecimal(preOrder.Num)
	}
	return totalNum
}

// 获取系统内剩余数量
func getInternalNum(db *gorm.DB, mainId int) decimal.Decimal {
	var totalNum, totalSellNum decimal.Decimal
	var data DbModels.LinePreOrder
	if err := db.Model(&data).Where("(main_id =? OR id =?) AND order_type =0 AND status =6", mainId, mainId).Select("COALESCE(sum(num), 0)").Find(&totalNum).Error; err != nil {
		logger.Errorf("getSpotInternalNum 查询所有数量失败,mainId:%d err:%v", mainId, err)
	}

	if err := db.Model(&data).Where("main_id =? AND order_type =4 AND status =6", mainId).Select("COALESCE(sum(num), 0)").Find(&totalSellNum).Error; err != nil {
		logger.Errorf("getSpotInternalNum 查询所有减仓数量失败,mainId:%d err:%v", mainId, err)
	}

	return totalNum.Sub(totalSellNum).Abs()
}

// 获取现货持仓数量
func getSpotPositionNum(apiUserInfo DbModels.LineApiUser, preOrder *DbModels.LinePreOrder, tradeSet models2.TradeSet) (decimal.Decimal, error) {
	client := GetClient(&apiUserInfo)

	resp, _, err := client.SendSpotAuth("/api/v3/account", "GET", map[string]interface{}{
		"omitZeroBalances": true,
	})

	if err != nil {
		logger.Errorf("api_id:%d 交易对:%s 查询用户信息失败:%v", apiUserInfo.Id, preOrder.Symbol, err)
		return decimal.Decimal{}, err
	}

	var balanceInfo SpotAccountInfo
	sonic.Unmarshal(resp, &balanceInfo)
	suffix := utility.ReplaceSuffix(preOrder.Symbol, preOrder.QuoteSymbol, "")
	var num decimal.Decimal

	for _, item := range balanceInfo.Balances {
		if item.Asset == suffix {
			if utility.StrToDecimal(item.Free).Cmp(decimal.Zero) <= 0 {
				logger.Error("handleMainReduceFilled 账户余额不足,交易对：%s 订单号:%s", preOrder.Symbol, preOrder.OrderSn)
				return decimal.Decimal{}, errors.New("账户余额不足")
			}

			num = utility.StrToDecimal(item.Free).Mul(decimal.NewFromFloat(0.998)).Truncate(int32(tradeSet.AmountDigit))
			break
		}
	}
	return num, nil
}

// 主单取消
// symbolType 1:现货 2:合约
func handleMainOrderCancel(db *gorm.DB, preOrder *DbModels.LinePreOrder, symboType int) {
	var preOrderKey string

	if symboType == 1 {
		preOrderKey = fmt.Sprintf(rediskey.PreSpotOrderList, global.EXCHANGE_BINANCE)
	} else {
		preOrderKey = fmt.Sprintf(rediskey.PreFutOrderList, global.EXCHANGE_BINANCE)
	}

	preSpotOrders, _ := helper.DefaultRedis.GetAllList(preOrderKey)
	preOrderCache := DbModels.LinePreOrder{}

	for _, v := range preSpotOrders {
		if v == "" {
			continue
		}

		sonic.Unmarshal([]byte(v), &preOrderCache)
		if preOrderCache.Pid == preOrder.Id {
			var count int64
			db.Model(&models.LinePreOrder{}).
				Where("api_id =? AND site=? AND symbol=? AND symbol_type =? AND exchange_type =? AND status =6",
					preOrder.ApiId, preOrder.Site, preOrder.Symbol, preOrder.SymbolType, preOrder.ExchangeType).Count(&count)

			if count == 0 {
				_, err := helper.DefaultRedis.LRem(preOrderKey, v)

				if err != nil {
					logger.Errorf("订单回调失败, 回调订单号：%s 删除缓存失败:%v", preOrder.OrderSn, err)
				}
			}
		}
	}
}

// 平仓单
func handleMainOrderClosePosition(db *gorm.DB, preOrder *DbModels.LinePreOrder) {
	//主单平仓
	if preOrder.OrderCategory == 1 {
		ids := []int{preOrder.Pid, preOrder.MainId}
		db.Transaction(func(tx *gorm.DB) error {
			if err := db.Model(&DbModels.LinePreOrder{}).Where("id IN ? AND status=6", ids).Update("status", 9).Error; err != nil {
				logger.Errorf("平仓订单回调失败, 回调订单号：%s 更新主单失败:%v", preOrder.OrderSn, err)
				return err
			}

			if err := db.Model(&DbModels.LinePreOrder{}).Where("main_id =? AND status=0", preOrder.MainId).Update("status", 4).Error; err != nil {
				logger.Errorf("平仓订单回调失败, 回调订单号：%s 更新子单失败:%v", preOrder.OrderSn, err)
				return err
			}

			return nil
		})
	}

	removeSpotLossAndAddPosition(preOrder.MainId, preOrder.OrderSn)
	removePosition(db, preOrder)

	spotApi := SpotRestApi{}
	apiUserInfo, _ := GetApiInfo(preOrder.ApiId)

	if apiUserInfo.Id > 0 {
		req := CancelOpenOrdersReq{
			Symbol: preOrder.Symbol,
			ApiId:  preOrder.ApiId,
		}
		if err := spotApi.CancelOpenOrders(db, req); err != nil {
			logger.Errorf("平仓单成功 取消其它订单失败 订单号：%s:", err)
		}
	}
}

// 止盈成交
func handleSpotTakeProfitFilled(db *gorm.DB, preOrder *DbModels.LinePreOrder) {
	childCount, _ := GetChildTpOrder(db, preOrder.Id)

	if childCount > 0 {
		extOrderId := preOrder.Pid //ext主单id
		positionData := savePosition(db, preOrder)
		processTakeProfitAndStopLossOrders(db, preOrder, &positionData, extOrderId, false)
	} else {
		removeSpotLossAndAddPosition(preOrder.MainId, preOrder.OrderSn)

		spotApi := SpotRestApi{}
		apiUserInfo, _ := GetApiInfo(preOrder.ApiId)

		if apiUserInfo.Id > 0 {
			req := CancelOpenOrdersReq{
				Symbol: preOrder.Symbol,
				ApiId:  preOrder.ApiId,
			}
			if err := spotApi.CancelOpenOrders(db, req); err != nil {
				logger.Errorf("止盈单成功 取消其它订单失败 订单号：%s:", err)
			}
		}

		removePosition(db, preOrder)

		db.Transaction(func(tx *gorm.DB) error {
			ids := []int{preOrder.Pid, preOrder.MainId}
			if err := db.Model(&DbModels.LinePreOrder{}).Where("id IN ? AND status =6 AND order_type=0", ids).Update("status", 9).Error; err != nil {
				logger.Errorf("止盈订单回调失败, 回调订单号：%s 更新主单失败:%v", preOrder.OrderSn, err)

				return err
			}

			return nil
		})
	}
}

// 移除仓位信息
func removePosition(db *gorm.DB, preOrder *DbModels.LinePreOrder) {
	positionMangement := positionservice.BinancePositionManagement{}
	positionMangement.Orm = db
	positionDelReq := positiondto.LinePreOrderPositioinDelReq{
		ApiId:        preOrder.ApiId,
		SymbolType:   preOrder.SymbolType,
		Symbol:       preOrder.Symbol,
		ExchangeType: preOrder.ExchangeType,
	}

	if preOrder.Site == "BUY" {
		positionDelReq.Side = "SELL"
	} else {
		positionDelReq.Side = "BUY"
	}

	positionMangement.RemovePosition(&positionDelReq)
}

// 清理待加仓、待止损、待减仓缓存
func removeSpotLossAndAddPosition(mainId int, orderSn string) {
	stoplossKey := fmt.Sprintf(rediskey.SpotStopLossList, global.EXCHANGE_BINANCE)
	stoplossVal, _ := helper.DefaultRedis.GetAllList(stoplossKey)
	addPositionKey := fmt.Sprintf(rediskey.SpotAddPositionList, global.EXCHANGE_BINANCE)
	addPositionVal, _ := helper.DefaultRedis.GetAllList(addPositionKey)
	reduceKey := fmt.Sprintf(rediskey.SpotReduceList, global.EXCHANGE_BINANCE)
	reduceVal, _ := helper.DefaultRedis.GetAllList(reduceKey)
	stoploss := dto.StopLossRedisList{}
	addPosition := positiondto.AddPositionList{}
	reduce := positiondto.ReduceListItem{}

	//移除减仓后减仓策略
	RemoveReduceReduceCacheByMainId(mainId, 1)

	//止损缓存
	for _, v := range stoplossVal {
		sonic.Unmarshal([]byte(v), &stoploss)
		if stoploss.MainId == mainId {
			_, err := helper.DefaultRedis.LRem(stoplossKey, v)

			if err != nil {
				logger.Errorf("订单回调失败, 回调订单号：%s 删除止损缓存失败:%v", orderSn, err)
			}
		}
	}

	//加仓缓存
	for _, v := range addPositionVal {
		sonic.Unmarshal([]byte(v), &addPosition)
		if addPosition.MainId == mainId {
			_, err := helper.DefaultRedis.LRem(addPositionKey, v)

			if err != nil {
				logger.Errorf("订单回调失败, 回调订单号：%s 删除加仓缓存失败:%v", orderSn, err)
			}
		}
	}

	//减仓缓存
	for _, v := range reduceVal {
		sonic.Unmarshal([]byte(v), &reduce)
		if reduce.MainId == mainId {
			_, err := helper.DefaultRedis.LRem(reduceKey, v)

			if err != nil {
				logger.Errorf("订单回调失败, 回调订单号：%s 删除减仓缓存失败:%v", orderSn, err)
			}
		}
	}
}

// 主单成交
func handleMainOrderFilled(db *gorm.DB, preOrder *DbModels.LinePreOrder) {
	//修改持仓信息
	positionData := savePosition(db, preOrder)

	if preOrder.OrderCategory == 3 {
		if err := db.Model(&DbModels.LinePreOrderStatus{}).Where("order_id = ?", preOrder.MainId).Update("add_position_status", 1).Error; err != nil {
			logger.Errorf("更新主单加仓状态失败, 主单号：%s, 错误信息：%v", preOrder.MainId, err)
		}
	} else {
		mainOrders, _ := getOpenPositionMainOrderId(db, preOrder.Id, preOrder.ApiId, preOrder.SymbolType, preOrder.ExchangeType, preOrder.Symbol, preOrder.Site)

		if len(mainOrders) > 0 {
			mainIds := []int{}
			for _, mainOrder := range mainOrders {
				mainId := mainOrder.Id

				if mainOrder.MainId > 0 {
					mainId = mainOrder.MainId
				}

				removeSpotLossAndAddPosition(mainId, mainOrder.OrderSn)

				if !utility.ContainsInt(mainIds, mainId) {
					mainIds = append(mainIds, mainId)
				}
			}

			spotApi := SpotRestApi{}
			err := spotApi.CancelOpenOrdersLoop(db, CancelOpenOrdersReq{ApiId: preOrder.ApiId, Symbol: preOrder.Symbol}, 4)

			if err != nil {
				logger.Errorf("取消未成交订单失败, 交易对:%s 主单号：%s, 错误信息：%v", preOrder.Symbol, preOrder.MainId, err)
			} else if len(mainIds) > 0 {
				if err := db.Exec("UPDATE line_pre_order SET `status`=4,`desc`=CONCAT(`desc`, ' 新单触发取消') WHERE id IN ? AND `status` =6", mainIds).Error; err != nil {
					logger.Errorf("新下单成功后更新主单取消状态失败, 新主单号：%s, 错误信息：%v", preOrder.MainId, err)
				}
			}
		}

		//加仓待触发
		addPositionOrders := make([]DbModels.LinePreOrder, 0)

		if err := db.Model(&DbModels.LinePreOrder{}).Where("main_id =? AND order_category=3 AND order_type=0 AND status=0", preOrder.Id).Order("rate asc").Find(&addPositionOrders).Error; err != nil {
			logger.Errorf("handleMainReduceFilled 获取加仓单失败,订单号:%s err:%v", preOrder.OrderSn, err)
		}

		keySpotAddPosition := fmt.Sprintf(rediskey.SpotAddPositionList, global.EXCHANGE_BINANCE)

		for _, addPositionOrder := range addPositionOrders {
			addPositionData := positiondto.AddPositionList{
				Id:         addPositionOrder.Id,
				OrderSn:    addPositionOrder.OrderSn,
				MainId:     addPositionOrder.MainId,
				Pid:        addPositionOrder.Pid,
				Price:      utility.StrToDecimal(addPositionOrder.Price),
				ApiId:      addPositionOrder.ApiId,
				Symbol:     addPositionOrder.Symbol,
				Side:       addPositionOrder.Site,
				SymbolType: addPositionOrder.SymbolType,
			}

			addVal, err := sonic.MarshalString(addPositionData)

			if err != nil {
				logger.Errorf("handleMainReduceFilled 序列化加仓单失败,订单号:%s err:%v", preOrder.OrderSn, err)
				return
			}

			if err := helper.DefaultRedis.RPushList(keySpotAddPosition, addVal); err != nil {
				logger.Errorf("handleMainReduceFilled 添加加仓单失败,订单号:%s err:%v", preOrder.OrderSn, err)
			}
		}

	}

	processTakeProfitAndStopLossOrders(db, preOrder, &positionData, preOrder.Id, true)

}

// 解析订单状态
// 5:委托中 6:完全成交 4:取消 9:已平仓
func parseOrderStatus(status interface{}, mapData map[string]interface{}) (int, string) {
	reason, _ := mapData["r"].(string)

	if strings.ToLower(reason) == "none" {
		reason = ""
	}

	switch status {
	case "NEW": // 未成交
		return 5, reason
	case "FILLED": // 完全成交
		return 6, reason
	case "CANCELED", "EXPIRED", "EXPIRED_IN_MATCH": // 取消
		return 4, reason
	default:
		return 0, reason
	}
}

// 更新订单状态
func updateOrderStatus(db *gorm.DB, preOrder *models.LinePreOrder, status int, reason string, isSpot bool, mapData map[string]interface{}) error {
	params := map[string]interface{}{"status": strconv.Itoa(status), "desc": reason}

	switch isSpot {
	case true:
		total := decimal.Zero
		totalAmount := decimal.Zero
		if totalStr, ok := mapData["Z"].(string); ok {
			total, _ = decimal.NewFromString(totalStr)
		}
		if totalAmountStr, ok := mapData["z"].(string); ok {
			totalAmount, _ = decimal.NewFromString(totalAmountStr)
		}

		//主单 修改单价 和成交数量
		if total.Cmp(decimal.Zero) > 0 && totalAmount.Cmp(decimal.Zero) > 0 {
			num := totalAmount.Mul(decimal.NewFromFloat(0.998))
			params["num"] = num
			params["price"] = total.Div(totalAmount)
			preOrder.Num = num.String()
			preOrder.Price = total.Div(totalAmount).String()
		}
	case false:
		status, _ := mapData["X"].(string)

		if status == "FILLED" {
			num, _ := decimal.NewFromString(mapData["z"].(string))
			params["num"] = num.Mul(decimal.NewFromFloat(0.998)).String()
			price, _ := mapData["ap"].(string)

			params["price"] = price
			preOrder.Num = num.String()
			preOrder.Price = price
		}
	}

	return db.Model(&DbModels.LinePreOrder{}).Where("order_sn = ? AND status < 6", preOrder.OrderSn).
		Updates(params).Error
}

// 主单成交 处理止盈止损订单
// preOrder 主单
// positionData 持仓缓存信息
// fist 首次止盈止损
func processTakeProfitAndStopLossOrders(db *gorm.DB, preOrder *models.LinePreOrder, positionData *positiondto.PositionDto, extOrderId int, fist bool) {
	orders := []models.LinePreOrder{}
	tradeSet, _ := cacheservice.GetTradeSet(global.EXCHANGE_BINANCE, preOrder.Symbol, 1)
	mainId := preOrder.Id

	if preOrder.MainId > 0 {
		mainId = preOrder.MainId
	}

	if tradeSet.Coin == "" {
		logger.Error("获取交易对失败")
		return
	}

	apiInfo, err := GetApiInfo(preOrder.ApiId)
	if apiInfo.Id == 0 {
		logger.Error("订单回调查询apiuserinfo失败 err:", err)
		return
	}

	if preOrder.OrderCategory == 3 {
		if err := cancelSymbolTakeAndStop(db, preOrder.MainId, preOrder.SymbolType); err != nil {
			logger.Errorf("取消止盈止损订单失败 orderSn:%s err:%v", preOrder.OrderSn, err)
		}
	}

	num := getSpotPositionAvailableQuantity(db, apiInfo, preOrder, tradeSet)

	if err := db.Model(&DbModels.LinePreOrder{}).
		Where("pid = ? AND order_type > 0 AND status = '0' ", preOrder.Id).
		Find(&orders).Error; err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		logger.Error("订单回调查询止盈止损单失败:", err)
		return
	}

	price := utility.StrToDecimal(preOrder.Price)
	spotApi := SpotRestApi{}
	orderExt := models.LinePreOrderExt{}
	db.Model(&orderExt).Where("order_id =?", extOrderId).First(&orderExt)
	totalNum := num.Mul(decimal.NewFromFloat(0.998)).Truncate(int32(tradeSet.AmountDigit))
	num = totalNum

	//止盈止损
	for _, order := range orders {
		order.Num = num.String()

		if fist && (order.OrderCategory == 3 || order.OrderCategory == 1) && order.OrderType == 1 && orderExt.TakeProfitNumRatio.Cmp(decimal.Zero) > 0 && orderExt.TakeProfitNumRatio.Cmp(decimal.NewFromInt(100)) != 0 {
			//主单第一次且止盈数量不是100% 止盈数量
			order.Num = num.Mul(orderExt.TakeProfitNumRatio.Div(decimal.NewFromInt(100))).Truncate(int32(tradeSet.AmountDigit)).String()
		}

		if err := db.Model(&order).Update("num", order.Num).Error; err != nil {
			logger.Errorf("修改止盈止损数量失败 订单号:%s err:%v", order.OrderSn, err)
		}

		switch order.OrderType {
		case 1: // 止盈
			//亏损大于0 重新计算比例
			if fist && positionData.TotalLoss.Cmp(decimal.Zero) > 0 && orderExt.Id > 0 && preOrder.SignPriceType != "mixture" {
				percentag := positionData.TotalLoss.Div(num).Div(price).Mul(decimal.NewFromInt(100))

				if fist {
					percentag = percentag.Add(orderExt.TakeProfitRatio).Truncate(2)
				}

				order.Rate = percentag.String()
				percentag = percentag.Div(decimal.NewFromInt(100))
				order.Price = price.Mul(decimal.NewFromInt(1).Add(percentag)).Truncate(int32(tradeSet.PriceDigit)).String()
			} else if !fist && orderExt.TpTpPriceRatio.Cmp(decimal.Zero) > 0 {
				percentag := orderExt.TpTpPriceRatio
				order.Rate = percentag.String()
				order.Price = price.Mul(decimal.NewFromInt(1).Add(percentag.Div(decimal.NewFromInt(100)))).Truncate(int32(tradeSet.PriceDigit)).String()
			}

			processTakeProfitOrder(db, spotApi, order)
		case 2: // 止损
			if !fist {
				order.Rate = orderExt.TpSlPriceRatio.Truncate(2).String()
				order.Price = price.Mul(decimal.NewFromInt(1).Sub(orderExt.TpSlPriceRatio.Div(decimal.NewFromInt(100)))).Truncate(int32(tradeSet.PriceDigit)).String()
			}

			processStopLossOrder(order)
		}
	}

	//待触发减仓单
	nextSpotReduceTrigger(db, mainId, totalNum, tradeSet)
}

// 根据下单百分比计算价格
func SetPrice(order *models.LinePreOrder, preOrder *models.LinePreOrder, tradeSet models2.TradeSet) {
	orderType := order.OrderType
	itemSide := order.Site
	rate := utility.StrToDecimal(order.Rate)

	switch {
	//做多止盈、做空止损或减仓
	case (orderType == 1 && itemSide == "SELL"), ((orderType == 2 || orderType == 4) && itemSide == "BUY"):
		order.Price = utility.StrToDecimal(preOrder.Price).Mul(decimal.NewFromInt(1).Add(rate.Div(decimal.NewFromInt(100)))).Truncate(int32(tradeSet.PriceDigit)).String()
	//做多止损或减仓、做空止盈
	case ((orderType == 2 || orderType == 4) && itemSide == "SELL"), (orderType == 1 && itemSide == "BUY"):
		order.Price = utility.StrToDecimal(preOrder.Price).Mul(decimal.NewFromInt(1).Sub(rate.Div(decimal.NewFromInt(100)))).Truncate(int32(tradeSet.PriceDigit)).String()
	}
}

// 现货减仓
func processSpotReduceOrder(preOrder DbModels.LinePreOrder) {
	key := fmt.Sprintf(rediskey.SpotReduceList, global.EXCHANGE_BINANCE)
	item := positiondto.ReduceListItem{
		Id:      preOrder.Id,
		ApiId:   preOrder.ApiId,
		Pid:     preOrder.Pid,
		MainId:  preOrder.MainId,
		Price:   utility.StrToDecimal(preOrder.Price),
		Num:     utility.StrToDecimal(preOrder.Num),
		Side:    preOrder.Site,
		Symbol:  preOrder.Symbol,
		OrderSn: preOrder.OrderSn,
	}

	itemVal, err := sonic.MarshalString(&item)

	if err != nil {
		logger.Errorf("序列化失败 err:%v", err)
		return
	}

	if err := helper.DefaultRedis.RPushList(key, itemVal); err != nil {
		logger.Errorf("减仓单写入缓存失败 err:%v", err)
		return
	}
}

// 处理止盈订单
func processTakeProfitOrder(db *gorm.DB, spotApi SpotRestApi, order models.LinePreOrder) {
	tradeSet, _ := cacheservice.GetTradeSet(global.EXCHANGE_BINANCE, order.Symbol, 1)

	if tradeSet.Coin == "" {
		logger.Error("获取交易对失败")
		return
	}

	price, _ := decimal.NewFromString(order.Price)

	params := OrderPlacementService{
		ApiId:            order.ApiId,
		Symbol:           order.Symbol,
		Side:             order.Site,
		Price:            price.Truncate(int32(tradeSet.PriceDigit)),
		Quantity:         utility.StrToDecimal(order.Num),
		Type:             "TAKE_PROFIT_LIMIT",
		TimeInForce:      "GTC",
		StopPrice:        price.Truncate(int32(tradeSet.PriceDigit)),
		NewClientOrderId: order.OrderSn,
	}

	err := spotApi.OrderPlaceLoop(db, params, 3)

	if err != nil {
		logger.Error("现货止盈下单失败:", order.OrderSn, " err:", err)
		if err = db.Model(&DbModels.LinePreOrder{}).Where("id = ?", order.Id).
			Updates(map[string]interface{}{"status": "2", "desc": err.Error(), "num": params.Quantity, "rate": order.Rate, "price": order.Price}).Error; err != nil {
			logger.Error("现货止盈下单失败,更新状态失败:", order.OrderSn, " err:", err)
		}
	} else {
		if err := db.Model(&DbModels.LinePreOrder{}).Where("id =? ", order.Id).
			Updates(map[string]interface{}{"trigger_time": time.Now(), "rate": order.Rate, "price": order.Price, "num": order.Num}).Error; err != nil {
			logger.Error("更新现货止盈单触发事件 ordersn:", order.OrderSn)
		}
		if err := db.Model(&DbModels.LinePreOrder{}).Where("id = ? and status ='0'", order.Id).
			Updates(map[string]interface{}{"status": "1"}).Error; err != nil {
			logger.Error("现货止盈下单成功,更新状态失败:", order.OrderSn, " err:", err)
		}
	}
}

// 止盈 放到止盈缓存中
// Deprecated 暂时没用
func processTakeProfitOrderNew(db *gorm.DB, spotApi SpotRestApi, order models.LinePreOrder) error {
	tradeSet, _ := cacheservice.GetTradeSet(global.EXCHANGE_BINANCE, order.Symbol, 1)

	if tradeSet.Coin == "" {
		logger.Error("获取交易对失败")
		return errors.New("获取交易对失败")
	}

	price, _ := decimal.NewFromString(order.Price)
	price = price.Truncate(int32(tradeSet.PriceDigit))
	takeOrder := dto.TakeProfitRedisList{
		Id:            order.Id,
		MainId:        order.MainId,
		PId:           order.Pid,
		ApiId:         order.ApiId,
		OrderTye:      order.OrderType,
		OrderCategory: order.OrderCategory,
		Price:         price,
		SymbolType:    order.SymbolType,
		Symbol:        order.Symbol,
		Site:          order.Site,
		OrderSn:       order.OrderSn,
	}

	takeVal, _ := sonic.MarshalString(&takeOrder)
	takeKey := fmt.Sprintf(rediskey.SpotTakeList, global.EXCHANGE_BINANCE)

	if takeVal == "" {
		return errors.New("takeVal is empty")
	}

	if err := helper.DefaultRedis.RPushList(takeKey, takeVal); err != nil {
		return err
	}

	return nil
}

// 处理止损订单
func processStopLossOrder(order models.LinePreOrder) error {
	price := utility.StrToDecimal(order.Price)
	stopLoss := dto.StopLossRedisList{
		Id:            order.Id,
		MainId:        order.MainId,
		PId:           order.Pid,
		ApiId:         order.ApiId,
		OrderTye:      order.OrderType,
		OrderCategory: order.OrderCategory,
		Price:         price,
		SymbolType:    order.SymbolType,
		Symbol:        order.Symbol,
		Site:          order.Site,
	}

	stoplossVal, _ := sonic.MarshalString(&stopLoss)
	stoplossKey := fmt.Sprintf(rediskey.SpotStopLossList, global.EXCHANGE_BINANCE)

	if stoplossVal == "" {
		return errors.New("stoplossVal is empty")
	}

	if err := helper.DefaultRedis.RPushList(stoplossKey, stoplossVal); err != nil {
		return err
	}

	return nil
}

// 处理止损订单 直接下到交易所
// Deprecated: 现在不用，等止盈不挂到交易所得时候使用
func processStopLossOrderNew(db *gorm.DB, spotApi SpotRestApi, order models.LinePreOrder) error {
	tradeSet, _ := cacheservice.GetTradeSet(global.EXCHANGE_BINANCE, order.Symbol, 1)

	if tradeSet.Coin == "" {
		logger.Error("获取交易对失败")
		return errors.New("获取交易对失败")
	}

	price, _ := decimal.NewFromString(order.Price)

	params := OrderPlacementService{
		ApiId:            order.ApiId,
		Symbol:           order.Symbol,
		Side:             order.Site,
		Price:            price.Truncate(int32(tradeSet.PriceDigit)),
		Quantity:         utility.StrToDecimal(order.Num),
		Type:             "STOP_LOSS_LIMIT",
		TimeInForce:      "GTC",
		StopPrice:        price.Truncate(int32(tradeSet.PriceDigit)),
		NewClientOrderId: order.OrderSn,
	}

	err := spotApi.OrderPlaceLoop(db, params, 3)

	if err != nil {
		logger.Error("现货止损下单失败:", order.OrderSn, " err:", err)
		if err = db.Model(&DbModels.LinePreOrder{}).Where("id = ?", order.Id).
			Updates(map[string]interface{}{"status": "2", "desc": err.Error(), "num": params.Quantity, "rate": order.Rate, "price": order.Price}).Error; err != nil {
			logger.Error("现货止损下单失败,更新状态失败:", order.OrderSn, " err:", err)
		}
	} else {
		if err := db.Model(&DbModels.LinePreOrder{}).Where("id =? ", order.Id).
			Updates(map[string]interface{}{"trigger_time": time.Now(), "rate": order.Rate, "price": order.Price, "num": order.Num}).Error; err != nil {
			logger.Error("更新现货止损单触发事件 ordersn:", order.OrderSn)
		}
		if err := db.Model(&DbModels.LinePreOrder{}).Where("id = ? and status ='0'", order.Id).
			Updates(map[string]interface{}{"status": "1"}).Error; err != nil {
			logger.Error("现货止损下单成功,更新状态失败:", order.OrderSn, " err:", err)
		}
	}

	return nil
}

// 循环取消订单
func CancelOpenOrderByOrderSnLoop(apiInfo DbModels.LineApiUser, symbol string, orderSn string) error {
	spotApi := SpotRestApi{}
	var err error
	for x := 1; x <= 4; x++ {
		err = spotApi.CancelOpenOrderByOrderSn(apiInfo, symbol, orderSn)

		if err == nil || strings.Contains(err.Error(), "该交易对没有订单") {
			err = nil
			break
		}

		time.Sleep(time.Millisecond * 300)
	}
	return err
}

// NEW
// PENDING_NEW
// PARTIALLY_FILLED
// FILLED
// CANCELED
// PENDING_CANCEL
// REJECTED
// EXPIRED
// EXPIRED_IN_MATCH
