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

	_adTemplate.adTemplateDo.UseDB(db, opts...)
	_adTemplate.adTemplateDo.UseModel(&model.AdTemplate{})

	tableName := _adTemplate.adTemplateDo.TableName()
	_adTemplate.ALL = field.NewAsterisk(tableName)
	_adTemplate.ID = field.NewString(tableName, "id")
	_adTemplate.UserID = field.NewString(tableName, "user_id")
	_adTemplate.Name = field.NewString(tableName, "name")
	_adTemplate.Description = field.NewString(tableName, "description")
	_adTemplate.VastTagURL = field.NewString(tableName, "vast_tag_url")
	_adTemplate.AdFormat = field.NewString(tableName, "ad_format")
	_adTemplate.Duration = field.NewInt64(tableName, "duration")
	_adTemplate.IsActive = field.NewBool(tableName, "is_active")
	_adTemplate.CreatedAt = field.NewTime(tableName, "created_at")
	_adTemplate.UpdatedAt = field.NewTime(tableName, "updated_at")
	_adTemplate.IsDefault = field.NewBool(tableName, "is_default")
	_adTemplate.Version = field.NewInt64(tableName, "version")

	_adTemplate.fillFieldMap()

	return _adTemplate
}

type adTemplate struct {
	adTemplateDo adTemplateDo

	ALL         field.Asterisk
	ID          field.String
	UserID      field.String
	Name        field.String
	Description field.String
	VastTagURL  field.String
	AdFormat    field.String
	Duration    field.Int64
	IsActive    field.Bool
	CreatedAt   field.Time
	UpdatedAt   field.Time
	IsDefault   field.Bool
	Version     field.Int64

	fieldMap map[string]field.Expr
}

func (a adTemplate) Table(newTableName string) *adTemplate {
	a.adTemplateDo.UseTable(newTableName)
	return a.updateTableName(newTableName)
}

func (a adTemplate) As(alias string) *adTemplate {
	a.adTemplateDo.DO = *(a.adTemplateDo.As(alias).(*gen.DO))
	return a.updateTableName(alias)
}

func (a *adTemplate) updateTableName(table string) *adTemplate {
	a.ALL = field.NewAsterisk(table)
	a.ID = field.NewString(table, "id")
	a.UserID = field.NewString(table, "user_id")
	a.Name = field.NewString(table, "name")
	a.Description = field.NewString(table, "description")
	a.VastTagURL = field.NewString(table, "vast_tag_url")
	a.AdFormat = field.NewString(table, "ad_format")
	a.Duration = field.NewInt64(table, "duration")
	a.IsActive = field.NewBool(table, "is_active")
	a.CreatedAt = field.NewTime(table, "created_at")
	a.UpdatedAt = field.NewTime(table, "updated_at")
	a.IsDefault = field.NewBool(table, "is_default")
	a.Version = field.NewInt64(table, "version")

	a.fillFieldMap()

	return a
}

func (a *adTemplate) WithContext(ctx context.Context) IAdTemplateDo {
	return a.adTemplateDo.WithContext(ctx)
}

func (a adTemplate) TableName() string { return a.adTemplateDo.TableName() }

func (a adTemplate) Alias() string { return a.adTemplateDo.Alias() }

func (a adTemplate) Columns(cols ...field.Expr) gen.Columns { return a.adTemplateDo.Columns(cols...) }

func (a *adTemplate) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
	_f, ok := a.fieldMap[fieldName]
	if !ok || _f == nil {
		return nil, false
	}
	_oe, ok := _f.(field.OrderExpr)
	return _oe, ok
}

func (a *adTemplate) fillFieldMap() {
	a.fieldMap = make(map[string]field.Expr, 12)
	a.fieldMap["id"] = a.ID
	a.fieldMap["user_id"] = a.UserID
	a.fieldMap["name"] = a.Name
	a.fieldMap["description"] = a.Description
	a.fieldMap["vast_tag_url"] = a.VastTagURL
	a.fieldMap["ad_format"] = a.AdFormat
	a.fieldMap["duration"] = a.Duration
	a.fieldMap["is_active"] = a.IsActive
	a.fieldMap["created_at"] = a.CreatedAt
	a.fieldMap["updated_at"] = a.UpdatedAt
	a.fieldMap["is_default"] = a.IsDefault
	a.fieldMap["version"] = a.Version
}

