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

	_playerPatrol.playerPatrolDo.UseDB(db, opts...)
	_playerPatrol.playerPatrolDo.UseModel(&model.PlayerPatrol{})

	tableName := _playerPatrol.playerPatrolDo.TableName()
	_playerPatrol.ALL = field.NewAsterisk(tableName)
	_playerPatrol.Playerid = field.NewInt64(tableName, "playerid")
	_playerPatrol.Level = field.NewInt32(tableName, "level")
	_playerPatrol.Materials = field.NewString(tableName, "materials")
	_playerPatrol.Equipments = field.NewString(tableName, "equipments")
	_playerPatrol.CalcItems = field.NewString(tableName, "calcItems")
	_playerPatrol.StartTick = field.NewInt64(tableName, "startTick")
	_playerPatrol.TimeLen = field.NewInt64(tableName, "timeLen")
	_playerPatrol.MaxTimeLen = field.NewInt64(tableName, "maxTimeLen")
	_playerPatrol.DayCount = field.NewInt32(tableName, "dayCount")

	_playerPatrol.fillFieldMap()

	return _playerPatrol
}

type playerPatrol struct {
	playerPatrolDo

	ALL        field.Asterisk
	Playerid   field.Int64
	Level      field.Int32
	Materials  field.String
	Equipments field.String
	CalcItems  field.String
	StartTick  field.Int64
	TimeLen    field.Int64
	MaxTimeLen field.Int64
	DayCount   field.Int32

	fieldMap map[string]field.Expr
}

func (p playerPatrol) Table(newTableName string) *playerPatrol {
	p.playerPatrolDo.UseTable(newTableName)
	return p.updateTableName(newTableName)
}

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

func (p *playerPatrol) updateTableName(table string) *playerPatrol {
	p.ALL = field.NewAsterisk(table)
	p.Playerid = field.NewInt64(table, "playerid")
	p.Level = field.NewInt32(table, "level")
	p.Materials = field.NewString(table, "materials")
	p.Equipments = field.NewString(table, "equipments")
	p.CalcItems = field.NewString(table, "calcItems")
	p.StartTick = field.NewInt64(table, "startTick")
	p.TimeLen = field.NewInt64(table, "timeLen")
	p.MaxTimeLen = field.NewInt64(table, "maxTimeLen")
	p.DayCount = field.NewInt32(table, "dayCount")

	p.fillFieldMap()

	return p
}

func (p *playerPatrol) 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 *playerPatrol) fillFieldMap() {
	p.fieldMap = make(map[string]field.Expr, 9)
	p.fieldMap["playerid"] = p.Playerid
	p.fieldMap["level"] = p.Level
	p.fieldMap["materials"] = p.Materials
	p.fieldMap["equipments"] = p.Equipments
	p.fieldMap["calcItems"] = p.CalcItems
	p.fieldMap["startTick"] = p.StartTick
	p.fieldMap["timeLen"] = p.TimeLen
	p.fieldMap["maxTimeLen"] = p.MaxTimeLen
	p.fieldMap["dayCount"] = p.DayCount
}

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

func (p playerPatrol) replaceDB(db *gorm.DB) playerPatrol {
	p.playerPatrolDo.ReplaceDB(db)
	return p
}

type playerPatrolDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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