// 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"
	"strings"

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

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

	"gorm.io/plugin/dbresolver"

	"github.com/0xJacky/Nginx-UI/model"
)

func newDnsDomain(db *gorm.DB, opts ...gen.DOOption) dnsDomain {
	_dnsDomain := dnsDomain{}

	_dnsDomain.dnsDomainDo.UseDB(db, opts...)
	_dnsDomain.dnsDomainDo.UseModel(&model.DnsDomain{})

	tableName := _dnsDomain.dnsDomainDo.TableName()
	_dnsDomain.ALL = field.NewAsterisk(tableName)
	_dnsDomain.ID = field.NewUint64(tableName, "id")
	_dnsDomain.CreatedAt = field.NewTime(tableName, "created_at")
	_dnsDomain.UpdatedAt = field.NewTime(tableName, "updated_at")
	_dnsDomain.DeletedAt = field.NewField(tableName, "deleted_at")
	_dnsDomain.Domain = field.NewString(tableName, "domain")
	_dnsDomain.Description = field.NewString(tableName, "description")
	_dnsDomain.DnsCredentialID = field.NewUint64(tableName, "dns_credential_id")
	_dnsDomain.DDNSConfig = field.NewField(tableName, "ddns_config")
	_dnsDomain.DnsCredential = dnsDomainBelongsToDnsCredential{
		db: db.Session(&gorm.Session{}),

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

	_dnsDomain.fillFieldMap()

	return _dnsDomain
}

type dnsDomain struct {
	dnsDomainDo

	ALL             field.Asterisk
	ID              field.Uint64
	CreatedAt       field.Time
	UpdatedAt       field.Time
	DeletedAt       field.Field
	Domain          field.String
	Description     field.String
	DnsCredentialID field.Uint64
	DDNSConfig      field.Field
	DnsCredential   dnsDomainBelongsToDnsCredential

	fieldMap map[string]field.Expr
}

func (d dnsDomain) Table(newTableName string) *dnsDomain {
	d.dnsDomainDo.UseTable(newTableName)
	return d.updateTableName(newTableName)
}

func (d dnsDomain) As(alias string) *dnsDomain {
	d.dnsDomainDo.DO = *(d.dnsDomainDo.As(alias).(*gen.DO))
	return d.updateTableName(alias)
}

func (d *dnsDomain) updateTableName(table string) *dnsDomain {
	d.ALL = field.NewAsterisk(table)
	d.ID = field.NewUint64(table, "id")
	d.CreatedAt = field.NewTime(table, "created_at")
	d.UpdatedAt = field.NewTime(table, "updated_at")
	d.DeletedAt = field.NewField(table, "deleted_at")
	d.Domain = field.NewString(table, "domain")
	d.Description = field.NewString(table, "description")
	d.DnsCredentialID = field.NewUint64(table, "dns_credential_id")
	d.DDNSConfig = field.NewField(table, "ddns_config")

	d.fillFieldMap()

	return d
}

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

func (d *dnsDomain) fillFieldMap() {
	d.fieldMap = make(map[string]field.Expr, 9)
	d.fieldMap["id"] = d.ID
	d.fieldMap["created_at"] = d.CreatedAt
	d.fieldMap["updated_at"] = d.UpdatedAt
	d.fieldMap["deleted_at"] = d.DeletedAt
	d.fieldMap["domain"] = d.Domain
	d.fieldMap["description"] = d.Description
	d.fieldMap["dns_credential_id"] = d.DnsCredentialID
	d.fieldMap["ddns_config"] = d.DDNSConfig

}

func (d dnsDomain) clone(db *gorm.DB) dnsDomain {
	d.dnsDomainDo.ReplaceConnPool(db.Statement.ConnPool)
	d.DnsCredential.db = db.Session(&gorm.Session{Initialized: true})
	d.DnsCredential.db.Statement.ConnPool = db.Statement.ConnPool
	return d
}

func (d dnsDomain) replaceDB(db *gorm.DB) dnsDomain {
	d.dnsDomainDo.ReplaceDB(db)
	d.DnsCredential.db = db.Session(&gorm.Session{})
	return d
}

type dnsDomainBelongsToDnsCredential struct {
	db *gorm.DB

	field.RelationField
}

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

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

func (a dnsDomainBelongsToDnsCredential) Model(m *model.DnsDomain) *dnsDomainBelongsToDnsCredentialTx {
	return &dnsDomainBelongsToDnsCredentialTx{a.db.Model(m).Association(a.Name())}
}

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

type dnsDomainBelongsToDnsCredentialTx struct{ tx *gorm.Association }

func (a dnsDomainBelongsToDnsCredentialTx) Find() (result *model.DnsCredential, err error) {
	return result, a.tx.Find(&result)
}

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

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

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

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

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

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

type dnsDomainDo struct{ gen.DO }

// FirstByID Where("id=@id")
func (d dnsDomainDo) FirstByID(id uint64) (result *model.DnsDomain, err error) {
	var params []interface{}

	var generateSQL strings.Builder
	params = append(params, id)
	generateSQL.WriteString("id=? ")

	var executeSQL *gorm.DB
	executeSQL = d.UnderlyingDB().Where(generateSQL.String(), params...).Take(&result) // ignore_security_alert
	err = executeSQL.Error

	return
}

// DeleteByID update @@table set deleted_at=strftime('%Y-%m-%d %H:%M:%S','now') where id=@id
func (d dnsDomainDo) DeleteByID(id uint64) (err error) {
	var params []interface{}

	var generateSQL strings.Builder
	params = append(params, id)
	generateSQL.WriteString("update dns_domains set deleted_at=strftime('%Y-%m-%d %H:%M:%S','now') where id=? ")

	var executeSQL *gorm.DB
	executeSQL = d.UnderlyingDB().Exec(generateSQL.String(), params...) // ignore_security_alert
	err = executeSQL.Error

	return
}

func (d dnsDomainDo) Debug() *dnsDomainDo {
	return d.withDO(d.DO.Debug())
}

func (d dnsDomainDo) WithContext(ctx context.Context) *dnsDomainDo {
	return d.withDO(d.DO.WithContext(ctx))
}

func (d dnsDomainDo) ReadDB() *dnsDomainDo {
	return d.Clauses(dbresolver.Read)
}

func (d dnsDomainDo) WriteDB() *dnsDomainDo {
	return d.Clauses(dbresolver.Write)
}

func (d dnsDomainDo) Session(config *gorm.Session) *dnsDomainDo {
	return d.withDO(d.DO.Session(config))
}

func (d dnsDomainDo) Clauses(conds ...clause.Expression) *dnsDomainDo {
	return d.withDO(d.DO.Clauses(conds...))
}

func (d dnsDomainDo) Returning(value interface{}, columns ...string) *dnsDomainDo {
	return d.withDO(d.DO.Returning(value, columns...))
}

func (d dnsDomainDo) Not(conds ...gen.Condition) *dnsDomainDo {
	return d.withDO(d.DO.Not(conds...))
}

func (d dnsDomainDo) Or(conds ...gen.Condition) *dnsDomainDo {
	return d.withDO(d.DO.Or(conds...))
}

func (d dnsDomainDo) Select(conds ...field.Expr) *dnsDomainDo {
	return d.withDO(d.DO.Select(conds...))
}

func (d dnsDomainDo) Where(conds ...gen.Condition) *dnsDomainDo {
	return d.withDO(d.DO.Where(conds...))
}

func (d dnsDomainDo) Order(conds ...field.Expr) *dnsDomainDo {
	return d.withDO(d.DO.Order(conds...))
}

func (d dnsDomainDo) Distinct(cols ...field.Expr) *dnsDomainDo {
	return d.withDO(d.DO.Distinct(cols...))
}

func (d dnsDomainDo) Omit(cols ...field.Expr) *dnsDomainDo {
	return d.withDO(d.DO.Omit(cols...))
}

func (d dnsDomainDo) Join(table schema.Tabler, on ...field.Expr) *dnsDomainDo {
	return d.withDO(d.DO.Join(table, on...))
}

func (d dnsDomainDo) LeftJoin(table schema.Tabler, on ...field.Expr) *dnsDomainDo {
	return d.withDO(d.DO.LeftJoin(table, on...))
}

func (d dnsDomainDo) RightJoin(table schema.Tabler, on ...field.Expr) *dnsDomainDo {
	return d.withDO(d.DO.RightJoin(table, on...))
}

func (d dnsDomainDo) Group(cols ...field.Expr) *dnsDomainDo {
	return d.withDO(d.DO.Group(cols...))
}

func (d dnsDomainDo) Having(conds ...gen.Condition) *dnsDomainDo {
	return d.withDO(d.DO.Having(conds...))
}

func (d dnsDomainDo) Limit(limit int) *dnsDomainDo {
	return d.withDO(d.DO.Limit(limit))
}

func (d dnsDomainDo) Offset(offset int) *dnsDomainDo {
	return d.withDO(d.DO.Offset(offset))
}

func (d dnsDomainDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *dnsDomainDo {
	return d.withDO(d.DO.Scopes(funcs...))
}

func (d dnsDomainDo) Unscoped() *dnsDomainDo {
	return d.withDO(d.DO.Unscoped())
}

func (d dnsDomainDo) Create(values ...*model.DnsDomain) error {
	if len(values) == 0 {
		return nil
	}
	return d.DO.Create(values)
}

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

func (d dnsDomainDo) First() (*model.DnsDomain, error) {
	if result, err := d.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.DnsDomain), nil
	}
}

