base_from_ly_data.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. "strings"
  8. "github.com/rdlucklib/rdluck_tools/paging"
  9. "gorm.io/gorm"
  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 (b *BaseFromLyData) AfterFind(db *gorm.DB) (err error) {
  21. b.CreateTime = utils.GormDateStrToDateTimeStr(b.CreateTime)
  22. b.ModifyTime = utils.GormDateStrToDateTimeStr(b.ModifyTime)
  23. b.DataTime = utils.GormDateStrToDateStr(b.DataTime)
  24. return
  25. }
  26. type BaseFromLyDataPage struct {
  27. List []*BaseFromLyData `description:"指标数据列表"`
  28. Paging *paging.PagingItem `description:"分页数据"`
  29. }
  30. // GetLyDataCountByIndexId 获取指标数据总数
  31. func GetLyDataCountByIndexId(indexId int) (count int, err error) {
  32. o := global.DbMap[utils.DbNameIndex]
  33. sql := `SELECT count(*) FROM base_from_ly_data WHERE base_from_ly_index_id=?`
  34. err = o.Raw(sql, indexId).Scan(&count).Error
  35. return
  36. }
  37. // GetLyDataPageByIndexId 获取指标数据分页列表
  38. func GetLyDataPageByIndexId(indexId int, startSize, pageSize int) (items []*BaseFromLyData, err error) {
  39. o := global.DbMap[utils.DbNameIndex]
  40. sql := `SELECT * FROM base_from_ly_data WHERE base_from_ly_index_id=? ORDER BY data_time desc LIMIT ?,?`
  41. err = o.Raw(sql, indexId, (startSize-1)*pageSize, pageSize).Find(&items).Error
  42. return
  43. }
  44. // GetBaseFromLyDataByIndexCode 根据指标编码查询
  45. func GetBaseFromLyDataByIndexCode(indexCode string) (items []*BaseFromLyData, err error) {
  46. sql := `SELECT * FROM base_from_ly_data WHERE index_code=? ORDER BY data_time desc`
  47. o := global.DbMap[utils.DbNameIndex]
  48. err = o.Raw(sql, indexCode).Find(&items).Error
  49. return
  50. }
  51. func GetLyDataListByIndexCodes(IndexCodes string) (items []string, err error) {
  52. sql := ` SELECT DISTINCT data_time FROM base_from_ly_data WHERE index_code IN(` + IndexCodes + `) ORDER BY data_time DESC `
  53. o := global.DbMap[utils.DbNameIndex]
  54. err = o.Raw(sql).Find(&items).Error
  55. return
  56. }
  57. // GetLyLastUpdateTimeLastByIndexCode 根据指标编码查询 返回ModifyTime最后一条数据
  58. func GetLyLastUpdateTimeLastByIndexCode(indexCodes []string) (items []*BaseFromLyData, err error) {
  59. o := global.DbMap[utils.DbNameIndex]
  60. // 将 indexCodes 切片转换为逗号分隔的字符串
  61. placeholders := strings.Repeat("?,", len(indexCodes)-1) + "?"
  62. // 构造 SQL 查询
  63. sql := `SELECT index_code, MAX(modify_time) AS modify_time
  64. FROM base_from_ly_data
  65. WHERE index_code IN (` + placeholders + `)
  66. GROUP BY index_code`
  67. // 执行 SQL 查询
  68. err = o.Raw(sql, indexCodes).Find(&items).Error
  69. if err != nil {
  70. return nil, err
  71. }
  72. return items, nil
  73. }
  74. // GetLyLastDataTimeByIndexCode 根据指标编码查询 返回data_time最后一条数据的value
  75. func GetLyLastDataTimeByIndexCode(indexCodes []string) (items []*BaseFromLyData, err error) {
  76. o := global.DbMap[utils.DbNameIndex]
  77. // 将 indexCodes 切片转换为逗号分隔的字符串
  78. placeholders := strings.Repeat("?,", len(indexCodes)-1) + "?"
  79. // 构造 SQL 查询
  80. sql := `
  81. SELECT t1.*
  82. FROM base_from_ly_data t1
  83. INNER JOIN (
  84. SELECT index_code, MAX(data_time) AS data_time
  85. FROM base_from_ly_data
  86. WHERE index_code IN (` + placeholders + `)
  87. GROUP BY index_code
  88. ) t2
  89. ON t1.index_code = t2.index_code AND t1.data_time = t2.data_time
  90. `
  91. // 执行 SQL 查询
  92. err = o.Raw(sql, indexCodes).Find(&items).Error
  93. if err != nil {
  94. return nil, err
  95. }
  96. return items, nil
  97. }