media.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package media
  2. import (
  3. "errors"
  4. logger "eta/eta_mini_ht_api/common/component/log"
  5. "eta/eta_mini_ht_api/models"
  6. "eta/eta_mini_ht_api/models/config"
  7. "eta/eta_mini_ht_api/models/report"
  8. "gorm.io/gorm"
  9. "time"
  10. )
  11. const (
  12. CommonColumns = "id,media_type,src,media_name,source_type,media_play_milliseconds,permission_ids,published_time,cover_src"
  13. DetailColumns = "id,media_type,src,media_name,source_type,media_play_milliseconds,permission_ids,published_time,author_id,author_name,cover_src"
  14. )
  15. type MediaType string
  16. const (
  17. Video MediaType = "video"
  18. Audio MediaType = "audio"
  19. )
  20. type Media struct {
  21. Id int `gorm:"primary_key;auto_increment;column:id"`
  22. AuthorID int `gorm:"column:author_id"`
  23. AuthorName string `gorm:"column:author_name"`
  24. MediaType MediaType `gorm:"column:media_type"`
  25. Src string `gorm:"column:src"`
  26. MediaName string `gorm:"column:media_name"`
  27. SourceType string `gorm:"column:source_type"`
  28. CoverSrc string `gorm:"column:cover_src"`
  29. MediaPlayMilliseconds int `gorm:"column:media_play_milliseconds"`
  30. PermissionIDs string `gorm:"column:permission_ids"`
  31. PublishedTime time.Time `gorm:"column:published_time"`
  32. SendStatus report.SendStatus `gorm:"column:send_status"`
  33. Deleted int `gorm:"column:deleted"`
  34. CreatedTime time.Time `gorm:"column:created_time"`
  35. UpdatedTime time.Time `gorm:"column:updated_time"`
  36. }
  37. func (m *Media) BeforeCreate(_ *gorm.DB) (err error) {
  38. m.PublishedTime = time.Now()
  39. m.CreatedTime = time.Now()
  40. return
  41. }
  42. func GetCountByMediaType(mediaType string) (count int64, latestId int64) {
  43. db := models.Main()
  44. var ids []int
  45. err := db.Model(&Media{}).Select("id").Where("media_type = ? and deleted = ?", mediaType, false).Order("id desc").Scan(&ids).Error
  46. if err != nil {
  47. return 0, 0
  48. }
  49. count = int64(len(ids))
  50. err = db.Model(&Media{}).Select("MAX(id) id ").Where("media_type = ? and deleted = ?", mediaType, false).Scan(&latestId).Error
  51. if err != nil {
  52. logger.Error("获取最新的媒体Id失败:%v", err)
  53. return 0, 0
  54. }
  55. //latestId = int64(ids[0])
  56. return
  57. }
  58. func GetCountByMediaTypeByAnalystId(mediaType string, analystId int) (count int64, latestId int64) {
  59. db := models.Main()
  60. err := db.Model(&Media{}).Select("id").Where("media_type = ? and deleted = ? and author_id = ?", mediaType, false, analystId).Count(&count).Error
  61. if err != nil {
  62. return
  63. }
  64. err = db.Model(&Media{}).Select("Max(id)").Where("media_type = ? and deleted = ? and author_id = ?", mediaType, false, analystId).Scan(&latestId).Error
  65. if err != nil {
  66. return
  67. }
  68. return
  69. }
  70. func GetMediaPage(latestId int64, limit int, offset int, mediaType string) (mediaList []Media, err error) {
  71. if latestId < 0 {
  72. err = errors.New("非法的id参数")
  73. logger.Error("非法的id参数:%d", latestId)
  74. return
  75. }
  76. if limit <= 0 {
  77. err = errors.New("非法的limit参数")
  78. logger.Error("非法的limit参数:%d", limit)
  79. }
  80. db := models.Main()
  81. err = db.Select(CommonColumns).Where("media_type= ? and id<= ? and deleted=?", mediaType, latestId, false).Order("created_time desc").Limit(limit).Offset(offset).Find(&mediaList).Error
  82. return
  83. }
  84. func GetMediaPageByIds(latestId int64, limit int, offset int, mediaType string, mediasIds []int) (mediaList []Media, err error) {
  85. if len(mediasIds) == 0 {
  86. return GetMediaPage(latestId, limit, offset, mediaType)
  87. }
  88. if latestId < 0 {
  89. err = errors.New("非法的id参数")
  90. logger.Error("非法的id参数:%d", latestId)
  91. return
  92. }
  93. if limit <= 0 {
  94. err = errors.New("非法的limit参数")
  95. logger.Error("非法的limit参数:%d", limit)
  96. }
  97. db := models.Main()
  98. err = db.Select(CommonColumns).Where(" id<= ? and media_type= ? and id in ? and deleted =?", latestId, mediaType, mediasIds, false).Order("created_time desc").Limit(limit).Offset(offset).Find(&mediaList).Error
  99. return
  100. }
  101. func GetAnalystMediaRangeMediaIds(mediaType string, srcIds []int, analystId int) (mediaIds []int, err error) {
  102. if len(srcIds) == 0 {
  103. logger.Info("过滤的媒体ID为空")
  104. return
  105. }
  106. db := models.Main()
  107. err = db.Model(&Media{}).Select("id").Where(" id in ? and media_type= ? and author_id = ? and deleted =?", srcIds, mediaType, analystId, false).Find(&mediaIds).Error
  108. return
  109. }
  110. func GetMediaPageByAnalystId(latestId int64, limit int, offset int, mediaType string, analystId int, mediaIds []int) (mediaList []Media, err error) {
  111. if latestId < 0 {
  112. err = errors.New("非法的id参数")
  113. logger.Error("非法的id参数:%d", latestId)
  114. return
  115. }
  116. if limit <= 0 {
  117. err = errors.New("非法的limit参数")
  118. logger.Error("非法的limit参数:%d", limit)
  119. }
  120. db := models.Main()
  121. err = db.Select(CommonColumns).Where(" id<= ? and media_type= ? and author_id = ? and id in ? and deleted = ?", latestId, mediaType, analystId, mediaIds, false).Order("published_time desc").Limit(limit).Offset(offset).Find(&mediaList).Error
  122. return
  123. }
  124. func GetMediaById(mediaType string, mediaId int) (media Media, err error) {
  125. db := models.Main()
  126. err = db.Select(DetailColumns).Where("id =? and media_type=? and deleted =?", mediaId, mediaType, false).First(&media).Error
  127. return
  128. }
  129. func CountPermissionWeight(ids []int) (list []config.PermissionWeight, err error) {
  130. db := models.Main()
  131. sql := `select permission_id,count(*) num from(select mpm.permission_id from media m LEFT JOIN (select permission_id,media_id from media_permission_mappings where deleted=0) mpm on mpm.media_id=m.id where m.id in (?)) a GROUP BY a.permission_id`
  132. err = db.Raw(sql, ids).Find(&list).Error
  133. return
  134. }