package dao import ( "gorm.io/gorm" "gorm.io/gorm/clause" "vote-admin-api/api/model" "vote-admin-api/global" ) type ArticleAuthorDao struct { } // GetArticleAuthorById 获取数据-id func (r *ArticleAuthorDao) GetArticleAuthorById(AuthorId int64) (m *model.ArticleAuthor, err error) { err = global.Db.First(&m, AuthorId).Error if err != nil { return nil, err } return m, nil } // GetArticleAuthorPreloadById 获取数据-加载全部关联-id func (r *ArticleAuthorDao) GetArticleAuthorPreloadById(AuthorId int64) (m *model.ArticleAuthor, err error) { err = global.Db.Preload(clause.Associations).First(&m, AuthorId).Error if err != nil { return nil, err } return m, nil } // DeleteArticleAuthor 删除 func (r *ArticleAuthorDao) DeleteArticleAuthor(tx *gorm.DB, maps interface{}) error { err := tx.Where(maps).Delete(&model.ArticleAuthor{}).Error if err != nil { return err } return nil } // DeleteArticleAuthorById 删除-id func (r *ArticleAuthorDao) DeleteArticleAuthorById(tx *gorm.DB, ArticleAuthorId int64) error { if err := tx.Delete(&model.ArticleAuthor{}, ArticleAuthorId).Error; err != nil { return err } return nil } // EditArticleAuthor 修改 func (r *ArticleAuthorDao) EditArticleAuthor(tx *gorm.DB, maps interface{}, data interface{}) error { err := tx.Model(&model.ArticleAuthor{}).Where(maps).Updates(data).Error if err != nil { return err } return nil } // EditArticleAuthorById 修改-id func (r *ArticleAuthorDao) EditArticleAuthorById(tx *gorm.DB, AuthorId int64, data interface{}) error { err := tx.Model(&model.ArticleAuthor{}).Where("author_id = ?", AuthorId).Updates(data).Error if err != nil { return err } return nil } // GetArticleAuthorList 获取列表 func (r *ArticleAuthorDao) GetArticleAuthorList(maps interface{}) (m []*model.ArticleAuthor, err error) { err = global.Db.Where(maps).Find(&m).Error if err != nil { return nil, err } return m, nil } // GetArticleAuthorPreloadList 获取列表-加载全部关联 func (r *ArticleAuthorDao) GetArticleAuthorPreloadList(maps interface{}) (m []*model.ArticleAuthor, err error) { err = global.Db.Preload(clause.Associations).Where(maps).Find(&m).Error if err != nil { return nil, err } return m, nil } // GetArticleAuthorCount 获取数量 func (r *ArticleAuthorDao) GetArticleAuthorCount(maps interface{}) (total int64, err error) { err = global.Db.Model(&model.ArticleAuthor{}).Where(maps).Count(&total).Error if err != nil { return 0, err } return total, nil } // GetArticleAuthorListRand 获取列表-随机 func (r *ArticleAuthorDao) GetArticleAuthorListRand(maps interface{}, limit int) (m []*model.ArticleAuthor, err error) { err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error if err != nil { return nil, err } return m, nil } // AddArticleAuthor 新增 func (r *ArticleAuthorDao) AddArticleAuthor(tx *gorm.DB, model *model.ArticleAuthor) (*model.ArticleAuthor, error) { if err := tx.Create(model).Error; err != nil { return nil, err } return model, nil } // GetArticleAuthor 获取 func (r *ArticleAuthorDao) GetArticleAuthor(maps interface{}) (m *model.ArticleAuthor, err error) { err = global.Db.Where(maps).First(&m).Error if err != nil { return nil, err } return m, nil } // Inc 自增 func (r *ArticleAuthorDao) Inc(tx *gorm.DB, AuthorId int64, field string, numeral int) error { err := tx.Model(&model.ArticleAuthor{}).Where("author_id = ?", AuthorId).UpdateColumn(field, gorm.Expr(field+" + ?", numeral)).Error if err != nil { return err } return nil } // Dec 自减 func (r *ArticleAuthorDao) Dec(tx *gorm.DB, AuthorId int64, field string, numeral int) error { err := tx.Model(&model.ArticleAuthor{}).Where("author_id = ?", AuthorId).UpdateColumn(field, gorm.Expr(field+" - ?", numeral)).Error if err != nil { return err } return nil }