eta_report.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package eta
  2. import (
  3. "eta/eta_mini_ht_api/models"
  4. "strings"
  5. "time"
  6. )
  7. const (
  8. colunms = "id,author,abstract,title,publish_time,"
  9. 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"
  10. published = 2
  11. passed = 6
  12. limit = 500
  13. TlbChartPermissionSearchKeyWordMapping = "chart_permission_search_key_word_mapping"
  14. )
  15. var (
  16. classifyIds = []string{"classify_id_third", "classify_id_second", "classify_id_first"}
  17. )
  18. func (ETAReport) TableName() string {
  19. return "report"
  20. }
  21. type ETAReport struct {
  22. ID int `gorm:"primary_key;auto_increment"`
  23. ClassifyID int `gorm:"_"`
  24. ClassifyIDFirst int `gorm:"column:classify_id_first;default:0"`
  25. ClassifyIDSecond int `gorm:"column:classify_id_second;default:0"`
  26. ClassifyIDThird int `gorm:"column:classify_id_third;default:0"`
  27. Title string `gorm:"column:title;size:125;"`
  28. Abstract string `gorm:"column:abstract;size:255;"`
  29. Author string `gorm:"column:author;size:50;"`
  30. PublishTime string `gorm:"column:publish_time"`
  31. Content string `gorm:"column:content"`
  32. CollaborateType int `gorm:"column:collaborate_type"`
  33. ReportLayout int `gorm:"column:report_layout"`
  34. VideoUrl string `gorm:"column:video_url"`
  35. VideoName string `gorm:"column:video_name"`
  36. VideoPlaySeconds string `gorm:"column:video_play_seconds"`
  37. HeadResourceId int `gorm:"column:head_resource_id"`
  38. EndResourceId int `gorm:"column:end_resource_id"`
  39. HasChapter bool `gorm:"column:has_chapter"`
  40. NeedSplice bool `gorm:"column:need_splice"`
  41. }
  42. //type ReportClassify struct {
  43. // ClassifyID int `gorm:"column:classify_id" json:"classify_id"`
  44. // ReportPermission
  45. //}
  46. type ReportPermission struct {
  47. ChartPermissionID int `gorm:"primaryKey;autoIncrement;column:chart_permission_id;comment:主键"`
  48. ParentID int `gorm:"size:11;default:0;column:parent_id;comment:父级权限id"`
  49. }
  50. func GetETAReports(id int) (reports []ETAReport, err error) {
  51. 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
  52. if reports != nil {
  53. for i := 0; i < len(reports); i++ {
  54. setClassifyIdValue(&reports[i])
  55. }
  56. }
  57. return
  58. }
  59. func GetUpdateETAReports(id int) (reports []ETAReport, err error) {
  60. duration := time.Now().Add(-10 * time.Minute)
  61. err = models.ETA().Table("report").Select(colunms+strings.Join(classifyIds, ",")).Where("state =? or state=? and modify_time >=?", published, passed, duration).Where("id <= ?", id).Order("id asc").Find(&reports).Error
  62. if reports != nil {
  63. for _, report := range reports {
  64. setClassifyIdValue(&report)
  65. }
  66. }
  67. return
  68. }
  69. func GetETAReportById(id int) (report ETAReport, err error) {
  70. err = models.ETA().Table("report").Select(detailColumn).Where("id = ?", id).Where("state =? or state=?", published, passed).First(&report).Error
  71. return
  72. }
  73. func DoSql(sql string, result interface{}, values ...interface{}) (err error) {
  74. db := models.ETA()
  75. return db.Raw(sql, values...).Scan(&result).Error
  76. }
  77. func GetReportClassifyById(id int) (classifyId int, err error) {
  78. db := models.ETA()
  79. err = db.Table("report").
  80. Select("COALESCE(NULLIF(classify_id_third,0),NULLIF(classify_id_second,0),classify_id_first) classify_id").
  81. Where("id =?", id).Scan(&classifyId).Error
  82. return
  83. }
  84. func GetETAReportIdsByClassifyIds(classifyIds []int) (reportIds []int, err error) {
  85. db := models.ETA()
  86. 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
  87. return
  88. }
  89. func setClassifyIdValue(report *ETAReport) {
  90. if report.ClassifyIDThird > 0 {
  91. report.ClassifyID = report.ClassifyIDThird
  92. return
  93. }
  94. if report.ClassifyIDSecond > 0 {
  95. report.ClassifyID = report.ClassifyIDSecond
  96. return
  97. }
  98. report.ClassifyID = report.ClassifyIDFirst
  99. }