func (a adTemplate) clone(db *gorm.DB) adTemplate {
	a.adTemplateDo.ReplaceConnPool(db.Statement.ConnPool)
	return a
}

func (a adTemplate) replaceDB(db *gorm.DB) adTemplate {
	a.adTemplateDo.ReplaceDB(db)
	return a
}

type adTemplateDo struct{ gen.DO }

type IAdTemplateDo interface {
	gen.SubQuery
	Debug() IAdTemplateDo
	WithContext(ctx context.Context) IAdTemplateDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() IAdTemplateDo
	WriteDB() IAdTemplateDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) IAdTemplateDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) IAdTemplateDo
	Not(conds ...gen.Condition) IAdTemplateDo
	Or(conds ...gen.Condition) IAdTemplateDo
	Select(conds ...field.Expr) IAdTemplateDo
	Where(conds ...gen.Condition) IAdTemplateDo
	Order(conds ...field.Expr) IAdTemplateDo
	Distinct(cols ...field.Expr) IAdTemplateDo
	Omit(cols ...field.Expr) IAdTemplateDo
	Join(table schema.Tabler, on ...field.Expr) IAdTemplateDo
	LeftJoin(table schema.Tabler, on ...field.Expr) IAdTemplateDo
	RightJoin(table schema.Tabler, on ...field.Expr) IAdTemplateDo
	Group(cols ...field.Expr) IAdTemplateDo
	Having(conds ...gen.Condition) IAdTemplateDo
	Limit(limit int) IAdTemplateDo
	Offset(offset int) IAdTemplateDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) IAdTemplateDo
	Unscoped() IAdTemplateDo
	Create(values ...*model.AdTemplate) error
	CreateInBatches(values []*model.AdTemplate, batchSize int) error
	Save(values ...*model.AdTemplate) error
	First() (*model.AdTemplate, error)
	Take() (*model.AdTemplate, error)
	Last() (*model.AdTemplate, error)
	Find() ([]*model.AdTemplate, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.AdTemplate, err error)
	FindInBatches(result *[]*model.AdTemplate, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.AdTemplate) (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) IAdTemplateDo
	Assign(attrs ...field.AssignExpr) IAdTemplateDo
	Joins(fields ...field.RelationField) IAdTemplateDo
	Preload(fields ...field.RelationField) IAdTemplateDo
	FirstOrInit() (*model.AdTemplate, error)
	FirstOrCreate() (*model.AdTemplate, error)
	FindByPage(offset int, limit int) (result []*model.AdTemplate, 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) IAdTemplateDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (a adTemplateDo) Debug() IAdTemplateDo {
	return a.withDO(a.DO.Debug())
}

func (a adTemplateDo) WithContext(ctx context.Context) IAdTemplateDo {
	return a.withDO(a.DO.WithContext(ctx))
}

func (a adTemplateDo) ReadDB() IAdTemplateDo {
	return a.Clauses(dbresolver.Read)
}

func (a adTemplateDo) WriteDB() IAdTemplateDo {
	return a.Clauses(dbresolver.Write)
}

func (a adTemplateDo) Session(config *gorm.Session) IAdTemplateDo {
	return a.withDO(a.DO.Session(config))
}

func (a adTemplateDo) Clauses(conds ...clause.Expression) IAdTemplateDo {
	return a.withDO(a.DO.Clauses(conds...))
}

func (a adTemplateDo) Returning(value interface{}, columns ...string) IAdTemplateDo {
	return a.withDO(a.DO.Returning(value, columns...))
}

func (a adTemplateDo) Not(conds ...gen.Condition) IAdTemplateDo {
	return a.withDO(a.DO.Not(conds...))
}

func (a adTemplateDo) Or(conds ...gen.Condition) IAdTemplateDo {
	return a.withDO(a.DO.Or(conds...))
}

func (a adTemplateDo) Select(conds ...field.Expr) IAdTemplateDo {
	return a.withDO(a.DO.Select(conds...))
}

func (a adTemplateDo) Where(conds ...gen.Condition) IAdTemplateDo {
	return a.withDO(a.DO.Where(conds...))
}

