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

	_feed.feedDo.UseDB(db, opts...)
	_feed.feedDo.UseModel(&models.Feed{})

	tableName := _feed.feedDo.TableName()
	_feed.ALL = field.NewAsterisk(tableName)
	_feed.ID = field.NewUint(tableName, "id")
	_feed.CreatedAt = field.NewTime(tableName, "created_at")
	_feed.UpdatedAt = field.NewTime(tableName, "updated_at")
	_feed.DeletedAt = field.NewField(tableName, "deleted_at")
	_feed.Name = field.NewString(tableName, "name")
	_feed.OwnerId = field.NewString(tableName, "owner_id")
	_feed.IsDefault = field.NewBool(tableName, "is_default")
	_feed.PublicKey = field.NewField(tableName, "public_key")
	_feed.Owner = feedBelongsToOwner{
		db: db.Session(&gorm.Session{}),

		RelationField: field.NewRelation("Owner", "models.User"),
		Server: struct {
			field.RelationField
			Icon struct {
				field.RelationField
			}
		}{
			RelationField: field.NewRelation("Owner.Server", "models.RemoteServer"),
			Icon: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("Owner.Server.Icon", "models.MediaMetadata"),
			},
		},
		Icon: struct {
			field.RelationField
		}{
			RelationField: field.NewRelation("Owner.Icon", "models.MediaMetadata"),
		},
		Background: struct {
			field.RelationField
		}{
			RelationField: field.NewRelation("Owner.Background", "models.MediaMetadata"),
		},
		Banner: struct {
			field.RelationField
		}{
			RelationField: field.NewRelation("Owner.Banner", "models.MediaMetadata"),
		},
		RemoteInfo: struct {
			field.RelationField
			User struct {
				field.RelationField
			}
		}{
			RelationField: field.NewRelation("Owner.RemoteInfo", "models.UserRemoteLinks"),
			User: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("Owner.RemoteInfo.User", "models.User"),
			},
		},
		InfoFields: struct {
			field.RelationField
			User struct {
				field.RelationField
			}
		}{
			RelationField: field.NewRelation("Owner.InfoFields", "models.UserInfoField"),
			User: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("Owner.InfoFields.User", "models.User"),
			},
		},
		BeingTypes: struct {
			field.RelationField
			User struct {
				field.RelationField
			}
		}{
			RelationField: field.NewRelation("Owner.BeingTypes", "models.UserToBeing"),
			User: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("Owner.BeingTypes.User", "models.User"),
			},
		},
		Tags: struct {
			field.RelationField
			User struct {
				field.RelationField
			}
		}{
			RelationField: field.NewRelation("Owner.Tags", "models.UserToTag"),
			User: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("Owner.Tags.User", "models.User"),
			},
		},
		Relations: struct {
			field.RelationField
			User struct {
				field.RelationField
			}
			TargetUser struct {
				field.RelationField
			}
		}{
			RelationField: field.NewRelation("Owner.Relations", "models.UserToUserRelation"),
			User: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("Owner.Relations.User", "models.User"),
			},
			TargetUser: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("Owner.Relations.TargetUser", "models.User"),
			},
		},
		Pronouns: struct {
			field.RelationField
			User struct {
				field.RelationField
			}
		}{
			RelationField: field.NewRelation("Owner.Pronouns", "models.UserToPronoun"),
			User: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("Owner.Pronouns.User", "models.User"),
			},
		},
		Roles: struct {
			field.RelationField
			User struct {
				field.RelationField
			}
			Role struct {
				field.RelationField
			}
		}{
			RelationField: field.NewRelation("Owner.Roles", "models.UserToRole"),
			User: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("Owner.Roles.User", "models.User"),
			},
			Role: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("Owner.Roles.Role", "models.Role"),
			},
		},
		AuthMethods: struct {
			field.RelationField
			User struct {
				field.RelationField
			}
		}{
			RelationField: field.NewRelation("Owner.AuthMethods", "models.UserAuthMethod"),
			User: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("Owner.AuthMethods.User", "models.User"),
			},
		},
	}

	_feed.fillFieldMap()

	return _feed
}

