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

	_ansprechpartner.ansprechpartnerDo.UseDB(db, opts...)
	_ansprechpartner.ansprechpartnerDo.UseModel(&model.Ansprechpartner{})

	tableName := _ansprechpartner.ansprechpartnerDo.TableName()
	_ansprechpartner.ALL = field.NewAsterisk(tableName)
	_ansprechpartner.ID = field.NewUint(tableName, "id")
	_ansprechpartner.CreatedAt = field.NewTime(tableName, "created_at")
	_ansprechpartner.UpdatedAt = field.NewTime(tableName, "updated_at")
	_ansprechpartner.DeletedAt = field.NewField(tableName, "deleted_at")
	_ansprechpartner.ImageURL = field.NewString(tableName, "image_url")
	_ansprechpartner.Title = field.NewString(tableName, "title")
	_ansprechpartner.FirstName = field.NewString(tableName, "first_name")
	_ansprechpartner.LastName = field.NewString(tableName, "last_name")
	_ansprechpartner.LastTitle = field.NewString(tableName, "last_title")
	_ansprechpartner.EMail = field.NewString(tableName, "e_mail")
	_ansprechpartner.Phone = field.NewString(tableName, "phone")
	_ansprechpartner.Mobile = field.NewString(tableName, "mobile")
	_ansprechpartner.Notes = field.NewString(tableName, "notes")
	_ansprechpartner.Description = field.NewString(tableName, "description")
	_ansprechpartner.Active = field.NewBool(tableName, "active")
	_ansprechpartner.Gender = field.NewInt(tableName, "gender")
	_ansprechpartner.Firmen = ansprechpartnerManyToManyFirmen{
		db: db.Session(&gorm.Session{}),

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

	_ansprechpartner.fillFieldMap()

	return _ansprechpartner
}

type ansprechpartner struct {
	ansprechpartnerDo

	ALL         field.Asterisk
	ID          field.Uint
	CreatedAt   field.Time
	UpdatedAt   field.Time
	DeletedAt   field.Field
	ImageURL    field.String
	Title       field.String
	FirstName   field.String
	LastName    field.String
	LastTitle   field.String
	EMail       field.String
	Phone       field.String
	Mobile      field.String
	Notes       field.String
	Description field.String
	Active      field.Bool
	Gender      field.Int
	Firmen      ansprechpartnerManyToManyFirmen

	fieldMap map[string]field.Expr
}

func (a ansprechpartner) Table(newTableName string) *ansprechpartner {
	a.ansprechpartnerDo.UseTable(newTableName)
	return a.updateTableName(newTableName)
}

func (a ansprechpartner) As(alias string) *ansprechpartner {
	a.ansprechpartnerDo.DO = *(a.ansprechpartnerDo.As(alias).(*gen.DO))
	return a.updateTableName(alias)
}

func (a *ansprechpartner) updateTableName(table string) *ansprechpartner {
	a.ALL = field.NewAsterisk(table)
	a.ID = field.NewUint(table, "id")
	a.CreatedAt = field.NewTime(table, "created_at")
	a.UpdatedAt = field.NewTime(table, "updated_at")
	a.DeletedAt = field.NewField(table, "deleted_at")
	a.ImageURL = field.NewString(table, "image_url")
	a.Title = field.NewString(table, "title")
	a.FirstName = field.NewString(table, "first_name")
	a.LastName = field.NewString(table, "last_name")
	a.LastTitle = field.NewString(table, "last_title")
	a.EMail = field.NewString(table, "e_mail")
	a.Phone = field.NewString(table, "phone")
	a.Mobile = field.NewString(table, "mobile")
	a.Notes = field.NewString(table, "notes")
	a.Description = field.NewString(table, "description")
	a.Active = field.NewBool(table, "active")
	a.Gender = field.NewInt(table, "gender")

	a.fillFieldMap()

	return a
}

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

