// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.

package query

import (
	"context"

	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"gorm.io/gorm/schema"

	"gorm.io/gen"
	"gorm.io/gen/field"

	"gorm.io/plugin/dbresolver"

	"git.hlsq.asia/mmorpg/user_db/model"
)

func newQuestion(db *gorm.DB, opts ...gen.DOOption) question {
	_question := question{}

	_question.questionDo.UseDB(db, opts...)
	_question.questionDo.UseModel(&model.Question{})

	tableName := _question.questionDo.TableName()
	_question.ALL = field.NewAsterisk(tableName)
	_question.ID = field.NewUint64(tableName, "id")
	_question.Sn = field.NewString(tableName, "sn")
	_question.Question = field.NewString(tableName, "question")
	_question.Options = field.NewString(tableName, "options")
	_question.Answer = field.NewString(tableName, "answer")
	_question.Explanation = field.NewString(tableName, "explanation")
	_question.Difficulty = field.NewInt32(tableName, "difficulty")
	_question.Category = field.NewString(tableName, "category")
	_question.CreatedAt = field.NewTime(tableName, "created_at")
	_question.UpdatedAt = field.NewTime(tableName, "updated_at")
	_question.DeletedAt = field.NewField(tableName, "deleted_at")

	_question.fillFieldMap()

	return _question
}

type question struct {
	questionDo questionDo

	ALL         field.Asterisk
	ID          field.Uint64
	Sn          field.String // 业务唯一编号
	Question    field.String // 题干
	Options     field.String // 选项
	Answer      field.String // 答案
	Explanation field.String // 解析
	Difficulty  field.Int32  // 难度分 0 - 100
	Category    field.String // 分类
	CreatedAt   field.Time
	UpdatedAt   field.Time
	DeletedAt   field.Field

	fieldMap map[string]field.Expr
}

func (q question) Table(newTableName string) *question {
	q.questionDo.UseTable(newTableName)
	return q.updateTableName(newTableName)
}

func (q question) As(alias string) *question {
	q.questionDo.DO = *(q.questionDo.As(alias).(*gen.DO))
	return q.updateTableName(alias)
}

func (q *question) updateTableName(table string) *question {
	q.ALL = field.NewAsterisk(table)
	q.ID = field.NewUint64(table, "id")
	q.Sn = field.NewString(table, "sn")
	q.Question = field.NewString(table, "question")
	q.Options = field.NewString(table, "options")
	q.Answer = field.NewString(table, "answer")
	q.Explanation = field.NewString(table, "explanation")
	q.Difficulty = field.NewInt32(table, "difficulty")
	q.Category = field.NewString(table, "category")
	q.CreatedAt = field.NewTime(table, "created_at")
	q.UpdatedAt = field.NewTime(table, "updated_at")
	q.DeletedAt = field.NewField(table, "deleted_at")

	q.fillFieldMap()

	return q
}

func (q *question) WithContext(ctx context.Context) *questionDo { return q.questionDo.WithContext(ctx) }

func (q question) TableName() string { return q.questionDo.TableName() }

func (q question) Alias() string { return q.questionDo.Alias() }

func (q question) Columns(cols ...field.Expr) gen.Columns { return q.questionDo.Columns(cols...) }

func (q *question) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
	_f, ok := q.fieldMap[fieldName]
	if !ok || _f == nil {
		return nil, false
	}
	_oe, ok := _f.(field.OrderExpr)
	return _oe, ok
}

func (q *question) fillFieldMap() {
	q.fieldMap = make(map[string]field.Expr, 11)
	q.fieldMap["id"] = q.ID
	q.fieldMap["sn"] = q.Sn
	q.fieldMap["question"] = q.Question
	q.fieldMap["options"] = q.Options
	q.fieldMap["answer"] = q.Answer
	q.fieldMap["explanation"] = q.Explanation
	q.fieldMap["difficulty"] = q.Difficulty
	q.fieldMap["category"] = q.Category
	q.fieldMap["created_at"] = q.CreatedAt
	q.fieldMap["updated_at"] = q.UpdatedAt
	q.fieldMap["deleted_at"] = q.DeletedAt
}

func (q question) clone(db *gorm.DB) question {
	q.questionDo.ReplaceConnPool(db.Statement.ConnPool)
	return q
}

func (q question) replaceDB(db *gorm.DB) question {
	q.questionDo.ReplaceDB(db)
	return q
}

type questionDo struct{ gen.DO }

func (q questionDo) Debug() *questionDo {
	return q.withDO(q.DO.Debug())
}

func (q questionDo) WithContext(ctx context.Context) *questionDo {
	return q.withDO(q.DO.WithContext(ctx))
}

func (q questionDo) ReadDB() *questionDo {
	return q.Clauses(dbresolver.Read)
}

func (q questionDo) WriteDB() *questionDo {
	return q.Clauses(dbresolver.Write)
}

func (q questionDo) Session(config *gorm.Session) *questionDo {
	return q.withDO(q.DO.Session(config))
}

func (q questionDo) Clauses(conds ...clause.Expression) *questionDo {
	return q.withDO(q.DO.Clauses(conds...))
}

func (q questionDo) Returning(value interface{}, columns ...string) *questionDo {
	return q.withDO(q.DO.Returning(value, columns...))
}

func (q questionDo) Not(conds ...gen.Condition) *questionDo {
	return q.withDO(q.DO.Not(conds...))
}

