eta_report.go 4.7 KB

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