package trade_analysis

import (
	"github.com/beego/beego/v2/client/orm"
	"time"
)

// BaseFromTradeClassify 交易所分类表
type BaseFromTradeClassify struct {
	Id           uint64    `orm:"column(id);pk"`
	ClassifyName string    //分类名称
	ClassifyType string    //分类名称下的类型
	Exchange     string    //交易所
	LatestDate   time.Time //数据最近的日期
	CreateTime   time.Time //插入时间
	ModifyTime   time.Time //修改时间
}

// GetAllBaseFromTradeClassify 获取所有的交易所分类列表
func GetAllBaseFromTradeClassify() (list []*BaseFromTradeClassify, err error) {
	sql := `SELECT * FROM base_from_trade_classify   `

	o := orm.NewOrmUsingDB("data")
	_, err = o.Raw(sql).QueryRows(&list)

	return
}

func GetTradeTopLastDataTime(exchange string, classifyName, classifyType string) (item *BaseFromTradeClassify, err error) {
	sql := `SELECT * FROM base_from_trade_classify WHERE exchange = ? AND classify_name=? `
	pars := make([]interface{}, 0)
	pars = append(pars, exchange)

	if exchange == "zhengzhou" {
		pars = append(pars, classifyType)
	} else {
		pars = append(pars, classifyName)
		sql += ` AND classify_type=? `
		pars = append(pars, classifyType)
	}
	sql += ` ORDER BY latest_date desc`
	o := orm.NewOrmUsingDB("data")
	err = o.Raw(sql, pars...).QueryRow(&item)

	return
}

// GetClassifyTypeByClassifyName 根据分类名称获取分类类型
func GetClassifyTypeByClassifyName(exchange, classifyName string) (item *TradeClassifyName, err error) {
	sql := `SELECT classify_name, classify_type FROM base_from_trade_classify WHERE exchange = ? AND classify_name=? `
	o := orm.NewOrmUsingDB("data")
	err = o.Raw(sql, exchange, classifyName).QueryRow(&item)

	return
}
