package model

import (
	"errors"
	"gorm.io/gorm"
	"time"
	"wss-pool/internal/data"
)

const (
	AlreadyReceivedForex int = 2 //已领取
	UnclaimedForex       int = 1 //未领取
	ModifyForex          int = 0 //插针
	Forex                int = 1 //外汇
)

type ForexMarket struct {
	ID          int64    `gorm:"column:id;"json:"id"`
	TradeName   string   `gorm:"column:trade_name;"  json:"name"`
	BeginTime   string   `gorm:"column:begin_time;" json:"begin_time"`
	Step        int      `gorm:"column:step;" json:"step"`
	EndTime     string   `gorm:"column:end_time;" json:"end_time"`
	MaxPrice    string   `gorm:"column:max_price" json:"max_price"`
	IsType      int      `gorm:"column:type;not null;type:tinyint(2);COMMENT:'0 插针 1 自发合约 2 自发现货 '"json:"type"`
	IsGet       int      `gorm:"column:is_get;not null;type:tinyint(2);DEFAULT:1;COMMENT:'是否完成(1 未领取 2 已领取 ) '"json:"is_get"`
	KeepDecimal int      `gorm:"column:keep_decimal; '"json:"keep_decimal"`
	Gorm        *gorm.DB `gorm:"-" json:"-"`
}

func (this *ForexMarket) TableName() string {
	return "bot_forex_market"
}

func NewForexMarket() *ForexMarket {
	ForexMarket := &ForexMarket{}
	ForexMarket.NewGorm()
	return ForexMarket
}

func (this *ForexMarket) NewGorm() *ForexMarket {
	this.Gorm = data.WebGorm.Table(this.TableName())
	return this
}

func (this *ForexMarket) WhereBegin() *ForexMarket {

	this.Gorm = this.Gorm.Table(this.TableName()).Where("begin_time >= ?", time.Now().Format("2006-01-02 15:04:05"))
	return this
}

func (this *ForexMarket) WhereID() *ForexMarket {
	if this.ID == 0 {
		this.Gorm.Error = errors.New("param is null")
		return this
	}
	this.Gorm = this.Gorm.Table(this.TableName()).Where(&ForexMarket{ID: this.ID})
	return this
}

func (this *ForexMarket) WhereName() *ForexMarket {
	if this.TradeName == "" {
		this.Gorm.Error = errors.New("param is null")
		return this
	}
	this.Gorm = this.Gorm.Table(this.TableName()).Where(&ForexMarket{TradeName: this.TradeName})
	return this
}

func (this *ForexMarket) WhereModifyForex() *ForexMarket {
	this.Gorm = this.Gorm.Table(this.TableName()).Where("`bot_forex_market`.`type` = ?", ModifyForex)
	return this
}

func (this *ForexMarket) WhereInID(ids []int64) *ForexMarket {
	if len(ids) <= 0 {
		this.Gorm.Error = errors.New("param is null")
		return this
	}
	this.Gorm = this.Gorm.Table(this.TableName()).Where("id in (?)", ids)
	return this
}

func (this *ForexMarket) WhereIsTyep() *ForexMarket {
	if this.IsType <= 0 {
		this.Gorm.Error = errors.New("param is null")
		return this
	}
	this.Gorm = this.Gorm.Table(this.TableName()).Where(&ForexMarket{IsType: this.IsType})
	return this
}

func (this *ForexMarket) WhereIsGet() *ForexMarket {
	if this.IsGet == 0 {
		this.Gorm.Error = errors.New("param is null")
		return this
	}
	this.Gorm = this.Gorm.Table(this.TableName()).Where(&ForexMarket{IsGet: this.IsGet})
	return this
}

func (this *ForexMarket) Update(record *ForexMarket) *ForexMarket {
	this.Gorm = this.Gorm.Updates(record)
	return this
}

func (this *ForexMarket) Create() *ForexMarket {
	this.Gorm = this.Gorm.Create(&this)
	return this
}
func (this *ForexMarket) Joins(sql string) *ForexMarket {
	this.Gorm = this.Gorm.Joins(sql)
	return this
}

func (this *ForexMarket) Limit(limit int) *ForexMarket {
	this.Gorm = this.Gorm.Limit(limit)
	return this
}
func (this *ForexMarket) Offset(Offset int) *ForexMarket {
	this.Gorm = this.Gorm.Offset(Offset)
	return this
}
func (this *ForexMarket) First() *ForexMarket {
	Accounts := ForexMarket{}
	this.Gorm = this.Gorm.First(&Accounts)
	return &Accounts
}

func (this *ForexMarket) Assign(ForexMarket *ForexMarket) *ForexMarket {
	this.Gorm = this.Gorm.Assign(ForexMarket)
	return this
}

func (this *ForexMarket) FirstOrCreate() *ForexMarket {
	this.Gorm = this.Gorm.FirstOrCreate(&this)
	return this
}

func (this *ForexMarket) Order(value interface{}) *ForexMarket {
	this.Gorm = this.Gorm.Order(value)
	return this
}

func (this *ForexMarket) Count() int64 {
	var num int64
	this.Gorm = this.Gorm.Count(&num)
	return num
}
func (this *ForexMarket) Select(column string) *ForexMarket {
	this.Gorm = this.Gorm.Select(column)
	return this
}

func (this *ForexMarket) WhereTime(LoginAt string) *ForexMarket {
	if LoginAt == "" {
		this.Gorm.Error = errors.New("date require ")
		return this
	}
	this.Gorm = this.Gorm.Table(this.TableName()).Where("login_at = ?", LoginAt)
	return this
}

func (this *ForexMarket) Pluck(column string, value interface{}) *ForexMarket {
	this.Gorm = this.Gorm.Pluck(column, value)
	return this
}

func (this *ForexMarket) Find(list *[]ForexMarket) *ForexMarket {
	this.Gorm = this.Gorm.Find(&list)
	return this
}

func (this *ForexMarket) List() (result []ForexMarket) {
	this.IsGet = UnclaimedForex
	this.WhereIsTyep().WhereIsGet().WhereBegin().WhereName().Order("id asc").Find(&result)
	return result
}

func (this *ForexMarket) ListModifyForex() (result []ForexMarket) {
	this.IsGet = UnclaimedForex
	this.Select("l.keep_decimal,bot_forex_market.*").WhereModifyForex().Joins("inner join bot_forex_list  l on l.trade_name = bot_forex_market.trade_name").WhereIsGet().Order("id asc").Find(&result)
	return result
}

func (this *ForexMarket) UpdateIsGet(ids []int64) error {
	this.IsGet = UnclaimedForex
	return this.WhereInID(ids).WhereIsGet().Update(&ForexMarket{IsGet: AlreadyReceivedForex}).Gorm.Error
}

func (this *ForexMarket) UpdateIsGetOne() error {
	this.IsGet = UnclaimedForex
	return this.WhereID().WhereIsGet().Update(&ForexMarket{IsGet: AlreadyReceivedForex}).Gorm.Error
}