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

	_firma.firmaDo.UseDB(db, opts...)
	_firma.firmaDo.UseModel(&model.Firma{})

	tableName := _firma.firmaDo.TableName()
	_firma.ALL = field.NewAsterisk(tableName)
	_firma.ID = field.NewUint(tableName, "id")
	_firma.CreatedAt = field.NewTime(tableName, "created_at")
	_firma.UpdatedAt = field.NewTime(tableName, "updated_at")
	_firma.DeletedAt = field.NewField(tableName, "deleted_at")
	_firma.FirmaTyp = field.NewInt(tableName, "firma_typ")
	_firma.Name = field.NewString(tableName, "name")
	_firma.InternerName = field.NewString(tableName, "interner_name")
	_firma.Slug = field.NewString(tableName, "slug")
	_firma.Branche = field.NewString(tableName, "branche")
	_firma.ParentCompany = field.NewUint(tableName, "parent_company")
	_firma.Tochergesellschaften = firmaHasManyTochergesellschaften{
		db: db.Session(&gorm.Session{}),

		RelationField: field.NewRelation("Tochergesellschaften", "model.Firma"),
		Tochergesellschaften: struct {
			field.RelationField
		}{
			RelationField: field.NewRelation("Tochergesellschaften.Tochergesellschaften", "model.Firma"),
		},
		Ansprechpartner: struct {
			field.RelationField
			Firmen struct {
				field.RelationField
			}
		}{
			RelationField: field.NewRelation("Tochergesellschaften.Ansprechpartner", "model.Ansprechpartner"),
			Firmen: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("Tochergesellschaften.Ansprechpartner.Firmen", "model.Firma"),
			},
		},
	}

	_firma.Ansprechpartner = firmaManyToManyAnsprechpartner{
		db: db.Session(&gorm.Session{}),

		RelationField: field.NewRelation("Ansprechpartner", "model.Ansprechpartner"),
	}

	_firma.fillFieldMap()

	return _firma
}

type firma struct {
	firmaDo

	ALL                  field.Asterisk
	ID                   field.Uint
	CreatedAt            field.Time
	UpdatedAt            field.Time
	DeletedAt            field.Field
	FirmaTyp             field.Int
	Name                 field.String
	InternerName         field.String
	Slug                 field.String
	Branche              field.String
	ParentCompany        field.Uint
	Tochergesellschaften firmaHasManyTochergesellschaften

	Ansprechpartner firmaManyToManyAnsprechpartner

	fieldMap map[string]field.Expr
}

func (f firma) Table(newTableName string) *firma {
	f.firmaDo.UseTable(newTableName)
	return f.updateTableName(newTableName)
}

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

func (f *firma) updateTableName(table string) *firma {
	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.FirmaTyp = field.NewInt(table, "firma_typ")
	f.Name = field.NewString(table, "name")
	f.InternerName = field.NewString(table, "interner_name")
	f.Slug = field.NewString(table, "slug")
	f.Branche = field.NewString(table, "branche")
	f.ParentCompany = field.NewUint(table, "parent_company")

	f.fillFieldMap()

	return f
}

func (f *firma) 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 *firma) fillFieldMap() {
	f.fieldMap = make(map[string]field.Expr, 12)
	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["firma_typ"] = f.FirmaTyp
	f.fieldMap["name"] = f.Name
	f.fieldMap["interner_name"] = f.InternerName
	f.fieldMap["slug"] = f.Slug
	f.fieldMap["branche"] = f.Branche
	f.fieldMap["parent_company"] = f.ParentCompany

}

func (f firma) clone(db *gorm.DB) firma {
	f.firmaDo.ReplaceConnPool(db.Statement.ConnPool)
	f.Tochergesellschaften.db = db.Session(&gorm.Session{Initialized: true})
	f.Tochergesellschaften.db.Statement.ConnPool = db.Statement.ConnPool
	f.Ansprechpartner.db = db.Session(&gorm.Session{Initialized: true})
	f.Ansprechpartner.db.Statement.ConnPool = db.Statement.ConnPool
	return f
}

func (f firma) replaceDB(db *gorm.DB) firma {
	f.firmaDo.ReplaceDB(db)
	f.Tochergesellschaften.db = db.Session(&gorm.Session{})
	f.Ansprechpartner.db = db.Session(&gorm.Session{})
	return f
}

type firmaHasManyTochergesellschaften struct {
	db *gorm.DB

	field.RelationField

	Tochergesellschaften struct {
		field.RelationField
	}
	Ansprechpartner struct {
		field.RelationField
		Firmen struct {
			field.RelationField
		}
	}
}

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

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

func (a firmaHasManyTochergesellschaften) Model(m *model.Firma) *firmaHasManyTochergesellschaftenTx {
	return &firmaHasManyTochergesellschaftenTx{a.db.Model(m).Association(a.Name())}
}

func (a firmaHasManyTochergesellschaften) Unscoped() *firmaHasManyTochergesellschaften {
	a.db = a.db.Unscoped()
	return &a
}

type firmaHasManyTochergesellschaftenTx struct{ tx *gorm.Association }

