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

	_playerChannel.playerChannelDo.UseDB(db, opts...)
	_playerChannel.playerChannelDo.UseModel(&model.PlayerChannel{})

	tableName := _playerChannel.playerChannelDo.TableName()
	_playerChannel.ALL = field.NewAsterisk(tableName)
	_playerChannel.ID = field.NewInt64(tableName, "id")
	_playerChannel.Playerid = field.NewInt64(tableName, "playerid")
	_playerChannel.ChannelID = field.NewString(tableName, "channel_id")

	_playerChannel.fillFieldMap()

	return _playerChannel
}

type playerChannel struct {
	playerChannelDo

	ALL       field.Asterisk
	ID        field.Int64
	Playerid  field.Int64  // 用户ID
	ChannelID field.String // 渠道ID

	fieldMap map[string]field.Expr
}

func (p playerChannel) Table(newTableName string) *playerChannel {
	p.playerChannelDo.UseTable(newTableName)
	return p.updateTableName(newTableName)
}

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

func (p *playerChannel) updateTableName(table string) *playerChannel {
	p.ALL = field.NewAsterisk(table)
	p.ID = field.NewInt64(table, "id")
	p.Playerid = field.NewInt64(table, "playerid")
	p.ChannelID = field.NewString(table, "channel_id")

	p.fillFieldMap()

	return p
}

func (p *playerChannel) 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 *playerChannel) fillFieldMap() {
	p.fieldMap = make(map[string]field.Expr, 3)
	p.fieldMap["id"] = p.ID
	p.fieldMap["playerid"] = p.Playerid
	p.fieldMap["channel_id"] = p.ChannelID
}

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

func (p playerChannel) replaceDB(db *gorm.DB) playerChannel {
	p.playerChannelDo.ReplaceDB(db)
	return p
}

type playerChannelDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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