base_from_rzd_data.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // @Author gmy 2024/10/21 9:50:00
  2. package models
  3. import (
  4. "errors"
  5. "github.com/beego/beego/v2/client/orm"
  6. )
  7. type BaseFromRzdData struct {
  8. BaseFromRzdDataId int `orm:"column(base_from_rzd_data_id);pk"`
  9. BaseFromRzdIndexId int `orm:"column(base_from_rzd_index_id)"`
  10. CreateTime string `orm:"column(create_time)"`
  11. DataTime string `orm:"column(data_time)"`
  12. IndexCode string `orm:"column(index_code)"`
  13. ModifyTime string `orm:"column(modify_time)"`
  14. Value float64 `orm:"column(value)"`
  15. }
  16. func init() {
  17. orm.RegisterModel(new(BaseFromRzdData))
  18. }
  19. // AddRzdDataList 批量插入数据记录列表
  20. func AddRzdDataList(items []BaseFromRzdData) (err error) {
  21. o := orm.NewOrm()
  22. _, err = o.InsertMulti(len(items), items)
  23. return
  24. }
  25. // GetRzdDataByIndexCodeAndDataTime 根据指标id和数据日期查询数据
  26. func GetRzdDataByIndexCodeAndDataTime(indexCode string, dataTime string) (items BaseFromRzdData, err error) {
  27. o := orm.NewOrm()
  28. sql := `SELECT * FROM base_from_rzd_data WHERE index_code=? AND data_time=?`
  29. err = o.Raw(sql, indexCode, dataTime).QueryRow(&items) // 使用 QueryRow
  30. if errors.Is(err, orm.ErrNoRows) {
  31. return items, nil
  32. }
  33. if err != nil {
  34. return items, err
  35. }
  36. return
  37. }
  38. // UpdateRzdDataById 根据主键id更新数据
  39. func UpdateRzdDataById(dataId int, value float64) (err error) {
  40. o := orm.NewOrm()
  41. sql := `UPDATE base_from_rzd_data SET value=? WHERE base_from_rzd_data_id=?`
  42. _, err = o.Raw(sql, value, dataId).Exec()
  43. return
  44. }
  45. // GetBaseFromRzdDataByCondition 添加查询
  46. func GetBaseFromRzdDataByCondition(condition string, pars []interface{}) (items []BaseFromRzdData, err error) {
  47. sql := `SELECT * FROM base_from_rzd_data WHERE 1=1 `
  48. o := orm.NewOrm()
  49. if condition != "" {
  50. sql += condition
  51. }
  52. _, err = o.Raw(sql, pars...).QueryRows(&items)
  53. if errors.Is(err, orm.ErrNoRows) {
  54. return nil, nil
  55. }
  56. return
  57. }
  58. // UpdateRzdData 修改睿姿得数据
  59. func UpdateRzdData(item *BaseFromRzdData) (err error) {
  60. o := orm.NewOrm()
  61. _, err = o.Update(item)
  62. return
  63. }