base_from_rzd_data.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. "time"
  7. )
  8. type BaseFromRzdDataOrm struct {
  9. BaseFromRzdDataId int `orm:"column(base_from_rzd_data_id);pk"`
  10. BaseFromRzdIndexId int `orm:"column(base_from_rzd_index_id)"`
  11. CreateTime time.Time `orm:"column(create_time)"`
  12. DataTime time.Time `orm:"column(data_time)"`
  13. IndexCode string `orm:"column(index_code)"`
  14. ModifyTime time.Time `orm:"column(modify_time)"`
  15. Value float64 `orm:"column(value)"`
  16. }
  17. func (m *BaseFromRzdDataOrm) ToItem() (item *BaseFromRzdData) {
  18. return &BaseFromRzdData{
  19. BaseFromRzdDataId: m.BaseFromRzdDataId,
  20. BaseFromRzdIndexId: m.BaseFromRzdIndexId,
  21. CreateTime: m.CreateTime.Format(utils.FormatDateTime),
  22. DataTime: m.DataTime.Format(utils.FormatDate),
  23. IndexCode: m.IndexCode,
  24. ModifyTime: m.ModifyTime.Format(utils.FormatDateTime),
  25. Value: m.Value,
  26. }
  27. }
  28. type BaseFromRzdData struct {
  29. BaseFromRzdDataId int `orm:"column(base_from_rzd_data_id);pk"`
  30. BaseFromRzdIndexId int `orm:"column(base_from_rzd_index_id)"`
  31. CreateTime string `orm:"column(create_time)"`
  32. DataTime string `orm:"column(data_time)"`
  33. IndexCode string `orm:"column(index_code)"`
  34. ModifyTime string `orm:"column(modify_time)"`
  35. Value float64 `orm:"column(value)"`
  36. }
  37. // RzdIndexAddReq 指标添加vo
  38. type RzdIndexAddReq struct {
  39. EdbCode string `description:"指标编码"`
  40. EdbName string `description:"指标名称"`
  41. Frequency string `description:"频度"`
  42. Unit string `description:"单位"`
  43. ClassifyId int `description:"分类ID"`
  44. AdminId int `description:"管理员ID"`
  45. AdminRealName string `description:"管理员名称"`
  46. }
  47. type RzdIndexDataCountGroup struct {
  48. IndexCode string
  49. Count int
  50. }
  51. func init() {
  52. orm.RegisterModel(new(BaseFromRzdData))
  53. }
  54. func GetRzdIndexDataCountGroup(indexCodes []string) (items []*RzdIndexDataCountGroup, err error) {
  55. if len(indexCodes) <= 0 {
  56. return
  57. }
  58. o := orm.NewOrmUsingDB("data")
  59. 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`
  60. _, err = o.Raw(sql, indexCodes).QueryRows(&items)
  61. return
  62. }
  63. func GetRzdIndexData(indexCode string, startSize, pageSize int) (items []*BaseFromRzdData, err error) {
  64. var ormList []*BaseFromRzdDataOrm
  65. items = make([]*BaseFromRzdData, 0)
  66. o := orm.NewOrmUsingDB("data")
  67. sql := ` SELECT * FROM base_from_rzd_data WHERE index_code=? ORDER BY data_time DESC LIMIT ?,? `
  68. _, err = o.Raw(sql, indexCode, startSize, pageSize).QueryRows(&ormList)
  69. if err != nil {
  70. return
  71. }
  72. for _, ormItem := range ormList {
  73. items = append(items, ormItem.ToItem())
  74. }
  75. return
  76. }
  77. // GetBaseFormRzdDataByIndexCode 根据指标编码查询
  78. func GetBaseFormRzdDataByIndexCode(indexCode string) (items []*BaseFromRzdData, err error) {
  79. var ormList []*BaseFromRzdDataOrm
  80. items = make([]*BaseFromRzdData, 0)
  81. sql := `SELECT * FROM base_from_rzd_data WHERE index_code=? ORDER BY data_time desc`
  82. o := orm.NewOrmUsingDB("data")
  83. _, err = o.Raw(sql, indexCode).QueryRows(&ormList)
  84. if err != nil {
  85. return
  86. }
  87. for _, ormItem := range ormList {
  88. items = append(items, ormItem.ToItem())
  89. }
  90. return
  91. }
  92. func GetBaseFormRzdDataByConditionCount(condition string, pars interface{}) (count int, err error) {
  93. o := orm.NewOrmUsingDB("data")
  94. sql := ` SELECT count(1) FROM base_from_rzd_data WHERE 1=1 `
  95. if condition != "" {
  96. sql += condition
  97. }
  98. err = o.Raw(sql, pars).QueryRow(&count)
  99. if err != nil {
  100. return 0, err
  101. }
  102. return
  103. }
  104. func GetBaseFormRzdDataByCondition(condition string, pars interface{}) (items []*BaseFromRzdData, err error) {
  105. var ormList []*BaseFromRzdDataOrm
  106. items = make([]*BaseFromRzdData, 0)
  107. o := orm.NewOrmUsingDB("data")
  108. sql := ` SELECT * FROM base_from_rzd_data WHERE 1=1 `
  109. if condition != "" {
  110. sql += condition
  111. }
  112. _, err = o.Raw(sql, pars).QueryRows(&ormList)
  113. if err != nil {
  114. return
  115. }
  116. for _, ormItem := range ormList {
  117. items = append(items, ormItem.ToItem())
  118. }
  119. return
  120. }
  121. // GetRzdDataListByIndexCodes 根据指标编码查询
  122. func GetRzdDataListByIndexCodes(IndexCodes string) (items []time.Time, err error) {
  123. sql := ` SELECT data_time FROM base_from_rzd_data WHERE index_code IN(` + IndexCodes + `) GROUP BY data_time ORDER BY data_time DESC `
  124. o := orm.NewOrmUsingDB("data")
  125. _, err = o.Raw(sql).QueryRows(&items)
  126. return
  127. }
  128. // GetRzdLastUpdateTimeLastByIndexCode 根据指标编码查询 返回ModifyTime最后一条数据
  129. func GetRzdLastUpdateTimeLastByIndexCode(indexCodes []string) (items []*BaseFromRzdData, err error) {
  130. var ormList []*BaseFromRzdDataOrm
  131. items = make([]*BaseFromRzdData, 0)
  132. o := orm.NewOrmUsingDB("data")
  133. // 构造 SQL 查询
  134. sql := `SELECT t1.index_code, t1.data_time, t2.value
  135. FROM (
  136. SELECT index_code, MAX(data_time) AS data_time
  137. FROM base_from_rzd_data
  138. WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodes)) + `)
  139. GROUP BY index_code
  140. ) AS t1
  141. JOIN base_from_rzd_data AS t2 ON t1.index_code = t2.index_code AND t1.data_time = t2.data_time`
  142. // 执行 SQL 查询
  143. _, err = o.Raw(sql, indexCodes).QueryRows(&ormList)
  144. if err != nil {
  145. return nil, err
  146. }
  147. for _, ormItem := range ormList {
  148. items = append(items, ormItem.ToItem())
  149. }
  150. return items, nil
  151. }