package dao import ( "gorm.io/gorm" "gorm.io/gorm/clause" "vote-admin-api/api/model" "vote-admin-api/api/requests" "vote-admin-api/global" ) type VideoDao struct { } // GetVideoById 获取数据-id func (r *VideoDao) GetVideoById(VideoId int64) (m *model.Video, err error) { err = global.Db.First(&m, VideoId).Error if err != nil { return nil, err } return m, nil } // GetVideoPreloadById 获取数据-加载全部关联-id func (r *VideoDao) GetVideoPreloadById(VideoId int64) (m *model.Video, err error) { err = global.Db.Preload(clause.Associations).First(&m, VideoId).Error if err != nil { return nil, err } return m, nil } // DeleteVideo 删除 func (r *VideoDao) DeleteVideo(tx *gorm.DB, maps interface{}) error { err := tx.Where(maps).Delete(&model.Video{}).Error if err != nil { return err } return nil } // DeleteVideoById 删除-id func (r *VideoDao) DeleteVideoById(tx *gorm.DB, VideoId int64) error { if err := tx.Delete(&model.Video{}, VideoId).Error; err != nil { return err } return nil } // EditVideo 修改 func (r *VideoDao) EditVideo(tx *gorm.DB, maps interface{}, data interface{}) error { err := tx.Model(&model.Video{}).Where(maps).Updates(data).Error if err != nil { return err } return nil } // EditVideoById 修改-id func (r *VideoDao) EditVideoById(tx *gorm.DB, VideoId int64, data interface{}) error { err := tx.Model(&model.Video{}).Where("video_id = ?", VideoId).Updates(data).Error if err != nil { return err } return nil } // GetVideoList 获取列表 func (r *VideoDao) GetVideoList(maps interface{}) (m []*model.Video, err error) { err = global.Db.Where(maps).Find(&m).Error if err != nil { return nil, err } return m, nil } // GetVideoCount 获取数量 func (r *VideoDao) GetVideoCount(maps interface{}) (total int64, err error) { err = global.Db.Model(&model.Video{}).Where(maps).Count(&total).Error if err != nil { return 0, err } return total, nil } // GetVideoListRand 获取列表-随机 func (r *VideoDao) GetVideoListRand(maps interface{}, limit int) (m []*model.Video, err error) { err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error if err != nil { return nil, err } return m, nil } // AddVideo 新增 func (r *VideoDao) AddVideo(tx *gorm.DB, model *model.Video) (*model.Video, error) { if err := tx.Create(model).Error; err != nil { return nil, err } return model, nil } // GetVideo 获取 func (r *VideoDao) GetVideo(maps interface{}) (m *model.Video, err error) { err = global.Db.Where(maps).First(&m).Error if err != nil { return nil, err } return m, nil } // Inc 自增 func (r *VideoDao) Inc(tx *gorm.DB, VideoId int64, field string, numeral int) error { err := tx.Model(&model.Video{}).Where("video_id = ?", VideoId).UpdateColumn(field, gorm.Expr(field+" + ?", numeral)).Error if err != nil { return err } return nil } // Dec 自减 func (r *VideoDao) Dec(tx *gorm.DB, VideoId int64, field string, numeral int) error { err := tx.Model(&model.Video{}).Where("video_id = ?", VideoId).UpdateColumn(field, gorm.Expr(field+" - ?", numeral)).Error if err != nil { return err } return nil } // GetVideoPageSearch 获取视频列表-分页 func (r *VideoDao) GetVideoPageSearch(req requests.GetVideoPage, page, pageSize int) (m []*model.Video, total int64, err error) { var totalRecords int64 // 构建查询条件 query := global.Db.Model(&model.Video{}) // 作者 query = query.Preload("VideoAuthor") // 作者关联医院 query = query.Preload("VideoAuthor.BaseHospital") // 文章状态(1:正常 2:禁用) query = query.Where("video_status = ?", 1) // 搜索关键字 if req.Keyword != "" { keyword := "%" + req.Keyword + "%" // // 标题 orQuery := global.Db.Model(&model.Video{}).Or("video_title LIKE ?", keyword) // 医院名称 hospitalSubQuery := global.Db.Model(&model.BaseHospital{}). Select("hospital_id"). Where("hospital_name LIKE ?", keyword) articleAuthorSubQuery := global.Db.Model(&model.VideoAuthor{}). Select("video_id"). Where(gorm.Expr("hospital_id IN (?)", hospitalSubQuery)) orQuery = orQuery.Or(gorm.Expr("video_id IN (?)", articleAuthorSubQuery)) // 作者姓名 subQuery := global.Db.Model(&model.VideoAuthor{}). Select("video_id"). Where("author_name LIKE ?", keyword) orQuery = orQuery.Or(gorm.Expr("video_id IN (?)", subQuery)) // 执行组建 query = query.Where(orQuery) } // 排序 query = query.Order("created_at asc") // 查询总数量 if err := query.Count(&totalRecords).Error; err != nil { return nil, 0, err } err = query.Scopes(model.Paginate(page, pageSize)).Find(&m).Error if err != nil { return nil, 0, err } return m, totalRecords, nil } // GetVideoOrderList 获取列表-排序 func (r *VideoDao) GetVideoOrderList(maps interface{}, orderField string, limit int) (m []*model.Video, err error) { err = global.Db.Where(maps).Preload(clause.Associations).Order(orderField).Limit(limit).Find(&m).Error if err != nil { return nil, err } return m, nil } // GetVideoRank 获取某一条数据的排名 func (r *VideoDao) GetVideoRank(videoID int64) (int, error) { var rank int // 定义子查询 subQuery := global.Db.Model(&model.Video{}). Select("video_id, vote_num, (@rank := @rank + 1) AS rank"). Where("video_status = ?", 1). Order("vote_num DESC"). Joins(", (SELECT @rank := 0) AS r") // 将子查询作为命名子查询的一部分进行查询 err := global.Db.Table("(?) AS sub", subQuery). Where("sub.video_id = ?", videoID). Pluck("sub.rank", &rank).Error if err != nil { return 0, err } return rank, nil }