109 lines
2.7 KiB
Go
109 lines
2.7 KiB
Go
package dao
|
|
|
|
import (
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
"hepa-calc-api/api/model"
|
|
"hepa-calc-api/global"
|
|
)
|
|
|
|
type QuestionDao struct {
|
|
}
|
|
|
|
// GetQuestionById 获取数据-id
|
|
func (r *QuestionDao) GetQuestionById(QuestionId int64) (m *model.Question, err error) {
|
|
err = global.Db.First(&m, QuestionId).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// GetQuestionPreloadById 获取数据-加载全部关联-id
|
|
func (r *QuestionDao) GetQuestionPreloadById(QuestionId int64) (m *model.Question, err error) {
|
|
err = global.Db.Preload(clause.Associations).First(&m, QuestionId).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// DeleteQuestion 删除
|
|
func (r *QuestionDao) DeleteQuestion(tx *gorm.DB, maps interface{}) error {
|
|
err := tx.Where(maps).Delete(&model.Question{}).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteQuestionById 删除-id
|
|
func (r *QuestionDao) DeleteQuestionById(tx *gorm.DB, QuestionId int64) error {
|
|
if err := tx.Delete(&model.Question{}, QuestionId).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EditQuestion 修改
|
|
func (r *QuestionDao) EditQuestion(tx *gorm.DB, maps interface{}, data interface{}) error {
|
|
err := tx.Model(&model.Question{}).Where(maps).Updates(data).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EditQuestionById 修改-id
|
|
func (r *QuestionDao) EditQuestionById(tx *gorm.DB, QuestionId int64, data interface{}) error {
|
|
err := tx.Model(&model.Question{}).Where("question_id = ?", QuestionId).Updates(data).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetQuestionList 获取列表
|
|
func (r *QuestionDao) GetQuestionList(maps interface{}) (m []*model.Question, err error) {
|
|
err = global.Db.Where(maps).Find(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// GetQuestionCount 获取数量
|
|
func (r *QuestionDao) GetQuestionCount(maps interface{}) (total int64, err error) {
|
|
err = global.Db.Model(&model.Question{}).Where(maps).Count(&total).Error
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return total, nil
|
|
}
|
|
|
|
// GetQuestionListRand 获取列表-随机
|
|
func (r *QuestionDao) GetQuestionListRand(maps interface{}, limit int) (m []*model.Question, err error) {
|
|
err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// AddQuestion 新增
|
|
func (r *QuestionDao) AddQuestion(tx *gorm.DB, model *model.Question) (*model.Question, error) {
|
|
if err := tx.Create(model).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return model, nil
|
|
}
|
|
|
|
// GetQuestion 获取
|
|
func (r *QuestionDao) GetQuestion(maps interface{}) (m *model.Question, err error) {
|
|
err = global.Db.Where(maps).First(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|