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

	_userInfoField.userInfoFieldDo.UseDB(db, opts...)
	_userInfoField.userInfoFieldDo.UseModel(&models.UserInfoField{})

	tableName := _userInfoField.userInfoFieldDo.TableName()
	_userInfoField.ALL = field.NewAsterisk(tableName)
	_userInfoField.ID = field.NewUint(tableName, "id")
	_userInfoField.CreatedAt = field.NewTime(tableName, "created_at")
	_userInfoField.UpdatedAt = field.NewTime(tableName, "updated_at")
	_userInfoField.DeletedAt = field.NewField(tableName, "deleted_at")
	_userInfoField.Name = field.NewString(tableName, "name")
	_userInfoField.Value = field.NewString(tableName, "value")
	_userInfoField.LastUrlCheckDate = field.NewTime(tableName, "last_url_check_date")
	_userInfoField.Confirmed = field.NewBool(tableName, "confirmed")
	_userInfoField.UserId = field.NewString(tableName, "user_id")
	_userInfoField.User = userInfoFieldBelongsToUser{
		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"),
			},
		},
	}

	_userInfoField.fillFieldMap()

	return _userInfoField
}

type userInfoField struct {
	userInfoFieldDo

	ALL              field.Asterisk
	ID               field.Uint
	CreatedAt        field.Time
	UpdatedAt        field.Time
	DeletedAt        field.Field
	Name             field.String
	Value            field.String
	LastUrlCheckDate field.Time
	Confirmed        field.Bool
	UserId           field.String
	User             userInfoFieldBelongsToUser

	fieldMap map[string]field.Expr
}

func (u userInfoField) Table(newTableName string) *userInfoField {
	u.userInfoFieldDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

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

func (u *userInfoField) updateTableName(table string) *userInfoField {
	u.ALL = field.NewAsterisk(table)
	u.ID = field.NewUint(table, "id")
	u.CreatedAt = field.NewTime(table, "created_at")
	u.UpdatedAt = field.NewTime(table, "updated_at")
	u.DeletedAt = field.NewField(table, "deleted_at")
	u.Name = field.NewString(table, "name")
	u.Value = field.NewString(table, "value")
	u.LastUrlCheckDate = field.NewTime(table, "last_url_check_date")
	u.Confirmed = field.NewBool(table, "confirmed")
	u.UserId = field.NewString(table, "user_id")

	u.fillFieldMap()

	return u
}

func (u *userInfoField) 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 *userInfoField) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 10)
	u.fieldMap["id"] = u.ID
	u.fieldMap["created_at"] = u.CreatedAt
	u.fieldMap["updated_at"] = u.UpdatedAt
	u.fieldMap["deleted_at"] = u.DeletedAt
	u.fieldMap["name"] = u.Name
	u.fieldMap["value"] = u.Value
	u.fieldMap["last_url_check_date"] = u.LastUrlCheckDate
	u.fieldMap["confirmed"] = u.Confirmed
	u.fieldMap["user_id"] = u.UserId

}

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

func (u userInfoField) replaceDB(db *gorm.DB) userInfoField {
	u.userInfoFieldDo.ReplaceDB(db)
	return u
}

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

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

func (a userInfoFieldBelongsToUser) Model(m *models.UserInfoField) *userInfoFieldBelongsToUserTx {
	return &userInfoFieldBelongsToUserTx{a.db.Model(m).Association(a.Name())}
}

type userInfoFieldBelongsToUserTx struct{ tx *gorm.Association }

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

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

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

type userInfoFieldDo struct{ gen.DO }

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

func (u userInfoFieldDo) Debug() IUserInfoFieldDo {
	return u.withDO(u.DO.Debug())
}

func (u userInfoFieldDo) WithContext(ctx context.Context) IUserInfoFieldDo {
	return u.withDO(u.DO.WithContext(ctx))
}

func (u userInfoFieldDo) ReadDB() IUserInfoFieldDo {
	return u.Clauses(dbresolver.Read)
}

func (u userInfoFieldDo) WriteDB() IUserInfoFieldDo {
	return u.Clauses(dbresolver.Write)
}

func (u userInfoFieldDo) Session(config *gorm.Session) IUserInfoFieldDo {
	return u.withDO(u.DO.Session(config))
}

func (u userInfoFieldDo) Clauses(conds ...clause.Expression) IUserInfoFieldDo {
	return u.withDO(u.DO.Clauses(conds...))
}

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

func (u userInfoFieldDo) Not(conds ...gen.Condition) IUserInfoFieldDo {
	return u.withDO(u.DO.Not(conds...))
}

func (u userInfoFieldDo) Or(conds ...gen.Condition) IUserInfoFieldDo {
	return u.withDO(u.DO.Or(conds...))
}

func (u userInfoFieldDo) Select(conds ...field.Expr) IUserInfoFieldDo {
	return u.withDO(u.DO.Select(conds...))
}

func (u userInfoFieldDo) Where(conds ...gen.Condition) IUserInfoFieldDo {
	return u.withDO(u.DO.Where(conds...))
}

func (u userInfoFieldDo) Order(conds ...field.Expr) IUserInfoFieldDo {
	return u.withDO(u.DO.Order(conds...))
}

func (u userInfoFieldDo) Distinct(cols ...field.Expr) IUserInfoFieldDo {
	return u.withDO(u.DO.Distinct(cols...))
}

func (u userInfoFieldDo) Omit(cols ...field.Expr) IUserInfoFieldDo {
	return u.withDO(u.DO.Omit(cols...))
}

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

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

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

func (u userInfoFieldDo) Group(cols ...field.Expr) IUserInfoFieldDo {
	return u.withDO(u.DO.Group(cols...))
}

func (u userInfoFieldDo) Having(conds ...gen.Condition) IUserInfoFieldDo {
	return u.withDO(u.DO.Having(conds...))
}

func (u userInfoFieldDo) Limit(limit int) IUserInfoFieldDo {
	return u.withDO(u.DO.Limit(limit))
}

func (u userInfoFieldDo) Offset(offset int) IUserInfoFieldDo {
	return u.withDO(u.DO.Offset(offset))
}

func (u userInfoFieldDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IUserInfoFieldDo {
	return u.withDO(u.DO.Scopes(funcs...))
}

func (u userInfoFieldDo) Unscoped() IUserInfoFieldDo {
	return u.withDO(u.DO.Unscoped())
}

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

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

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

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

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

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

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

func (u userInfoFieldDo) Attrs(attrs ...field.AssignExpr) IUserInfoFieldDo {
	return u.withDO(u.DO.Attrs(attrs...))
}

func (u userInfoFieldDo) Assign(attrs ...field.AssignExpr) IUserInfoFieldDo {
	return u.withDO(u.DO.Assign(attrs...))
}

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

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

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

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

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

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

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