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

	_loginProcessToken.loginProcessTokenDo.UseDB(db, opts...)
	_loginProcessToken.loginProcessTokenDo.UseModel(&models.LoginProcessToken{})

	tableName := _loginProcessToken.loginProcessTokenDo.TableName()
	_loginProcessToken.ALL = field.NewAsterisk(tableName)
	_loginProcessToken.ID = field.NewUint64(tableName, "id")
	_loginProcessToken.UserId = field.NewString(tableName, "user_id")
	_loginProcessToken.Token = field.NewString(tableName, "token")
	_loginProcessToken.Name = field.NewString(tableName, "name")
	_loginProcessToken.ExpiresAt = field.NewTime(tableName, "expires_at")
	_loginProcessToken.User = loginProcessTokenBelongsToUser{
		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"),
			},
		},
	}

	_loginProcessToken.fillFieldMap()

	return _loginProcessToken
}

type loginProcessToken struct {
	loginProcessTokenDo

	ALL       field.Asterisk
	ID        field.Uint64
	UserId    field.String
	Token     field.String
	Name      field.String
	ExpiresAt field.Time
	User      loginProcessTokenBelongsToUser

	fieldMap map[string]field.Expr
}

func (l loginProcessToken) Table(newTableName string) *loginProcessToken {
	l.loginProcessTokenDo.UseTable(newTableName)
	return l.updateTableName(newTableName)
}

func (l loginProcessToken) As(alias string) *loginProcessToken {
	l.loginProcessTokenDo.DO = *(l.loginProcessTokenDo.As(alias).(*gen.DO))
	return l.updateTableName(alias)
}

func (l *loginProcessToken) updateTableName(table string) *loginProcessToken {
	l.ALL = field.NewAsterisk(table)
	l.ID = field.NewUint64(table, "id")
	l.UserId = field.NewString(table, "user_id")
	l.Token = field.NewString(table, "token")
	l.Name = field.NewString(table, "name")
	l.ExpiresAt = field.NewTime(table, "expires_at")

	l.fillFieldMap()

	return l
}

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

func (l *loginProcessToken) fillFieldMap() {
	l.fieldMap = make(map[string]field.Expr, 6)
	l.fieldMap["id"] = l.ID
	l.fieldMap["user_id"] = l.UserId
	l.fieldMap["token"] = l.Token
	l.fieldMap["name"] = l.Name
	l.fieldMap["expires_at"] = l.ExpiresAt

}

func (l loginProcessToken) clone(db *gorm.DB) loginProcessToken {
	l.loginProcessTokenDo.ReplaceConnPool(db.Statement.ConnPool)
	return l
}

func (l loginProcessToken) replaceDB(db *gorm.DB) loginProcessToken {
	l.loginProcessTokenDo.ReplaceDB(db)
	return l
}

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

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

func (a loginProcessTokenBelongsToUser) Model(m *models.LoginProcessToken) *loginProcessTokenBelongsToUserTx {
	return &loginProcessTokenBelongsToUserTx{a.db.Model(m).Association(a.Name())}
}

type loginProcessTokenBelongsToUserTx struct{ tx *gorm.Association }

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

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

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

type loginProcessTokenDo struct{ gen.DO }

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

func (l loginProcessTokenDo) Debug() ILoginProcessTokenDo {
	return l.withDO(l.DO.Debug())
}

func (l loginProcessTokenDo) WithContext(ctx context.Context) ILoginProcessTokenDo {
	return l.withDO(l.DO.WithContext(ctx))
}

func (l loginProcessTokenDo) ReadDB() ILoginProcessTokenDo {
	return l.Clauses(dbresolver.Read)
}

func (l loginProcessTokenDo) WriteDB() ILoginProcessTokenDo {
	return l.Clauses(dbresolver.Write)
}

func (l loginProcessTokenDo) Session(config *gorm.Session) ILoginProcessTokenDo {
	return l.withDO(l.DO.Session(config))
}

func (l loginProcessTokenDo) Clauses(conds ...clause.Expression) ILoginProcessTokenDo {
	return l.withDO(l.DO.Clauses(conds...))
}

func (l loginProcessTokenDo) Returning(value interface{}, columns ...string) ILoginProcessTokenDo {
	return l.withDO(l.DO.Returning(value, columns...))
}

func (l loginProcessTokenDo) Not(conds ...gen.Condition) ILoginProcessTokenDo {
	return l.withDO(l.DO.Not(conds...))
}

func (l loginProcessTokenDo) Or(conds ...gen.Condition) ILoginProcessTokenDo {
	return l.withDO(l.DO.Or(conds...))
}

func (l loginProcessTokenDo) Select(conds ...field.Expr) ILoginProcessTokenDo {
	return l.withDO(l.DO.Select(conds...))
}

