// 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"
	"database/sql"

	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"gorm.io/gorm/schema"

	"gorm.io/gen"
	"gorm.io/gen/field"

	"gorm.io/plugin/dbresolver"

	"stream.api/internal/database/model"
)

func newPlayerConfig(db *gorm.DB, opts ...gen.DOOption) playerConfig {
	_playerConfig := playerConfig{}

	_playerConfig.playerConfigDo.UseDB(db, opts...)
	_playerConfig.playerConfigDo.UseModel(&model.PlayerConfig{})

	tableName := _playerConfig.playerConfigDo.TableName()
	_playerConfig.ALL = field.NewAsterisk(tableName)
	_playerConfig.ID = field.NewString(tableName, "id")
	_playerConfig.UserID = field.NewString(tableName, "user_id")
	_playerConfig.Name = field.NewString(tableName, "name")
	_playerConfig.Description = field.NewString(tableName, "description")
	_playerConfig.Autoplay = field.NewBool(tableName, "autoplay")
	_playerConfig.Loop = field.NewBool(tableName, "loop")
	_playerConfig.Muted = field.NewBool(tableName, "muted")
	_playerConfig.ShowControls = field.NewBool(tableName, "show_controls")
	_playerConfig.Pip = field.NewBool(tableName, "pip")
	_playerConfig.Airplay = field.NewBool(tableName, "airplay")
	_playerConfig.Chromecast = field.NewBool(tableName, "chromecast")
	_playerConfig.IsActive = field.NewBool(tableName, "is_active")
	_playerConfig.IsDefault = field.NewBool(tableName, "is_default")
	_playerConfig.CreatedAt = field.NewTime(tableName, "created_at")
	_playerConfig.UpdatedAt = field.NewTime(tableName, "updated_at")
	_playerConfig.Version = field.NewInt64(tableName, "version")
	_playerConfig.EncrytionM3u8 = field.NewBool(tableName, "encrytion_m3u8")
	_playerConfig.LogoURL = field.NewString(tableName, "logo_url")

	_playerConfig.fillFieldMap()

	return _playerConfig
}

type playerConfig struct {
	playerConfigDo playerConfigDo

	ALL           field.Asterisk
	ID            field.String
	UserID        field.String
	Name          field.String
	Description   field.String
	Autoplay      field.Bool
	Loop          field.Bool
	Muted         field.Bool
	ShowControls  field.Bool
	Pip           field.Bool
	Airplay       field.Bool
	Chromecast    field.Bool
	IsActive      field.Bool
	IsDefault     field.Bool
	CreatedAt     field.Time
	UpdatedAt     field.Time
	Version       field.Int64
	EncrytionM3u8 field.Bool
	LogoURL       field.String

	fieldMap map[string]field.Expr
}

func (p playerConfig) Table(newTableName string) *playerConfig {
	p.playerConfigDo.UseTable(newTableName)
	return p.updateTableName(newTableName)
}

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

func (p *playerConfig) updateTableName(table string) *playerConfig {
	p.ALL = field.NewAsterisk(table)
	p.ID = field.NewString(table, "id")
	p.UserID = field.NewString(table, "user_id")
	p.Name = field.NewString(table, "name")
	p.Description = field.NewString(table, "description")
	p.Autoplay = field.NewBool(table, "autoplay")
	p.Loop = field.NewBool(table, "loop")
	p.Muted = field.NewBool(table, "muted")
	p.ShowControls = field.NewBool(table, "show_controls")
	p.Pip = field.NewBool(table, "pip")
	p.Airplay = field.NewBool(table, "airplay")
	p.Chromecast = field.NewBool(table, "chromecast")
	p.IsActive = field.NewBool(table, "is_active")
	p.IsDefault = field.NewBool(table, "is_default")
	p.CreatedAt = field.NewTime(table, "created_at")
	p.UpdatedAt = field.NewTime(table, "updated_at")
	p.Version = field.NewInt64(table, "version")
	p.EncrytionM3u8 = field.NewBool(table, "encrytion_m3u8")
	p.LogoURL = field.NewString(table, "logo_url")

	p.fillFieldMap()

	return p
}