func (a adTemplateDo) Order(conds ...field.Expr) IAdTemplateDo {
	return a.withDO(a.DO.Order(conds...))
}

func (a adTemplateDo) Distinct(cols ...field.Expr) IAdTemplateDo {
	return a.withDO(a.DO.Distinct(cols...))
}

func (a adTemplateDo) Omit(cols ...field.Expr) IAdTemplateDo {
	return a.withDO(a.DO.Omit(cols...))
}

func (a adTemplateDo) Join(table schema.Tabler, on ...field.Expr) IAdTemplateDo {
	return a.withDO(a.DO.Join(table, on...))
}

func (a adTemplateDo) LeftJoin(table schema.Tabler, on ...field.Expr) IAdTemplateDo {
	return a.withDO(a.DO.LeftJoin(table, on...))
}

func (a adTemplateDo) RightJoin(table schema.Tabler, on ...field.Expr) IAdTemplateDo {
	return a.withDO(a.DO.RightJoin(table, on...))
}

func (a adTemplateDo) Group(cols ...field.Expr) IAdTemplateDo {
	return a.withDO(a.DO.Group(cols...))
}

func (a adTemplateDo) Having(conds ...gen.Condition) IAdTemplateDo {
	return a.withDO(a.DO.Having(conds...))
}

func (a adTemplateDo) Limit(limit int) IAdTemplateDo {
	return a.withDO(a.DO.Limit(limit))
}

func (a adTemplateDo) Offset(offset int) IAdTemplateDo {
	return a.withDO(a.DO.Offset(offset))
}

func (a adTemplateDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IAdTemplateDo {
	return a.withDO(a.DO.Scopes(funcs...))
}

func (a adTemplateDo) Unscoped() IAdTemplateDo {
	return a.withDO(a.DO.Unscoped())
}

func (a adTemplateDo) Create(values ...*model.AdTemplate) error {
	if len(values) == 0 {
		return nil
	}
	return a.DO.Create(values)
}

func (a adTemplateDo) CreateInBatches(values []*model.AdTemplate, batchSize int) error {
	return a.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 (a adTemplateDo) Save(values ...*model.AdTemplate) error {
	if len(values) == 0 {
		return nil
	}
	return a.DO.Save(values)
}

func (a adTemplateDo) First() (*model.AdTemplate, error) {
	if result, err := a.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.AdTemplate), nil
	}
}

func (a adTemplateDo) Take() (*model.AdTemplate, error) {
	if result, err := a.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.AdTemplate), nil
	}
}

func (a adTemplateDo) Last() (*model.AdTemplate, error) {
	if result, err := a.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.AdTemplate), nil
	}
}

func (a adTemplateDo) Find() ([]*model.AdTemplate, error) {
	result, err := a.DO.Find()
	return result.([]*model.AdTemplate), err
}

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

func (a adTemplateDo) Attrs(attrs ...field.AssignExpr) IAdTemplateDo {
	return a.withDO(a.DO.Attrs(attrs...))
}

func (a adTemplateDo) Assign(attrs ...field.AssignExpr) IAdTemplateDo {
	return a.withDO(a.DO.Assign(attrs...))
}

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

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

func (a adTemplateDo) FirstOrInit() (*model.AdTemplate, error) {
	if result, err := a.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.AdTemplate), nil
	}
}

func (a adTemplateDo) FirstOrCreate() (*model.AdTemplate, error) {
	if result, err := a.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.AdTemplate), nil
	}
}

func (a adTemplateDo) FindByPage(offset int, limit int) (result []*model.AdTemplate, count int64, err error) {
	result, err = a.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 = a.Offset(-1).Limit(-1).Count()
	return
}

func (a adTemplateDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
	count, err = a.Count()
	if err != nil {
		return
	}

	err = a.Offset(offset).Limit(limit).Scan(result)
	return
}

func (a adTemplateDo) Scan(result interface{}) (err error) {
	return a.DO.Scan(result)
}

func (a adTemplateDo) Delete(models ...*model.AdTemplate) (result gen.ResultInfo, err error) {
	return a.DO.Delete(models)
}

func (a *adTemplateDo) withDO(do gen.Dao) *adTemplateDo {
	a.DO = *do.(*gen.DO)
	return a
}