func (a firmaHasManyTochergesellschaftenTx) Find() (result []*model.Firma, err error) {
	return result, a.tx.Find(&result)
}

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

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

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

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

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

func (a firmaHasManyTochergesellschaftenTx) Unscoped() *firmaHasManyTochergesellschaftenTx {
	a.tx = a.tx.Unscoped()
	return &a
}

type firmaManyToManyAnsprechpartner struct {
	db *gorm.DB

	field.RelationField
}

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

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

func (a firmaManyToManyAnsprechpartner) Model(m *model.Firma) *firmaManyToManyAnsprechpartnerTx {
	return &firmaManyToManyAnsprechpartnerTx{a.db.Model(m).Association(a.Name())}
}

func (a firmaManyToManyAnsprechpartner) Unscoped() *firmaManyToManyAnsprechpartner {
	a.db = a.db.Unscoped()
	return &a
}

type firmaManyToManyAnsprechpartnerTx struct{ tx *gorm.Association }

func (a firmaManyToManyAnsprechpartnerTx) Find() (result []*model.Ansprechpartner, err error) {
	return result, a.tx.Find(&result)
}

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

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

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

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

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

func (a firmaManyToManyAnsprechpartnerTx) Unscoped() *firmaManyToManyAnsprechpartnerTx {
	a.tx = a.tx.Unscoped()
	return &a
}

type firmaDo struct{ gen.DO }

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

func (f firmaDo) Debug() IFirmaDo {
	return f.withDO(f.DO.Debug())
}

func (f firmaDo) WithContext(ctx context.Context) IFirmaDo {
	return f.withDO(f.DO.WithContext(ctx))
}

func (f firmaDo) ReadDB() IFirmaDo {
	return f.Clauses(dbresolver.Read)
}

func (f firmaDo) WriteDB() IFirmaDo {
	return f.Clauses(dbresolver.Write)
}

func (f firmaDo) Session(config *gorm.Session) IFirmaDo {
	return f.withDO(f.DO.Session(config))
}

func (f firmaDo) Clauses(conds ...clause.Expression) IFirmaDo {
	return f.withDO(f.DO.Clauses(conds...))
}

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

func (f firmaDo) Not(conds ...gen.Condition) IFirmaDo {
	return f.withDO(f.DO.Not(conds...))
}

func (f firmaDo) Or(conds ...gen.Condition) IFirmaDo {
	return f.withDO(f.DO.Or(conds...))
}

func (f firmaDo) Select(conds ...field.Expr) IFirmaDo {
	return f.withDO(f.DO.Select(conds...))
}

func (f firmaDo) Where(conds ...gen.Condition) IFirmaDo {
	return f.withDO(f.DO.Where(conds...))
}

func (f firmaDo) Order(conds ...field.Expr) IFirmaDo {
	return f.withDO(f.DO.Order(conds...))
}

func (f firmaDo) Distinct(cols ...field.Expr) IFirmaDo {
	return f.withDO(f.DO.Distinct(cols...))
}

func (f firmaDo) Omit(cols ...field.Expr) IFirmaDo {
	return f.withDO(f.DO.Omit(cols...))
}

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

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

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

func (f firmaDo) Group(cols ...field.Expr) IFirmaDo {
	return f.withDO(f.DO.Group(cols...))
}

func (f firmaDo) Having(conds ...gen.Condition) IFirmaDo {
	return f.withDO(f.DO.Having(conds...))
}

func (f firmaDo) Limit(limit int) IFirmaDo {
	return f.withDO(f.DO.Limit(limit))
}

func (f firmaDo) Offset(offset int) IFirmaDo {
	return f.withDO(f.DO.Offset(offset))
}

func (f firmaDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IFirmaDo {
	return f.withDO(f.DO.Scopes(funcs...))
}

func (f firmaDo) Unscoped() IFirmaDo {
	return f.withDO(f.DO.Unscoped())
}

func (f firmaDo) Create(values ...*model.Firma) error {
	if len(values) == 0 {
		return nil
	}
	return f.DO.Create(values)
}

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

func (f firmaDo) First() (*model.Firma, error) {
	if result, err := f.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Firma), nil
	}
}

func (f firmaDo) Take() (*model.Firma, error) {
	if result, err := f.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Firma), nil
	}
}

func (f firmaDo) Last() (*model.Firma, error) {
	if result, err := f.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Firma), nil
	}
}

func (f firmaDo) Find() ([]*model.Firma, error) {
	result, err := f.DO.Find()
	return result.([]*model.Firma), err
}

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

func (f firmaDo) Attrs(attrs ...field.AssignExpr) IFirmaDo {
	return f.withDO(f.DO.Attrs(attrs...))
}

func (f firmaDo) Assign(attrs ...field.AssignExpr) IFirmaDo {
	return f.withDO(f.DO.Assign(attrs...))
}

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

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

func (f firmaDo) FirstOrInit() (*model.Firma, error) {
	if result, err := f.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Firma), nil
	}
}

func (f firmaDo) FirstOrCreate() (*model.Firma, error) {
	if result, err := f.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Firma), nil
	}
}

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

func (f firmaDo) Delete(models ...*model.Firma) (result gen.ResultInfo, err error) {
	return f.DO.Delete(models)
}

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