vote-video-api/api/dao/VideoAuthor.go

136 lines
3.7 KiB
Go

package dao
import (
"gorm.io/gorm"
"gorm.io/gorm/clause"
"vote-video-api/api/model"
"vote-video-api/global"
)
type VideoAuthorDao struct {
}
// GetVideoAuthorById 获取数据-id
func (r *VideoAuthorDao) GetVideoAuthorById(authorId int64) (m *model.VideoAuthor, err error) {
err = global.Db.First(&m, authorId).Error
if err != nil {
return nil, err
}
return m, nil
}
// GetVideoAuthorPreloadById 获取数据-加载全部关联-id
func (r *VideoAuthorDao) GetVideoAuthorPreloadById(authorId int64) (m *model.VideoAuthor, err error) {
err = global.Db.Preload(clause.Associations).First(&m, authorId).Error
if err != nil {
return nil, err
}
return m, nil
}
// GetVideoAuthorListPreloadByVideoId 获取数据-列表-视频id
func (r *VideoAuthorDao) GetVideoAuthorListPreloadByVideoId(videoId int64) (m []*model.VideoAuthor, err error) {
err = global.Db.Preload(clause.Associations).Where("video_id = ?", videoId).Find(&m).Error
if err != nil {
return nil, err
}
return m, nil
}
// DeleteVideoAuthor 删除
func (r *VideoAuthorDao) DeleteVideoAuthor(tx *gorm.DB, maps interface{}) error {
err := tx.Where(maps).Delete(&model.VideoAuthor{}).Error
if err != nil {
return err
}
return nil
}
// DeleteVideoAuthorById 删除-id
func (r *VideoAuthorDao) DeleteVideoAuthorById(tx *gorm.DB, authorId int64) error {
if err := tx.Delete(&model.VideoAuthor{}, authorId).Error; err != nil {
return err
}
return nil
}
// EditVideoAuthor 修改
func (r *VideoAuthorDao) EditVideoAuthor(tx *gorm.DB, maps interface{}, data interface{}) error {
err := tx.Model(&model.VideoAuthor{}).Where(maps).Updates(data).Error
if err != nil {
return err
}
return nil
}
// EditVideoAuthorById 修改-id
func (r *VideoAuthorDao) EditVideoAuthorById(tx *gorm.DB, authorId int64, data interface{}) error {
err := tx.Model(&model.VideoAuthor{}).Where("authorId = ?", authorId).Updates(data).Error
if err != nil {
return err
}
return nil
}
// GetVideoAuthorList 获取列表
func (r *VideoAuthorDao) GetVideoAuthorList(maps interface{}) (m []*model.VideoAuthor, err error) {
err = global.Db.Where(maps).Find(&m).Error
if err != nil {
return nil, err
}
return m, nil
}
// GetVideoAuthorCount 获取数量
func (r *VideoAuthorDao) GetVideoAuthorCount(maps interface{}) (total int64, err error) {
err = global.Db.Model(&model.VideoAuthor{}).Where(maps).Count(&total).Error
if err != nil {
return 0, err
}
return total, nil
}
// GetVideoAuthorListRand 获取列表-随机
func (r *VideoAuthorDao) GetVideoAuthorListRand(maps interface{}, limit int) (m []*model.VideoAuthor, err error) {
err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error
if err != nil {
return nil, err
}
return m, nil
}
// AddVideoAuthor 新增
func (r *VideoAuthorDao) AddVideoAuthor(tx *gorm.DB, model *model.VideoAuthor) (*model.VideoAuthor, error) {
if err := tx.Create(model).Error; err != nil {
return nil, err
}
return model, nil
}
// GetVideoAuthor 获取
func (r *VideoAuthorDao) GetVideoAuthor(maps interface{}) (m *model.VideoAuthor, err error) {
err = global.Db.Where(maps).First(&m).Error
if err != nil {
return nil, err
}
return m, nil
}
// Inc 自增
func (r *VideoAuthorDao) Inc(tx *gorm.DB, authorId int64, field string, numeral int) error {
err := tx.Model(&model.VideoAuthor{}).Where("authorId = ?", authorId).UpdateColumn(field, gorm.Expr(field+" + ?", numeral)).Error
if err != nil {
return err
}
return nil
}
// Dec 自减
func (r *VideoAuthorDao) Dec(tx *gorm.DB, authorId int64, field string, numeral int) error {
err := tx.Model(&model.VideoAuthor{}).Where("authorId = ?", authorId).UpdateColumn(field, gorm.Expr(field+" - ?", numeral)).Error
if err != nil {
return err
}
return nil
}