// 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 query

import (
	"context"
	"database/sql"

	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"gorm.io/gorm/schema"

	"gorm.io/gen"
	"gorm.io/gen/field"

	"gorm.io/plugin/dbresolver"

	"git.kocoder.xyz/kocoded/vt/model"
)

func newKostenstelle(db *gorm.DB, opts ...gen.DOOption) kostenstelle {
	_kostenstelle := kostenstelle{}

	_kostenstelle.kostenstelleDo.UseDB(db, opts...)
	_kostenstelle.kostenstelleDo.UseModel(&model.Kostenstelle{})

	tableName := _kostenstelle.kostenstelleDo.TableName()
	_kostenstelle.ALL = field.NewAsterisk(tableName)
	_kostenstelle.ID = field.NewUint(tableName, "id")
	_kostenstelle.CreatedAt = field.NewTime(tableName, "created_at")
	_kostenstelle.UpdatedAt = field.NewTime(tableName, "updated_at")
	_kostenstelle.DeletedAt = field.NewField(tableName, "deleted_at")

	_kostenstelle.fillFieldMap()

	return _kostenstelle
}

type kostenstelle struct {
	kostenstelleDo

	ALL       field.Asterisk
	ID        field.Uint
	CreatedAt field.Time
	UpdatedAt field.Time
	DeletedAt field.Field

	fieldMap map[string]field.Expr
}

func (k kostenstelle) Table(newTableName string) *kostenstelle {
	k.kostenstelleDo.UseTable(newTableName)
	return k.updateTableName(newTableName)
}

func (k kostenstelle) As(alias string) *kostenstelle {
	k.kostenstelleDo.DO = *(k.kostenstelleDo.As(alias).(*gen.DO))
	return k.updateTableName(alias)
}

func (k *kostenstelle) updateTableName(table string) *kostenstelle {
	k.ALL = field.NewAsterisk(table)
	k.ID = field.NewUint(table, "id")
	k.CreatedAt = field.NewTime(table, "created_at")
	k.UpdatedAt = field.NewTime(table, "updated_at")
	k.DeletedAt = field.NewField(table, "deleted_at")

	k.fillFieldMap()

	return k
}

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

func (k *kostenstelle) fillFieldMap() {
	k.fieldMap = make(map[string]field.Expr, 4)
	k.fieldMap["id"] = k.ID
	k.fieldMap["created_at"] = k.CreatedAt
	k.fieldMap["updated_at"] = k.UpdatedAt
	k.fieldMap["deleted_at"] = k.DeletedAt
}

func (k kostenstelle) clone(db *gorm.DB) kostenstelle {
	k.kostenstelleDo.ReplaceConnPool(db.Statement.ConnPool)
	return k
}

func (k kostenstelle) replaceDB(db *gorm.DB) kostenstelle {
	k.kostenstelleDo.ReplaceDB(db)
	return k
}

type kostenstelleDo struct{ gen.DO }

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

func (k kostenstelleDo) Debug() IKostenstelleDo {
	return k.withDO(k.DO.Debug())
}

func (k kostenstelleDo) WithContext(ctx context.Context) IKostenstelleDo {
	return k.withDO(k.DO.WithContext(ctx))
}

func (k kostenstelleDo) ReadDB() IKostenstelleDo {
	return k.Clauses(dbresolver.Read)
}

func (k kostenstelleDo) WriteDB() IKostenstelleDo {
	return k.Clauses(dbresolver.Write)
}

func (k kostenstelleDo) Session(config *gorm.Session) IKostenstelleDo {
	return k.withDO(k.DO.Session(config))
}

func (k kostenstelleDo) Clauses(conds ...clause.Expression) IKostenstelleDo {
	return k.withDO(k.DO.Clauses(conds...))
}

func (k kostenstelleDo) Returning(value interface{}, columns ...string) IKostenstelleDo {
	return k.withDO(k.DO.Returning(value, columns...))
}

func (k kostenstelleDo) Not(conds ...gen.Condition) IKostenstelleDo {
	return k.withDO(k.DO.Not(conds...))
}

func (k kostenstelleDo) Or(conds ...gen.Condition) IKostenstelleDo {
	return k.withDO(k.DO.Or(conds...))
}

func (k kostenstelleDo) Select(conds ...field.Expr) IKostenstelleDo {
	return k.withDO(k.DO.Select(conds...))
}

func (k kostenstelleDo) Where(conds ...gen.Condition) IKostenstelleDo {
	return k.withDO(k.DO.Where(conds...))
}

