package trade_analysis

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

// 上期能源持仓榜单表
type TradePositionTop struct {
	Id            uint64    `gorm:"primaryKey;column:id" json:"id"`
	ClassifyName  string    `gorm:"column:classify_name" json:"classify_name"`     //分类名称
	ClassifyType  string    `gorm:"column:classify_type" json:"classify_type"`     //分类名称下的类型
	DealShortName string    `gorm:"column:deal_short_name" json:"deal_short_name"` //成交量公司简称
	DealValue     int       `gorm:"column:deal_value" json:"deal_value"`           //成交量
	DealChange    int       `gorm:"column:deal_change" json:"deal_change"`         //成交变化量
	DataTime      time.Time `gorm:"column:data_time" json:"data_time"`             //数据日期
	CreateTime    time.Time `gorm:"column:create_time" json:"create_time"`         //插入时间
	ModifyTime    time.Time `gorm:"column:modify_time" json:"modify_time"`         //修改时间
	DealType      int       `gorm:"column:deal_type" json:"deal_type"`             //交易类型：1多单，2空单，3净多单，4净空单
	SourceType    int       `gorm:"column:source_type" json:"source_type"`         //数据来源，0是原始数据的值，1是由T+1日推算出的值，2是由T日的榜单数据推算出的值
	Rank          int       `gorm:"column:rank" json:"rank"`                       //排名
}

type TradeClassifyNameList struct {
	Exchange string                      `description:"交易所"`
	Sort     int                         `description:"排序字段" `
	Num      int                         `description:"品种数量"`
	DataTime string                      `description:"最新更新时间"`
	CurrDate string                      `description:"当前日期"`
	Items    []TradeClassifyNameListItem `description:"子类"`
}
type TradeClassifyNameListSort []TradeClassifyNameList

func (v TradeClassifyNameListSort) Len() int {
	return len(v)
}

func (v TradeClassifyNameListSort) Swap(i, j int) {
	v[i], v[j] = v[j], v[i]
}

func (v TradeClassifyNameListSort) Less(i, j int) bool {
	return v[i].Sort < v[j].Sort
}

type TradeClassifyNameListItemSort []TradeClassifyNameListItem

func (v TradeClassifyNameListItemSort) Len() int {
	return len(v)
}

func (v TradeClassifyNameListItemSort) Swap(i, j int) {
	v[i], v[j] = v[j], v[i]
}

func (v TradeClassifyNameListItemSort) Less(i, j int) bool {
	return v[i].ClassifyName < v[j].ClassifyName
}

type TradeClassifyNameListItem struct {
	ClassifyName string                          `description:"交易分类"`
	Items        []TradeClassifyNameListItemItem `description:"合约代码"`
}

type TradeClassifyNameListItemItemSort []TradeClassifyNameListItemItem

func (v TradeClassifyNameListItemItemSort) Len() int {
	return len(v)
}

func (v TradeClassifyNameListItemItemSort) Swap(i, j int) {
	v[i], v[j] = v[j], v[i]
}

func (v TradeClassifyNameListItemItemSort) Less(i, j int) bool {
	return v[i].ClassifyType < v[j].ClassifyType
}

type TradeClassifyNameListItemItem struct {
	ClassifyType string `description:"分类名称下的类型"`
}

type TradeClassifyName struct {
	ClassifyName string //分类名称
	ClassifyType string //分类名称下的类型
	LatestDate   string //分类下最晚日期
}

// GetExchangeClassify 获取交易所分类列表
func GetExchangeClassify(exchange string) (list []TradeClassifyName, err error) {
	tableName := "base_from_trade_" + exchange + "_index"
	orderStr := "classify_name DESC, classify_type asc"
	if exchange == "zhengzhou" {
		orderStr = "classify_name asc"
	}
	sql := `SELECT classify_name, classify_type FROM ` + tableName + ` WHERE rank <=20 and rank > 0 GROUP BY classify_name, classify_type  `
	sql += ` ORDER BY ` + orderStr

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

	return
}

type LastTimeItem struct {
	CreateTime time.Time
}

// GetExchangeLastTime 获取交易所数据最晚的时间
func GetExchangeLastTime(exchange string) (item LastTimeItem, err error) {
	tableName := "base_from_trade_" + exchange + "_index"
	sql := `SELECT create_time FROM ` + tableName + ` ORDER BY create_time desc`
	o := orm.NewOrmUsingDB("data")
	err = o.Raw(sql).QueryRow(&item)

	return
}

type GetPositionTopReq struct {
	Exchange     string `json:"exchange" form:"exchange"`           //交易所
	ClassifyName string `json:"classify_name" form:"classify_name"` //分类名称
	ClassifyType string `json:"classify_type" form:"classify_type"` //具体合约名称
	DataTime     string `json:"data_time" form:"data_time"`         //请求日期，如果为空，则返回最新的榜单日期
}

type GetPositionTopResp struct {
	BuyList       GetPositionTopList `description:"多单列表"`
	SoldList      GetPositionTopList `description:"空单列表"`
	CleanBuyList  GetPositionTopList `description:"净多单列表"`
	CleanSoldList GetPositionTopList `description:"净空单列表"`
	DataTime      string             `description:"最新日期或者请求日期"`
	LastDataTime  string             `description:"最新日期"`
}

type GetPositionTopList struct {
	TotalDealValue  int                      `description:"总计成交量"`
	TotalDealChange int                      `description:"校昨日变化"`
	List            []GetPositionTopListItem `description:"榜单详情列表"`
}

type GetPositionTopListItem struct {
	DealShortName   string `description:"成交量公司简称"`
	DealValue       int    `description:"成交量"`
	DealChange      int    `description:"成交变化量"`
	Rank            int    `description:"当前名次"`
	Rate            string `description:"当前占比"`
	BeforeAllRate   string `description:"排在前面的成交量总计占比（包含）"`
	BeforeAllValue  int    `description:"排在前面的成交量总计"`
	BeforeAllChange int    `description:"排在前面的成交量增减总计"`
}

func GetTradePositionTop(exchange string, classifyName, classifyType, dataTime string) (list []TradePositionTop, err error) {
	tableName := "trade_position_" + exchange + "_top"
	sql := `SELECT * FROM ` + tableName + ` WHERE classify_name=? and classify_type=? and data_time=? and rank <=20 and rank > 0 ORDER BY deal_value desc`

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

	return
}
