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

	_node.nodeDo.UseDB(db, opts...)
	_node.nodeDo.UseModel(&model.Node{})

	tableName := _node.nodeDo.TableName()
	_node.ALL = field.NewAsterisk(tableName)
	_node.ID = field.NewUint64(tableName, "id")
	_node.CreatedAt = field.NewTime(tableName, "created_at")
	_node.UpdatedAt = field.NewTime(tableName, "updated_at")
	_node.DeletedAt = field.NewField(tableName, "deleted_at")
	_node.Name = field.NewString(tableName, "name")
	_node.URL = field.NewString(tableName, "url")
	_node.Token = field.NewString(tableName, "token")
	_node.Enabled = field.NewBool(tableName, "enabled")

	_node.fillFieldMap()

	return _node
}

type node struct {
	nodeDo

	ALL       field.Asterisk
	ID        field.Uint64
	CreatedAt field.Time
	UpdatedAt field.Time
	DeletedAt field.Field
	Name      field.String
	URL       field.String
	Token     field.String
	Enabled   field.Bool

	fieldMap map[string]field.Expr
}

func (n node) Table(newTableName string) *node {
	n.nodeDo.UseTable(newTableName)
	return n.updateTableName(newTableName)
}

func (n node) As(alias string) *node {
	n.nodeDo.DO = *(n.nodeDo.As(alias).(*gen.DO))
	return n.updateTableName(alias)
}

func (n *node) updateTableName(table string) *node {
	n.ALL = field.NewAsterisk(table)
	n.ID = field.NewUint64(table, "id")
	n.CreatedAt = field.NewTime(table, "created_at")
	n.UpdatedAt = field.NewTime(table, "updated_at")
	n.DeletedAt = field.NewField(table, "deleted_at")
	n.Name = field.NewString(table, "name")
	n.URL = field.NewString(table, "url")
	n.Token = field.NewString(table, "token")
	n.Enabled = field.NewBool(table, "enabled")

	n.fillFieldMap()

	return n
}

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

func (n *node) fillFieldMap() {
	n.fieldMap = make(map[string]field.Expr, 8)
	n.fieldMap["id"] = n.ID
	n.fieldMap["created_at"] = n.CreatedAt
	n.fieldMap["updated_at"] = n.UpdatedAt
	n.fieldMap["deleted_at"] = n.DeletedAt
	n.fieldMap["name"] = n.Name
	n.fieldMap["url"] = n.URL
	n.fieldMap["token"] = n.Token
	n.fieldMap["enabled"] = n.Enabled
}

func (n node) clone(db *gorm.DB) node {
	n.nodeDo.ReplaceConnPool(db.Statement.ConnPool)
	return n
}

func (n node) replaceDB(db *gorm.DB) node {
	n.nodeDo.ReplaceDB(db)
	return n
}

type nodeDo struct{ gen.DO }

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

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

	var executeSQL *gorm.DB
	executeSQL = n.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 (n nodeDo) DeleteByID(id uint64) (err error) {
	var params []interface{}

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

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

	return
}

func (n nodeDo) Debug() *nodeDo {
	return n.withDO(n.DO.Debug())
}

func (n nodeDo) WithContext(ctx context.Context) *nodeDo {
	return n.withDO(n.DO.WithContext(ctx))
}

func (n nodeDo) ReadDB() *nodeDo {
	return n.Clauses(dbresolver.Read)
}

func (n nodeDo) WriteDB() *nodeDo {
	return n.Clauses(dbresolver.Write)
}

func (n nodeDo) Session(config *gorm.Session) *nodeDo {
	return n.withDO(n.DO.Session(config))
}

func (n nodeDo) Clauses(conds ...clause.Expression) *nodeDo {
	return n.withDO(n.DO.Clauses(conds...))
}

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

func (n nodeDo) Not(conds ...gen.Condition) *nodeDo {
	return n.withDO(n.DO.Not(conds...))
}

func (n nodeDo) Or(conds ...gen.Condition) *nodeDo {
	return n.withDO(n.DO.Or(conds...))
}

func (n nodeDo) Select(conds ...field.Expr) *nodeDo {
	return n.withDO(n.DO.Select(conds...))
}

func (n nodeDo) Where(conds ...gen.Condition) *nodeDo {
	return n.withDO(n.DO.Where(conds...))
}

func (n nodeDo) Order(conds ...field.Expr) *nodeDo {
	return n.withDO(n.DO.Order(conds...))
}

func (n nodeDo) Distinct(cols ...field.Expr) *nodeDo {
	return n.withDO(n.DO.Distinct(cols...))
}

func (n nodeDo) Omit(cols ...field.Expr) *nodeDo {
	return n.withDO(n.DO.Omit(cols...))
}

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

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

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

func (n nodeDo) Group(cols ...field.Expr) *nodeDo {
	return n.withDO(n.DO.Group(cols...))
}

func (n nodeDo) Having(conds ...gen.Condition) *nodeDo {
	return n.withDO(n.DO.Having(conds...))
}

func (n nodeDo) Limit(limit int) *nodeDo {
	return n.withDO(n.DO.Limit(limit))
}

func (n nodeDo) Offset(offset int) *nodeDo {
	return n.withDO(n.DO.Offset(offset))
}

func (n nodeDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *nodeDo {
	return n.withDO(n.DO.Scopes(funcs...))
}

func (n nodeDo) Unscoped() *nodeDo {
	return n.withDO(n.DO.Unscoped())
}

func (n nodeDo) Create(values ...*model.Node) error {
	if len(values) == 0 {
		return nil
	}
	return n.DO.Create(values)
}

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

func (n nodeDo) First() (*model.Node, error) {
	if result, err := n.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Node), nil
	}
}

func (n nodeDo) Take() (*model.Node, error) {
	if result, err := n.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Node), nil
	}
}

func (n nodeDo) Last() (*model.Node, error) {
	if result, err := n.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Node), nil
	}
}

func (n nodeDo) Find() ([]*model.Node, error) {
	result, err := n.DO.Find()
	return result.([]*model.Node), err
}

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

func (n nodeDo) Attrs(attrs ...field.AssignExpr) *nodeDo {
	return n.withDO(n.DO.Attrs(attrs...))
}

func (n nodeDo) Assign(attrs ...field.AssignExpr) *nodeDo {
	return n.withDO(n.DO.Assign(attrs...))
}

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

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

func (n nodeDo) FirstOrInit() (*model.Node, error) {
	if result, err := n.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Node), nil
	}
}

func (n nodeDo) FirstOrCreate() (*model.Node, error) {
	if result, err := n.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Node), nil
	}
}

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

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

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

func (n nodeDo) Scan(result interface{}) (err error) {
	return n.DO.Scan(result)
}

func (n nodeDo) Delete(models ...*model.Node) (result gen.ResultInfo, err error) {
	return n.DO.Delete(models)
}

func (n *nodeDo) withDO(do gen.Dao) *nodeDo {
	n.DO = *do.(*gen.DO)
	return n
}