func (p *playerConfig) WithContext(ctx context.Context) IPlayerConfigDo {
	return p.playerConfigDo.WithContext(ctx)
}

func (p playerConfig) TableName() string { return p.playerConfigDo.TableName() }

func (p playerConfig) Alias() string { return p.playerConfigDo.Alias() }

func (p playerConfig) Columns(cols ...field.Expr) gen.Columns {
	return p.playerConfigDo.Columns(cols...)
}

func (p *playerConfig) 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 *playerConfig) fillFieldMap() {
	p.fieldMap = make(map[string]field.Expr, 18)
	p.fieldMap["id"] = p.ID
	p.fieldMap["user_id"] = p.UserID
	p.fieldMap["name"] = p.Name
	p.fieldMap["description"] = p.Description
	p.fieldMap["autoplay"] = p.Autoplay
	p.fieldMap["loop"] = p.Loop
	p.fieldMap["muted"] = p.Muted
	p.fieldMap["show_controls"] = p.ShowControls
	p.fieldMap["pip"] = p.Pip
	p.fieldMap["airplay"] = p.Airplay
	p.fieldMap["chromecast"] = p.Chromecast
	p.fieldMap["is_active"] = p.IsActive
	p.fieldMap["is_default"] = p.IsDefault
	p.fieldMap["created_at"] = p.CreatedAt
	p.fieldMap["updated_at"] = p.UpdatedAt
	p.fieldMap["version"] = p.Version
	p.fieldMap["encrytion_m3u8"] = p.EncrytionM3u8
	p.fieldMap["logo_url"] = p.LogoURL
}

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

func (p playerConfig) replaceDB(db *gorm.DB) playerConfig {
	p.playerConfigDo.ReplaceDB(db)
	return p
}

type playerConfigDo struct{ gen.DO }

