package data_manage import ( "eta/eta_api/utils" "time" "github.com/beego/beego/v2/client/orm" ) type BaseFromClarksonsData struct { BaseFromClarksonsDataId int `orm:"column(base_from_clarksons_data_id);pk"` BaseFromClarksonsIndexId int `description:"指标id"` IndexCode string `description:"指标编码"` DataTime string `description:"数据日期"` Value float64 `description:"数据值"` CreateTime time.Time `description:"创建时间"` ModifyTime time.Time `description:"修改时间"` DataTimestamp int64 `description:"数据时间戳"` } // GetClarksonsDataByIndexId 根据指标id获取指标数据 func GetClarksonsDataByIndexId(indexId int) (items []*BaseFromClarksonsData, err error) { o := orm.NewOrmUsingDB("data") sql := ` SELECT * FROM base_from_clarksons_data WHERE base_from_clarksons_index_id=? ORDER BY data_time DESC` _, err = o.Raw(sql, indexId).QueryRows(&items) return } // GetClarksonsDataDataTimeByIndexId 根据指标id获取指标数据的日期列表 func GetClarksonsDataDataTimeByIndexId(indexIdList []int) (items []string, err error) { if len(indexIdList) == 0 { return } o := orm.NewOrmUsingDB("data") sql := ` SELECT DISTINCT data_time FROM base_from_clarksons_data WHERE base_from_clarksons_index_id IN (` + utils.GetOrmInReplace(len(indexIdList)) + `) ORDER BY data_time DESC` _, err = o.Raw(sql, indexIdList).QueryRows(&items) return } func GetClarksonsIndexDataByCode(indexCode string) (items []*BaseFromClarksonsData, err error) { o := orm.NewOrmUsingDB("data") sql := ` SELECT * FROM base_from_clarksons_data WHERE index_code=? ORDER BY data_time DESC ` _, err = o.Raw(sql, indexCode).QueryRows(&items) return } func GetClarksonsIndexDataCount(indexCode string) (count int, err error) { o := orm.NewOrmUsingDB("data") sql := ` SELECT COUNT(1) AS count FROM base_from_clarksons_data WHERE index_code=? ` err = o.Raw(sql, indexCode).QueryRow(&count) return } // GetClarksonsLastUpdateTimeLastByIndexCode 根据指标编码查询 返回ModifyTime最后一条数据 func GetClarksonsLastUpdateTimeLastByIndexCode(indexCodes []string) (items []*BaseFromClarksonsData, err error) { o := orm.NewOrmUsingDB("data") // 构造 SQL 查询 sql := `SELECT t1.index_code, t1.data_time, t2.value FROM ( SELECT index_code, MAX(data_time) AS data_time FROM base_from_clarksons_data WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodes)) + `) GROUP BY index_code ) AS t1 JOIN base_from_clarksons_data AS t2 ON t1.index_code = t2.index_code AND t1.data_time = t2.data_time` // 执行 SQL 查询 _, err = o.Raw(sql, indexCodes).QueryRows(&items) if err != nil { return nil, err } return items, nil } func GetClarksonsIndexData(indexCode string, startSize, pageSize int) (items []*BaseFromClarksonsData, err error) { o := orm.NewOrmUsingDB("data") sql := ` SELECT * FROM base_from_clarksons_data WHERE index_code=? ORDER BY data_time DESC LIMIT ?,? ` _, err = o.Raw(sql, indexCode, startSize, pageSize).QueryRows(&items) return }