package dto

import (
	"errors"
	"go-admin/app/admin/models"
	"go-admin/common/dto"
	common "go-admin/common/models"
	"go-admin/pkg/utility"

	"github.com/shopspring/decimal"
)

type LineReduceStrategyItemGetPageReq struct {
	dto.Pagination `search:"-"`
	LineReduceStrategyItemOrder
}

type LineReduceStrategyItemOrder struct {
	Id               string `form:"idOrder"  search:"type:order;column:id;table:line_reduce_strategy_item"`
	ReduceStrategyId string `form:"reduceStrategyIdOrder"  search:"type:order;column:reduce_strategy_id;table:line_reduce_strategy_item"`
	LossPercent      string `form:"lossPercentOrder"  search:"type:order;column:loss_percent;table:line_reduce_strategy_item"`
	OrderType        string `form:"orderTypeOrder"  search:"type:order;column:order_type;table:line_reduce_strategy_item"`
	CreatedAt        string `form:"createdAtOrder"  search:"type:order;column:created_at;table:line_reduce_strategy_item"`
	UpdatedAt        string `form:"updatedAtOrder"  search:"type:order;column:updated_at;table:line_reduce_strategy_item"`
	DeletedAt        string `form:"deletedAtOrder"  search:"type:order;column:deleted_at;table:line_reduce_strategy_item"`
	CreateBy         string `form:"createByOrder"  search:"type:order;column:create_by;table:line_reduce_strategy_item"`
	UpdateBy         string `form:"updateByOrder"  search:"type:order;column:update_by;table:line_reduce_strategy_item"`
}

func (m *LineReduceStrategyItemGetPageReq) GetNeedSearch() interface{} {
	return *m
}

type LineReduceStrategyItem struct {
	LossPercent     decimal.Decimal `json:"lossPercent" comment:"止损百分比"`
	QuantityPercent decimal.Decimal `json:"quantityPercent" comment:"数量百分比"`
	OrderType       string          `json:"orderType" comment:"订单类型 LIMIT-限价 MARKET-市价"`
}

func (s *LineReduceStrategyItem) Valid() error {

	if s.LossPercent.Cmp(decimal.Zero) <= 0 {
		return errors.New("百分比不能小于等于0")
	}

	if s.LossPercent.Cmp(decimal.NewFromFloat(100)) >= 0 {
		return errors.New("百分比不能大于等于100")
	}

	if s.QuantityPercent.Cmp(decimal.Zero) <= 0 {
		return errors.New("百分比不能小于等于0")
	}

	if s.QuantityPercent.Cmp(decimal.NewFromInt(100)) > 0 {
		return errors.New("数量百分比不能大于等于100")
	}

	keys := []string{"LIMIT", "MARKET"}

	if !utility.ContainsStr(keys, s.OrderType) {
		return errors.New("订单类型不正确")
	}

	return nil
}

type LineReduceStrategyItemResp struct {
	ReduceStrategyId int             `json:"reduceStrategyId" comment:"减仓策略id"`
	LossPercent      decimal.Decimal `json:"lossPercent" comment:"亏损百分比"`
	OrderType        string          `json:"orderType" comment:"订单类型 LIMIT-限价 MARKET-市价"`
	Actived          int             `json:"actived" comment:"是否已减仓 1=未减仓 2=已减仓"`
}

type LineReduceStrategyItemInsertReq struct {
	Id               int             `json:"-" comment:"主键id"` // 主键id
	ReduceStrategyId int             `json:"reduceStrategyId" comment:"减仓策略id"`
	LossPercent      decimal.Decimal `json:"lossPercent" comment:"亏损百分比"`
	OrderType        string          `json:"orderType" comment:"订单类型 LIMIT-限价 MARKET-市价"`
	common.ControlBy
}

func (s *LineReduceStrategyItemInsertReq) Generate(model *models.LineReduceStrategyItem) {
	if s.Id == 0 {
		model.Model = common.Model{Id: s.Id}
	}
	model.ReduceStrategyId = s.ReduceStrategyId
	model.LossPercent = s.LossPercent
	model.OrderType = s.OrderType
	model.CreateBy = s.CreateBy // 添加这而，需要记录是被谁创建的
}

func (s *LineReduceStrategyItemInsertReq) GetId() interface{} {
	return s.Id
}

type LineReduceStrategyItemUpdateReq struct {
	Id               int             `uri:"id" comment:"主键id"` // 主键id
	ReduceStrategyId int             `json:"reduceStrategyId" comment:"减仓策略id"`
	LossPercent      decimal.Decimal `json:"lossPercent" comment:"亏损百分比"`
	OrderType        string          `json:"orderType" comment:"订单类型 LIMIT-限价 MARKET-市价"`
	common.ControlBy
}

func (s *LineReduceStrategyItemUpdateReq) Generate(model *models.LineReduceStrategyItem) {
	if s.Id == 0 {
		model.Model = common.Model{Id: s.Id}
	}
	model.ReduceStrategyId = s.ReduceStrategyId
	model.LossPercent = s.LossPercent
	model.OrderType = s.OrderType
	model.UpdateBy = s.UpdateBy // 添加这而，需要记录是被谁更新的
}

func (s *LineReduceStrategyItemUpdateReq) GetId() interface{} {
	return s.Id
}

// LineReduceStrategyItemGetReq 功能获取请求参数
type LineReduceStrategyItemGetReq struct {
	Id int `uri:"id"`
}

func (s *LineReduceStrategyItemGetReq) GetId() interface{} {
	return s.Id
}

// LineReduceStrategyItemDeleteReq 功能删除请求参数
type LineReduceStrategyItemDeleteReq struct {
	Ids []int `json:"ids"`
}

func (s *LineReduceStrategyItemDeleteReq) GetId() interface{} {
	return s.Ids
}
