eta_report.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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,"
  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"
  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. }
  43. //type ReportClassify struct {
  44. // ClassifyID int `gorm:"column:classify_id" json:"classify_id"`
  45. // ReportPermission
  46. //}
  47. type ReportPermission struct {
  48. ChartPermissionID int `gorm:"primaryKey;autoIncrement;column:chart_permission_id;comment:主键"`
  49. ParentID int `gorm:"size:11;default:0;column:parent_id;comment:父级权限id"`
  50. }
  51. func GetETAReports(id int) (reports []ETAReport, err error) {
  52. 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
  53. if reports != nil {
  54. for i := 0; i < len(reports); i++ {
  55. setClassifyIdValue(&reports[i])
  56. }
  57. }
  58. return
  59. }
  60. func GetUpdateETAReports() (reports []ETAReport, err error) {
  61. duration := time.Now().Add(-30 * time.Second)
  62. modifyTime := duration.Format(time.DateTime)
  63. err = models.ETA().Table("report").Select(colunms+strings.Join(classifyIds, ",")).Where("(state =? or state=?) and modify_time >=?", published, passed, modifyTime).Order("id asc").Find(&reports).Error
  64. if err != nil {
  65. logger.Error("同步eta研报数据失败:%v", err)
  66. }
  67. if reports != nil {
  68. for i := 0; i < len(reports); i++ {
  69. //var date time.Time
  70. //date, err = time.Parse(time.DateTime, reports[i].PublishTime)
  71. //if err != nil {
  72. // logger.Error("时间转换错误:%v", err)
  73. //} else {
  74. // reports[i].PublishedTime = date
  75. //}
  76. setClassifyIdValue(&reports[i])
  77. }
  78. }
  79. return
  80. }
  81. func GetETAReportById(id int) (report ETAReport, err error) {
  82. err = models.ETA().Table("report").Select(detailColumn).Where("id = ?", id).Where("state =? or state=?", published, passed).First(&report).Error
  83. if err != nil {
  84. return
  85. }
  86. //var date time.Time
  87. //date, err = time.Parse(time.DateTime, report.PublishTime)
  88. //if err != nil {
  89. // logger.Error("时间转换错误:%v", err)
  90. // err = nil
  91. //} else {
  92. // report.PublishedTime = date
  93. //}
  94. return
  95. }
  96. func DoSql(sql string, result interface{}, values ...interface{}) (err error) {
  97. db := models.ETA()
  98. return db.Raw(sql, values...).Scan(&result).Error
  99. }
  100. func GetReportClassifyById(id int) (classifyId int, err error) {
  101. db := models.ETA()
  102. err = db.Table("report").
  103. Select("COALESCE(NULLIF(classify_id_third,0),NULLIF(classify_id_second,0),classify_id_first) classify_id").
  104. Where("id =?", id).Scan(&classifyId).Error
  105. return
  106. }
  107. func GetETAReportIdsByClassifyIds(classifyIds []int) (reportIds []int, err error) {
  108. db := models.ETA()
  109. 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
  110. return
  111. }
  112. func setClassifyIdValue(report *ETAReport) {
  113. if report.ClassifyIDThird > 0 {
  114. report.ClassifyID = report.ClassifyIDThird
  115. return
  116. }
  117. if report.ClassifyIDSecond > 0 {
  118. report.ClassifyID = report.ClassifyIDSecond
  119. return
  120. }
  121. report.ClassifyID = report.ClassifyIDFirst
  122. }