eta_report.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package eta
  2. import (
  3. "eta/eta_mini_ht_api/common/component/cache"
  4. logger "eta/eta_mini_ht_api/common/component/log"
  5. "eta/eta_mini_ht_api/common/utils/redis"
  6. "eta/eta_mini_ht_api/models"
  7. "strings"
  8. "time"
  9. )
  10. const (
  11. colunms = "id,author,abstract,title,publish_time,state,modify_time,"
  12. detailColumn = "id,author,abstract,title,publish_time,content,content_sub,collaborate_type,report_layout,video_url,video_name,video_play_seconds,head_resource_id,end_resource_id,has_chapter,need_splice,state,modify_time"
  13. Published = 2
  14. Passed = 6
  15. limit = 500
  16. TlbChartPermissionSearchKeyWordMapping = "chart_permission_search_key_word_mapping"
  17. )
  18. var (
  19. classifyIds = []string{"classify_id_third", "classify_id_second", "classify_id_first"}
  20. redisCache *cache.RedisCache
  21. )
  22. func rd() *cache.RedisCache {
  23. if redisCache == nil {
  24. redisCache = cache.GetInstance()
  25. }
  26. return redisCache
  27. }
  28. func (ETAReport) TableName() string {
  29. return "report"
  30. }
  31. type ETAReport struct {
  32. ID int `gorm:"primary_key;auto_increment"`
  33. ClassifyID int `gorm:"_"`
  34. ClassifyIDFirst int `gorm:"column:classify_id_first;default:0"`
  35. ClassifyIDSecond int `gorm:"column:classify_id_second;default:0"`
  36. ClassifyIDThird int `gorm:"column:classify_id_third;default:0"`
  37. Title string `gorm:"column:title;size:125;"`
  38. Abstract string `gorm:"column:abstract;size:255;"`
  39. Author string `gorm:"column:author;size:50;"`
  40. PublishTime time.Time `gorm:"column:publish_time"`
  41. Content string `gorm:"column:content"`
  42. ContentSub string `gorm:"column:content_sub"`
  43. CollaborateType int `gorm:"column:collaborate_type"`
  44. ReportLayout int `gorm:"column:report_layout"`
  45. VideoUrl string `gorm:"column:video_url"`
  46. VideoName string `gorm:"column:video_name"`
  47. VideoPlaySeconds string `gorm:"column:video_play_seconds"`
  48. HeadResourceId int `gorm:"column:head_resource_id"`
  49. EndResourceId int `gorm:"column:end_resource_id"`
  50. HasChapter bool `gorm:"column:has_chapter"`
  51. NeedSplice bool `gorm:"column:need_splice"`
  52. ModifyTime time.Time `gorm:"column:modify_time"`
  53. State int `gorm:"column:state"`
  54. }
  55. type ReportPermission struct {
  56. ChartPermissionID int `gorm:"primaryKey;autoIncrement;column:chart_permission_id;comment:主键"`
  57. ParentID int `gorm:"size:11;default:0;column:parent_id;comment:父级权限id"`
  58. }
  59. func GetETAReports(id int) (reports []ETAReport, err error) {
  60. err = models.ETA().Table("report").Select(colunms+strings.Join(classifyIds, ",")).Where("state =? or state=?", Published, Passed).Where("id > ?", id).Order("id asc").Limit(limit).Find(&reports).Error
  61. if reports != nil {
  62. for i := 0; i < len(reports); i++ {
  63. setClassifyIdValue(&reports[i])
  64. }
  65. }
  66. return
  67. }
  68. func GetUpdateETAReports() (filterList []ETAReport, err error) {
  69. duration := time.Now().Add(-30 * time.Second)
  70. modifyTime := duration.Format(time.DateTime)
  71. var reports []ETAReport
  72. err = models.ETA().Table("report").Select(colunms+strings.Join(classifyIds, ",")).Where("modify_time >=?", modifyTime).Order("id asc").Find(&reports).Error
  73. if err != nil {
  74. logger.Error("同步eta研报数据失败:%v", err)
  75. }
  76. if reports != nil {
  77. for i := 0; i < len(reports); i++ {
  78. setClassifyIdValue(&reports[i])
  79. key := redis.GenerateReportRefreshKey("ETA", reports[i].ID, reports[i].ModifyTime.UnixMilli())
  80. if rd().SetIfNotExist(key, "processed", 3600) {
  81. logger.Info("同步eta报告 ID :%d,修改时间:%v", reports[i].ID, reports[i].ModifyTime)
  82. filterList = append(filterList, reports[i])
  83. } else {
  84. logger.Info("过滤ETA重复的消息 ID :%d,修改时间:%v", reports[i].ID, reports[i].ModifyTime)
  85. }
  86. }
  87. }
  88. return
  89. }
  90. func GetETAReportById(id int) (report ETAReport, err error) {
  91. err = models.ETA().Table("report").Select(detailColumn).Where("id = ?", id).Where("state =? or state=?", Published, Passed).First(&report).Error
  92. if err != nil {
  93. return
  94. }
  95. return
  96. }
  97. func GetReportClassifyById(id int) (classifyId int, err error) {
  98. db := models.ETA()
  99. err = db.Table("report").
  100. Select("COALESCE(NULLIF(classify_id_third,0),NULLIF(classify_id_second,0),classify_id_first) classify_id").
  101. Where("id =?", id).Scan(&classifyId).Error
  102. return
  103. }
  104. func setClassifyIdValue(report *ETAReport) {
  105. if report.ClassifyIDThird > 0 {
  106. report.ClassifyID = report.ClassifyIDThird
  107. return
  108. }
  109. if report.ClassifyIDSecond > 0 {
  110. report.ClassifyID = report.ClassifyIDSecond
  111. return
  112. }
  113. report.ClassifyID = report.ClassifyIDFirst
  114. }