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

	_remoteServer.remoteServerDo.UseDB(db, opts...)
	_remoteServer.remoteServerDo.UseModel(&models.RemoteServer{})

	tableName := _remoteServer.remoteServerDo.TableName()
	_remoteServer.ALL = field.NewAsterisk(tableName)
	_remoteServer.ID = field.NewUint(tableName, "id")
	_remoteServer.CreatedAt = field.NewTime(tableName, "created_at")
	_remoteServer.UpdatedAt = field.NewTime(tableName, "updated_at")
	_remoteServer.DeletedAt = field.NewField(tableName, "deleted_at")
	_remoteServer.ServerType = field.NewField(tableName, "server_type")
	_remoteServer.Domain = field.NewString(tableName, "domain")
	_remoteServer.Name = field.NewString(tableName, "name")
	_remoteServer.IconId = field.NewField(tableName, "icon_id")
	_remoteServer.IsSelf = field.NewBool(tableName, "is_self")
	_remoteServer.Icon = remoteServerBelongsToIcon{
		db: db.Session(&gorm.Session{}),

		RelationField: field.NewRelation("Icon", "models.MediaMetadata"),
	}

	_remoteServer.fillFieldMap()

	return _remoteServer
}

type remoteServer struct {
	remoteServerDo

	ALL        field.Asterisk
	ID         field.Uint
	CreatedAt  field.Time
	UpdatedAt  field.Time
	DeletedAt  field.Field
	ServerType field.Field
	Domain     field.String
	Name       field.String
	IconId     field.Field
	IsSelf     field.Bool
	Icon       remoteServerBelongsToIcon

	fieldMap map[string]field.Expr
}

func (r remoteServer) Table(newTableName string) *remoteServer {
	r.remoteServerDo.UseTable(newTableName)
	return r.updateTableName(newTableName)
}

func (r remoteServer) As(alias string) *remoteServer {
	r.remoteServerDo.DO = *(r.remoteServerDo.As(alias).(*gen.DO))
	return r.updateTableName(alias)
}

func (r *remoteServer) updateTableName(table string) *remoteServer {
	r.ALL = field.NewAsterisk(table)
	r.ID = field.NewUint(table, "id")
	r.CreatedAt = field.NewTime(table, "created_at")
	r.UpdatedAt = field.NewTime(table, "updated_at")
	r.DeletedAt = field.NewField(table, "deleted_at")
	r.ServerType = field.NewField(table, "server_type")
	r.Domain = field.NewString(table, "domain")
	r.Name = field.NewString(table, "name")
	r.IconId = field.NewField(table, "icon_id")
	r.IsSelf = field.NewBool(table, "is_self")

	r.fillFieldMap()

	return r
}

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

func (r *remoteServer) fillFieldMap() {
	r.fieldMap = make(map[string]field.Expr, 10)
	r.fieldMap["id"] = r.ID
	r.fieldMap["created_at"] = r.CreatedAt
	r.fieldMap["updated_at"] = r.UpdatedAt
	r.fieldMap["deleted_at"] = r.DeletedAt
	r.fieldMap["server_type"] = r.ServerType
	r.fieldMap["domain"] = r.Domain
	r.fieldMap["name"] = r.Name
	r.fieldMap["icon_id"] = r.IconId
	r.fieldMap["is_self"] = r.IsSelf

}

func (r remoteServer) clone(db *gorm.DB) remoteServer {
	r.remoteServerDo.ReplaceConnPool(db.Statement.ConnPool)
	return r
}

func (r remoteServer) replaceDB(db *gorm.DB) remoteServer {
	r.remoteServerDo.ReplaceDB(db)
	return r
}

type remoteServerBelongsToIcon struct {
	db *gorm.DB

	field.RelationField
}

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

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

func (a remoteServerBelongsToIcon) Model(m *models.RemoteServer) *remoteServerBelongsToIconTx {
	return &remoteServerBelongsToIconTx{a.db.Model(m).Association(a.Name())}
}

type remoteServerBelongsToIconTx struct{ tx *gorm.Association }

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

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

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

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

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

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

type remoteServerDo struct{ gen.DO }

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

func (r remoteServerDo) Debug() IRemoteServerDo {
	return r.withDO(r.DO.Debug())
}

func (r remoteServerDo) WithContext(ctx context.Context) IRemoteServerDo {
	return r.withDO(r.DO.WithContext(ctx))
}

func (r remoteServerDo) ReadDB() IRemoteServerDo {
	return r.Clauses(dbresolver.Read)
}

func (r remoteServerDo) WriteDB() IRemoteServerDo {
	return r.Clauses(dbresolver.Write)
}

func (r remoteServerDo) Session(config *gorm.Session) IRemoteServerDo {
	return r.withDO(r.DO.Session(config))
}

func (r remoteServerDo) Clauses(conds ...clause.Expression) IRemoteServerDo {
	return r.withDO(r.DO.Clauses(conds...))
}

