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

	_playerMailbox.playerMailboxDo.UseDB(db, opts...)
	_playerMailbox.playerMailboxDo.UseModel(&model.PlayerMailbox{})

	tableName := _playerMailbox.playerMailboxDo.TableName()
	_playerMailbox.ALL = field.NewAsterisk(tableName)
	_playerMailbox.ID = field.NewInt64(tableName, "id")
	_playerMailbox.PlayerID = field.NewInt64(tableName, "playerId")
	_playerMailbox.LastGlobalID = field.NewInt32(tableName, "lastGlobalId")

	_playerMailbox.fillFieldMap()

	return _playerMailbox
}

type playerMailbox struct {
	playerMailboxDo

	ALL          field.Asterisk
	ID           field.Int64
	PlayerID     field.Int64
	LastGlobalID field.Int32

	fieldMap map[string]field.Expr
}

func (p playerMailbox) Table(newTableName string) *playerMailbox {
	p.playerMailboxDo.UseTable(newTableName)
	return p.updateTableName(newTableName)
}

func (p playerMailbox) As(alias string) *playerMailbox {
	p.playerMailboxDo.DO = *(p.playerMailboxDo.As(alias).(*gen.DO))
	return p.updateTableName(alias)
}

func (p *playerMailbox) updateTableName(table string) *playerMailbox {
	p.ALL = field.NewAsterisk(table)
	p.ID = field.NewInt64(table, "id")
	p.PlayerID = field.NewInt64(table, "playerId")
	p.LastGlobalID = field.NewInt32(table, "lastGlobalId")

	p.fillFieldMap()

	return p
}

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

func (p *playerMailbox) fillFieldMap() {
	p.fieldMap = make(map[string]field.Expr, 3)
	p.fieldMap["id"] = p.ID
	p.fieldMap["playerId"] = p.PlayerID
	p.fieldMap["lastGlobalId"] = p.LastGlobalID
}

func (p playerMailbox) clone(db *gorm.DB) playerMailbox {
	p.playerMailboxDo.ReplaceConnPool(db.Statement.ConnPool)
	return p
}

func (p playerMailbox) replaceDB(db *gorm.DB) playerMailbox {
	p.playerMailboxDo.ReplaceDB(db)
	return p
}

type playerMailboxDo struct{ gen.DO }

func (p playerMailboxDo) Debug() *playerMailboxDo {
	return p.withDO(p.DO.Debug())
}

func (p playerMailboxDo) WithContext(ctx context.Context) *playerMailboxDo {
	return p.withDO(p.DO.WithContext(ctx))
}

func (p playerMailboxDo) ReadDB() *playerMailboxDo {
	return p.Clauses(dbresolver.Read)
}

func (p playerMailboxDo) WriteDB() *playerMailboxDo {
	return p.Clauses(dbresolver.Write)
}

func (p playerMailboxDo) Session(config *gorm.Session) *playerMailboxDo {
	return p.withDO(p.DO.Session(config))
}

func (p playerMailboxDo) Clauses(conds ...clause.Expression) *playerMailboxDo {
	return p.withDO(p.DO.Clauses(conds...))
}

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

func (p playerMailboxDo) Not(conds ...gen.Condition) *playerMailboxDo {
	return p.withDO(p.DO.Not(conds...))
}

func (p playerMailboxDo) Or(conds ...gen.Condition) *playerMailboxDo {
	return p.withDO(p.DO.Or(conds...))
}

func (p playerMailboxDo) Select(conds ...field.Expr) *playerMailboxDo {
	return p.withDO(p.DO.Select(conds...))
}

func (p playerMailboxDo) Where(conds ...gen.Condition) *playerMailboxDo {
	return p.withDO(p.DO.Where(conds...))
}

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

func (p playerMailboxDo) Order(conds ...field.Expr) *playerMailboxDo {
	return p.withDO(p.DO.Order(conds...))
}

func (p playerMailboxDo) Distinct(cols ...field.Expr) *playerMailboxDo {
	return p.withDO(p.DO.Distinct(cols...))
}

func (p playerMailboxDo) Omit(cols ...field.Expr) *playerMailboxDo {
	return p.withDO(p.DO.Omit(cols...))
}

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

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

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

func (p playerMailboxDo) Group(cols ...field.Expr) *playerMailboxDo {
	return p.withDO(p.DO.Group(cols...))
}

func (p playerMailboxDo) Having(conds ...gen.Condition) *playerMailboxDo {
	return p.withDO(p.DO.Having(conds...))
}

func (p playerMailboxDo) Limit(limit int) *playerMailboxDo {
	return p.withDO(p.DO.Limit(limit))
}

func (p playerMailboxDo) Offset(offset int) *playerMailboxDo {
	return p.withDO(p.DO.Offset(offset))
}

func (p playerMailboxDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *playerMailboxDo {
	return p.withDO(p.DO.Scopes(funcs...))
}

func (p playerMailboxDo) Unscoped() *playerMailboxDo {
	return p.withDO(p.DO.Unscoped())
}

func (p playerMailboxDo) Create(values ...*model.PlayerMailbox) error {
	if len(values) == 0 {
		return nil
	}
	return p.DO.Create(values)
}

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

func (p playerMailboxDo) First() (*model.PlayerMailbox, error) {
	if result, err := p.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.PlayerMailbox), nil
	}
}

func (p playerMailboxDo) Take() (*model.PlayerMailbox, error) {
	if result, err := p.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.PlayerMailbox), nil
	}
}

func (p playerMailboxDo) Last() (*model.PlayerMailbox, error) {
	if result, err := p.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.PlayerMailbox), nil
	}
}

func (p playerMailboxDo) Find() ([]*model.PlayerMailbox, error) {
	result, err := p.DO.Find()
	return result.([]*model.PlayerMailbox), err
}

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

func (p playerMailboxDo) Attrs(attrs ...field.AssignExpr) *playerMailboxDo {
	return p.withDO(p.DO.Attrs(attrs...))
}

func (p playerMailboxDo) Assign(attrs ...field.AssignExpr) *playerMailboxDo {
	return p.withDO(p.DO.Assign(attrs...))
}

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

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

func (p playerMailboxDo) FirstOrInit() (*model.PlayerMailbox, error) {
	if result, err := p.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.PlayerMailbox), nil
	}
}

func (p playerMailboxDo) FirstOrCreate() (*model.PlayerMailbox, error) {
	if result, err := p.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.PlayerMailbox), nil
	}
}

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

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

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

func (p playerMailboxDo) Scan(result interface{}) (err error) {
	return p.DO.Scan(result)
}

func (p playerMailboxDo) Delete(models ...*model.PlayerMailbox) (result gen.ResultInfo, err error) {
	return p.DO.Delete(models)
}

func (p *playerMailboxDo) withDO(do gen.Dao) *playerMailboxDo {
	p.DO = *do.(*gen.DO)
	return p
}
