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

	_userPreference.userPreferenceDo.UseDB(db, opts...)
	_userPreference.userPreferenceDo.UseModel(&model.UserPreference{})

	tableName := _userPreference.userPreferenceDo.TableName()
	_userPreference.ALL = field.NewAsterisk(tableName)
	_userPreference.UserID = field.NewString(tableName, "user_id")
	_userPreference.Language = field.NewString(tableName, "language")
	_userPreference.Locale = field.NewString(tableName, "locale")
	_userPreference.EmailNotifications = field.NewBool(tableName, "email_notifications")
	_userPreference.PushNotifications = field.NewBool(tableName, "push_notifications")
	_userPreference.MarketingNotifications = field.NewBool(tableName, "marketing_notifications")
	_userPreference.TelegramNotifications = field.NewBool(tableName, "telegram_notifications")
	_userPreference.CreatedAt = field.NewTime(tableName, "created_at")
	_userPreference.UpdatedAt = field.NewTime(tableName, "updated_at")
	_userPreference.Version = field.NewInt64(tableName, "version")

	_userPreference.fillFieldMap()

	return _userPreference
}

type userPreference struct {
	userPreferenceDo userPreferenceDo

	ALL                    field.Asterisk
	UserID                 field.String
	Language               field.String
	Locale                 field.String
	EmailNotifications     field.Bool
	PushNotifications      field.Bool
	MarketingNotifications field.Bool
	TelegramNotifications  field.Bool
	CreatedAt              field.Time
	UpdatedAt              field.Time
	Version                field.Int64

	fieldMap map[string]field.Expr
}

func (u userPreference) Table(newTableName string) *userPreference {
	u.userPreferenceDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

func (u userPreference) As(alias string) *userPreference {
	u.userPreferenceDo.DO = *(u.userPreferenceDo.As(alias).(*gen.DO))
	return u.updateTableName(alias)
}

func (u *userPreference) updateTableName(table string) *userPreference {
	u.ALL = field.NewAsterisk(table)
	u.UserID = field.NewString(table, "user_id")
	u.Language = field.NewString(table, "language")
	u.Locale = field.NewString(table, "locale")
	u.EmailNotifications = field.NewBool(table, "email_notifications")
	u.PushNotifications = field.NewBool(table, "push_notifications")
	u.MarketingNotifications = field.NewBool(table, "marketing_notifications")
	u.TelegramNotifications = field.NewBool(table, "telegram_notifications")
	u.CreatedAt = field.NewTime(table, "created_at")
	u.UpdatedAt = field.NewTime(table, "updated_at")
	u.Version = field.NewInt64(table, "version")

	u.fillFieldMap()

	return u
}

func (u *userPreference) WithContext(ctx context.Context) IUserPreferenceDo {
	return u.userPreferenceDo.WithContext(ctx)
}

func (u userPreference) TableName() string { return u.userPreferenceDo.TableName() }

func (u userPreference) Alias() string { return u.userPreferenceDo.Alias() }

func (u userPreference) Columns(cols ...field.Expr) gen.Columns {
	return u.userPreferenceDo.Columns(cols...)
}

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

func (u *userPreference) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 10)
	u.fieldMap["user_id"] = u.UserID
	u.fieldMap["language"] = u.Language
	u.fieldMap["locale"] = u.Locale
	u.fieldMap["email_notifications"] = u.EmailNotifications
	u.fieldMap["push_notifications"] = u.PushNotifications
	u.fieldMap["marketing_notifications"] = u.MarketingNotifications
	u.fieldMap["telegram_notifications"] = u.TelegramNotifications
	u.fieldMap["created_at"] = u.CreatedAt
	u.fieldMap["updated_at"] = u.UpdatedAt
	u.fieldMap["version"] = u.Version
}

func (u userPreference) clone(db *gorm.DB) userPreference {
	u.userPreferenceDo.ReplaceConnPool(db.Statement.ConnPool)
	return u
}

func (u userPreference) replaceDB(db *gorm.DB) userPreference {
	u.userPreferenceDo.ReplaceDB(db)
	return u
}

type userPreferenceDo struct{ gen.DO }

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

func (u userPreferenceDo) Debug() IUserPreferenceDo {
	return u.withDO(u.DO.Debug())
}

func (u userPreferenceDo) WithContext(ctx context.Context) IUserPreferenceDo {
	return u.withDO(u.DO.WithContext(ctx))
}

func (u userPreferenceDo) ReadDB() IUserPreferenceDo {
	return u.Clauses(dbresolver.Read)
}

func (u userPreferenceDo) WriteDB() IUserPreferenceDo {
	return u.Clauses(dbresolver.Write)
}