func (a *ansprechpartner) fillFieldMap() {
	a.fieldMap = make(map[string]field.Expr, 17)
	a.fieldMap["id"] = a.ID
	a.fieldMap["created_at"] = a.CreatedAt
	a.fieldMap["updated_at"] = a.UpdatedAt
	a.fieldMap["deleted_at"] = a.DeletedAt
	a.fieldMap["image_url"] = a.ImageURL
	a.fieldMap["title"] = a.Title
	a.fieldMap["first_name"] = a.FirstName
	a.fieldMap["last_name"] = a.LastName
	a.fieldMap["last_title"] = a.LastTitle
	a.fieldMap["e_mail"] = a.EMail
	a.fieldMap["phone"] = a.Phone
	a.fieldMap["mobile"] = a.Mobile
	a.fieldMap["notes"] = a.Notes
	a.fieldMap["description"] = a.Description
	a.fieldMap["active"] = a.Active
	a.fieldMap["gender"] = a.Gender

}

func (a ansprechpartner) clone(db *gorm.DB) ansprechpartner {
	a.ansprechpartnerDo.ReplaceConnPool(db.Statement.ConnPool)
	a.Firmen.db = db.Session(&gorm.Session{Initialized: true})
	a.Firmen.db.Statement.ConnPool = db.Statement.ConnPool
	return a
}

func (a ansprechpartner) replaceDB(db *gorm.DB) ansprechpartner {
	a.ansprechpartnerDo.ReplaceDB(db)
	a.Firmen.db = db.Session(&gorm.Session{})
	return a
}

type ansprechpartnerManyToManyFirmen struct {
	db *gorm.DB

	field.RelationField

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

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

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

func (a ansprechpartnerManyToManyFirmen) Model(m *model.Ansprechpartner) *ansprechpartnerManyToManyFirmenTx {
	return &ansprechpartnerManyToManyFirmenTx{a.db.Model(m).Association(a.Name())}
}

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

type ansprechpartnerManyToManyFirmenTx struct{ tx *gorm.Association }

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

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

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

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

type ansprechpartnerDo struct{ gen.DO }

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

func (a ansprechpartnerDo) Debug() IAnsprechpartnerDo {
	return a.withDO(a.DO.Debug())
}

func (a ansprechpartnerDo) WithContext(ctx context.Context) IAnsprechpartnerDo {
	return a.withDO(a.DO.WithContext(ctx))
}

func (a ansprechpartnerDo) ReadDB() IAnsprechpartnerDo {
	return a.Clauses(dbresolver.Read)
}

func (a ansprechpartnerDo) WriteDB() IAnsprechpartnerDo {
	return a.Clauses(dbresolver.Write)
}

func (a ansprechpartnerDo) Session(config *gorm.Session) IAnsprechpartnerDo {
	return a.withDO(a.DO.Session(config))
}

func (a ansprechpartnerDo) Clauses(conds ...clause.Expression) IAnsprechpartnerDo {
	return a.withDO(a.DO.Clauses(conds...))
}

func (a ansprechpartnerDo) Returning(value interface{}, columns ...string) IAnsprechpartnerDo {
	return a.withDO(a.DO.Returning(value, columns...))
}

func (a ansprechpartnerDo) Not(conds ...gen.Condition) IAnsprechpartnerDo {
	return a.withDO(a.DO.Not(conds...))
}

func (a ansprechpartnerDo) Or(conds ...gen.Condition) IAnsprechpartnerDo {
	return a.withDO(a.DO.Or(conds...))
}

func (a ansprechpartnerDo) Select(conds ...field.Expr) IAnsprechpartnerDo {
	return a.withDO(a.DO.Select(conds...))
}

func (a ansprechpartnerDo) Where(conds ...gen.Condition) IAnsprechpartnerDo {
	return a.withDO(a.DO.Where(conds...))
}

func (a ansprechpartnerDo) Order(conds ...field.Expr) IAnsprechpartnerDo {
	return a.withDO(a.DO.Order(conds...))
}

func (a ansprechpartnerDo) Distinct(cols ...field.Expr) IAnsprechpartnerDo {
	return a.withDO(a.DO.Distinct(cols...))
}

func (a ansprechpartnerDo) Omit(cols ...field.Expr) IAnsprechpartnerDo {
	return a.withDO(a.DO.Omit(cols...))
}

func (a ansprechpartnerDo) Join(table schema.Tabler, on ...field.Expr) IAnsprechpartnerDo {
	return a.withDO(a.DO.Join(table, on...))
}

func (a ansprechpartnerDo) LeftJoin(table schema.Tabler, on ...field.Expr) IAnsprechpartnerDo {
	return a.withDO(a.DO.LeftJoin(table, on...))
}

func (a ansprechpartnerDo) RightJoin(table schema.Tabler, on ...field.Expr) IAnsprechpartnerDo {
	return a.withDO(a.DO.RightJoin(table, on...))
}

func (a ansprechpartnerDo) Group(cols ...field.Expr) IAnsprechpartnerDo {
	return a.withDO(a.DO.Group(cols...))
}

func (a ansprechpartnerDo) Having(conds ...gen.Condition) IAnsprechpartnerDo {
	return a.withDO(a.DO.Having(conds...))
}

func (a ansprechpartnerDo) Limit(limit int) IAnsprechpartnerDo {
	return a.withDO(a.DO.Limit(limit))
}

func (a ansprechpartnerDo) Offset(offset int) IAnsprechpartnerDo {
	return a.withDO(a.DO.Offset(offset))
}

func (a ansprechpartnerDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IAnsprechpartnerDo {
	return a.withDO(a.DO.Scopes(funcs...))
}

func (a ansprechpartnerDo) Unscoped() IAnsprechpartnerDo {
	return a.withDO(a.DO.Unscoped())
}

func (a ansprechpartnerDo) Create(values ...*model.Ansprechpartner) error {
	if len(values) == 0 {
		return nil
	}
	return a.DO.Create(values)
}

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

func (a ansprechpartnerDo) First() (*model.Ansprechpartner, error) {
	if result, err := a.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Ansprechpartner), nil
	}
}

