// 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 dbgen

import (
	"context"

	"git.mstar.dev/mstar/linstrom/storage-new/models"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"gorm.io/gorm/schema"

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

	"gorm.io/plugin/dbresolver"
)

func newUserToTag(db *gorm.DB, opts ...gen.DOOption) userToTag {
	_userToTag := userToTag{}

	_userToTag.userToTagDo.UseDB(db, opts...)
	_userToTag.userToTagDo.UseModel(&models.UserToTag{})

	tableName := _userToTag.userToTagDo.TableName()
	_userToTag.ALL = field.NewAsterisk(tableName)
	_userToTag.ID = field.NewUint64(tableName, "id")
	_userToTag.UserId = field.NewString(tableName, "user_id")
	_userToTag.Tag = field.NewString(tableName, "tag")
	_userToTag.User = userToTagBelongsToUser{
		db: db.Session(&gorm.Session{}),

		RelationField: field.NewRelation("User", "models.User"),
		Server: struct {
			field.RelationField
			Icon struct {
				field.RelationField
			}
		}{
			RelationField: field.NewRelation("User.Server", "models.RemoteServer"),
			Icon: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("User.Server.Icon", "models.MediaMetadata"),
			},
		},
		Icon: struct {
			field.RelationField
		}{
			RelationField: field.NewRelation("User.Icon", "models.MediaMetadata"),
		},
		Background: struct {
			field.RelationField
		}{
			RelationField: field.NewRelation("User.Background", "models.MediaMetadata"),
		},
		Banner: struct {
			field.RelationField
		}{
			RelationField: field.NewRelation("User.Banner", "models.MediaMetadata"),
		},
		RemoteInfo: struct {
			field.RelationField
			User struct {
				field.RelationField
			}
		}{
			RelationField: field.NewRelation("User.RemoteInfo", "models.UserRemoteLinks"),
			User: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("User.RemoteInfo.User", "models.User"),
			},
		},
		InfoFields: struct {
			field.RelationField
			User struct {
				field.RelationField
			}
		}{
			RelationField: field.NewRelation("User.InfoFields", "models.UserInfoField"),
			User: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("User.InfoFields.User", "models.User"),
			},
		},
		BeingTypes: struct {
			field.RelationField
			User struct {
				field.RelationField
			}
		}{
			RelationField: field.NewRelation("User.BeingTypes", "models.UserToBeing"),
			User: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("User.BeingTypes.User", "models.User"),
			},
		},
		Tags: struct {
			field.RelationField
			User struct {
				field.RelationField
			}
		}{
			RelationField: field.NewRelation("User.Tags", "models.UserToTag"),
			User: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("User.Tags.User", "models.User"),
			},
		},
		Relations: struct {
			field.RelationField
			User struct {
				field.RelationField
			}
			TargetUser struct {
				field.RelationField
			}
		}{
			RelationField: field.NewRelation("User.Relations", "models.UserToUserRelation"),
			User: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("User.Relations.User", "models.User"),
			},
			TargetUser: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("User.Relations.TargetUser", "models.User"),
			},
		},
		Pronouns: struct {
			field.RelationField
			User struct {
				field.RelationField
			}
		}{
			RelationField: field.NewRelation("User.Pronouns", "models.UserToPronoun"),
			User: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("User.Pronouns.User", "models.User"),
			},
		},
		Roles: struct {
			field.RelationField
			User struct {
				field.RelationField
			}
			Role struct {
				field.RelationField
			}
		}{
			RelationField: field.NewRelation("User.Roles", "models.UserToRole"),
			User: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("User.Roles.User", "models.User"),
			},
			Role: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("User.Roles.Role", "models.Role"),
			},
		},
		AuthMethods: struct {
			field.RelationField
			User struct {
				field.RelationField
			}
		}{
			RelationField: field.NewRelation("User.AuthMethods", "models.UserAuthMethod"),
			User: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("User.AuthMethods.User", "models.User"),
			},
		},
	}

	_userToTag.fillFieldMap()

	return _userToTag
}

type userToTag struct {
	userToTagDo

	ALL    field.Asterisk
	ID     field.Uint64
	UserId field.String
	Tag    field.String
	User   userToTagBelongsToUser

	fieldMap map[string]field.Expr
}

