123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- package media
- import (
- "errors"
- logger "eta/eta_mini_ht_api/common/component/log"
- "eta/eta_mini_ht_api/models"
- "eta/eta_mini_ht_api/models/config"
- "eta/eta_mini_ht_api/models/report"
- "gorm.io/gorm"
- "time"
- )
- const (
- CommonColumns = "id,media_type,src,media_name,source_type,media_play_milliseconds,permission_ids,published_time,cover_src"
- DetailColumns = "id,media_type,src,media_name,source_type,media_play_milliseconds,permission_ids,published_time,author_id,author_name,cover_src"
- )
- type MediaType string
- const (
- Video MediaType = "video"
- Audio MediaType = "audio"
- )
- type Media struct {
- Id int `gorm:"primary_key;auto_increment;column:id"`
- AuthorID int `gorm:"column:author_id"`
- AuthorName string `gorm:"column:author_name"`
- MediaType MediaType `gorm:"column:media_type"`
- Src string `gorm:"column:src"`
- MediaName string `gorm:"column:media_name"`
- SourceType string `gorm:"column:source_type"`
- CoverSrc string `gorm:"column:cover_src"`
- MediaPlayMilliseconds int `gorm:"column:media_play_milliseconds"`
- PermissionIDs string `gorm:"column:permission_ids"`
- PublishedTime time.Time `gorm:"column:published_time"`
- SendStatus report.SendStatus `gorm:"column:send_status"`
- Deleted int `gorm:"column:deleted"`
- CreatedTime time.Time `gorm:"column:created_time"`
- UpdatedTime time.Time `gorm:"column:updated_time"`
- }
- func (m *Media) BeforeCreate(_ *gorm.DB) (err error) {
- m.PublishedTime = time.Now()
- m.CreatedTime = time.Now()
- return
- }
- func GetCountByMediaType(mediaType string) (count int64, latestId int64) {
- db := models.Main()
- var ids []int
- err := db.Model(&Media{}).Select("id").Where("media_type = ? and deleted = ?", mediaType, false).Order("id desc").Scan(&ids).Error
- if err != nil {
- return 0, 0
- }
- count = int64(len(ids))
- err = db.Model(&Media{}).Select("MAX(id) id ").Where("media_type = ? and deleted = ?", mediaType, false).Scan(&latestId).Error
- if err != nil {
- logger.Error("获取最新的媒体Id失败:%v", err)
- return 0, 0
- }
- //latestId = int64(ids[0])
- return
- }
- func GetCountByMediaTypeByAnalystId(mediaType string, analystId int) (count int64, latestId int64) {
- db := models.Main()
- err := db.Model(&Media{}).Select("id").Where("media_type = ? and deleted = ? and author_id = ?", mediaType, false, analystId).Count(&count).Error
- if err != nil {
- return
- }
- err = db.Model(&Media{}).Select("Max(id)").Where("media_type = ? and deleted = ? and author_id = ?", mediaType, false, analystId).Scan(&latestId).Error
- if err != nil {
- return
- }
- return
- }
- func GetMediaPage(latestId int64, limit int, offset int, mediaType string) (mediaList []Media, err error) {
- if latestId < 0 {
- err = errors.New("非法的id参数")
- logger.Error("非法的id参数:%d", latestId)
- return
- }
- if limit <= 0 {
- err = errors.New("非法的limit参数")
- logger.Error("非法的limit参数:%d", limit)
- }
- db := models.Main()
- 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
- return
- }
- func GetMediaPageByIds(latestId int64, limit int, offset int, mediaType string, mediasIds []int) (mediaList []Media, err error) {
- if len(mediasIds) == 0 {
- return GetMediaPage(latestId, limit, offset, mediaType)
- }
- if latestId < 0 {
- err = errors.New("非法的id参数")
- logger.Error("非法的id参数:%d", latestId)
- return
- }
- if limit <= 0 {
- err = errors.New("非法的limit参数")
- logger.Error("非法的limit参数:%d", limit)
- }
- db := models.Main()
- 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
- return
- }
- func GetAnalystMediaRangeMediaIds(mediaType string, srcIds []int, analystId int) (mediaIds []int, err error) {
- if len(srcIds) == 0 {
- logger.Info("过滤的媒体ID为空")
- return
- }
- db := models.Main()
- err = db.Model(&Media{}).Select("id").Where(" id in ? and media_type= ? and author_id = ? and deleted =?", srcIds, mediaType, analystId, false).Find(&mediaIds).Error
- return
- }
- func GetMediaPageByAnalystId(latestId int64, limit int, offset int, mediaType string, analystId int, mediaIds []int) (mediaList []Media, err error) {
- if latestId < 0 {
- err = errors.New("非法的id参数")
- logger.Error("非法的id参数:%d", latestId)
- return
- }
- if limit <= 0 {
- err = errors.New("非法的limit参数")
- logger.Error("非法的limit参数:%d", limit)
- }
- db := models.Main()
- 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
- return
- }
- func GetMediaById(mediaType string, mediaId int) (media Media, err error) {
- db := models.Main()
- err = db.Select(DetailColumns).Where("id =? and media_type=? and deleted =?", mediaId, mediaType, false).First(&media).Error
- return
- }
- func CountPermissionWeight(ids []int) (list []config.PermissionWeight, err error) {
- db := models.Main()
- 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`
- err = db.Raw(sql, ids).Find(&list).Error
- return
- }
|