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

	_rechnung.rechnungDo.UseDB(db, opts...)
	_rechnung.rechnungDo.UseModel(&model.Rechnung{})

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

	_rechnung.fillFieldMap()

	return _rechnung
}

type rechnung struct {
	rechnungDo

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

	fieldMap map[string]field.Expr
}

func (r rechnung) Table(newTableName string) *rechnung {
	r.rechnungDo.UseTable(newTableName)
	return r.updateTableName(newTableName)
}

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

func (r *rechnung) updateTableName(table string) *rechnung {
	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.fillFieldMap()

	return r
}

func (r *rechnung) 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 *rechnung) fillFieldMap() {
	r.fieldMap = make(map[string]field.Expr, 4)
	r.fieldMap["id"] = r.ID
	r.fieldMap["created_at"] = r.CreatedAt
	r.fieldMap["updated_at"] = r.UpdatedAt
	r.fieldMap["deleted_at"] = r.DeletedAt
}

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

func (r rechnung) replaceDB(db *gorm.DB) rechnung {
	r.rechnungDo.ReplaceDB(db)
	return r
}

type rechnungDo struct{ gen.DO }

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

func (r rechnungDo) Debug() IRechnungDo {
	return r.withDO(r.DO.Debug())
}

func (r rechnungDo) WithContext(ctx context.Context) IRechnungDo {
	return r.withDO(r.DO.WithContext(ctx))
}

func (r rechnungDo) ReadDB() IRechnungDo {
	return r.Clauses(dbresolver.Read)
}

func (r rechnungDo) WriteDB() IRechnungDo {
	return r.Clauses(dbresolver.Write)
}

func (r rechnungDo) Session(config *gorm.Session) IRechnungDo {
	return r.withDO(r.DO.Session(config))
}

func (r rechnungDo) Clauses(conds ...clause.Expression) IRechnungDo {
	return r.withDO(r.DO.Clauses(conds...))
}

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

func (r rechnungDo) Not(conds ...gen.Condition) IRechnungDo {
	return r.withDO(r.DO.Not(conds...))
}

func (r rechnungDo) Or(conds ...gen.Condition) IRechnungDo {
	return r.withDO(r.DO.Or(conds...))
}

func (r rechnungDo) Select(conds ...field.Expr) IRechnungDo {
	return r.withDO(r.DO.Select(conds...))
}

func (r rechnungDo) Where(conds ...gen.Condition) IRechnungDo {
	return r.withDO(r.DO.Where(conds...))
}

func (r rechnungDo) Order(conds ...field.Expr) IRechnungDo {
	return r.withDO(r.DO.Order(conds...))
}

func (r rechnungDo) Distinct(cols ...field.Expr) IRechnungDo {
	return r.withDO(r.DO.Distinct(cols...))
}

func (r rechnungDo) Omit(cols ...field.Expr) IRechnungDo {
	return r.withDO(r.DO.Omit(cols...))
}

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

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

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

func (r rechnungDo) Group(cols ...field.Expr) IRechnungDo {
	return r.withDO(r.DO.Group(cols...))
}

func (r rechnungDo) Having(conds ...gen.Condition) IRechnungDo {
	return r.withDO(r.DO.Having(conds...))
}

func (r rechnungDo) Limit(limit int) IRechnungDo {
	return r.withDO(r.DO.Limit(limit))
}

func (r rechnungDo) Offset(offset int) IRechnungDo {
	return r.withDO(r.DO.Offset(offset))
}

func (r rechnungDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IRechnungDo {
	return r.withDO(r.DO.Scopes(funcs...))
}

func (r rechnungDo) Unscoped() IRechnungDo {
	return r.withDO(r.DO.Unscoped())
}

func (r rechnungDo) Create(values ...*model.Rechnung) error {
	if len(values) == 0 {
		return nil
	}
	return r.DO.Create(values)
}

func (r rechnungDo) CreateInBatches(values []*model.Rechnung, 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 rechnungDo) Save(values ...*model.Rechnung) error {
	if len(values) == 0 {
		return nil
	}
	return r.DO.Save(values)
}

func (r rechnungDo) First() (*model.Rechnung, error) {
	if result, err := r.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Rechnung), nil
	}
}

func (r rechnungDo) Take() (*model.Rechnung, error) {
	if result, err := r.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Rechnung), nil
	}
}

func (r rechnungDo) Last() (*model.Rechnung, error) {
	if result, err := r.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Rechnung), nil
	}
}

func (r rechnungDo) Find() ([]*model.Rechnung, error) {
	result, err := r.DO.Find()
	return result.([]*model.Rechnung), err
}

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

func (r rechnungDo) Attrs(attrs ...field.AssignExpr) IRechnungDo {
	return r.withDO(r.DO.Attrs(attrs...))
}

func (r rechnungDo) Assign(attrs ...field.AssignExpr) IRechnungDo {
	return r.withDO(r.DO.Assign(attrs...))
}

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

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

func (r rechnungDo) FirstOrInit() (*model.Rechnung, error) {
	if result, err := r.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Rechnung), nil
	}
}

func (r rechnungDo) FirstOrCreate() (*model.Rechnung, error) {
	if result, err := r.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Rechnung), nil
	}
}

func (r rechnungDo) FindByPage(offset int, limit int) (result []*model.Rechnung, 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 rechnungDo) 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 rechnungDo) Scan(result interface{}) (err error) {
	return r.DO.Scan(result)
}

func (r rechnungDo) Delete(models ...*model.Rechnung) (result gen.ResultInfo, err error) {
	return r.DO.Delete(models)
}

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