type IPlayerConfigDo interface {
	gen.SubQuery
	Debug() IPlayerConfigDo
	WithContext(ctx context.Context) IPlayerConfigDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() IPlayerConfigDo
	WriteDB() IPlayerConfigDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) IPlayerConfigDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) IPlayerConfigDo
	Not(conds ...gen.Condition) IPlayerConfigDo
	Or(conds ...gen.Condition) IPlayerConfigDo
	Select(conds ...field.Expr) IPlayerConfigDo
	Where(conds ...gen.Condition) IPlayerConfigDo
	Order(conds ...field.Expr) IPlayerConfigDo
	Distinct(cols ...field.Expr) IPlayerConfigDo
	Omit(cols ...field.Expr) IPlayerConfigDo
	Join(table schema.Tabler, on ...field.Expr) IPlayerConfigDo
	LeftJoin(table schema.Tabler, on ...field.Expr) IPlayerConfigDo
	RightJoin(table schema.Tabler, on ...field.Expr) IPlayerConfigDo
	Group(cols ...field.Expr) IPlayerConfigDo
	Having(conds ...gen.Condition) IPlayerConfigDo
	Limit(limit int) IPlayerConfigDo
	Offset(offset int) IPlayerConfigDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) IPlayerConfigDo
	Unscoped() IPlayerConfigDo
	Create(values ...*model.PlayerConfig) error
	CreateInBatches(values []*model.PlayerConfig, batchSize int) error
	Save(values ...*model.PlayerConfig) error
	First() (*model.PlayerConfig, error)
	Take() (*model.PlayerConfig, error)
	Last() (*model.PlayerConfig, error)
	Find() ([]*model.PlayerConfig, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.PlayerConfig, err error)
	FindInBatches(result *[]*model.PlayerConfig, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.PlayerConfig) (info gen.ResultInfo, err error)
	Update(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	Updates(value interface{}) (info gen.ResultInfo, err error)
	UpdateColumn(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateColumnSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	UpdateColumns(value interface{}) (info gen.ResultInfo, err error)
	UpdateFrom(q gen.SubQuery) gen.Dao
	Attrs(attrs ...field.AssignExpr) IPlayerConfigDo
	Assign(attrs ...field.AssignExpr) IPlayerConfigDo
	Joins(fields ...field.RelationField) IPlayerConfigDo
	Preload(fields ...field.RelationField) IPlayerConfigDo
	FirstOrInit() (*model.PlayerConfig, error)
	FirstOrCreate() (*model.PlayerConfig, error)
	FindByPage(offset int, limit int) (result []*model.PlayerConfig, count int64, err error)
	ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
	Rows() (*sql.Rows, error)
	Row() *sql.Row
	Scan(result interface{}) (err error)
	Returning(value interface{}, columns ...string) IPlayerConfigDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (p playerConfigDo) Debug() IPlayerConfigDo {
	return p.withDO(p.DO.Debug())
}

func (p playerConfigDo) WithContext(ctx context.Context) IPlayerConfigDo {
	return p.withDO(p.DO.WithContext(ctx))
}

func (p playerConfigDo) ReadDB() IPlayerConfigDo {
	return p.Clauses(dbresolver.Read)
}

func (p playerConfigDo) WriteDB() IPlayerConfigDo {
	return p.Clauses(dbresolver.Write)
}

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

func (p playerConfigDo) Clauses(conds ...clause.Expression) IPlayerConfigDo {
	return p.withDO(p.DO.Clauses(conds...))
}

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

func (p playerConfigDo) Not(conds ...gen.Condition) IPlayerConfigDo {
	return p.withDO(p.DO.Not(conds...))
}

func (p playerConfigDo) Or(conds ...gen.Condition) IPlayerConfigDo {
	return p.withDO(p.DO.Or(conds...))
}

func (p playerConfigDo) Select(conds ...field.Expr) IPlayerConfigDo {
	return p.withDO(p.DO.Select(conds...))
}

func (p playerConfigDo) Where(conds ...gen.Condition) IPlayerConfigDo {
	return p.withDO(p.DO.Where(conds...))
}

func (p playerConfigDo) Order(conds ...field.Expr) IPlayerConfigDo {
	return p.withDO(p.DO.Order(conds...))
}

func (p playerConfigDo) Distinct(cols ...field.Expr) IPlayerConfigDo {
	return p.withDO(p.DO.Distinct(cols...))
}

func (p playerConfigDo) Omit(cols ...field.Expr) IPlayerConfigDo {
	return p.withDO(p.DO.Omit(cols...))
}

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

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

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

func (p playerConfigDo) Group(cols ...field.Expr) IPlayerConfigDo {
	return p.withDO(p.DO.Group(cols...))
}

func (p playerConfigDo) Having(conds ...gen.Condition) IPlayerConfigDo {
	return p.withDO(p.DO.Having(conds...))
}

func (p playerConfigDo) Limit(limit int) IPlayerConfigDo {
	return p.withDO(p.DO.Limit(limit))
}

func (p playerConfigDo) Offset(offset int) IPlayerConfigDo {
	return p.withDO(p.DO.Offset(offset))
}

func (p playerConfigDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IPlayerConfigDo {
	return p.withDO(p.DO.Scopes(funcs...))
}

func (p playerConfigDo) Unscoped() IPlayerConfigDo {
	return p.withDO(p.DO.Unscoped())
}

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

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

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

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

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

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

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

func (p playerConfigDo) Attrs(attrs ...field.AssignExpr) IPlayerConfigDo {
	return p.withDO(p.DO.Attrs(attrs...))
}

func (p playerConfigDo) Assign(attrs ...field.AssignExpr) IPlayerConfigDo {
	return p.withDO(p.DO.Assign(attrs...))
}

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

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

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

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

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

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

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