// 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"

	"gadmin/internal/gorm/model"
)

func newBuyStamina(db *gorm.DB, opts ...gen.DOOption) buyStamina {
	_buyStamina := buyStamina{}

	_buyStamina.buyStaminaDo.UseDB(db, opts...)
	_buyStamina.buyStaminaDo.UseModel(&model.BuyStamina{})

	tableName := _buyStamina.buyStaminaDo.TableName()
	_buyStamina.ALL = field.NewAsterisk(tableName)
	_buyStamina.ID = field.NewInt64(tableName, "id")
	_buyStamina.Playerid = field.NewInt64(tableName, "playerid")
	_buyStamina.Stamina = field.NewString(tableName, "stamina")
	_buyStamina.AllGoods = field.NewString(tableName, "allGoods")

	_buyStamina.fillFieldMap()

	return _buyStamina
}

type buyStamina struct {
	buyStaminaDo

	ALL      field.Asterisk
	ID       field.Int64
	Playerid field.Int64
	Stamina  field.String
	AllGoods field.String

	fieldMap map[string]field.Expr
}

func (b buyStamina) Table(newTableName string) *buyStamina {
	b.buyStaminaDo.UseTable(newTableName)
	return b.updateTableName(newTableName)
}

func (b buyStamina) As(alias string) *buyStamina {
	b.buyStaminaDo.DO = *(b.buyStaminaDo.As(alias).(*gen.DO))
	return b.updateTableName(alias)
}

func (b *buyStamina) updateTableName(table string) *buyStamina {
	b.ALL = field.NewAsterisk(table)
	b.ID = field.NewInt64(table, "id")
	b.Playerid = field.NewInt64(table, "playerid")
	b.Stamina = field.NewString(table, "stamina")
	b.AllGoods = field.NewString(table, "allGoods")

	b.fillFieldMap()

	return b
}

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

func (b *buyStamina) fillFieldMap() {
	b.fieldMap = make(map[string]field.Expr, 4)
	b.fieldMap["id"] = b.ID
	b.fieldMap["playerid"] = b.Playerid
	b.fieldMap["stamina"] = b.Stamina
	b.fieldMap["allGoods"] = b.AllGoods
}

func (b buyStamina) clone(db *gorm.DB) buyStamina {
	b.buyStaminaDo.ReplaceConnPool(db.Statement.ConnPool)
	return b
}

func (b buyStamina) replaceDB(db *gorm.DB) buyStamina {
	b.buyStaminaDo.ReplaceDB(db)
	return b
}

type buyStaminaDo struct{ gen.DO }

func (b buyStaminaDo) Debug() *buyStaminaDo {
	return b.withDO(b.DO.Debug())
}

func (b buyStaminaDo) WithContext(ctx context.Context) *buyStaminaDo {
	return b.withDO(b.DO.WithContext(ctx))
}

func (b buyStaminaDo) ReadDB() *buyStaminaDo {
	return b.Clauses(dbresolver.Read)
}

func (b buyStaminaDo) WriteDB() *buyStaminaDo {
	return b.Clauses(dbresolver.Write)
}

func (b buyStaminaDo) Session(config *gorm.Session) *buyStaminaDo {
	return b.withDO(b.DO.Session(config))
}

func (b buyStaminaDo) Clauses(conds ...clause.Expression) *buyStaminaDo {
	return b.withDO(b.DO.Clauses(conds...))
}

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

func (b buyStaminaDo) Not(conds ...gen.Condition) *buyStaminaDo {
	return b.withDO(b.DO.Not(conds...))
}

func (b buyStaminaDo) Or(conds ...gen.Condition) *buyStaminaDo {
	return b.withDO(b.DO.Or(conds...))
}

func (b buyStaminaDo) Select(conds ...field.Expr) *buyStaminaDo {
	return b.withDO(b.DO.Select(conds...))
}

func (b buyStaminaDo) Where(conds ...gen.Condition) *buyStaminaDo {
	return b.withDO(b.DO.Where(conds...))
}

