package models

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

type BaseFromTradeDalianIndex struct {
	BaseFromTradeDalianIndexId int    `orm:"column(base_from_trade_dalian_index_id);pk"`
	Rank                       string `description:"排名"`
	DealShortName              string `description:"成交量公司简称"`
	DealName                   string `description:"成交量指标名称"`
	DealCode                   string `description:"成交量指标编码"`
	DealValue                  string `description:"成交量"`
	DealChange                 string `description:"成交变化量"`
	BuyShortName               string `description:"成交量公司简称"`
	BuyName                    string `description:"持买单量指标名称"`
	BuyCode                    string `description:"持买单量指标编码"`
	BuyValue                   string `description:"持买单量"`
	BuyChange                  string `description:"持买单量变化量"`
	SoldShortName              string `description:"成交量公司简称"`
	SoldName                   string `description:"持买单量指标名称"`
	SoldCode                   string `description:"持买单量指标编码"`
	SoldValue                  string `description:"持买单量"`
	SoldChange                 string `description:"持买单量变化量"`
	Frequency                  string `description:"频度"`
	ClassifyName               string `description:"分类名称"`
	ClassifyType               string `description:"分类名称下的类型"`
	//CreateTime                 time.Time `description:"插入时间"`
	//ModifyTime                 time.Time `description:"修改时间"`
	//DataTime                   time.Time `description:"数据日期"`
	CreateTime string `description:"插入时间"`
	ModifyTime string `description:"修改时间"`
	DataTime   string `description:"数据日期"`
}

func AddBaseFromTradeDalianIndex(item *BaseFromTradeDalianIndex) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("data")
	lastId, err = o.Insert(item)
	return
}

type BaseFromTradeDalianData struct {
	BaseFromTradeDalianDataId  int `orm:"column(base_from_trade_dalian_data_id);pk"`
	BaseFromTradeDalianIndexId int
	IndexCode                  string
	DataTime                   time.Time
	Value                      string
	AddCutValue                string
	CreateTime                 time.Time
	ModifyTime                 time.Time
	DataTimestamp              string
}

func GetAddSql(item *BaseFromTradeDalianIndex) (addSql string) {
	addSql += "('" + item.Rank + "','" + item.DealShortName + "','" + item.DealName + "','" + item.DealCode + "','" + item.DealValue + "','" + item.BuyShortName + "','" + item.DealChange + "','" + item.BuyName + "','" + item.BuyCode + "','" + item.BuyValue + "','" + item.BuyChange + "','" + item.SoldShortName + "','" + item.SoldName + "','" + item.SoldCode + "','" + item.SoldValue + "','" + item.SoldChange + "','" + item.Frequency + "','" + item.ClassifyName + "','" + item.ClassifyType + "','" + item.CreateTime + "','" + item.ModifyTime + "','" + item.DataTime + "'),"
	return
}

//刷新大连指标数据
func RefreshEdbDataByDaLian(addSql string) (err error) {
	o := orm.NewOrmUsingDB("data")
	_, err = o.Raw(addSql).Exec()
	if err != nil {
		return err
	}
	return
}

//获取当天数据信息列表
func GetBaseFromTradeDalianDataList(dateTime string) (items []*BaseFromTradeDalianIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_trade_dalian_index WHERE data_time=?`
	_, err = o.Raw(sql, dateTime).QueryRows(&items)
	return
}

func UpdateBaseFromTradeDalianIndex(item *BaseFromTradeDalianIndex, dataId int) (err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `UPDATE base_from_trade_dalian_index SET rank=?,deal_short_name=?,deal_name=?,deal_code=?,deal_value=?,buy_short_name=?,deal_change=?,buy_name=?,buy_code=?,buy_value=?,buy_change=?,sold_short_name=?,sold_name=?,sold_code=?,sold_value=?,sold_change=?,frequency=?,classify_name=?,classify_type=?,modify_time=?,data_time=? WHERE base_from_trade_dalian_index_id=? `
	_, err = o.Raw(sql, item.Rank, item.DealShortName, item.DealName, item.DealCode, item.DealValue, item.BuyShortName, item.DealChange, item.BuyName, item.BuyCode, item.BuyValue, item.BuyChange, item.SoldShortName, item.SoldName, item.SoldCode, item.SoldValue, item.SoldChange, item.Frequency, item.ClassifyName, item.ClassifyType, item.ModifyTime, item.DataTime, dataId).Exec()
	return
}

//获取所有指标Id
func GetIndexCodeMapList(exchange string) (items []*BaseFromTradeMapping, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_trade_mapping WHERE exchange=?`
	_, err = o.Raw(sql, exchange).QueryRows(&items)
	return
}