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

	_globalletter.globalletterDo.UseDB(db, opts...)
	_globalletter.globalletterDo.UseModel(&model.Globalletter{})

	tableName := _globalletter.globalletterDo.TableName()
	_globalletter.ALL = field.NewAsterisk(tableName)
	_globalletter.ID = field.NewInt64(tableName, "id")
	_globalletter.Type = field.NewInt32(tableName, "type")
	_globalletter.MaxPlayerID = field.NewInt32(tableName, "maxPlayerId")
	_globalletter.Title = field.NewString(tableName, "title")
	_globalletter.Content = field.NewString(tableName, "content")
	_globalletter.Extra = field.NewString(tableName, "extra")
	_globalletter.ExtraInfo = field.NewString(tableName, "extraInfo")
	_globalletter.CreateTime = field.NewTime(tableName, "create_time")
	_globalletter.ExpireTime = field.NewTime(tableName, "expireTime")

	_globalletter.fillFieldMap()

	return _globalletter
}

type globalletter struct {
	globalletterDo

	ALL         field.Asterisk
	ID          field.Int64
	Type        field.Int32
	MaxPlayerID field.Int32
	Title       field.String
	Content     field.String
	Extra       field.String
	ExtraInfo   field.String
	CreateTime  field.Time
	ExpireTime  field.Time

	fieldMap map[string]field.Expr
}

func (g globalletter) Table(newTableName string) *globalletter {
	g.globalletterDo.UseTable(newTableName)
	return g.updateTableName(newTableName)
}

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

func (g *globalletter) updateTableName(table string) *globalletter {
	g.ALL = field.NewAsterisk(table)
	g.ID = field.NewInt64(table, "id")
	g.Type = field.NewInt32(table, "type")
	g.MaxPlayerID = field.NewInt32(table, "maxPlayerId")
	g.Title = field.NewString(table, "title")
	g.Content = field.NewString(table, "content")
	g.Extra = field.NewString(table, "extra")
	g.ExtraInfo = field.NewString(table, "extraInfo")
	g.CreateTime = field.NewTime(table, "create_time")
	g.ExpireTime = field.NewTime(table, "expireTime")

	g.fillFieldMap()

	return g
}

func (g *globalletter) 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 *globalletter) fillFieldMap() {
	g.fieldMap = make(map[string]field.Expr, 9)
	g.fieldMap["id"] = g.ID
	g.fieldMap["type"] = g.Type
	g.fieldMap["maxPlayerId"] = g.MaxPlayerID
	g.fieldMap["title"] = g.Title
	g.fieldMap["content"] = g.Content
	g.fieldMap["extra"] = g.Extra
	g.fieldMap["extraInfo"] = g.ExtraInfo
	g.fieldMap["create_time"] = g.CreateTime
	g.fieldMap["expireTime"] = g.ExpireTime
}

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

func (g globalletter) replaceDB(db *gorm.DB) globalletter {
	g.globalletterDo.ReplaceDB(db)
	return g
}

type globalletterDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

func (g globalletterDo) FindByPage(offset int, limit int) (result []*model.Globalletter, 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 globalletterDo) 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 globalletterDo) Scan(result interface{}) (err error) {
	return g.DO.Scan(result)
}

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

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