// 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 newGrandmaster(db *gorm.DB, opts ...gen.DOOption) grandmaster {
	_grandmaster := grandmaster{}

	_grandmaster.grandmasterDo.UseDB(db, opts...)
	_grandmaster.grandmasterDo.UseModel(&model.Grandmaster{})

	tableName := _grandmaster.grandmasterDo.TableName()
	_grandmaster.ALL = field.NewAsterisk(tableName)
	_grandmaster.ID = field.NewInt64(tableName, "id")
	_grandmaster.Playerid = field.NewInt64(tableName, "playerid")
	_grandmaster.JoinTime = field.NewString(tableName, "joinTime")
	_grandmaster.JoinNum = field.NewString(tableName, "joinNum")
	_grandmaster.MaxHistoryScore = field.NewInt64(tableName, "maxHistoryScore")
	_grandmaster.Season = field.NewString(tableName, "season")
	_grandmaster.PrevSeason = field.NewString(tableName, "prevSeason")
	_grandmaster.Worship = field.NewString(tableName, "worship")
	_grandmaster.IsDisplay = field.NewInt32(tableName, "isDisplay")
	_grandmaster.DayReward = field.NewString(tableName, "dayReward")
	_grandmaster.LogIds = field.NewString(tableName, "logIds")

	_grandmaster.fillFieldMap()

	return _grandmaster
}

type grandmaster struct {
	grandmasterDo

	ALL             field.Asterisk
	ID              field.Int64
	Playerid        field.Int64
	JoinTime        field.String
	JoinNum         field.String
	MaxHistoryScore field.Int64
	Season          field.String
	PrevSeason      field.String
	Worship         field.String
	IsDisplay       field.Int32
	DayReward       field.String
	LogIds          field.String

	fieldMap map[string]field.Expr
}

func (g grandmaster) Table(newTableName string) *grandmaster {
	g.grandmasterDo.UseTable(newTableName)
	return g.updateTableName(newTableName)
}

func (g grandmaster) As(alias string) *grandmaster {
	g.grandmasterDo.DO = *(g.grandmasterDo.As(alias).(*gen.DO))
	return g.updateTableName(alias)
}

func (g *grandmaster) updateTableName(table string) *grandmaster {
	g.ALL = field.NewAsterisk(table)
	g.ID = field.NewInt64(table, "id")
	g.Playerid = field.NewInt64(table, "playerid")
	g.JoinTime = field.NewString(table, "joinTime")
	g.JoinNum = field.NewString(table, "joinNum")
	g.MaxHistoryScore = field.NewInt64(table, "maxHistoryScore")
	g.Season = field.NewString(table, "season")
	g.PrevSeason = field.NewString(table, "prevSeason")
	g.Worship = field.NewString(table, "worship")
	g.IsDisplay = field.NewInt32(table, "isDisplay")
	g.DayReward = field.NewString(table, "dayReward")
	g.LogIds = field.NewString(table, "logIds")

	g.fillFieldMap()

	return g
}

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

func (g *grandmaster) fillFieldMap() {
	g.fieldMap = make(map[string]field.Expr, 11)
	g.fieldMap["id"] = g.ID
	g.fieldMap["playerid"] = g.Playerid
	g.fieldMap["joinTime"] = g.JoinTime
	g.fieldMap["joinNum"] = g.JoinNum
	g.fieldMap["maxHistoryScore"] = g.MaxHistoryScore
	g.fieldMap["season"] = g.Season
	g.fieldMap["prevSeason"] = g.PrevSeason
	g.fieldMap["worship"] = g.Worship
	g.fieldMap["isDisplay"] = g.IsDisplay
	g.fieldMap["dayReward"] = g.DayReward
	g.fieldMap["logIds"] = g.LogIds
}

func (g grandmaster) clone(db *gorm.DB) grandmaster {
	g.grandmasterDo.ReplaceConnPool(db.Statement.ConnPool)
	return g
}

func (g grandmaster) replaceDB(db *gorm.DB) grandmaster {
	g.grandmasterDo.ReplaceDB(db)
	return g
}

type grandmasterDo struct{ gen.DO }

func (g grandmasterDo) Debug() *grandmasterDo {
	return g.withDO(g.DO.Debug())
}

