// @Author gmy 2024/10/21 9:50:00 package models import ( "errors" "eta/eta_index_lib/utils" "github.com/beego/beego/v2/client/orm" ) type BaseFromRzdData struct { BaseFromRzdDataId int `orm:"column(base_from_rzd_data_id);pk"` BaseFromRzdIndexId int `orm:"column(base_from_rzd_index_id)"` CreateTime string `orm:"column(create_time)"` DataTime string `orm:"column(data_time)"` IndexCode string `orm:"column(index_code)"` ModifyTime string `orm:"column(modify_time)"` Value float64 `orm:"column(value)"` } func init() { orm.RegisterModel(new(BaseFromRzdData)) } // AddRzdDataList 批量插入数据记录列表 func AddRzdDataList(items []BaseFromRzdData) (err error) { o := orm.NewOrm() _, err = o.InsertMulti(len(items), items) return } // GetRzdDataByIndexCodeAndDataTime 根据指标id和数据日期查询数据 func GetRzdDataByIndexCodeAndDataTime(indexCode string, dataTime string) (items BaseFromRzdData, err error) { o := orm.NewOrm() sql := `SELECT * FROM base_from_rzd_data WHERE index_code=? AND data_time=?` err = o.Raw(sql, indexCode, dataTime).QueryRow(&items) // 使用 QueryRow if errors.Is(err, orm.ErrNoRows) { return items, nil } if err != nil { return items, err } return } // UpdateRzdDataById 根据主键id更新数据 func UpdateRzdDataById(dataId int, value float64) (err error) { o := orm.NewOrm() sql := `UPDATE base_from_rzd_data SET value=? WHERE base_from_rzd_data_id=?` _, err = o.Raw(sql, value, dataId).Exec() return } // GetBaseFromRzdDataByCondition 添加查询 func GetBaseFromRzdDataByCondition(condition string, pars []interface{}) (items []BaseFromRzdData, err error) { sql := `SELECT * FROM base_from_rzd_data WHERE 1=1 ` o := orm.NewOrm() if condition != "" { sql += condition } _, err = o.Raw(sql, pars...).QueryRows(&items) if errors.Is(err, orm.ErrNoRows) { return nil, nil } return } // UpdateRzdData 修改睿姿得数据 func UpdateRzdData(item *BaseFromRzdData) (err error) { o := orm.NewOrm() _, err = o.Update(item) return } // GetRzdDataMaxAndMinDateByIndexCodes 查询数据时间最新值,数据时间最小值和最新值对应的value值 func GetRzdDataMaxAndMinDateByIndexCodes(indexCodes []string) (resultMap map[string]*EdbInfoMaxAndMinInfo, err error) { resultMap = make(map[string]*EdbInfoMaxAndMinInfo) o := orm.NewOrm() sql := ` SELECT index_code, MIN(data_time) AS min_date,MAX(data_time) AS max_date,MIN(value) AS min_value,MAX(value) AS max_value FROM base_from_rzd_data WHERE index_code in (` + utils.GetOrmInReplace(len(indexCodes)) + `) GROUP BY index_code` var items []EdbInfoMaxAndMinInfo _, err = o.Raw(sql, indexCodes).QueryRows(&items) if err != nil { return nil, err } for _, row := range items { resultMap[row.IndexCode] = &EdbInfoMaxAndMinInfo{ MinDate: row.MinDate, MaxDate: row.MaxDate, MinValue: row.MinValue, MaxValue: row.MaxValue, } } // 查询最新的值(最新数据时间对应的 value) sql = `SELECT a.index_code, a.value AS latest_value FROM base_from_rzd_data a JOIN ( SELECT index_code, MAX(data_time) AS latest_time FROM base_from_rzd_data WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodes)) + `) GROUP BY index_code ) b ON a.index_code = b.index_code AND a.data_time = b.latest_time` _, err = o.Raw(sql, indexCodes).QueryRows(&items) if err != nil { return nil, err } for _, row := range items { if _, ok := resultMap[row.IndexCode]; ok { resultMap[row.IndexCode].LatestValue = row.LatestValue } } return resultMap, nil } func UpdateRzdEdbDataByIndexCodes(indexCode string, info *EdbInfoMaxAndMinInfo) error { o := orm.NewOrm() sql := `UPDATE base_from_rzd_index SET start_date=?, end_date=?, latest_value=? WHERE index_code=?` _, err := o.Raw(sql, info.MinDate, info.MaxDate, info.LatestValue, indexCode).Exec() return err }