base_from_ly_data.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Package data_manage
  2. // @Author gmy 2024/8/7 9:50:00
  3. package data_manage
  4. import (
  5. "eta/eta_api/global"
  6. "eta/eta_api/utils"
  7. "github.com/beego/beego/v2/client/orm"
  8. "github.com/rdlucklib/rdluck_tools/paging"
  9. "strings"
  10. )
  11. type BaseFromLyData struct {
  12. BaseFromLyDataId int `orm:"column(base_from_ly_data_id);pk" gorm:"primaryKey" description:"数据ID"`
  13. CreateTime string `orm:"column(create_time)" description:"创建时间"`
  14. ModifyTime string `orm:"column(modify_time)" description:"修改时间"`
  15. BaseFromLyIndexId int `orm:"column(base_from_ly_index_id)" description:"指标id"`
  16. IndexCode string `orm:"column(index_code)" description:"指标编码"`
  17. DataTime string `orm:"column(data_time)" description:"数据日期"`
  18. Value float64 `orm:"column(value)" description:"数据值"`
  19. }
  20. func init() {
  21. orm.RegisterModel(new(BaseFromLyData))
  22. }
  23. type BaseFromLyDataPage struct {
  24. List []*BaseFromLyData `description:"指标数据列表"`
  25. Paging *paging.PagingItem `description:"分页数据"`
  26. }
  27. // GetLyDataCountByIndexId 获取指标数据总数
  28. func GetLyDataCountByIndexId(indexId int) (count int, err error) {
  29. o := global.DbMap[utils.DbNameIndex]
  30. sql := `SELECT count(*) FROM base_from_ly_data WHERE base_from_ly_index_id=?`
  31. err = o.Raw(sql, indexId).Scan(&count).Error
  32. return
  33. }
  34. // GetLyDataPageByIndexId 获取指标数据分页列表
  35. func GetLyDataPageByIndexId(indexId int, startSize, pageSize int) (items []*BaseFromLyData, err error) {
  36. o := global.DbMap[utils.DbNameIndex]
  37. sql := `SELECT * FROM base_from_ly_data WHERE base_from_ly_index_id=? ORDER BY data_time desc LIMIT ?,?`
  38. err = o.Raw(sql, indexId, (startSize-1)*pageSize, pageSize).Find(&items).Error
  39. return
  40. }
  41. // GetBaseFromLyDataByIndexCode 根据指标编码查询
  42. func GetBaseFromLyDataByIndexCode(indexCode string) (items []*BaseFromLyData, err error) {
  43. sql := `SELECT * FROM base_from_ly_data WHERE index_code=? ORDER BY data_time desc`
  44. o := global.DbMap[utils.DbNameIndex]
  45. err = o.Raw(sql, indexCode).Find(&items).Error
  46. return
  47. }
  48. func GetLyDataListByIndexCodes(IndexCodes string) (items []string, err error) {
  49. sql := ` SELECT data_time FROM base_from_ly_data WHERE index_code IN(` + IndexCodes + `) GROUP BY data_time DESC `
  50. o := global.DbMap[utils.DbNameIndex]
  51. err = o.Raw(sql).Find(&items).Error
  52. return
  53. }
  54. // GetLyLastUpdateTimeLastByIndexCode 根据指标编码查询 返回ModifyTime最后一条数据
  55. func GetLyLastUpdateTimeLastByIndexCode(indexCodes []string) (items []*BaseFromLyData, err error) {
  56. o := global.DbMap[utils.DbNameIndex]
  57. // 将 indexCodes 切片转换为逗号分隔的字符串
  58. placeholders := strings.Repeat("?,", len(indexCodes)-1) + "?"
  59. // 构造 SQL 查询
  60. sql := `SELECT index_code, MAX(modify_time) AS modify_time
  61. FROM base_from_ly_data
  62. WHERE index_code IN (` + placeholders + `)
  63. GROUP BY index_code`
  64. // 执行 SQL 查询
  65. err = o.Raw(sql, indexCodes).Find(&items).Error
  66. if err != nil {
  67. return nil, err
  68. }
  69. return items, nil
  70. }
  71. // GetLyLastDataTimeByIndexCode 根据指标编码查询 返回data_time最后一条数据的value
  72. func GetLyLastDataTimeByIndexCode(indexCodes []string) (items []*BaseFromLyData, err error) {
  73. o := global.DbMap[utils.DbNameIndex]
  74. // 将 indexCodes 切片转换为逗号分隔的字符串
  75. placeholders := strings.Repeat("?,", len(indexCodes)-1) + "?"
  76. // 构造 SQL 查询
  77. sql := `
  78. SELECT t1.*
  79. FROM base_from_ly_data t1
  80. INNER JOIN (
  81. SELECT index_code, MAX(data_time) AS data_time
  82. FROM base_from_ly_data
  83. WHERE index_code IN (` + placeholders + `)
  84. GROUP BY index_code
  85. ) t2
  86. ON t1.index_code = t2.index_code AND t1.data_time = t2.data_time
  87. `
  88. // 执行 SQL 查询
  89. err = o.Raw(sql, indexCodes).Find(&items).Error
  90. if err != nil {
  91. return nil, err
  92. }
  93. return items, nil
  94. }