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

	_namespace.namespaceDo.UseDB(db, opts...)
	_namespace.namespaceDo.UseModel(&model.Namespace{})

	tableName := _namespace.namespaceDo.TableName()
	_namespace.ALL = field.NewAsterisk(tableName)
	_namespace.ID = field.NewUint64(tableName, "id")
	_namespace.CreatedAt = field.NewTime(tableName, "created_at")
	_namespace.UpdatedAt = field.NewTime(tableName, "updated_at")
	_namespace.DeletedAt = field.NewField(tableName, "deleted_at")
	_namespace.Name = field.NewString(tableName, "name")
	_namespace.SyncNodeIds = field.NewField(tableName, "sync_node_ids")
	_namespace.OrderID = field.NewInt(tableName, "order_id")
	_namespace.PostSyncAction = field.NewString(tableName, "post_sync_action")
	_namespace.UpstreamTestType = field.NewString(tableName, "upstream_test_type")
	_namespace.DeployMode = field.NewString(tableName, "deploy_mode")

	_namespace.fillFieldMap()

	return _namespace
}

type namespace struct {
	namespaceDo

	ALL              field.Asterisk
	ID               field.Uint64
	CreatedAt        field.Time
	UpdatedAt        field.Time
	DeletedAt        field.Field
	Name             field.String
	SyncNodeIds      field.Field
	OrderID          field.Int
	PostSyncAction   field.String
	UpstreamTestType field.String
	DeployMode       field.String

	fieldMap map[string]field.Expr
}

func (n namespace) Table(newTableName string) *namespace {
	n.namespaceDo.UseTable(newTableName)
	return n.updateTableName(newTableName)
}

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

func (n *namespace) updateTableName(table string) *namespace {
	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.SyncNodeIds = field.NewField(table, "sync_node_ids")
	n.OrderID = field.NewInt(table, "order_id")
	n.PostSyncAction = field.NewString(table, "post_sync_action")
	n.UpstreamTestType = field.NewString(table, "upstream_test_type")
	n.DeployMode = field.NewString(table, "deploy_mode")

	n.fillFieldMap()

	return n
}

func (n *namespace) 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 *namespace) fillFieldMap() {
	n.fieldMap = make(map[string]field.Expr, 10)
	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["sync_node_ids"] = n.SyncNodeIds
	n.fieldMap["order_id"] = n.OrderID
	n.fieldMap["post_sync_action"] = n.PostSyncAction
	n.fieldMap["upstream_test_type"] = n.UpstreamTestType
	n.fieldMap["deploy_mode"] = n.DeployMode
}

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

func (n namespace) replaceDB(db *gorm.DB) namespace {
	n.namespaceDo.ReplaceDB(db)
	return n
}

type namespaceDo struct{ gen.DO }

// FirstByID Where("id=@id")
func (n namespaceDo) FirstByID(id uint64) (result *model.Namespace, 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 namespaceDo) DeleteByID(id uint64) (err error) {
	var params []interface{}

	var generateSQL strings.Builder
	params = append(params, id)
	generateSQL.WriteString("update namespaces 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 namespaceDo) Debug() *namespaceDo {
	return n.withDO(n.DO.Debug())
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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