base_from_ly_data.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // @Author gmy 2024/8/7 9:50:00
  2. package models
  3. import (
  4. "errors"
  5. "github.com/beego/beego/v2/client/orm"
  6. )
  7. type BaseFromLyData struct {
  8. BaseFromLyDataId int `orm:"column(base_from_ly_data_id);pk"` // 数据ID
  9. CreateTime string `orm:"column(create_time)"` // 创建时间
  10. ModifyTime string `orm:"column(modify_time)"` // 修改时间
  11. BaseFromLyIndexId int `orm:"column(base_from_ly_index_id)"` // 指标id
  12. IndexCode string `orm:"column(index_code)"` // 指标编码
  13. DataTime string `orm:"column(data_time)"` // 数据日期
  14. Value float64 `orm:"column(value)"` // 数据值
  15. }
  16. func init() {
  17. orm.RegisterModel(new(BaseFromLyData))
  18. }
  19. // AddLyDataList 批量插入数据记录列表
  20. func AddLyDataList(items []BaseFromLyData) (err error) {
  21. o := orm.NewOrm()
  22. _, err = o.InsertMulti(len(items), items)
  23. return
  24. }
  25. // GetLyDataByIndexIdAndDataTime 根据指标id和数据日期查询数据
  26. func GetLyDataByIndexIdAndDataTime(indexId int, dataTime string) (items []BaseFromLyData, err error) {
  27. o := orm.NewOrm()
  28. sql := `SELECT * FROM base_from_ly_data WHERE base_from_ly_index_id=? AND data_time=?`
  29. _, err = o.Raw(sql, indexId, dataTime).QueryRows(&items)
  30. if errors.Is(err, orm.ErrNoRows) {
  31. return nil, nil
  32. }
  33. return
  34. }
  35. // GetLyDataByIndexIdAndDataTimeYM 根据指标id和数据日期的年月查询数据
  36. func GetLyDataByIndexIdAndDataTimeYM(indexId int, dataTime string) (items []BaseFromLyData, err error) {
  37. o := orm.NewOrm()
  38. sql := `SELECT * FROM base_from_ly_data WHERE base_from_ly_index_id=? AND data_time like ?`
  39. _, err = o.Raw(sql, indexId, dataTime+"%").QueryRows(&items)
  40. if errors.Is(err, orm.ErrNoRows) {
  41. return nil, nil
  42. }
  43. return
  44. }
  45. // UpdateLyDataById 根据主键id更新数据
  46. func UpdateLyDataById(dataId int, value float64) (err error) {
  47. o := orm.NewOrm()
  48. sql := `UPDATE base_from_ly_data SET value=? WHERE base_from_ly_data_id=?`
  49. _, err = o.Raw(sql, value, dataId).Exec()
  50. return
  51. }
  52. // GetBaseFromLyDataByIndexCode 根据指标编码查询
  53. func GetBaseFromLyDataByIndexCode(condition string, pars []interface{}) (items []BaseFromLyData, err error) {
  54. sql := `SELECT * FROM base_from_ly_data WHERE 1=1 `
  55. o := orm.NewOrm()
  56. if condition != "" {
  57. sql += condition
  58. }
  59. _, err = o.Raw(sql, pars...).QueryRows(&items)
  60. if errors.Is(err, orm.ErrNoRows) {
  61. return nil, nil
  62. }
  63. return
  64. }