func (b buyStaminaDo) Exists(subquery interface{ UnderlyingDB() *gorm.DB }) *buyStaminaDo {
	return b.Where(field.CompareSubQuery(field.ExistsOp, nil, subquery.UnderlyingDB()))
}

func (b buyStaminaDo) Order(conds ...field.Expr) *buyStaminaDo {
	return b.withDO(b.DO.Order(conds...))
}

func (b buyStaminaDo) Distinct(cols ...field.Expr) *buyStaminaDo {
	return b.withDO(b.DO.Distinct(cols...))
}

func (b buyStaminaDo) Omit(cols ...field.Expr) *buyStaminaDo {
	return b.withDO(b.DO.Omit(cols...))
}

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

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

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

func (b buyStaminaDo) Group(cols ...field.Expr) *buyStaminaDo {
	return b.withDO(b.DO.Group(cols...))
}

func (b buyStaminaDo) Having(conds ...gen.Condition) *buyStaminaDo {
	return b.withDO(b.DO.Having(conds...))
}

func (b buyStaminaDo) Limit(limit int) *buyStaminaDo {
	return b.withDO(b.DO.Limit(limit))
}

func (b buyStaminaDo) Offset(offset int) *buyStaminaDo {
	return b.withDO(b.DO.Offset(offset))
}

func (b buyStaminaDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *buyStaminaDo {
	return b.withDO(b.DO.Scopes(funcs...))
}

func (b buyStaminaDo) Unscoped() *buyStaminaDo {
	return b.withDO(b.DO.Unscoped())
}

func (b buyStaminaDo) Create(values ...*model.BuyStamina) error {
	if len(values) == 0 {
		return nil
	}
	return b.DO.Create(values)
}

func (b buyStaminaDo) CreateInBatches(values []*model.BuyStamina, batchSize int) error {
	return b.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 (b buyStaminaDo) Save(values ...*model.BuyStamina) error {
	if len(values) == 0 {
		return nil
	}
	return b.DO.Save(values)
}

func (b buyStaminaDo) First() (*model.BuyStamina, error) {
	if result, err := b.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.BuyStamina), nil
	}
}

func (b buyStaminaDo) Take() (*model.BuyStamina, error) {
	if result, err := b.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.BuyStamina), nil
	}
}

func (b buyStaminaDo) Last() (*model.BuyStamina, error) {
	if result, err := b.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.BuyStamina), nil
	}
}

func (b buyStaminaDo) Find() ([]*model.BuyStamina, error) {
	result, err := b.DO.Find()
	return result.([]*model.BuyStamina), err
}

func (b buyStaminaDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.BuyStamina, err error) {
	buf := make([]*model.BuyStamina, 0, batchSize)
	err = b.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 (b buyStaminaDo) FindInBatches(result *[]*model.BuyStamina, batchSize int, fc func(tx gen.Dao, batch int) error) error {
	return b.DO.FindInBatches(result, batchSize, fc)
}

func (b buyStaminaDo) Attrs(attrs ...field.AssignExpr) *buyStaminaDo {
	return b.withDO(b.DO.Attrs(attrs...))
}

func (b buyStaminaDo) Assign(attrs ...field.AssignExpr) *buyStaminaDo {
	return b.withDO(b.DO.Assign(attrs...))
}

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

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

func (b buyStaminaDo) FirstOrInit() (*model.BuyStamina, error) {
	if result, err := b.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.BuyStamina), nil
	}
}

func (b buyStaminaDo) FirstOrCreate() (*model.BuyStamina, error) {
	if result, err := b.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.BuyStamina), nil
	}
}

func (b buyStaminaDo) FindByPage(offset int, limit int) (result []*model.BuyStamina, count int64, err error) {
	result, err = b.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 = b.Offset(-1).Limit(-1).Count()
	return
}

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

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

func (b buyStaminaDo) Scan(result interface{}) (err error) {
	return b.DO.Scan(result)
}

func (b buyStaminaDo) Delete(models ...*model.BuyStamina) (result gen.ResultInfo, err error) {
	return b.DO.Delete(models)
}

func (b *buyStaminaDo) withDO(do gen.Dao) *buyStaminaDo {
	b.DO = *do.(*gen.DO)
	return b
}
