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

	_emote.emoteDo.UseDB(db, opts...)
	_emote.emoteDo.UseModel(&models.Emote{})

	tableName := _emote.emoteDo.TableName()
	_emote.ALL = field.NewAsterisk(tableName)
	_emote.ID = field.NewUint(tableName, "id")
	_emote.CreatedAt = field.NewTime(tableName, "created_at")
	_emote.UpdatedAt = field.NewTime(tableName, "updated_at")
	_emote.DeletedAt = field.NewField(tableName, "deleted_at")
	_emote.MetadataId = field.NewString(tableName, "metadata_id")
	_emote.Name = field.NewString(tableName, "name")
	_emote.ServerId = field.NewUint(tableName, "server_id")
	_emote.Metadata = emoteBelongsToMetadata{
		db: db.Session(&gorm.Session{}),

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

	_emote.Server = emoteBelongsToServer{
		db: db.Session(&gorm.Session{}),

		RelationField: field.NewRelation("Server", "models.RemoteServer"),
		Icon: struct {
			field.RelationField
		}{
			RelationField: field.NewRelation("Server.Icon", "models.MediaMetadata"),
		},
	}

	_emote.fillFieldMap()

	return _emote
}

type emote struct {
	emoteDo

	ALL        field.Asterisk
	ID         field.Uint
	CreatedAt  field.Time
	UpdatedAt  field.Time
	DeletedAt  field.Field
	MetadataId field.String
	Name       field.String
	ServerId   field.Uint
	Metadata   emoteBelongsToMetadata

	Server emoteBelongsToServer

	fieldMap map[string]field.Expr
}

func (e emote) Table(newTableName string) *emote {
	e.emoteDo.UseTable(newTableName)
	return e.updateTableName(newTableName)
}

func (e emote) As(alias string) *emote {
	e.emoteDo.DO = *(e.emoteDo.As(alias).(*gen.DO))
	return e.updateTableName(alias)
}

func (e *emote) updateTableName(table string) *emote {
	e.ALL = field.NewAsterisk(table)
	e.ID = field.NewUint(table, "id")
	e.CreatedAt = field.NewTime(table, "created_at")
	e.UpdatedAt = field.NewTime(table, "updated_at")
	e.DeletedAt = field.NewField(table, "deleted_at")
	e.MetadataId = field.NewString(table, "metadata_id")
	e.Name = field.NewString(table, "name")
	e.ServerId = field.NewUint(table, "server_id")

	e.fillFieldMap()

	return e
}

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

func (e *emote) fillFieldMap() {
	e.fieldMap = make(map[string]field.Expr, 9)
	e.fieldMap["id"] = e.ID
	e.fieldMap["created_at"] = e.CreatedAt
	e.fieldMap["updated_at"] = e.UpdatedAt
	e.fieldMap["deleted_at"] = e.DeletedAt
	e.fieldMap["metadata_id"] = e.MetadataId
	e.fieldMap["name"] = e.Name
	e.fieldMap["server_id"] = e.ServerId

}

func (e emote) clone(db *gorm.DB) emote {
	e.emoteDo.ReplaceConnPool(db.Statement.ConnPool)
	return e
}

func (e emote) replaceDB(db *gorm.DB) emote {
	e.emoteDo.ReplaceDB(db)
	return e
}

type emoteBelongsToMetadata struct {
	db *gorm.DB

	field.RelationField
}

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

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

func (a emoteBelongsToMetadata) Model(m *models.Emote) *emoteBelongsToMetadataTx {
	return &emoteBelongsToMetadataTx{a.db.Model(m).Association(a.Name())}
}

type emoteBelongsToMetadataTx struct{ tx *gorm.Association }

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

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

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

type emoteBelongsToServer struct {
	db *gorm.DB

	field.RelationField

	Icon struct {
		field.RelationField
	}
}

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

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

func (a emoteBelongsToServer) Model(m *models.Emote) *emoteBelongsToServerTx {
	return &emoteBelongsToServerTx{a.db.Model(m).Association(a.Name())}
}

type emoteBelongsToServerTx struct{ tx *gorm.Association }

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

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

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

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

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

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

type emoteDo struct{ gen.DO }

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

func (e emoteDo) Debug() IEmoteDo {
	return e.withDO(e.DO.Debug())
}

func (e emoteDo) WithContext(ctx context.Context) IEmoteDo {
	return e.withDO(e.DO.WithContext(ctx))
}

func (e emoteDo) ReadDB() IEmoteDo {
	return e.Clauses(dbresolver.Read)
}

func (e emoteDo) WriteDB() IEmoteDo {
	return e.Clauses(dbresolver.Write)
}

