// Package models
// @Author gmy 2024/8/14 19:40:00
package models

import "github.com/beego/beego/v2/client/orm"

type EdbDataLy struct {
	edbDataId     int     `orm:"column(edb_data_id);pk"` // 数据ID
	CreateTime    string  `orm:"column(create_time)"`    // 创建时间
	ModifyTime    string  `orm:"column(modify_time)"`    // 修改时间
	EdbInfoId     int     `orm:"column(edb_info_id)"`    // 指标id
	EdbCode       string  `orm:"column(edb_code)"`       // 指标编码
	DataTime      string  `orm:"column(data_time)"`      // 数据日期
	Value         float64 `orm:"column(value)"`          // 数据值
	DataTimestamp uint64  `orm:"column(data_timestamp)"` // 数据日期时间戳
}

func GetLyEdbDataByIndexCodeAndDataTime(indexCode string, dataTime string) (items []EdbDataLy, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM edb_data_ly WHERE index_code=? AND data_time like ?`
	_, err = o.Raw(sql, indexCode, dataTime+"%").QueryRows(&items)
	return
}

func GetLyEdbDataByIndexCodeAndExactDataTime(indexCode string, dataTime string) (items []EdbDataLy, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM edb_data_ly WHERE edb_code=? AND data_time=?`
	_, err = o.Raw(sql, indexCode, dataTime).QueryRows(&items)
	return
}

// UpdateLyEdbDataById 更新指标库数据 须根据指标编码和日期更新 仅适合月度数据
func UpdateLyEdbDataById(id int, value float64) (err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `UPDATE edb_data_ly SET value=? WHERE edb_data_id=?`
	_, err = o.Raw(sql, value, id).Exec()
	return
}