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

	_playerLetter.playerLetterDo.UseDB(db, opts...)
	_playerLetter.playerLetterDo.UseModel(&model.PlayerLetter{})

	tableName := _playerLetter.playerLetterDo.TableName()
	_playerLetter.ALL = field.NewAsterisk(tableName)
	_playerLetter.ID = field.NewInt64(tableName, "id")
	_playerLetter.PlayerID = field.NewInt64(tableName, "playerId")
	_playerLetter.Type = field.NewInt32(tableName, "type")
	_playerLetter.ViewState = field.NewInt32(tableName, "viewState")
	_playerLetter.DrawState = field.NewInt32(tableName, "drawState")
	_playerLetter.Title = field.NewString(tableName, "title")
	_playerLetter.Content = field.NewString(tableName, "content")
	_playerLetter.Extra = field.NewString(tableName, "extra")
	_playerLetter.ExtraInfo = field.NewString(tableName, "extraInfo")
	_playerLetter.DisplayTime = field.NewTime(tableName, "displayTime")
	_playerLetter.CreateTime = field.NewTime(tableName, "create_time")
	_playerLetter.ExpireTime = field.NewTime(tableName, "expireTime")

	_playerLetter.fillFieldMap()

	return _playerLetter
}

type playerLetter struct {
	playerLetterDo

	ALL         field.Asterisk
	ID          field.Int64
	PlayerID    field.Int64
	Type        field.Int32
	ViewState   field.Int32
	DrawState   field.Int32
	Title       field.String
	Content     field.String
	Extra       field.String
	ExtraInfo   field.String
	DisplayTime field.Time
	CreateTime  field.Time
	ExpireTime  field.Time

	fieldMap map[string]field.Expr
}

func (p playerLetter) Table(newTableName string) *playerLetter {
	p.playerLetterDo.UseTable(newTableName)
	return p.updateTableName(newTableName)
}

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

func (p *playerLetter) updateTableName(table string) *playerLetter {
	p.ALL = field.NewAsterisk(table)
	p.ID = field.NewInt64(table, "id")
	p.PlayerID = field.NewInt64(table, "playerId")
	p.Type = field.NewInt32(table, "type")
	p.ViewState = field.NewInt32(table, "viewState")
	p.DrawState = field.NewInt32(table, "drawState")
	p.Title = field.NewString(table, "title")
	p.Content = field.NewString(table, "content")
	p.Extra = field.NewString(table, "extra")
	p.ExtraInfo = field.NewString(table, "extraInfo")
	p.DisplayTime = field.NewTime(table, "displayTime")
	p.CreateTime = field.NewTime(table, "create_time")
	p.ExpireTime = field.NewTime(table, "expireTime")

	p.fillFieldMap()

	return p
}

func (p *playerLetter) 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 *playerLetter) fillFieldMap() {
	p.fieldMap = make(map[string]field.Expr, 12)
	p.fieldMap["id"] = p.ID
	p.fieldMap["playerId"] = p.PlayerID
	p.fieldMap["type"] = p.Type
	p.fieldMap["viewState"] = p.ViewState
	p.fieldMap["drawState"] = p.DrawState
	p.fieldMap["title"] = p.Title
	p.fieldMap["content"] = p.Content
	p.fieldMap["extra"] = p.Extra
	p.fieldMap["extraInfo"] = p.ExtraInfo
	p.fieldMap["displayTime"] = p.DisplayTime
	p.fieldMap["create_time"] = p.CreateTime
	p.fieldMap["expireTime"] = p.ExpireTime
}

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

func (p playerLetter) replaceDB(db *gorm.DB) playerLetter {
	p.playerLetterDo.ReplaceDB(db)
	return p
}

type playerLetterDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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