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

	_playerCache.playerCacheDo.UseDB(db, opts...)
	_playerCache.playerCacheDo.UseModel(&model.PlayerCache{})

	tableName := _playerCache.playerCacheDo.TableName()
	_playerCache.ALL = field.NewAsterisk(tableName)
	_playerCache.ID = field.NewInt64(tableName, "id")
	_playerCache.Playerid = field.NewInt64(tableName, "playerid")
	_playerCache.AccID = field.NewInt64(tableName, "acc_id")
	_playerCache.ServerID = field.NewInt32(tableName, "server_id")
	_playerCache.Level = field.NewInt32(tableName, "level")
	_playerCache.PassRoom = field.NewString(tableName, "pass_room")
	_playerCache.Treasures = field.NewString(tableName, "treasures")
	_playerCache.LastSyncTime = field.NewInt64(tableName, "last_sync_time")

	_playerCache.fillFieldMap()

	return _playerCache
}

type playerCache struct {
	playerCacheDo

	ALL          field.Asterisk
	ID           field.Int64
	Playerid     field.Int64  // 用户ID
	AccID        field.Int64  // accId
	ServerID     field.Int32  // 服务器ID
	Level        field.Int32  // 玩家等级
	PassRoom     field.String // 通关数据
	Treasures    field.String // 宝物
	LastSyncTime field.Int64  // 最后同步时间

	fieldMap map[string]field.Expr
}

func (p playerCache) Table(newTableName string) *playerCache {
	p.playerCacheDo.UseTable(newTableName)
	return p.updateTableName(newTableName)
}

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

func (p *playerCache) updateTableName(table string) *playerCache {
	p.ALL = field.NewAsterisk(table)
	p.ID = field.NewInt64(table, "id")
	p.Playerid = field.NewInt64(table, "playerid")
	p.AccID = field.NewInt64(table, "acc_id")
	p.ServerID = field.NewInt32(table, "server_id")
	p.Level = field.NewInt32(table, "level")
	p.PassRoom = field.NewString(table, "pass_room")
	p.Treasures = field.NewString(table, "treasures")
	p.LastSyncTime = field.NewInt64(table, "last_sync_time")

	p.fillFieldMap()

	return p
}

func (p *playerCache) 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 *playerCache) fillFieldMap() {
	p.fieldMap = make(map[string]field.Expr, 8)
	p.fieldMap["id"] = p.ID
	p.fieldMap["playerid"] = p.Playerid
	p.fieldMap["acc_id"] = p.AccID
	p.fieldMap["server_id"] = p.ServerID
	p.fieldMap["level"] = p.Level
	p.fieldMap["pass_room"] = p.PassRoom
	p.fieldMap["treasures"] = p.Treasures
	p.fieldMap["last_sync_time"] = p.LastSyncTime
}

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

func (p playerCache) replaceDB(db *gorm.DB) playerCache {
	p.playerCacheDo.ReplaceDB(db)
	return p
}

type playerCacheDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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