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

	_userToRole.userToRoleDo.UseDB(db, opts...)
	_userToRole.userToRoleDo.UseModel(&models.UserToRole{})

	tableName := _userToRole.userToRoleDo.TableName()
	_userToRole.ALL = field.NewAsterisk(tableName)
	_userToRole.ID = field.NewUint64(tableName, "id")
	_userToRole.UserId = field.NewString(tableName, "user_id")
	_userToRole.RoleId = field.NewUint(tableName, "role_id")
	_userToRole.User = userToRoleBelongsToUser{
		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"),
			},
		},
	}

	_userToRole.Role = userToRoleBelongsToRole{
		db: db.Session(&gorm.Session{}),

		RelationField: field.NewRelation("Role", "models.Role"),
	}

	_userToRole.fillFieldMap()

	return _userToRole
}

type userToRole struct {
	userToRoleDo

	ALL    field.Asterisk
	ID     field.Uint64
	UserId field.String
	RoleId field.Uint
	User   userToRoleBelongsToUser

	Role userToRoleBelongsToRole

	fieldMap map[string]field.Expr
}

func (u userToRole) Table(newTableName string) *userToRole {
	u.userToRoleDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

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

func (u *userToRole) updateTableName(table string) *userToRole {
	u.ALL = field.NewAsterisk(table)
	u.ID = field.NewUint64(table, "id")
	u.UserId = field.NewString(table, "user_id")
	u.RoleId = field.NewUint(table, "role_id")

	u.fillFieldMap()

	return u
}

func (u *userToRole) 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 *userToRole) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 5)
	u.fieldMap["id"] = u.ID
	u.fieldMap["user_id"] = u.UserId
	u.fieldMap["role_id"] = u.RoleId

}

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

func (u userToRole) replaceDB(db *gorm.DB) userToRole {
	u.userToRoleDo.ReplaceDB(db)
	return u
}

type userToRoleBelongsToUser 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 userToRoleBelongsToUser) Where(conds ...field.Expr) *userToRoleBelongsToUser {
	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 userToRoleBelongsToUser) WithContext(ctx context.Context) *userToRoleBelongsToUser {
	a.db = a.db.WithContext(ctx)
	return &a
}

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

func (a userToRoleBelongsToUser) Model(m *models.UserToRole) *userToRoleBelongsToUserTx {
	return &userToRoleBelongsToUserTx{a.db.Model(m).Association(a.Name())}
}

type userToRoleBelongsToUserTx struct{ tx *gorm.Association }

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

func (a userToRoleBelongsToUserTx) 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 userToRoleBelongsToUserTx) 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 userToRoleBelongsToUserTx) 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 userToRoleBelongsToUserTx) Clear() error {
	return a.tx.Clear()
}

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

type userToRoleBelongsToRole struct {
	db *gorm.DB

	field.RelationField
}

func (a userToRoleBelongsToRole) Where(conds ...field.Expr) *userToRoleBelongsToRole {
	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 userToRoleBelongsToRole) WithContext(ctx context.Context) *userToRoleBelongsToRole {
	a.db = a.db.WithContext(ctx)
	return &a
}

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

func (a userToRoleBelongsToRole) Model(m *models.UserToRole) *userToRoleBelongsToRoleTx {
	return &userToRoleBelongsToRoleTx{a.db.Model(m).Association(a.Name())}
}

type userToRoleBelongsToRoleTx struct{ tx *gorm.Association }

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

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

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

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

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

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

type userToRoleDo struct{ gen.DO }

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

func (u userToRoleDo) Debug() IUserToRoleDo {
	return u.withDO(u.DO.Debug())
}

func (u userToRoleDo) WithContext(ctx context.Context) IUserToRoleDo {
	return u.withDO(u.DO.WithContext(ctx))
}

func (u userToRoleDo) ReadDB() IUserToRoleDo {
	return u.Clauses(dbresolver.Read)
}

func (u userToRoleDo) WriteDB() IUserToRoleDo {
	return u.Clauses(dbresolver.Write)
}

func (u userToRoleDo) Session(config *gorm.Session) IUserToRoleDo {
	return u.withDO(u.DO.Session(config))
}

func (u userToRoleDo) Clauses(conds ...clause.Expression) IUserToRoleDo {
	return u.withDO(u.DO.Clauses(conds...))
}

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

func (u userToRoleDo) Not(conds ...gen.Condition) IUserToRoleDo {
	return u.withDO(u.DO.Not(conds...))
}

func (u userToRoleDo) Or(conds ...gen.Condition) IUserToRoleDo {
	return u.withDO(u.DO.Or(conds...))
}

func (u userToRoleDo) Select(conds ...field.Expr) IUserToRoleDo {
	return u.withDO(u.DO.Select(conds...))
}

func (u userToRoleDo) Where(conds ...gen.Condition) IUserToRoleDo {
	return u.withDO(u.DO.Where(conds...))
}

func (u userToRoleDo) Order(conds ...field.Expr) IUserToRoleDo {
	return u.withDO(u.DO.Order(conds...))
}

func (u userToRoleDo) Distinct(cols ...field.Expr) IUserToRoleDo {
	return u.withDO(u.DO.Distinct(cols...))
}

func (u userToRoleDo) Omit(cols ...field.Expr) IUserToRoleDo {
	return u.withDO(u.DO.Omit(cols...))
}

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

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

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

func (u userToRoleDo) Group(cols ...field.Expr) IUserToRoleDo {
	return u.withDO(u.DO.Group(cols...))
}

func (u userToRoleDo) Having(conds ...gen.Condition) IUserToRoleDo {
	return u.withDO(u.DO.Having(conds...))
}

func (u userToRoleDo) Limit(limit int) IUserToRoleDo {
	return u.withDO(u.DO.Limit(limit))
}

func (u userToRoleDo) Offset(offset int) IUserToRoleDo {
	return u.withDO(u.DO.Offset(offset))
}

func (u userToRoleDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IUserToRoleDo {
	return u.withDO(u.DO.Scopes(funcs...))
}

func (u userToRoleDo) Unscoped() IUserToRoleDo {
	return u.withDO(u.DO.Unscoped())
}

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

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

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

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

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

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

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

func (u userToRoleDo) Attrs(attrs ...field.AssignExpr) IUserToRoleDo {
	return u.withDO(u.DO.Attrs(attrs...))
}

func (u userToRoleDo) Assign(attrs ...field.AssignExpr) IUserToRoleDo {
	return u.withDO(u.DO.Assign(attrs...))
}

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

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

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

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

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

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

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