base_from_rzd_data.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. "gorm.io/gorm"
  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. func (baseFromRzdData *BaseFromRzdData) AfterFind(tx *gorm.DB) (err error) {
  18. baseFromRzdData.ModifyTime = utils.GormDateStrToDateTimeStr(baseFromRzdData.ModifyTime)
  19. baseFromRzdData.CreateTime = utils.GormDateStrToDateTimeStr(baseFromRzdData.CreateTime)
  20. baseFromRzdData.DataTime = utils.GormDateStrToDateStr(baseFromRzdData.DataTime)
  21. return
  22. }
  23. // RzdIndexAddReq 指标添加vo
  24. type RzdIndexAddReq struct {
  25. EdbCode string `description:"指标编码"`
  26. EdbName string `description:"指标名称"`
  27. Frequency string `description:"频度"`
  28. Unit string `description:"单位"`
  29. ClassifyId int `description:"分类ID"`
  30. AdminId int `description:"管理员ID"`
  31. AdminRealName string `description:"管理员名称"`
  32. }
  33. type RzdIndexDataCountGroup struct {
  34. IndexCode string
  35. Count int
  36. }
  37. func GetRzdIndexDataCountGroup(indexCodes []string) (items []*RzdIndexDataCountGroup, err error) {
  38. if len(indexCodes) <= 0 {
  39. return
  40. }
  41. o := global.DbMap[utils.DbNameIndex]
  42. 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`
  43. err = o.Raw(sql, indexCodes).Find(&items).Error
  44. return
  45. }
  46. func GetRzdIndexData(indexCode string, startSize, pageSize int) (items []*BaseFromRzdData, err error) {
  47. o := global.DbMap[utils.DbNameIndex]
  48. sql := ` SELECT * FROM base_from_rzd_data WHERE index_code=? ORDER BY data_time DESC LIMIT ?,? `
  49. err = o.Raw(sql, indexCode, startSize, pageSize).Find(&items).Error
  50. return
  51. }
  52. // GetBaseFormRzdDataByIndexCode 根据指标编码查询
  53. func GetBaseFormRzdDataByIndexCode(indexCode string) (items []*BaseFromRzdData, err error) {
  54. sql := `SELECT * FROM base_from_rzd_data WHERE index_code=? ORDER BY data_time desc`
  55. o := global.DbMap[utils.DbNameIndex]
  56. err = o.Raw(sql, indexCode).Find(&items).Error
  57. return
  58. }
  59. func GetBaseFormRzdDataByConditionCount(condition string, pars []interface{}) (count int, err error) {
  60. o := global.DbMap[utils.DbNameIndex]
  61. sql := ` SELECT count(1) FROM base_from_rzd_data WHERE 1=1 `
  62. if condition != "" {
  63. sql += condition
  64. }
  65. err = o.Raw(sql, pars...).Scan(&count).Error
  66. if err != nil {
  67. return 0, err
  68. }
  69. return
  70. }
  71. func GetBaseFormRzdDataByCondition(condition string, pars []interface{}) (items []*BaseFromRzdData, err error) {
  72. o := global.DbMap[utils.DbNameIndex]
  73. sql := ` SELECT * FROM base_from_rzd_data WHERE 1=1 `
  74. if condition != "" {
  75. sql += condition
  76. }
  77. err = o.Raw(sql, pars...).Find(&items).Error
  78. return
  79. }
  80. // GetRzdDataListByIndexCodes 根据指标编码查询
  81. func GetRzdDataListByIndexCodes(IndexCodes string) (items []string, err error) {
  82. sql := ` SELECT DISTINCT data_time FROM base_from_rzd_data WHERE index_code IN(` + IndexCodes + `) ORDER BY data_time DESC `
  83. o := global.DbMap[utils.DbNameIndex]
  84. err = o.Raw(sql).Find(&items).Error
  85. for i := 0; i < len(items); i++ {
  86. items[i] = utils.GormDateStrToDateStr(items[i])
  87. }
  88. return
  89. }
  90. // GetRzdLastUpdateTimeLastByIndexCode 根据指标编码查询 返回ModifyTime最后一条数据
  91. func GetRzdLastUpdateTimeLastByIndexCode(indexCodes []string) (items []*BaseFromRzdData, err error) {
  92. o := global.DbMap[utils.DbNameIndex]
  93. // 构造 SQL 查询
  94. sql := `SELECT t1.index_code, t1.data_time, t2.value
  95. FROM (
  96. SELECT index_code, MAX(data_time) AS data_time
  97. FROM base_from_rzd_data
  98. WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodes)) + `)
  99. GROUP BY index_code
  100. ) AS t1
  101. JOIN base_from_rzd_data AS t2 ON t1.index_code = t2.index_code AND t1.data_time = t2.data_time`
  102. // 执行 SQL 查询
  103. err = o.Raw(sql, indexCodes).Find(&items).Error
  104. if err != nil {
  105. return nil, err
  106. }
  107. return items, nil
  108. }