type feed struct {
	feedDo

	ALL       field.Asterisk
	ID        field.Uint
	CreatedAt field.Time
	UpdatedAt field.Time
	DeletedAt field.Field
	Name      field.String
	OwnerId   field.String
	IsDefault field.Bool
	PublicKey field.Field
	Owner     feedBelongsToOwner

	fieldMap map[string]field.Expr
}

func (f feed) Table(newTableName string) *feed {
	f.feedDo.UseTable(newTableName)
	return f.updateTableName(newTableName)
}

func (f feed) As(alias string) *feed {
	f.feedDo.DO = *(f.feedDo.As(alias).(*gen.DO))
	return f.updateTableName(alias)
}

func (f *feed) updateTableName(table string) *feed {
	f.ALL = field.NewAsterisk(table)
	f.ID = field.NewUint(table, "id")
	f.CreatedAt = field.NewTime(table, "created_at")
	f.UpdatedAt = field.NewTime(table, "updated_at")
	f.DeletedAt = field.NewField(table, "deleted_at")
	f.Name = field.NewString(table, "name")
	f.OwnerId = field.NewString(table, "owner_id")
	f.IsDefault = field.NewBool(table, "is_default")
	f.PublicKey = field.NewField(table, "public_key")

	f.fillFieldMap()

	return f
}

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

func (f *feed) fillFieldMap() {
	f.fieldMap = make(map[string]field.Expr, 9)
	f.fieldMap["id"] = f.ID
	f.fieldMap["created_at"] = f.CreatedAt
	f.fieldMap["updated_at"] = f.UpdatedAt
	f.fieldMap["deleted_at"] = f.DeletedAt
	f.fieldMap["name"] = f.Name
	f.fieldMap["owner_id"] = f.OwnerId
	f.fieldMap["is_default"] = f.IsDefault
	f.fieldMap["public_key"] = f.PublicKey

}

func (f feed) clone(db *gorm.DB) feed {
	f.feedDo.ReplaceConnPool(db.Statement.ConnPool)
	return f
}

func (f feed) replaceDB(db *gorm.DB) feed {
	f.feedDo.ReplaceDB(db)
	return f
}

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

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

func (a feedBelongsToOwner) Model(m *models.Feed) *feedBelongsToOwnerTx {
	return &feedBelongsToOwnerTx{a.db.Model(m).Association(a.Name())}
}

type feedBelongsToOwnerTx struct{ tx *gorm.Association }

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

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

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

type feedDo struct{ gen.DO }

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

func (f feedDo) Debug() IFeedDo {
	return f.withDO(f.DO.Debug())
}

func (f feedDo) WithContext(ctx context.Context) IFeedDo {
	return f.withDO(f.DO.WithContext(ctx))
}

func (f feedDo) ReadDB() IFeedDo {
	return f.Clauses(dbresolver.Read)
}

func (f feedDo) WriteDB() IFeedDo {
	return f.Clauses(dbresolver.Write)
}

func (f feedDo) Session(config *gorm.Session) IFeedDo {
	return f.withDO(f.DO.Session(config))
}

func (f feedDo) Clauses(conds ...clause.Expression) IFeedDo {
	return f.withDO(f.DO.Clauses(conds...))
}

func (f feedDo) Returning(value interface{}, columns ...string) IFeedDo {
	return f.withDO(f.DO.Returning(value, columns...))
}

func (f feedDo) Not(conds ...gen.Condition) IFeedDo {
	return f.withDO(f.DO.Not(conds...))
}

func (f feedDo) Or(conds ...gen.Condition) IFeedDo {
	return f.withDO(f.DO.Or(conds...))
}