func (d dnsDomainDo) Take() (*model.DnsDomain, error) {
	if result, err := d.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.DnsDomain), nil
	}
}

func (d dnsDomainDo) Last() (*model.DnsDomain, error) {
	if result, err := d.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.DnsDomain), nil
	}
}

func (d dnsDomainDo) Find() ([]*model.DnsDomain, error) {
	result, err := d.DO.Find()
	return result.([]*model.DnsDomain), err
}

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

func (d dnsDomainDo) Attrs(attrs ...field.AssignExpr) *dnsDomainDo {
	return d.withDO(d.DO.Attrs(attrs...))
}

func (d dnsDomainDo) Assign(attrs ...field.AssignExpr) *dnsDomainDo {
	return d.withDO(d.DO.Assign(attrs...))
}

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

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

func (d dnsDomainDo) FirstOrInit() (*model.DnsDomain, error) {
	if result, err := d.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.DnsDomain), nil
	}
}

func (d dnsDomainDo) FirstOrCreate() (*model.DnsDomain, error) {
	if result, err := d.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.DnsDomain), nil
	}
}

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

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

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

func (d dnsDomainDo) Scan(result interface{}) (err error) {
	return d.DO.Scan(result)
}

func (d dnsDomainDo) Delete(models ...*model.DnsDomain) (result gen.ResultInfo, err error) {
	return d.DO.Delete(models)
}

func (d *dnsDomainDo) withDO(do gen.Dao) *dnsDomainDo {
	d.DO = *do.(*gen.DO)
	return d
}
