base_from_ly_data.go 3.7 KB

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