func (g grandmasterDo) WithContext(ctx context.Context) *grandmasterDo {
	return g.withDO(g.DO.WithContext(ctx))
}

func (g grandmasterDo) ReadDB() *grandmasterDo {
	return g.Clauses(dbresolver.Read)
}

func (g grandmasterDo) WriteDB() *grandmasterDo {
	return g.Clauses(dbresolver.Write)
}

func (g grandmasterDo) Session(config *gorm.Session) *grandmasterDo {
	return g.withDO(g.DO.Session(config))
}

func (g grandmasterDo) Clauses(conds ...clause.Expression) *grandmasterDo {
	return g.withDO(g.DO.Clauses(conds...))
}

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

func (g grandmasterDo) Not(conds ...gen.Condition) *grandmasterDo {
	return g.withDO(g.DO.Not(conds...))
}

func (g grandmasterDo) Or(conds ...gen.Condition) *grandmasterDo {
	return g.withDO(g.DO.Or(conds...))
}

func (g grandmasterDo) Select(conds ...field.Expr) *grandmasterDo {
	return g.withDO(g.DO.Select(conds...))
}

func (g grandmasterDo) Where(conds ...gen.Condition) *grandmasterDo {
	return g.withDO(g.DO.Where(conds...))
}

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

func (g grandmasterDo) Order(conds ...field.Expr) *grandmasterDo {
	return g.withDO(g.DO.Order(conds...))
}

func (g grandmasterDo) Distinct(cols ...field.Expr) *grandmasterDo {
	return g.withDO(g.DO.Distinct(cols...))
}

func (g grandmasterDo) Omit(cols ...field.Expr) *grandmasterDo {
	return g.withDO(g.DO.Omit(cols...))
}

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

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

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

func (g grandmasterDo) Group(cols ...field.Expr) *grandmasterDo {
	return g.withDO(g.DO.Group(cols...))
}

func (g grandmasterDo) Having(conds ...gen.Condition) *grandmasterDo {
	return g.withDO(g.DO.Having(conds...))
}

func (g grandmasterDo) Limit(limit int) *grandmasterDo {
	return g.withDO(g.DO.Limit(limit))
}

func (g grandmasterDo) Offset(offset int) *grandmasterDo {
	return g.withDO(g.DO.Offset(offset))
}

func (g grandmasterDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *grandmasterDo {
	return g.withDO(g.DO.Scopes(funcs...))
}

func (g grandmasterDo) Unscoped() *grandmasterDo {
	return g.withDO(g.DO.Unscoped())
}

func (g grandmasterDo) Create(values ...*model.Grandmaster) error {
	if len(values) == 0 {
		return nil
	}
	return g.DO.Create(values)
}

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

func (g grandmasterDo) First() (*model.Grandmaster, error) {
	if result, err := g.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Grandmaster), nil
	}
}

func (g grandmasterDo) Take() (*model.Grandmaster, error) {
	if result, err := g.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Grandmaster), nil
	}
}

func (g grandmasterDo) Last() (*model.Grandmaster, error) {
	if result, err := g.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Grandmaster), nil
	}
}

func (g grandmasterDo) Find() ([]*model.Grandmaster, error) {
	result, err := g.DO.Find()
	return result.([]*model.Grandmaster), err
}

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

func (g grandmasterDo) Attrs(attrs ...field.AssignExpr) *grandmasterDo {
	return g.withDO(g.DO.Attrs(attrs...))
}

func (g grandmasterDo) Assign(attrs ...field.AssignExpr) *grandmasterDo {
	return g.withDO(g.DO.Assign(attrs...))
}

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

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

func (g grandmasterDo) FirstOrInit() (*model.Grandmaster, error) {
	if result, err := g.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Grandmaster), nil
	}
}

func (g grandmasterDo) FirstOrCreate() (*model.Grandmaster, error) {
	if result, err := g.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Grandmaster), nil
	}
}

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

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

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

func (g grandmasterDo) Scan(result interface{}) (err error) {
	return g.DO.Scan(result)
}

func (g grandmasterDo) Delete(models ...*model.Grandmaster) (result gen.ResultInfo, err error) {
	return g.DO.Delete(models)
}

func (g *grandmasterDo) withDO(do gen.Dao) *grandmasterDo {
	g.DO = *do.(*gen.DO)
	return g
}
