case-admin-api/api/dao/StatCaseQuestion.go
2025-03-07 17:23:50 +08:00

127 lines
3.6 KiB
Go

package dao
import (
"case-admin-api/api/model"
"case-admin-api/global"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type StatCaseQuestionDao struct {
}
// GetStatCaseQuestionById 获取数据-id
func (r *StatCaseQuestionDao) GetStatCaseQuestionById(StatId int64) (m *model.StatCaseQuestion, err error) {
err = global.Db.First(&m, StatId).Error
if err != nil {
return nil, err
}
return m, nil
}
// GetStatCaseQuestionPreloadById 获取数据-加载全部关联-id
func (r *StatCaseQuestionDao) GetStatCaseQuestionPreloadById(StatId int64) (m *model.StatCaseQuestion, err error) {
err = global.Db.Preload(clause.Associations).First(&m, StatId).Error
if err != nil {
return nil, err
}
return m, nil
}
// DeleteStatCaseQuestion 删除
func (r *StatCaseQuestionDao) DeleteStatCaseQuestion(tx *gorm.DB, maps interface{}) error {
err := tx.Where(maps).Delete(&model.StatCaseQuestion{}).Error
if err != nil {
return err
}
return nil
}
// DeleteStatCaseQuestionById 删除-id
func (r *StatCaseQuestionDao) DeleteStatCaseQuestionById(tx *gorm.DB, StatId int64) error {
if err := tx.Delete(&model.StatCaseQuestion{}, StatId).Error; err != nil {
return err
}
return nil
}
// EditStatCaseQuestion 修改
func (r *StatCaseQuestionDao) EditStatCaseQuestion(tx *gorm.DB, maps interface{}, data interface{}) error {
err := tx.Model(&model.StatCaseQuestion{}).Where(maps).Updates(data).Error
if err != nil {
return err
}
return nil
}
// EditStatCaseQuestionById 修改-id
func (r *StatCaseQuestionDao) EditStatCaseQuestionById(tx *gorm.DB, StatId int64, data interface{}) error {
err := tx.Model(&model.StatCaseQuestion{}).Where("stat_id = ?", StatId).Updates(data).Error
if err != nil {
return err
}
return nil
}
// GetStatCaseQuestionList 获取列表
func (r *StatCaseQuestionDao) GetStatCaseQuestionList(maps interface{}) (m []*model.StatCaseQuestion, err error) {
err = global.Db.Where(maps).Find(&m).Error
if err != nil {
return nil, err
}
return m, nil
}
// GetStatCaseQuestionCount 获取数量
func (r *StatCaseQuestionDao) GetStatCaseQuestionCount(maps interface{}) (total int64, err error) {
err = global.Db.Model(&model.StatCaseQuestion{}).Where(maps).Count(&total).Error
if err != nil {
return 0, err
}
return total, nil
}
// GetStatCaseQuestionListRand 获取列表-随机
func (r *StatCaseQuestionDao) GetStatCaseQuestionListRand(maps interface{}, limit int) (m []*model.StatCaseQuestion, err error) {
err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error
if err != nil {
return nil, err
}
return m, nil
}
// AddStatCaseQuestion 新增
func (r *StatCaseQuestionDao) AddStatCaseQuestion(tx *gorm.DB, model *model.StatCaseQuestion) (*model.StatCaseQuestion, error) {
if err := tx.Create(model).Error; err != nil {
return nil, err
}
return model, nil
}
// GetStatCaseQuestion 获取
func (r *StatCaseQuestionDao) GetStatCaseQuestion(maps interface{}) (m *model.StatCaseQuestion, err error) {
err = global.Db.Where(maps).First(&m).Error
if err != nil {
return nil, err
}
return m, nil
}
// Inc 自增
func (r *StatCaseQuestionDao) Inc(tx *gorm.DB, StatId int64, field string, numeral int) error {
err := tx.Model(&model.StatCaseQuestion{}).Where("stat_id = ?", StatId).UpdateColumn(field, gorm.Expr(field+" + ?", numeral)).Error
if err != nil {
return err
}
return nil
}
// Dec 自减
func (r *StatCaseQuestionDao) Dec(tx *gorm.DB, StatId int64, field string, numeral int) error {
err := tx.Model(&model.StatCaseQuestion{}).Where("stat_id = ?", StatId).UpdateColumn(field, gorm.Expr(field+" - ?", numeral)).Error
if err != nil {
return err
}
return nil
}