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

	_userToBeing.userToBeingDo.UseDB(db, opts...)
	_userToBeing.userToBeingDo.UseModel(&models.UserToBeing{})

	tableName := _userToBeing.userToBeingDo.TableName()
	_userToBeing.ALL = field.NewAsterisk(tableName)
	_userToBeing.ID = field.NewUint64(tableName, "id")
	_userToBeing.UserId = field.NewString(tableName, "user_id")
	_userToBeing.Being = field.NewField(tableName, "being")
	_userToBeing.User = userToBeingBelongsToUser{
		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"),
			},
		},
	}

	_userToBeing.fillFieldMap()

	return _userToBeing
}

type userToBeing struct {
	userToBeingDo

	ALL    field.Asterisk
	ID     field.Uint64
	UserId field.String
	Being  field.Field
	User   userToBeingBelongsToUser

	fieldMap map[string]field.Expr
}

func (u userToBeing) Table(newTableName string) *userToBeing {
	u.userToBeingDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

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

func (u *userToBeing) updateTableName(table string) *userToBeing {
	u.ALL = field.NewAsterisk(table)
	u.ID = field.NewUint64(table, "id")
	u.UserId = field.NewString(table, "user_id")
	u.Being = field.NewField(table, "being")

	u.fillFieldMap()

	return u
}

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

}

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

func (u userToBeing) replaceDB(db *gorm.DB) userToBeing {
	u.userToBeingDo.ReplaceDB(db)
	return u
}

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

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

func (a userToBeingBelongsToUser) Model(m *models.UserToBeing) *userToBeingBelongsToUserTx {
	return &userToBeingBelongsToUserTx{a.db.Model(m).Association(a.Name())}
}

type userToBeingBelongsToUserTx struct{ tx *gorm.Association }

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

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

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

type userToBeingDo struct{ gen.DO }

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

func (u userToBeingDo) Debug() IUserToBeingDo {
	return u.withDO(u.DO.Debug())
}

func (u userToBeingDo) WithContext(ctx context.Context) IUserToBeingDo {
	return u.withDO(u.DO.WithContext(ctx))
}

func (u userToBeingDo) ReadDB() IUserToBeingDo {
	return u.Clauses(dbresolver.Read)
}

func (u userToBeingDo) WriteDB() IUserToBeingDo {
	return u.Clauses(dbresolver.Write)
}

func (u userToBeingDo) Session(config *gorm.Session) IUserToBeingDo {
	return u.withDO(u.DO.Session(config))
}

func (u userToBeingDo) Clauses(conds ...clause.Expression) IUserToBeingDo {
	return u.withDO(u.DO.Clauses(conds...))
}

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

func (u userToBeingDo) Not(conds ...gen.Condition) IUserToBeingDo {
	return u.withDO(u.DO.Not(conds...))
}

func (u userToBeingDo) Or(conds ...gen.Condition) IUserToBeingDo {
	return u.withDO(u.DO.Or(conds...))
}

func (u userToBeingDo) Select(conds ...field.Expr) IUserToBeingDo {
	return u.withDO(u.DO.Select(conds...))
}

func (u userToBeingDo) Where(conds ...gen.Condition) IUserToBeingDo {
	return u.withDO(u.DO.Where(conds...))
}

func (u userToBeingDo) Order(conds ...field.Expr) IUserToBeingDo {
	return u.withDO(u.DO.Order(conds...))
}

func (u userToBeingDo) Distinct(cols ...field.Expr) IUserToBeingDo {
	return u.withDO(u.DO.Distinct(cols...))
}

func (u userToBeingDo) Omit(cols ...field.Expr) IUserToBeingDo {
	return u.withDO(u.DO.Omit(cols...))
}

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

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

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

func (u userToBeingDo) Group(cols ...field.Expr) IUserToBeingDo {
	return u.withDO(u.DO.Group(cols...))
}

func (u userToBeingDo) Having(conds ...gen.Condition) IUserToBeingDo {
	return u.withDO(u.DO.Having(conds...))
}

func (u userToBeingDo) Limit(limit int) IUserToBeingDo {
	return u.withDO(u.DO.Limit(limit))
}

func (u userToBeingDo) Offset(offset int) IUserToBeingDo {
	return u.withDO(u.DO.Offset(offset))
}

func (u userToBeingDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IUserToBeingDo {
	return u.withDO(u.DO.Scopes(funcs...))
}

func (u userToBeingDo) Unscoped() IUserToBeingDo {
	return u.withDO(u.DO.Unscoped())
}

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

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

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

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

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

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

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

func (u userToBeingDo) Attrs(attrs ...field.AssignExpr) IUserToBeingDo {
	return u.withDO(u.DO.Attrs(attrs...))
}

func (u userToBeingDo) Assign(attrs ...field.AssignExpr) IUserToBeingDo {
	return u.withDO(u.DO.Assign(attrs...))
}

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

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

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

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

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

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

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