package future_good

import (
	"eta/eta_mobile/utils"
	"github.com/beego/beego/v2/client/orm"
	"github.com/rdlucklib/rdluck_tools/paging"
	"time"
)

// FutureGoodEdbData 期货指标数据的表
type FutureGoodEdbData struct {
	FutureGoodEdbDataId int       `orm:"column(future_good_edb_data_id);pk"`
	FutureGoodEdbInfoId int       `description:"期货指标id"`
	FutureGoodEdbCode   string    `description:"期货指标code"`
	DataTime            time.Time `description:"数据日期"`
	TradeCode           string    `description:"证券代码"`
	Open                float64   `description:"开盘价"`
	High                float64   `description:"最高价"`
	Low                 float64   `description:"最低价"`
	Close               float64   `description:"收盘价"`
	Volume              float64   `description:"成交量"`
	Amt                 float64   `description:"成交额"`
	Oi                  float64   `description:"持仓量"`
	Settle              float64   `description:"结算价"`
	DataTimestamp       int64     `description:"数据日期时间戳"`
	ModifyTime          time.Time
	CreateTime          time.Time
}

// FutureGoodEdbDataListResp 期货指标数据列表数据返回
type FutureGoodEdbDataListResp struct {
	Paging *paging.PagingItem
	List   []*FutureGoodEdbDataItem
}

// FutureGoodEdbDataItem 期货指标数据列表数据
type FutureGoodEdbDataItem struct {
	FutureGoodEdbDataId int     `orm:"column(future_good_edb_data_id);pk"`
	FutureGoodEdbInfoId int     `description:"期货指标id"`
	FutureGoodEdbCode   string  `description:"期货指标code"`
	FutureGoodEdbName   string  `description:"期货指标名称"`
	DataTime            string  `description:"数据日期"`
	TradeCode           string  `description:"证券代码"`
	Open                float64 `description:"开盘价"`
	High                float64 `description:"最高价"`
	Low                 float64 `description:"最低价"`
	Close               float64 `description:"收盘价"`
	Volume              float64 `description:"成交量"`
	Amt                 float64 `description:"成交额"`
	Oi                  float64 `description:"持仓量"`
	Settle              float64 `description:"结算价"`
	DataTimestamp       int64   `description:"数据日期时间戳"`
	ModifyTime          string
	CreateTime          string
}

// GetFutureGoodEdbDataListCount 获取期货指标数据汇总数
func GetFutureGoodEdbDataListCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT COUNT(1) AS count FROM future_good_edb_data WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

// GetFutureGoodEdbDataList 获取期货指标数据列表
func GetFutureGoodEdbDataList(condition string, pars []interface{}, startSize, pageSize int) (list []*FutureGoodEdbDataItem, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM future_good_edb_data WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += `ORDER BY data_time desc,modify_time DESC LIMIT ?,?`
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
	return
}

type EdbDataList struct {
	EdbDataId     int     `description:" 指标数据ID"`
	EdbInfoId     int     `description:"指标ID"`
	DataTime      string  //`json:"-" description:"数据日期"`
	DataTimestamp int64   `description:"数据日期"`
	Value         float64 `description:"数据值"`
}

func GetFutureGoodEdbDataListByDate(futureGoodEdbInfoId int, startDate, endDate string) (list []*FutureGoodEdbData, err error) {
	var pars []interface{}
	sql := `SELECT * FROM future_good_edb_data WHERE 1=1 AND future_good_edb_info_id = ? `
	if startDate != "" {
		sql += ` AND data_time>=? `
		pars = append(pars, startDate)
	}
	if endDate != "" {
		sql += ` AND data_time<=? `
		pars = append(pars, endDate)
	}

	sql += ` ORDER BY data_time ASC `
	o := orm.NewOrmUsingDB("data")
	_, err = o.Raw(sql, futureGoodEdbInfoId, pars).QueryRows(&list)
	return
}

func GetFutureGoodEdbDataListByIdsAndDate(futureGoodEdbInfoIds []int, startDate, endDate string) (list []*FutureGoodEdbData, err error) {
	var pars []interface{}
	sql := `SELECT * FROM future_good_edb_data WHERE 1=1 AND future_good_edb_info_id in (` + utils.GetOrmInReplace(len(futureGoodEdbInfoIds)) + `)  `
	if startDate != "" {
		sql += ` AND data_time>=? `
		pars = append(pars, startDate)
	}
	if endDate != "" {
		sql += ` AND data_time<=? `
		pars = append(pars, endDate)
	}

	sql += ` ORDER BY data_time ASC `
	o := orm.NewOrmUsingDB("data")
	_, err = o.Raw(sql, futureGoodEdbInfoIds, pars).QueryRows(&list)
	return
}
