base_from_rzd_data.go 3.9 KB

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