func (r remoteServerDo) Returning(value interface{}, columns ...string) IRemoteServerDo {
	return r.withDO(r.DO.Returning(value, columns...))
}

func (r remoteServerDo) Not(conds ...gen.Condition) IRemoteServerDo {
	return r.withDO(r.DO.Not(conds...))
}

func (r remoteServerDo) Or(conds ...gen.Condition) IRemoteServerDo {
	return r.withDO(r.DO.Or(conds...))
}

func (r remoteServerDo) Select(conds ...field.Expr) IRemoteServerDo {
	return r.withDO(r.DO.Select(conds...))
}

func (r remoteServerDo) Where(conds ...gen.Condition) IRemoteServerDo {
	return r.withDO(r.DO.Where(conds...))
}

func (r remoteServerDo) Order(conds ...field.Expr) IRemoteServerDo {
	return r.withDO(r.DO.Order(conds...))
}

func (r remoteServerDo) Distinct(cols ...field.Expr) IRemoteServerDo {
	return r.withDO(r.DO.Distinct(cols...))
}

func (r remoteServerDo) Omit(cols ...field.Expr) IRemoteServerDo {
	return r.withDO(r.DO.Omit(cols...))
}

func (r remoteServerDo) Join(table schema.Tabler, on ...field.Expr) IRemoteServerDo {
	return r.withDO(r.DO.Join(table, on...))
}

func (r remoteServerDo) LeftJoin(table schema.Tabler, on ...field.Expr) IRemoteServerDo {
	return r.withDO(r.DO.LeftJoin(table, on...))
}

func (r remoteServerDo) RightJoin(table schema.Tabler, on ...field.Expr) IRemoteServerDo {
	return r.withDO(r.DO.RightJoin(table, on...))
}

func (r remoteServerDo) Group(cols ...field.Expr) IRemoteServerDo {
	return r.withDO(r.DO.Group(cols...))
}

func (r remoteServerDo) Having(conds ...gen.Condition) IRemoteServerDo {
	return r.withDO(r.DO.Having(conds...))
}

func (r remoteServerDo) Limit(limit int) IRemoteServerDo {
	return r.withDO(r.DO.Limit(limit))
}

func (r remoteServerDo) Offset(offset int) IRemoteServerDo {
	return r.withDO(r.DO.Offset(offset))
}

func (r remoteServerDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IRemoteServerDo {
	return r.withDO(r.DO.Scopes(funcs...))
}

func (r remoteServerDo) Unscoped() IRemoteServerDo {
	return r.withDO(r.DO.Unscoped())
}

func (r remoteServerDo) Create(values ...*models.RemoteServer) error {
	if len(values) == 0 {
		return nil
	}
	return r.DO.Create(values)
}

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

func (r remoteServerDo) First() (*models.RemoteServer, error) {
	if result, err := r.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*models.RemoteServer), nil
	}
}

func (r remoteServerDo) Take() (*models.RemoteServer, error) {
	if result, err := r.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*models.RemoteServer), nil
	}
}

func (r remoteServerDo) Last() (*models.RemoteServer, error) {
	if result, err := r.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*models.RemoteServer), nil
	}
}

func (r remoteServerDo) Find() ([]*models.RemoteServer, error) {
	result, err := r.DO.Find()
	return result.([]*models.RemoteServer), err
}

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

func (r remoteServerDo) Attrs(attrs ...field.AssignExpr) IRemoteServerDo {
	return r.withDO(r.DO.Attrs(attrs...))
}

func (r remoteServerDo) Assign(attrs ...field.AssignExpr) IRemoteServerDo {
	return r.withDO(r.DO.Assign(attrs...))
}

func (r remoteServerDo) Joins(fields ...field.RelationField) IRemoteServerDo {
	for _, _f := range fields {
		r = *r.withDO(r.DO.Joins(_f))
	}
	return &r
}

func (r remoteServerDo) Preload(fields ...field.RelationField) IRemoteServerDo {
	for _, _f := range fields {
		r = *r.withDO(r.DO.Preload(_f))
	}
	return &r
}

func (r remoteServerDo) FirstOrInit() (*models.RemoteServer, error) {
	if result, err := r.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*models.RemoteServer), nil
	}
}

func (r remoteServerDo) FirstOrCreate() (*models.RemoteServer, error) {
	if result, err := r.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*models.RemoteServer), nil
	}
}

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

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

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

func (r remoteServerDo) Scan(result interface{}) (err error) {
	return r.DO.Scan(result)
}

func (r remoteServerDo) Delete(models ...*models.RemoteServer) (result gen.ResultInfo, err error) {
	return r.DO.Delete(models)
}

func (r *remoteServerDo) withDO(do gen.Dao) *remoteServerDo {
	r.DO = *do.(*gen.DO)
	return r
}