func (k kostenstelleDo) Order(conds ...field.Expr) IKostenstelleDo {
	return k.withDO(k.DO.Order(conds...))
}

func (k kostenstelleDo) Distinct(cols ...field.Expr) IKostenstelleDo {
	return k.withDO(k.DO.Distinct(cols...))
}

func (k kostenstelleDo) Omit(cols ...field.Expr) IKostenstelleDo {
	return k.withDO(k.DO.Omit(cols...))
}

func (k kostenstelleDo) Join(table schema.Tabler, on ...field.Expr) IKostenstelleDo {
	return k.withDO(k.DO.Join(table, on...))
}

func (k kostenstelleDo) LeftJoin(table schema.Tabler, on ...field.Expr) IKostenstelleDo {
	return k.withDO(k.DO.LeftJoin(table, on...))
}

func (k kostenstelleDo) RightJoin(table schema.Tabler, on ...field.Expr) IKostenstelleDo {
	return k.withDO(k.DO.RightJoin(table, on...))
}

func (k kostenstelleDo) Group(cols ...field.Expr) IKostenstelleDo {
	return k.withDO(k.DO.Group(cols...))
}

func (k kostenstelleDo) Having(conds ...gen.Condition) IKostenstelleDo {
	return k.withDO(k.DO.Having(conds...))
}

func (k kostenstelleDo) Limit(limit int) IKostenstelleDo {
	return k.withDO(k.DO.Limit(limit))
}

func (k kostenstelleDo) Offset(offset int) IKostenstelleDo {
	return k.withDO(k.DO.Offset(offset))
}

func (k kostenstelleDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IKostenstelleDo {
	return k.withDO(k.DO.Scopes(funcs...))
}

func (k kostenstelleDo) Unscoped() IKostenstelleDo {
	return k.withDO(k.DO.Unscoped())
}

func (k kostenstelleDo) Create(values ...*model.Kostenstelle) error {
	if len(values) == 0 {
		return nil
	}
	return k.DO.Create(values)
}

func (k kostenstelleDo) CreateInBatches(values []*model.Kostenstelle, batchSize int) error {
	return k.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 (k kostenstelleDo) Save(values ...*model.Kostenstelle) error {
	if len(values) == 0 {
		return nil
	}
	return k.DO.Save(values)
}

func (k kostenstelleDo) First() (*model.Kostenstelle, error) {
	if result, err := k.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Kostenstelle), nil
	}
}

func (k kostenstelleDo) Take() (*model.Kostenstelle, error) {
	if result, err := k.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Kostenstelle), nil
	}
}

func (k kostenstelleDo) Last() (*model.Kostenstelle, error) {
	if result, err := k.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Kostenstelle), nil
	}
}

func (k kostenstelleDo) Find() ([]*model.Kostenstelle, error) {
	result, err := k.DO.Find()
	return result.([]*model.Kostenstelle), err
}

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

func (k kostenstelleDo) Attrs(attrs ...field.AssignExpr) IKostenstelleDo {
	return k.withDO(k.DO.Attrs(attrs...))
}

func (k kostenstelleDo) Assign(attrs ...field.AssignExpr) IKostenstelleDo {
	return k.withDO(k.DO.Assign(attrs...))
}

func (k kostenstelleDo) Joins(fields ...field.RelationField) IKostenstelleDo {
	for _, _f := range fields {
		k = *k.withDO(k.DO.Joins(_f))
	}
	return &k
}

func (k kostenstelleDo) Preload(fields ...field.RelationField) IKostenstelleDo {
	for _, _f := range fields {
		k = *k.withDO(k.DO.Preload(_f))
	}
	return &k
}

func (k kostenstelleDo) FirstOrInit() (*model.Kostenstelle, error) {
	if result, err := k.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Kostenstelle), nil
	}
}

func (k kostenstelleDo) FirstOrCreate() (*model.Kostenstelle, error) {
	if result, err := k.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Kostenstelle), nil
	}
}

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

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

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

func (k kostenstelleDo) Scan(result interface{}) (err error) {
	return k.DO.Scan(result)
}

func (k kostenstelleDo) Delete(models ...*model.Kostenstelle) (result gen.ResultInfo, err error) {
	return k.DO.Delete(models)
}

func (k *kostenstelleDo) withDO(do gen.Dao) *kostenstelleDo {
	k.DO = *do.(*gen.DO)
	return k
}