func (q questionDo) Or(conds ...gen.Condition) *questionDo {
	return q.withDO(q.DO.Or(conds...))
}

func (q questionDo) Select(conds ...field.Expr) *questionDo {
	return q.withDO(q.DO.Select(conds...))
}

func (q questionDo) Where(conds ...gen.Condition) *questionDo {
	return q.withDO(q.DO.Where(conds...))
}

func (q questionDo) Order(conds ...field.Expr) *questionDo {
	return q.withDO(q.DO.Order(conds...))
}

func (q questionDo) Distinct(cols ...field.Expr) *questionDo {
	return q.withDO(q.DO.Distinct(cols...))
}

func (q questionDo) Omit(cols ...field.Expr) *questionDo {
	return q.withDO(q.DO.Omit(cols...))
}

func (q questionDo) Join(table schema.Tabler, on ...field.Expr) *questionDo {
	return q.withDO(q.DO.Join(table, on...))
}

func (q questionDo) LeftJoin(table schema.Tabler, on ...field.Expr) *questionDo {
	return q.withDO(q.DO.LeftJoin(table, on...))
}

func (q questionDo) RightJoin(table schema.Tabler, on ...field.Expr) *questionDo {
	return q.withDO(q.DO.RightJoin(table, on...))
}

func (q questionDo) Group(cols ...field.Expr) *questionDo {
	return q.withDO(q.DO.Group(cols...))
}

func (q questionDo) Having(conds ...gen.Condition) *questionDo {
	return q.withDO(q.DO.Having(conds...))
}

func (q questionDo) Limit(limit int) *questionDo {
	return q.withDO(q.DO.Limit(limit))
}

func (q questionDo) Offset(offset int) *questionDo {
	return q.withDO(q.DO.Offset(offset))
}

func (q questionDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *questionDo {
	return q.withDO(q.DO.Scopes(funcs...))
}

func (q questionDo) Unscoped() *questionDo {
	return q.withDO(q.DO.Unscoped())
}

func (q questionDo) Create(values ...*model.Question) error {
	if len(values) == 0 {
		return nil
	}
	return q.DO.Create(values)
}

func (q questionDo) CreateInBatches(values []*model.Question, batchSize int) error {
	return q.DO.CreateInBatches(values, batchSize)
}

// Save : !!! underlying implementation is different with GORM
// The method is equivalent to executing the statement: db.Clauses(clause.OnConflict{UpdateAll: true}).Create(values)
func (q questionDo) Save(values ...*model.Question) error {
	if len(values) == 0 {
		return nil
	}
	return q.DO.Save(values)
}

func (q questionDo) First() (*model.Question, error) {
	if result, err := q.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Question), nil
	}
}

func (q questionDo) Take() (*model.Question, error) {
	if result, err := q.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Question), nil
	}
}

func (q questionDo) Last() (*model.Question, error) {
	if result, err := q.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Question), nil
	}
}

func (q questionDo) Find() ([]*model.Question, error) {
	result, err := q.DO.Find()
	return result.([]*model.Question), err
}

func (q questionDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.Question, err error) {
	buf := make([]*model.Question, 0, batchSize)
	err = q.DO.FindInBatches(&buf, batchSize, func(tx gen.Dao, batch int) error {
		defer func() { results = append(results, buf...) }()
		return fc(tx, batch)
	})
	return results, err
}

func (q questionDo) FindInBatches(result *[]*model.Question, batchSize int, fc func(tx gen.Dao, batch int) error) error {
	return q.DO.FindInBatches(result, batchSize, fc)
}

func (q questionDo) Attrs(attrs ...field.AssignExpr) *questionDo {
	return q.withDO(q.DO.Attrs(attrs...))
}

func (q questionDo) Assign(attrs ...field.AssignExpr) *questionDo {
	return q.withDO(q.DO.Assign(attrs...))
}

func (q questionDo) Joins(fields ...field.RelationField) *questionDo {
	for _, _f := range fields {
		q = *q.withDO(q.DO.Joins(_f))
	}
	return &q
}

func (q questionDo) Preload(fields ...field.RelationField) *questionDo {
	for _, _f := range fields {
		q = *q.withDO(q.DO.Preload(_f))
	}
	return &q
}

func (q questionDo) FirstOrInit() (*model.Question, error) {
	if result, err := q.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Question), nil
	}
}

func (q questionDo) FirstOrCreate() (*model.Question, error) {
	if result, err := q.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Question), nil
	}
}

func (q questionDo) FindByPage(offset int, limit int) (result []*model.Question, count int64, err error) {
	result, err = q.Offset(offset).Limit(limit).Find()
	if err != nil {
		return
	}

	if size := len(result); 0 < limit && 0 < size && size < limit {
		count = int64(size + offset)
		return
	}

	count, err = q.Offset(-1).Limit(-1).Count()
	return
}

func (q questionDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
	count, err = q.Count()
	if err != nil {
		return
	}

	err = q.Offset(offset).Limit(limit).Scan(result)
	return
}

func (q questionDo) Scan(result interface{}) (err error) {
	return q.DO.Scan(result)
}

func (q questionDo) Delete(models ...*model.Question) (result gen.ResultInfo, err error) {
	return q.DO.Delete(models)
}

func (q *questionDo) withDO(do gen.Dao) *questionDo {
	q.DO = *do.(*gen.DO)
	return q
}