func (u userToTag) Table(newTableName string) *userToTag {
	u.userToTagDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

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

func (u *userToTag) updateTableName(table string) *userToTag {
	u.ALL = field.NewAsterisk(table)
	u.ID = field.NewUint64(table, "id")
	u.UserId = field.NewString(table, "user_id")
	u.Tag = field.NewString(table, "tag")

	u.fillFieldMap()

	return u
}

func (u *userToTag) 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 *userToTag) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 4)
	u.fieldMap["id"] = u.ID
	u.fieldMap["user_id"] = u.UserId
	u.fieldMap["tag"] = u.Tag

}

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

func (u userToTag) replaceDB(db *gorm.DB) userToTag {
	u.userToTagDo.ReplaceDB(db)
	return u
}

type userToTagBelongsToUser struct {
	db *gorm.DB

	field.RelationField

	Server struct {
		field.RelationField
		Icon struct {
			field.RelationField
		}
	}
	Icon struct {
		field.RelationField
	}
	Background struct {
		field.RelationField
	}
	Banner struct {
		field.RelationField
	}
	RemoteInfo struct {
		field.RelationField
		User struct {
			field.RelationField
		}
	}
	InfoFields struct {
		field.RelationField
		User struct {
			field.RelationField
		}
	}
	BeingTypes struct {
		field.RelationField
		User struct {
			field.RelationField
		}
	}
	Tags struct {
		field.RelationField
		User struct {
			field.RelationField
		}
	}
	Relations struct {
		field.RelationField
		User struct {
			field.RelationField
		}
		TargetUser struct {
			field.RelationField
		}
	}
	Pronouns struct {
		field.RelationField
		User struct {
			field.RelationField
		}
	}
	Roles struct {
		field.RelationField
		User struct {
			field.RelationField
		}
		Role struct {
			field.RelationField
		}
	}
	AuthMethods struct {
		field.RelationField
		User struct {
			field.RelationField
		}
	}
}

func (a userToTagBelongsToUser) Where(conds ...field.Expr) *userToTagBelongsToUser {
	if len(conds) == 0 {
		return &a
	}

	exprs := make([]clause.Expression, 0, len(conds))
	for _, cond := range conds {
		exprs = append(exprs, cond.BeCond().(clause.Expression))
	}
	a.db = a.db.Clauses(clause.Where{Exprs: exprs})
	return &a
}

func (a userToTagBelongsToUser) WithContext(ctx context.Context) *userToTagBelongsToUser {
	a.db = a.db.WithContext(ctx)
	return &a
}

func (a userToTagBelongsToUser) Session(session *gorm.Session) *userToTagBelongsToUser {
	a.db = a.db.Session(session)
	return &a
}

func (a userToTagBelongsToUser) Model(m *models.UserToTag) *userToTagBelongsToUserTx {
	return &userToTagBelongsToUserTx{a.db.Model(m).Association(a.Name())}
}

type userToTagBelongsToUserTx struct{ tx *gorm.Association }

func (a userToTagBelongsToUserTx) Find() (result *models.User, err error) {
	return result, a.tx.Find(&result)
}

func (a userToTagBelongsToUserTx) Append(values ...*models.User) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Append(targetValues...)
}

func (a userToTagBelongsToUserTx) Replace(values ...*models.User) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Replace(targetValues...)
}

func (a userToTagBelongsToUserTx) Delete(values ...*models.User) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Delete(targetValues...)
}

func (a userToTagBelongsToUserTx) Clear() error {
	return a.tx.Clear()
}

func (a userToTagBelongsToUserTx) Count() int64 {
	return a.tx.Count()
}

type userToTagDo struct{ gen.DO }

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

func (u userToTagDo) Debug() IUserToTagDo {
	return u.withDO(u.DO.Debug())
}

func (u userToTagDo) WithContext(ctx context.Context) IUserToTagDo {
	return u.withDO(u.DO.WithContext(ctx))
}

func (u userToTagDo) ReadDB() IUserToTagDo {
	return u.Clauses(dbresolver.Read)
}