func (e emoteDo) Session(config *gorm.Session) IEmoteDo {
	return e.withDO(e.DO.Session(config))
}

func (e emoteDo) Clauses(conds ...clause.Expression) IEmoteDo {
	return e.withDO(e.DO.Clauses(conds...))
}

func (e emoteDo) Returning(value interface{}, columns ...string) IEmoteDo {
	return e.withDO(e.DO.Returning(value, columns...))
}

func (e emoteDo) Not(conds ...gen.Condition) IEmoteDo {
	return e.withDO(e.DO.Not(conds...))
}

func (e emoteDo) Or(conds ...gen.Condition) IEmoteDo {
	return e.withDO(e.DO.Or(conds...))
}

func (e emoteDo) Select(conds ...field.Expr) IEmoteDo {
	return e.withDO(e.DO.Select(conds...))
}

func (e emoteDo) Where(conds ...gen.Condition) IEmoteDo {
	return e.withDO(e.DO.Where(conds...))
}

func (e emoteDo) Order(conds ...field.Expr) IEmoteDo {
	return e.withDO(e.DO.Order(conds...))
}

func (e emoteDo) Distinct(cols ...field.Expr) IEmoteDo {
	return e.withDO(e.DO.Distinct(cols...))
}

func (e emoteDo) Omit(cols ...field.Expr) IEmoteDo {
	return e.withDO(e.DO.Omit(cols...))
}

func (e emoteDo) Join(table schema.Tabler, on ...field.Expr) IEmoteDo {
	return e.withDO(e.DO.Join(table, on...))
}

func (e emoteDo) LeftJoin(table schema.Tabler, on ...field.Expr) IEmoteDo {
	return e.withDO(e.DO.LeftJoin(table, on...))
}

func (e emoteDo) RightJoin(table schema.Tabler, on ...field.Expr) IEmoteDo {
	return e.withDO(e.DO.RightJoin(table, on...))
}

func (e emoteDo) Group(cols ...field.Expr) IEmoteDo {
	return e.withDO(e.DO.Group(cols...))
}

func (e emoteDo) Having(conds ...gen.Condition) IEmoteDo {
	return e.withDO(e.DO.Having(conds...))
}

func (e emoteDo) Limit(limit int) IEmoteDo {
	return e.withDO(e.DO.Limit(limit))
}

func (e emoteDo) Offset(offset int) IEmoteDo {
	return e.withDO(e.DO.Offset(offset))
}

func (e emoteDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IEmoteDo {
	return e.withDO(e.DO.Scopes(funcs...))
}

func (e emoteDo) Unscoped() IEmoteDo {
	return e.withDO(e.DO.Unscoped())
}

func (e emoteDo) Create(values ...*models.Emote) error {
	if len(values) == 0 {
		return nil
	}
	return e.DO.Create(values)
}

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

func (e emoteDo) First() (*models.Emote, error) {
	if result, err := e.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*models.Emote), nil
	}
}

func (e emoteDo) Take() (*models.Emote, error) {
	if result, err := e.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*models.Emote), nil
	}
}

func (e emoteDo) Last() (*models.Emote, error) {
	if result, err := e.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*models.Emote), nil
	}
}

func (e emoteDo) Find() ([]*models.Emote, error) {
	result, err := e.DO.Find()
	return result.([]*models.Emote), err
}

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

func (e emoteDo) Attrs(attrs ...field.AssignExpr) IEmoteDo {
	return e.withDO(e.DO.Attrs(attrs...))
}

func (e emoteDo) Assign(attrs ...field.AssignExpr) IEmoteDo {
	return e.withDO(e.DO.Assign(attrs...))
}

func (e emoteDo) Joins(fields ...field.RelationField) IEmoteDo {
	for _, _f := range fields {
		e = *e.withDO(e.DO.Joins(_f))
	}
	return &e
}

func (e emoteDo) Preload(fields ...field.RelationField) IEmoteDo {
	for _, _f := range fields {
		e = *e.withDO(e.DO.Preload(_f))
	}
	return &e
}

func (e emoteDo) FirstOrInit() (*models.Emote, error) {
	if result, err := e.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*models.Emote), nil
	}
}

func (e emoteDo) FirstOrCreate() (*models.Emote, error) {
	if result, err := e.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*models.Emote), nil
	}
}

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

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

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

func (e emoteDo) Scan(result interface{}) (err error) {
	return e.DO.Scan(result)
}

func (e emoteDo) Delete(models ...*models.Emote) (result gen.ResultInfo, err error) {
	return e.DO.Delete(models)
}

func (e *emoteDo) withDO(do gen.Dao) *emoteDo {
	e.DO = *do.(*gen.DO)
	return e
}
