package dto

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

type LineReduceStrategyGetPageReq struct {
	dto.Pagination `search:"-"`
	Name           string `form:"name"  search:"type:contains;column:name;table:line_reduce_strategy" comment:"减仓策略名称"`
	Status         string `form:"status"  search:"type:exact;column:status;table:line_reduce_strategy" comment:"状态 1-启用 2-禁用"`
	LineReduceStrategyOrder
}

type LineReduceStrategyOrder struct {
	Id        string `form:"idOrder"  search:"type:order;column:id;table:line_reduce_strategy"`
	Name      string `form:"nameOrder"  search:"type:order;column:name;table:line_reduce_strategy"`
	Status    string `form:"statusOrder"  search:"type:order;column:status;table:line_reduce_strategy"`
	CreatedAt string `form:"createdAtOrder"  search:"type:order;column:created_at;table:line_reduce_strategy"`
	UpdatedAt string `form:"updatedAtOrder"  search:"type:order;column:updated_at;table:line_reduce_strategy"`
	DeletedAt string `form:"deletedAtOrder"  search:"type:order;column:deleted_at;table:line_reduce_strategy"`
	CreateBy  string `form:"createByOrder"  search:"type:order;column:create_by;table:line_reduce_strategy"`
	UpdateBy  string `form:"updateByOrder"  search:"type:order;column:update_by;table:line_reduce_strategy"`
}

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

type LineReduceStrategyInsertReq struct {
	Id     int                      `json:"-" comment:"主键id"` // 主键id
	Name   string                   `json:"name" comment:"减仓策略名称"`
	Status int                      `json:"status" comment:"状态 1-启用 2-禁用"`
	Items  []LineReduceStrategyItem `json:"items" comment:"减仓单节点"`
	common.ControlBy
}

// 参数校验
func (s *LineReduceStrategyInsertReq) Valid() error {
	if s.Name == "" {
		return errors.New("减仓策略名称不能为空")
	}

	if s.Status < 1 || s.Status > 2 {
		return errors.New("状态值不合法")
	}

	if len(s.Items) == 0 {
		return errors.New("减仓策略节点不能为空")
	}

	for index, item := range s.Items {
		if err := item.Valid(); err != nil {
			return err
		}

		if index > 0 && item.LossPercent.Cmp(s.Items[index-1].LossPercent) <= 0 {
			return errors.New("亏损比例必须递增")
		}
	}

	return nil
}

func (s *LineReduceStrategyInsertReq) Generate(model *models.LineReduceStrategy) {
	if s.Id == 0 {
		model.Model = common.Model{Id: s.Id}
	}
	model.Name = s.Name
	model.Status = s.Status

	for _, item := range s.Items {
		strategyItem := models.LineReduceStrategyItem{}
		strategyItem.OrderType = item.OrderType
		strategyItem.LossPercent = item.LossPercent
		strategyItem.QuantityPercent = item.QuantityPercent

		model.Items = append(model.Items, strategyItem)
	}

	model.CreateBy = s.CreateBy // 添加这而，需要记录是被谁创建的
}

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

type LineReduceStrategyUpdateReq struct {
	Id     int                      `uri:"id" comment:"主键id"` // 主键id
	Name   string                   `json:"name" comment:"减仓策略名称"`
	Status int                      `json:"status" comment:"状态 1-启用 2-禁用"`
	Items  []LineReduceStrategyItem `json:"items" comment:"减仓单节点"`
	common.ControlBy
}

func (s *LineReduceStrategyUpdateReq) Generate(model *models.LineReduceStrategy) {
	if s.Id == 0 {
		model.Model = common.Model{Id: s.Id}
	}
	model.Name = s.Name
	model.Status = s.Status

	for _, item := range s.Items {
		strategyItem := models.LineReduceStrategyItem{}
		strategyItem.OrderType = item.OrderType
		strategyItem.LossPercent = item.LossPercent
		strategyItem.QuantityPercent = item.QuantityPercent

		model.Items = append(model.Items, strategyItem)
	}
	model.UpdateBy = s.UpdateBy // 添加这而，需要记录是被谁更新的
}

// 参数校验
func (s *LineReduceStrategyUpdateReq) Valid() error {
	if s.Name == "" {
		return errors.New("减仓策略名称不能为空")
	}

	if s.Status < 1 || s.Status > 2 {
		return errors.New("状态值不合法")
	}

	if len(s.Items) == 0 {
		return errors.New("减仓策略节点不能为空")
	}

	for _, item := range s.Items {
		if err := item.Valid(); err != nil {
			return err
		}
	}

	return nil
}

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

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

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

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

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