base_from_ly_data.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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, 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. if err != nil {
  56. return
  57. }
  58. for i := range items {
  59. items[i] = utils.GormDateStrToDateStr(items[i])
  60. }
  61. return
  62. }
  63. // GetLyLastUpdateTimeLastByIndexCode 根据指标编码查询 返回ModifyTime最后一条数据
  64. func GetLyLastUpdateTimeLastByIndexCode(indexCodes []string) (items []*BaseFromLyData, err error) {
  65. o := global.DbMap[utils.DbNameIndex]
  66. // 将 indexCodes 切片转换为逗号分隔的字符串
  67. placeholders := strings.Repeat("?,", len(indexCodes)-1) + "?"
  68. // 构造 SQL 查询
  69. sql := `SELECT index_code, MAX(modify_time) AS modify_time
  70. FROM base_from_ly_data
  71. WHERE index_code IN (` + placeholders + `)
  72. GROUP BY index_code`
  73. // 执行 SQL 查询
  74. err = o.Raw(sql, indexCodes).Find(&items).Error
  75. if err != nil {
  76. return nil, err
  77. }
  78. return items, nil
  79. }
  80. // GetLyLastDataTimeByIndexCode 根据指标编码查询 返回data_time最后一条数据的value
  81. func GetLyLastDataTimeByIndexCode(indexCodes []string) (items []*BaseFromLyData, err error) {
  82. o := global.DbMap[utils.DbNameIndex]
  83. // 将 indexCodes 切片转换为逗号分隔的字符串
  84. placeholders := strings.Repeat("?,", len(indexCodes)-1) + "?"
  85. // 构造 SQL 查询
  86. sql := `
  87. SELECT t1.*
  88. FROM base_from_ly_data t1
  89. INNER JOIN (
  90. SELECT index_code, MAX(data_time) AS data_time
  91. FROM base_from_ly_data
  92. WHERE index_code IN (` + placeholders + `)
  93. GROUP BY index_code
  94. ) t2
  95. ON t1.index_code = t2.index_code AND t1.data_time = t2.data_time
  96. `
  97. // 执行 SQL 查询
  98. err = o.Raw(sql, indexCodes).Find(&items).Error
  99. if err != nil {
  100. return nil, err
  101. }
  102. return items, nil
  103. }