eta_report.go 5.0 KB

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