func (a ansprechpartnerDo) Take() (*model.Ansprechpartner, error) {
	if result, err := a.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Ansprechpartner), nil
	}
}

func (a ansprechpartnerDo) Last() (*model.Ansprechpartner, error) {
	if result, err := a.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Ansprechpartner), nil
	}
}

func (a ansprechpartnerDo) Find() ([]*model.Ansprechpartner, error) {
	result, err := a.DO.Find()
	return result.([]*model.Ansprechpartner), err
}

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

func (a ansprechpartnerDo) Attrs(attrs ...field.AssignExpr) IAnsprechpartnerDo {
	return a.withDO(a.DO.Attrs(attrs...))
}

func (a ansprechpartnerDo) Assign(attrs ...field.AssignExpr) IAnsprechpartnerDo {
	return a.withDO(a.DO.Assign(attrs...))
}

func (a ansprechpartnerDo) Joins(fields ...field.RelationField) IAnsprechpartnerDo {
	for _, _f := range fields {
		a = *a.withDO(a.DO.Joins(_f))
	}
	return &a
}

func (a ansprechpartnerDo) Preload(fields ...field.RelationField) IAnsprechpartnerDo {
	for _, _f := range fields {
		a = *a.withDO(a.DO.Preload(_f))
	}
	return &a
}

func (a ansprechpartnerDo) FirstOrInit() (*model.Ansprechpartner, error) {
	if result, err := a.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Ansprechpartner), nil
	}
}

func (a ansprechpartnerDo) FirstOrCreate() (*model.Ansprechpartner, error) {
	if result, err := a.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Ansprechpartner), nil
	}
}

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

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

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

func (a ansprechpartnerDo) Scan(result interface{}) (err error) {
	return a.DO.Scan(result)
}

func (a ansprechpartnerDo) Delete(models ...*model.Ansprechpartner) (result gen.ResultInfo, err error) {
	return a.DO.Delete(models)
}

func (a *ansprechpartnerDo) withDO(do gen.Dao) *ansprechpartnerDo {
	a.DO = *do.(*gen.DO)
	return a
}
