eta_report.go 3.6 KB

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