func (l loginProcessTokenDo) Where(conds ...gen.Condition) ILoginProcessTokenDo {
	return l.withDO(l.DO.Where(conds...))
}

func (l loginProcessTokenDo) Order(conds ...field.Expr) ILoginProcessTokenDo {
	return l.withDO(l.DO.Order(conds...))
}

func (l loginProcessTokenDo) Distinct(cols ...field.Expr) ILoginProcessTokenDo {
	return l.withDO(l.DO.Distinct(cols...))
}

func (l loginProcessTokenDo) Omit(cols ...field.Expr) ILoginProcessTokenDo {
	return l.withDO(l.DO.Omit(cols...))
}

func (l loginProcessTokenDo) Join(table schema.Tabler, on ...field.Expr) ILoginProcessTokenDo {
	return l.withDO(l.DO.Join(table, on...))
}

func (l loginProcessTokenDo) LeftJoin(table schema.Tabler, on ...field.Expr) ILoginProcessTokenDo {
	return l.withDO(l.DO.LeftJoin(table, on...))
}

func (l loginProcessTokenDo) RightJoin(table schema.Tabler, on ...field.Expr) ILoginProcessTokenDo {
	return l.withDO(l.DO.RightJoin(table, on...))
}

func (l loginProcessTokenDo) Group(cols ...field.Expr) ILoginProcessTokenDo {
	return l.withDO(l.DO.Group(cols...))
}

func (l loginProcessTokenDo) Having(conds ...gen.Condition) ILoginProcessTokenDo {
	return l.withDO(l.DO.Having(conds...))
}

func (l loginProcessTokenDo) Limit(limit int) ILoginProcessTokenDo {
	return l.withDO(l.DO.Limit(limit))
}

func (l loginProcessTokenDo) Offset(offset int) ILoginProcessTokenDo {
	return l.withDO(l.DO.Offset(offset))
}

func (l loginProcessTokenDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ILoginProcessTokenDo {
	return l.withDO(l.DO.Scopes(funcs...))
}

func (l loginProcessTokenDo) Unscoped() ILoginProcessTokenDo {
	return l.withDO(l.DO.Unscoped())
}

func (l loginProcessTokenDo) Create(values ...*models.LoginProcessToken) error {
	if len(values) == 0 {
		return nil
	}
	return l.DO.Create(values)
}

func (l loginProcessTokenDo) CreateInBatches(values []*models.LoginProcessToken, batchSize int) error {
	return l.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 (l loginProcessTokenDo) Save(values ...*models.LoginProcessToken) error {
	if len(values) == 0 {
		return nil
	}
	return l.DO.Save(values)
}

func (l loginProcessTokenDo) First() (*models.LoginProcessToken, error) {
	if result, err := l.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*models.LoginProcessToken), nil
	}
}

func (l loginProcessTokenDo) Take() (*models.LoginProcessToken, error) {
	if result, err := l.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*models.LoginProcessToken), nil
	}
}

func (l loginProcessTokenDo) Last() (*models.LoginProcessToken, error) {
	if result, err := l.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*models.LoginProcessToken), nil
	}
}

func (l loginProcessTokenDo) Find() ([]*models.LoginProcessToken, error) {
	result, err := l.DO.Find()
	return result.([]*models.LoginProcessToken), err
}

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

func (l loginProcessTokenDo) Attrs(attrs ...field.AssignExpr) ILoginProcessTokenDo {
	return l.withDO(l.DO.Attrs(attrs...))
}

func (l loginProcessTokenDo) Assign(attrs ...field.AssignExpr) ILoginProcessTokenDo {
	return l.withDO(l.DO.Assign(attrs...))
}

func (l loginProcessTokenDo) Joins(fields ...field.RelationField) ILoginProcessTokenDo {
	for _, _f := range fields {
		l = *l.withDO(l.DO.Joins(_f))
	}
	return &l
}

func (l loginProcessTokenDo) Preload(fields ...field.RelationField) ILoginProcessTokenDo {
	for _, _f := range fields {
		l = *l.withDO(l.DO.Preload(_f))
	}
	return &l
}

func (l loginProcessTokenDo) FirstOrInit() (*models.LoginProcessToken, error) {
	if result, err := l.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*models.LoginProcessToken), nil
	}
}

func (l loginProcessTokenDo) FirstOrCreate() (*models.LoginProcessToken, error) {
	if result, err := l.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*models.LoginProcessToken), nil
	}
}

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

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

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

func (l loginProcessTokenDo) Scan(result interface{}) (err error) {
	return l.DO.Scan(result)
}

func (l loginProcessTokenDo) Delete(models ...*models.LoginProcessToken) (result gen.ResultInfo, err error) {
	return l.DO.Delete(models)
}

func (l *loginProcessTokenDo) withDO(do gen.Dao) *loginProcessTokenDo {
	l.DO = *do.(*gen.DO)
	return l
}