func (u userToTagDo) WriteDB() IUserToTagDo {
	return u.Clauses(dbresolver.Write)
}

func (u userToTagDo) Session(config *gorm.Session) IUserToTagDo {
	return u.withDO(u.DO.Session(config))
}

func (u userToTagDo) Clauses(conds ...clause.Expression) IUserToTagDo {
	return u.withDO(u.DO.Clauses(conds...))
}

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

func (u userToTagDo) Not(conds ...gen.Condition) IUserToTagDo {
	return u.withDO(u.DO.Not(conds...))
}

func (u userToTagDo) Or(conds ...gen.Condition) IUserToTagDo {
	return u.withDO(u.DO.Or(conds...))
}

func (u userToTagDo) Select(conds ...field.Expr) IUserToTagDo {
	return u.withDO(u.DO.Select(conds...))
}

func (u userToTagDo) Where(conds ...gen.Condition) IUserToTagDo {
	return u.withDO(u.DO.Where(conds...))
}

func (u userToTagDo) Order(conds ...field.Expr) IUserToTagDo {
	return u.withDO(u.DO.Order(conds...))
}

func (u userToTagDo) Distinct(cols ...field.Expr) IUserToTagDo {
	return u.withDO(u.DO.Distinct(cols...))
}

func (u userToTagDo) Omit(cols ...field.Expr) IUserToTagDo {
	return u.withDO(u.DO.Omit(cols...))
}

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

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

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

func (u userToTagDo) Group(cols ...field.Expr) IUserToTagDo {
	return u.withDO(u.DO.Group(cols...))
}

func (u userToTagDo) Having(conds ...gen.Condition) IUserToTagDo {
	return u.withDO(u.DO.Having(conds...))
}

func (u userToTagDo) Limit(limit int) IUserToTagDo {
	return u.withDO(u.DO.Limit(limit))
}

func (u userToTagDo) Offset(offset int) IUserToTagDo {
	return u.withDO(u.DO.Offset(offset))
}

func (u userToTagDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IUserToTagDo {
	return u.withDO(u.DO.Scopes(funcs...))
}

func (u userToTagDo) Unscoped() IUserToTagDo {
	return u.withDO(u.DO.Unscoped())
}

func (u userToTagDo) Create(values ...*models.UserToTag) error {
	if len(values) == 0 {
		return nil
	}
	return u.DO.Create(values)
}

func (u userToTagDo) CreateInBatches(values []*models.UserToTag, 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 userToTagDo) Save(values ...*models.UserToTag) error {
	if len(values) == 0 {
		return nil
	}
	return u.DO.Save(values)
}

func (u userToTagDo) First() (*models.UserToTag, error) {
	if result, err := u.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*models.UserToTag), nil
	}
}

func (u userToTagDo) Take() (*models.UserToTag, error) {
	if result, err := u.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*models.UserToTag), nil
	}
}

func (u userToTagDo) Last() (*models.UserToTag, error) {
	if result, err := u.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*models.UserToTag), nil
	}
}

func (u userToTagDo) Find() ([]*models.UserToTag, error) {
	result, err := u.DO.Find()
	return result.([]*models.UserToTag), err
}

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

func (u userToTagDo) Attrs(attrs ...field.AssignExpr) IUserToTagDo {
	return u.withDO(u.DO.Attrs(attrs...))
}

func (u userToTagDo) Assign(attrs ...field.AssignExpr) IUserToTagDo {
	return u.withDO(u.DO.Assign(attrs...))
}

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

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

func (u userToTagDo) FirstOrInit() (*models.UserToTag, error) {
	if result, err := u.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*models.UserToTag), nil
	}
}

func (u userToTagDo) FirstOrCreate() (*models.UserToTag, error) {
	if result, err := u.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*models.UserToTag), nil
	}
}

func (u userToTagDo) FindByPage(offset int, limit int) (result []*models.UserToTag, 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 userToTagDo) 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 userToTagDo) Scan(result interface{}) (err error) {
	return u.DO.Scan(result)
}

func (u userToTagDo) Delete(models ...*models.UserToTag) (result gen.ResultInfo, err error) {
	return u.DO.Delete(models)
}

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