package data_manage

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

type EdbDataWind struct {
	EdbDataId     int `orm:"column(edb_data_id);pk"`
	EdbInfoId     int
	EdbCode       string
	DataTime      string
	Value         float64
	Status        int
	CreateTime    time.Time
	ModifyTime    time.Time
	DataTimestamp int64
}

func AddEdbDataWindBySql(sqlStr string) (err error) {
	o := orm.NewOrmUsingDB("data")
	_, err = o.Raw(sqlStr).Exec()
	return
}

func DeleteEdbDataWind(edbCode string) (err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `DELETE FROM edb_data_wind WHERE edb_code=? `
	_, err = o.Raw(sql, edbCode).Exec()
	return
}

func ModifyEdbDataWind(edbInfoId int64, dataTime string, value float64) (err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` UPDATE edb_data_wind SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
	_, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
	return
}

func GetEdbDataWindByCode(edbCode string, size int) (items []*EdbInfoSearchData, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT * FROM edb_data_wind WHERE edb_code=? ORDER BY data_time DESC LIMIT ? `
	_, err = o.Raw(sql, edbCode, size).QueryRows(&items)
	return
}

func GetEdbDataWindMaxOrMinDate(edbCode string) (min_date, max_date string, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date FROM edb_data_wind WHERE edb_code=? `
	err = o.Raw(sql, edbCode).QueryRow(&min_date, &max_date)
	return
}

func GetEdbDataWindByCodeAndDate(edbCode string, startDate string) (count int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT COUNT(1) AS count FROM edb_data_wind WHERE edb_code=? AND data_time=? `
	err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
	return
}

type EdbDataFromWind struct {
	Close  map[string]float64 `json:"CLOSE"`
	Dt     map[string]int64   `json:"DT"`
	ErrMsg string
}
