base_from_rzd_data.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Package data_manage @Author gmy 2024/8/7 9:50:00
  2. package data_manage
  3. import (
  4. "eta/eta_api/utils"
  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. // RzdIndexAddReq 指标添加vo
  17. type RzdIndexAddReq struct {
  18. EdbCode string `description:"指标编码"`
  19. EdbName string `description:"指标名称"`
  20. Frequency string `description:"频度"`
  21. Unit string `description:"单位"`
  22. ClassifyId int `description:"分类ID"`
  23. AdminId int `description:"管理员ID"`
  24. AdminRealName string `description:"管理员名称"`
  25. }
  26. type RzdIndexDataCountGroup struct {
  27. IndexCode string
  28. Count int
  29. }
  30. func init() {
  31. orm.RegisterModel(new(BaseFromRzdData))
  32. }
  33. func GetRzdIndexDataCountGroup(indexCodes []string) (items []*RzdIndexDataCountGroup, err error) {
  34. if len(indexCodes) <= 0 {
  35. return
  36. }
  37. o := orm.NewOrmUsingDB("data")
  38. sql := ` SELECT COUNT(1) AS count, index_code FROM base_from_rzd_data WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodes)) + `) GROUP BY index_code`
  39. _, err = o.Raw(sql, indexCodes).QueryRows(&items)
  40. return
  41. }
  42. func GetRzdIndexData(indexCode string, startSize, pageSize int) (items []*BaseFromRzdData, err error) {
  43. o := orm.NewOrmUsingDB("data")
  44. sql := ` SELECT * FROM base_from_rzd_data WHERE index_code=? ORDER BY data_time DESC LIMIT ?,? `
  45. _, err = o.Raw(sql, indexCode, startSize, pageSize).QueryRows(&items)
  46. return
  47. }
  48. // GetBaseFormRzdDataByIndexCode 根据指标编码查询
  49. func GetBaseFormRzdDataByIndexCode(indexCode string) (items []*BaseFromRzdData, err error) {
  50. sql := `SELECT * FROM base_from_rzd_data WHERE index_code=? ORDER BY data_time desc`
  51. o := orm.NewOrmUsingDB("data")
  52. _, err = o.Raw(sql, indexCode).QueryRows(&items)
  53. return
  54. }
  55. func GetBaseFormRzdDataByConditionCount(condition string, pars interface{}) (count int, err error) {
  56. o := orm.NewOrmUsingDB("data")
  57. sql := ` SELECT count(1) FROM base_from_rzd_data WHERE 1=1 `
  58. if condition != "" {
  59. sql += condition
  60. }
  61. err = o.Raw(sql, pars).QueryRow(&count)
  62. if err != nil {
  63. return 0, err
  64. }
  65. return
  66. }
  67. func GetBaseFormRzdDataByCondition(condition string, pars interface{}) (items []*BaseFromRzdData, err error) {
  68. o := orm.NewOrmUsingDB("data")
  69. sql := ` SELECT * FROM base_from_rzd_data WHERE 1=1 `
  70. if condition != "" {
  71. sql += condition
  72. }
  73. _, err = o.Raw(sql, pars).QueryRows(&items)
  74. return
  75. }
  76. // GetRzdDataListByIndexCodes 根据指标编码查询
  77. func GetRzdDataListByIndexCodes(IndexCodes string) (items []string, err error) {
  78. sql := ` SELECT data_time FROM base_from_rzd_data WHERE index_code IN(` + IndexCodes + `) GROUP BY data_time DESC `
  79. o := orm.NewOrmUsingDB("data")
  80. _, err = o.Raw(sql).QueryRows(&items)
  81. return
  82. }
  83. // GetRzdLastUpdateTimeLastByIndexCode 根据指标编码查询 返回ModifyTime最后一条数据
  84. func GetRzdLastUpdateTimeLastByIndexCode(indexCodes []string) (items []*BaseFromRzdData, err error) {
  85. o := orm.NewOrmUsingDB("data")
  86. // 构造 SQL 查询
  87. sql := `SELECT t1.index_code, t1.data_time, t2.value
  88. FROM (
  89. SELECT index_code, MAX(data_time) AS data_time
  90. FROM base_from_rzd_data
  91. WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodes)) + `)
  92. GROUP BY index_code
  93. ) AS t1
  94. JOIN base_from_rzd_data AS t2 ON t1.index_code = t2.index_code AND t1.data_time = t2.data_time`
  95. // 执行 SQL 查询
  96. _, err = o.Raw(sql, indexCodes).QueryRows(&items)
  97. if err != nil {
  98. return nil, err
  99. }
  100. return items, nil
  101. }