func (u userPreferenceDo) Session(config *gorm.Session) IUserPreferenceDo {
	return u.withDO(u.DO.Session(config))
}

func (u userPreferenceDo) Clauses(conds ...clause.Expression) IUserPreferenceDo {
	return u.withDO(u.DO.Clauses(conds...))
}

func (u userPreferenceDo) Returning(value interface{}, columns ...string) IUserPreferenceDo {
	return u.withDO(u.DO.Returning(value, columns...))
}

func (u userPreferenceDo) Not(conds ...gen.Condition) IUserPreferenceDo {
	return u.withDO(u.DO.Not(conds...))
}

func (u userPreferenceDo) Or(conds ...gen.Condition) IUserPreferenceDo {
	return u.withDO(u.DO.Or(conds...))
}

func (u userPreferenceDo) Select(conds ...field.Expr) IUserPreferenceDo {
	return u.withDO(u.DO.Select(conds...))
}

func (u userPreferenceDo) Where(conds ...gen.Condition) IUserPreferenceDo {
	return u.withDO(u.DO.Where(conds...))
}

func (u userPreferenceDo) Order(conds ...field.Expr) IUserPreferenceDo {
	return u.withDO(u.DO.Order(conds...))
}

func (u userPreferenceDo) Distinct(cols ...field.Expr) IUserPreferenceDo {
	return u.withDO(u.DO.Distinct(cols...))
}

func (u userPreferenceDo) Omit(cols ...field.Expr) IUserPreferenceDo {
	return u.withDO(u.DO.Omit(cols...))
}

func (u userPreferenceDo) Join(table schema.Tabler, on ...field.Expr) IUserPreferenceDo {
	return u.withDO(u.DO.Join(table, on...))
}

func (u userPreferenceDo) LeftJoin(table schema.Tabler, on ...field.Expr) IUserPreferenceDo {
	return u.withDO(u.DO.LeftJoin(table, on...))
}

func (u userPreferenceDo) RightJoin(table schema.Tabler, on ...field.Expr) IUserPreferenceDo {
	return u.withDO(u.DO.RightJoin(table, on...))
}

func (u userPreferenceDo) Group(cols ...field.Expr) IUserPreferenceDo {
	return u.withDO(u.DO.Group(cols...))
}

func (u userPreferenceDo) Having(conds ...gen.Condition) IUserPreferenceDo {
	return u.withDO(u.DO.Having(conds...))
}

func (u userPreferenceDo) Limit(limit int) IUserPreferenceDo {
	return u.withDO(u.DO.Limit(limit))
}

func (u userPreferenceDo) Offset(offset int) IUserPreferenceDo {
	return u.withDO(u.DO.Offset(offset))
}

func (u userPreferenceDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IUserPreferenceDo {
	return u.withDO(u.DO.Scopes(funcs...))
}

func (u userPreferenceDo) Unscoped() IUserPreferenceDo {
	return u.withDO(u.DO.Unscoped())
}

func (u userPreferenceDo) Create(values ...*model.UserPreference) error {
	if len(values) == 0 {
		return nil
	}
	return u.DO.Create(values)
}

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

func (u userPreferenceDo) First() (*model.UserPreference, error) {
	if result, err := u.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.UserPreference), nil
	}
}

func (u userPreferenceDo) Take() (*model.UserPreference, error) {
	if result, err := u.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.UserPreference), nil
	}
}

func (u userPreferenceDo) Last() (*model.UserPreference, error) {
	if result, err := u.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.UserPreference), nil
	}
}

func (u userPreferenceDo) Find() ([]*model.UserPreference, error) {
	result, err := u.DO.Find()
	return result.([]*model.UserPreference), err
}

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

func (u userPreferenceDo) Attrs(attrs ...field.AssignExpr) IUserPreferenceDo {
	return u.withDO(u.DO.Attrs(attrs...))
}

func (u userPreferenceDo) Assign(attrs ...field.AssignExpr) IUserPreferenceDo {
	return u.withDO(u.DO.Assign(attrs...))
}

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

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

func (u userPreferenceDo) FirstOrInit() (*model.UserPreference, error) {
	if result, err := u.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.UserPreference), nil
	}
}

func (u userPreferenceDo) FirstOrCreate() (*model.UserPreference, error) {
	if result, err := u.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.UserPreference), nil
	}
}

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

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

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

func (u userPreferenceDo) Scan(result interface{}) (err error) {
	return u.DO.Scan(result)
}

func (u userPreferenceDo) Delete(models ...*model.UserPreference) (result gen.ResultInfo, err error) {
	return u.DO.Delete(models)
}

func (u *userPreferenceDo) withDO(do gen.Dao) *userPreferenceDo {
	u.DO = *do.(*gen.DO)
	return u
}
