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

	_playerTask.playerTaskDo.UseDB(db, opts...)
	_playerTask.playerTaskDo.UseModel(&model.PlayerTask{})

	tableName := _playerTask.playerTaskDo.TableName()
	_playerTask.ALL = field.NewAsterisk(tableName)
	_playerTask.Playerid = field.NewInt64(tableName, "playerid")
	_playerTask.Task = field.NewString(tableName, "task")
	_playerTask.Materials = field.NewString(tableName, "materials")
	_playerTask.Funs = field.NewString(tableName, "funs")

	_playerTask.fillFieldMap()

	return _playerTask
}

type playerTask struct {
	playerTaskDo

	ALL       field.Asterisk
	Playerid  field.Int64
	Task      field.String
	Materials field.String
	Funs      field.String

	fieldMap map[string]field.Expr
}

func (p playerTask) Table(newTableName string) *playerTask {
	p.playerTaskDo.UseTable(newTableName)
	return p.updateTableName(newTableName)
}

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

func (p *playerTask) updateTableName(table string) *playerTask {
	p.ALL = field.NewAsterisk(table)
	p.Playerid = field.NewInt64(table, "playerid")
	p.Task = field.NewString(table, "task")
	p.Materials = field.NewString(table, "materials")
	p.Funs = field.NewString(table, "funs")

	p.fillFieldMap()

	return p
}

func (p *playerTask) 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 *playerTask) fillFieldMap() {
	p.fieldMap = make(map[string]field.Expr, 4)
	p.fieldMap["playerid"] = p.Playerid
	p.fieldMap["task"] = p.Task
	p.fieldMap["materials"] = p.Materials
	p.fieldMap["funs"] = p.Funs
}

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

func (p playerTask) replaceDB(db *gorm.DB) playerTask {
	p.playerTaskDo.ReplaceDB(db)
	return p
}

type playerTaskDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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