func (f feedDo) Select(conds ...field.Expr) IFeedDo {
	return f.withDO(f.DO.Select(conds...))
}

func (f feedDo) Where(conds ...gen.Condition) IFeedDo {
	return f.withDO(f.DO.Where(conds...))
}

func (f feedDo) Order(conds ...field.Expr) IFeedDo {
	return f.withDO(f.DO.Order(conds...))
}

func (f feedDo) Distinct(cols ...field.Expr) IFeedDo {
	return f.withDO(f.DO.Distinct(cols...))
}

func (f feedDo) Omit(cols ...field.Expr) IFeedDo {
	return f.withDO(f.DO.Omit(cols...))
}

func (f feedDo) Join(table schema.Tabler, on ...field.Expr) IFeedDo {
	return f.withDO(f.DO.Join(table, on...))
}

func (f feedDo) LeftJoin(table schema.Tabler, on ...field.Expr) IFeedDo {
	return f.withDO(f.DO.LeftJoin(table, on...))
}

func (f feedDo) RightJoin(table schema.Tabler, on ...field.Expr) IFeedDo {
	return f.withDO(f.DO.RightJoin(table, on...))
}

func (f feedDo) Group(cols ...field.Expr) IFeedDo {
	return f.withDO(f.DO.Group(cols...))
}

func (f feedDo) Having(conds ...gen.Condition) IFeedDo {
	return f.withDO(f.DO.Having(conds...))
}

func (f feedDo) Limit(limit int) IFeedDo {
	return f.withDO(f.DO.Limit(limit))
}

func (f feedDo) Offset(offset int) IFeedDo {
	return f.withDO(f.DO.Offset(offset))
}

func (f feedDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IFeedDo {
	return f.withDO(f.DO.Scopes(funcs...))
}

func (f feedDo) Unscoped() IFeedDo {
	return f.withDO(f.DO.Unscoped())
}

func (f feedDo) Create(values ...*models.Feed) error {
	if len(values) == 0 {
		return nil
	}
	return f.DO.Create(values)
}

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

func (f feedDo) First() (*models.Feed, error) {
	if result, err := f.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*models.Feed), nil
	}
}

func (f feedDo) Take() (*models.Feed, error) {
	if result, err := f.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*models.Feed), nil
	}
}

func (f feedDo) Last() (*models.Feed, error) {
	if result, err := f.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*models.Feed), nil
	}
}

func (f feedDo) Find() ([]*models.Feed, error) {
	result, err := f.DO.Find()
	return result.([]*models.Feed), err
}

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

func (f feedDo) Attrs(attrs ...field.AssignExpr) IFeedDo {
	return f.withDO(f.DO.Attrs(attrs...))
}

func (f feedDo) Assign(attrs ...field.AssignExpr) IFeedDo {
	return f.withDO(f.DO.Assign(attrs...))
}

func (f feedDo) Joins(fields ...field.RelationField) IFeedDo {
	for _, _f := range fields {
		f = *f.withDO(f.DO.Joins(_f))
	}
	return &f
}

func (f feedDo) Preload(fields ...field.RelationField) IFeedDo {
	for _, _f := range fields {
		f = *f.withDO(f.DO.Preload(_f))
	}
	return &f
}

func (f feedDo) FirstOrInit() (*models.Feed, error) {
	if result, err := f.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*models.Feed), nil
	}
}

func (f feedDo) FirstOrCreate() (*models.Feed, error) {
	if result, err := f.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*models.Feed), nil
	}
}

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

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

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

func (f feedDo) Scan(result interface{}) (err error) {
	return f.DO.Scan(result)
}

func (f feedDo) Delete(models ...*models.Feed) (result gen.ResultInfo, err error) {
	return f.DO.Delete(models)
}

func (f *feedDo) withDO(do gen.Dao) *feedDo {
	f.DO = *do.(*gen.DO)
	return f
}
