package binanceservice

import (
	"go-admin/app/admin/models"
	DbModels "go-admin/app/admin/models"

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

// 获取订单明细
func GetOrderById(db *gorm.DB, id int) (DbModels.LinePreOrder, error) {
	result := DbModels.LinePreOrder{}

	if err := db.Model(&result).Where("id =?", id).First(&result).Error; err != nil {
		return result, err
	}

	return result, nil
}

// 获取已开仓的对冲单、对冲加仓单id
// pid：主单id
// coverType：对冲类型 1-现货对合约 2-合约对合约 3-合约对现货
func GetHedgeOpenOrderIds(db *gorm.DB, pid int, coverType int) ([]int, error) {
	result := make([]DbModels.LinePreOrder, 0)
	resultIds := make([]int, 0)
	query := db.Model(&result)

	switch coverType {
	case 3:
		query = query.Where("pid =? AND order_type in ('7','8','10','11') AND operate_type =1 AND status in ('9','13')", pid)
	case 2, 1:
		query = query.Where("pid =? AND order_type in ('10','11') AND operate_type =1 AND status ='13'", pid)
	}

	if err := query.Select("id").Find(&result).Error; err != nil {
		return resultIds, err
	}

	for _, v := range result {
		resultIds = append(resultIds, v.Id)
	}

	return resultIds, nil
}

// 获得对冲单
func GetHedgeOpenOrder(db *gorm.DB, pid int, coverType int) (DbModels.LinePreOrder, error) {
	result := DbModels.LinePreOrder{}
	orderType := ""

	switch coverType {
	case 1:
		orderType = "7"
	case 2, 3:
		orderType = "10"
	}

	if err := db.Model(&result).Where("pid =? AND order_type =? AND operate_type =1", pid, orderType).First(&result).Error; err != nil {
		return result, err
	}

	return result, nil
}

// 获取止损单
func GetStopOrder(db *gorm.DB, pid int) (DbModels.LinePreOrder, error) {
	result := DbModels.LinePreOrder{}

	if err := db.Model(&result).Where("pid =? AND order_type in ('4','6')", pid).First(&result).Error; err != nil {
		return result, err
	}

	return result, nil
}

// 获取最后一条对冲的下单百分比
func GetStopOrderRate(db *gorm.DB, pid int) (decimal.Decimal, error) {
	var result decimal.Decimal

	if err := db.Model(&DbModels.LinePreOrder{}).Where("pid =? AND order_type in ('7','10')", pid).
		Select("rate").
		Order("id DESC").
		First(&result).Error; err != nil {
		return result, err
	}
	return result, nil
}

// 获取最后一条对冲
func GetLastStop(db *gorm.DB, pid int) (DbModels.LinePreOrder, error) {
	result := models.LinePreOrder{}

	if err := db.Model(&result).
		Joins("JOIN line_pre_order as o ON o.id = line_pre_order.pid AND o.status in ('9','13')").
		Where("line_pre_order.pid =?  AND line_pre_order.order_type in ('7','10')", pid).
		Order("line_pre_order.id DESC").Select("line_pre_order.*").First(&result).Error; err != nil {
		return result, err
	}

	return result, nil
}

// 获取主单配置
// mainId 主单Id
func GetOrderExts(db *gorm.DB, mainId int) ([]models.LinePreOrderExt, error) {
	result := make([]models.LinePreOrderExt, 0)
	if err := db.Model(&result).Where("main_order_id =?", mainId).Find(&result).Error; err != nil {
		return result, err
	}

	return result, nil
}

// 获取主单累计亏损
func GetTotalLossAmount(db *gorm.DB, mainId int) (decimal.Decimal, error) {
	var totalLossAmountU decimal.Decimal

	if err := db.Model(&DbModels.LinePreOrder{}).
		Where("main_id =? AND order_type =0", mainId).
		Select("sum(loss_amount)").
		Find(&totalLossAmountU).Error; err != nil {
		logger.Error("查询主订单的实际亏损总金额失败:", err)
		return totalLossAmountU, err
	}

	return totalLossAmountU, nil
}

// 获取交易对的 委托中的止盈止损
// mainId 主单id
// symbolType 交易对类型
func GetSymbolTakeAndStop(db *gorm.DB, mainId int, symbolType int) ([]models.LinePreOrder, error) {
	result := make([]models.LinePreOrder, 0)
	if err := db.Model(&DbModels.LinePreOrder{}).Where("main_id =? AND order_type IN (1,2,4) AND symbol_type =? AND status !=0 AND status !=4", mainId, symbolType).Find(&result).Error; err != nil {
		return result, err
	}

	return result, nil
}

// 获取交易对触发数量
// symbol 交易对
// symbolType 交易对类型 1-现货 2-合约
func GetSymbolTriggerCount(db *gorm.DB, symbol string, apiId, symbolType int) (int64, error) {
	var count int64

	if err := db.Model(&models.LinePreOrder{}).Where("symbol =? AND api_id =? AND symbol_type =? AND order_type =0 AND pid=0 AND status IN (1,5,6)", symbol, apiId, symbolType).Count(&count).Error; err != nil {
		logger.Error("查询交易对触发数量失败:", err)
		return count, err
	}

	return count, nil
}

//获取已开仓的
// func GetOpenedOrders(db *gorm.DB, apiId int, exchange, symbol, symbolType, side string) ([]models.LinePreOrder, error) {

// }

// 获取子订单止盈止损数量
func GetChildTpOrder(db *gorm.DB, pid int) (int, error) {
	var count int64

	if err := db.Model(&models.LinePreOrder{}).Where("pid =? AND order_type>0 and order_type <3 and status =0", pid).Count(&count).Error; err != nil {
		return 0, err
	}

	return int(count), nil
}
