base_from_clarksons_data.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package data_manage
  2. import (
  3. "eta/eta_api/utils"
  4. "time"
  5. "github.com/beego/beego/v2/client/orm"
  6. )
  7. type BaseFromClarksonsData struct {
  8. BaseFromClarksonsDataId int `orm:"column(base_from_clarksons_data_id);pk"`
  9. BaseFromClarksonsIndexId int `description:"指标id"`
  10. IndexCode string `description:"指标编码"`
  11. DataTime string `description:"数据日期"`
  12. Value float64 `description:"数据值"`
  13. CreateTime time.Time `description:"创建时间"`
  14. ModifyTime time.Time `description:"修改时间"`
  15. DataTimestamp int64 `description:"数据时间戳"`
  16. }
  17. // GetClarksonsDataByIndexId 根据指标id获取指标数据
  18. func GetClarksonsDataByIndexId(indexId int) (items []*BaseFromClarksonsData, err error) {
  19. o := orm.NewOrmUsingDB("data")
  20. sql := ` SELECT * FROM base_from_clarksons_data WHERE base_from_clarksons_index_id=? ORDER BY data_time DESC`
  21. _, err = o.Raw(sql, indexId).QueryRows(&items)
  22. return
  23. }
  24. // GetClarksonsDataDataTimeByIndexId 根据指标id获取指标数据的日期列表
  25. func GetClarksonsDataDataTimeByIndexId(indexIdList []int) (items []string, err error) {
  26. if len(indexIdList) == 0 {
  27. return
  28. }
  29. o := orm.NewOrmUsingDB("data")
  30. sql := ` SELECT DISTINCT data_time FROM base_from_clarksons_data WHERE base_from_clarksons_index_id IN (` + utils.GetOrmInReplace(len(indexIdList)) + `) ORDER BY data_time DESC`
  31. _, err = o.Raw(sql, indexIdList).QueryRows(&items)
  32. return
  33. }
  34. func GetClarksonsIndexDataByCode(indexCode string) (items []*BaseFromClarksonsData, err error) {
  35. o := orm.NewOrmUsingDB("data")
  36. sql := ` SELECT * FROM base_from_clarksons_data WHERE index_code=? ORDER BY data_time DESC `
  37. _, err = o.Raw(sql, indexCode).QueryRows(&items)
  38. return
  39. }
  40. func GetClarksonsIndexDataCount(indexCode string) (count int, err error) {
  41. o := orm.NewOrmUsingDB("data")
  42. sql := ` SELECT COUNT(1) AS count FROM base_from_clarksons_data WHERE index_code=? `
  43. err = o.Raw(sql, indexCode).QueryRow(&count)
  44. return
  45. }
  46. // GetClarksonsLastUpdateTimeLastByIndexCode 根据指标编码查询 返回ModifyTime最后一条数据
  47. func GetClarksonsLastUpdateTimeLastByIndexCode(indexCodes []string) (items []*BaseFromClarksonsData, err error) {
  48. o := orm.NewOrmUsingDB("data")
  49. // 构造 SQL 查询
  50. sql := `SELECT t1.index_code, t1.data_time, t2.value
  51. FROM (
  52. SELECT index_code, MAX(data_time) AS data_time
  53. FROM base_from_clarksons_data
  54. WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodes)) + `)
  55. GROUP BY index_code
  56. ) AS t1
  57. JOIN base_from_clarksons_data AS t2 ON t1.index_code = t2.index_code AND t1.data_time = t2.data_time`
  58. // 执行 SQL 查询
  59. _, err = o.Raw(sql, indexCodes).QueryRows(&items)
  60. if err != nil {
  61. return nil, err
  62. }
  63. return items, nil
  64. }
  65. func GetClarksonsIndexData(indexCode string, startSize, pageSize int) (items []*BaseFromClarksonsData, err error) {
  66. o := orm.NewOrmUsingDB("data")
  67. sql := ` SELECT * FROM base_from_clarksons_data WHERE index_code=? ORDER BY data_time DESC LIMIT ?,? `
  68. _, err = o.Raw(sql, indexCode, startSize, pageSize).QueryRows(&items)
  69. return
  70. }