// @Author gmy 2024/8/7 9:50:00 package models import ( "errors" "github.com/beego/beego/v2/client/orm" ) type BaseFromLyData struct { BaseFromLyDataId int `orm:"column(base_from_ly_data_id);pk"` // 数据ID CreateTime string `orm:"column(create_time)"` // 创建时间 ModifyTime string `orm:"column(modify_time)"` // 修改时间 BaseFromLyIndexId int `orm:"column(base_from_ly_index_id)"` // 指标id IndexCode string `orm:"column(index_code)"` // 指标编码 DataTime string `orm:"column(data_time)"` // 数据日期 Value float64 `orm:"column(value)"` // 数据值 } func init() { orm.RegisterModel(new(BaseFromLyData)) } // AddLyDataList 批量插入数据记录列表 func AddLyDataList(items []BaseFromLyData) (err error) { o := orm.NewOrm() _, err = o.InsertMulti(len(items), items) return } // GetLyDataByIndexIdAndDataTime 根据指标id和数据日期查询数据 func GetLyDataByIndexIdAndDataTime(indexId int, dataTime string) (items []BaseFromLyData, err error) { o := orm.NewOrm() sql := `SELECT * FROM base_from_ly_data WHERE base_from_ly_index_id=? AND data_time=?` _, err = o.Raw(sql, indexId, dataTime).QueryRows(&items) if errors.Is(err, orm.ErrNoRows) { return nil, nil } return } // GetLyDataByIndexIdAndDataTimeYM 根据指标id和数据日期的年月查询数据 func GetLyDataByIndexIdAndDataTimeYM(indexId int, dataTime string) (items []BaseFromLyData, err error) { o := orm.NewOrm() sql := `SELECT * FROM base_from_ly_data WHERE base_from_ly_index_id=? AND data_time like ?` _, err = o.Raw(sql, indexId, dataTime+"%").QueryRows(&items) if errors.Is(err, orm.ErrNoRows) { return nil, nil } return } // UpdateLyDataById 根据主键id更新数据 func UpdateLyDataById(dataId int, value float64) (err error) { o := orm.NewOrm() sql := `UPDATE base_from_ly_data SET value=? WHERE base_from_ly_data_id=?` _, err = o.Raw(sql, value, dataId).Exec() return } // GetBaseFromLyDataByIndexCode 根据指标编码查询 func GetBaseFromLyDataByIndexCode(condition string, pars []interface{}) (items []BaseFromLyData, err error) { sql := `SELECT * FROM base_from_ly_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 }