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

	_externalNotify.externalNotifyDo.UseDB(db, opts...)
	_externalNotify.externalNotifyDo.UseModel(&model.ExternalNotify{})

	tableName := _externalNotify.externalNotifyDo.TableName()
	_externalNotify.ALL = field.NewAsterisk(tableName)
	_externalNotify.ID = field.NewUint64(tableName, "id")
	_externalNotify.CreatedAt = field.NewTime(tableName, "created_at")
	_externalNotify.UpdatedAt = field.NewTime(tableName, "updated_at")
	_externalNotify.DeletedAt = field.NewField(tableName, "deleted_at")
	_externalNotify.Type = field.NewString(tableName, "type")
	_externalNotify.Language = field.NewString(tableName, "language")
	_externalNotify.Config = field.NewField(tableName, "config")
	_externalNotify.Enabled = field.NewBool(tableName, "enabled")

	_externalNotify.fillFieldMap()

	return _externalNotify
}

type externalNotify struct {
	externalNotifyDo

	ALL       field.Asterisk
	ID        field.Uint64
	CreatedAt field.Time
	UpdatedAt field.Time
	DeletedAt field.Field
	Type      field.String
	Language  field.String
	Config    field.Field
	Enabled   field.Bool

	fieldMap map[string]field.Expr
}

func (e externalNotify) Table(newTableName string) *externalNotify {
	e.externalNotifyDo.UseTable(newTableName)
	return e.updateTableName(newTableName)
}

func (e externalNotify) As(alias string) *externalNotify {
	e.externalNotifyDo.DO = *(e.externalNotifyDo.As(alias).(*gen.DO))
	return e.updateTableName(alias)
}

func (e *externalNotify) updateTableName(table string) *externalNotify {
	e.ALL = field.NewAsterisk(table)
	e.ID = field.NewUint64(table, "id")
	e.CreatedAt = field.NewTime(table, "created_at")
	e.UpdatedAt = field.NewTime(table, "updated_at")
	e.DeletedAt = field.NewField(table, "deleted_at")
	e.Type = field.NewString(table, "type")
	e.Language = field.NewString(table, "language")
	e.Config = field.NewField(table, "config")
	e.Enabled = field.NewBool(table, "enabled")

	e.fillFieldMap()

	return e
}

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

func (e *externalNotify) fillFieldMap() {
	e.fieldMap = make(map[string]field.Expr, 8)
	e.fieldMap["id"] = e.ID
	e.fieldMap["created_at"] = e.CreatedAt
	e.fieldMap["updated_at"] = e.UpdatedAt
	e.fieldMap["deleted_at"] = e.DeletedAt
	e.fieldMap["type"] = e.Type
	e.fieldMap["language"] = e.Language
	e.fieldMap["config"] = e.Config
	e.fieldMap["enabled"] = e.Enabled
}

func (e externalNotify) clone(db *gorm.DB) externalNotify {
	e.externalNotifyDo.ReplaceConnPool(db.Statement.ConnPool)
	return e
}

func (e externalNotify) replaceDB(db *gorm.DB) externalNotify {
	e.externalNotifyDo.ReplaceDB(db)
	return e
}

type externalNotifyDo struct{ gen.DO }

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

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

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

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

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

	return
}

func (e externalNotifyDo) Debug() *externalNotifyDo {
	return e.withDO(e.DO.Debug())
}

func (e externalNotifyDo) WithContext(ctx context.Context) *externalNotifyDo {
	return e.withDO(e.DO.WithContext(ctx))
}

func (e externalNotifyDo) ReadDB() *externalNotifyDo {
	return e.Clauses(dbresolver.Read)
}

func (e externalNotifyDo) WriteDB() *externalNotifyDo {
	return e.Clauses(dbresolver.Write)
}

func (e externalNotifyDo) Session(config *gorm.Session) *externalNotifyDo {
	return e.withDO(e.DO.Session(config))
}

func (e externalNotifyDo) Clauses(conds ...clause.Expression) *externalNotifyDo {
	return e.withDO(e.DO.Clauses(conds...))
}

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

func (e externalNotifyDo) Not(conds ...gen.Condition) *externalNotifyDo {
	return e.withDO(e.DO.Not(conds...))
}

func (e externalNotifyDo) Or(conds ...gen.Condition) *externalNotifyDo {
	return e.withDO(e.DO.Or(conds...))
}

func (e externalNotifyDo) Select(conds ...field.Expr) *externalNotifyDo {
	return e.withDO(e.DO.Select(conds...))
}

func (e externalNotifyDo) Where(conds ...gen.Condition) *externalNotifyDo {
	return e.withDO(e.DO.Where(conds...))
}

func (e externalNotifyDo) Order(conds ...field.Expr) *externalNotifyDo {
	return e.withDO(e.DO.Order(conds...))
}

func (e externalNotifyDo) Distinct(cols ...field.Expr) *externalNotifyDo {
	return e.withDO(e.DO.Distinct(cols...))
}

func (e externalNotifyDo) Omit(cols ...field.Expr) *externalNotifyDo {
	return e.withDO(e.DO.Omit(cols...))
}

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

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

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

func (e externalNotifyDo) Group(cols ...field.Expr) *externalNotifyDo {
	return e.withDO(e.DO.Group(cols...))
}

func (e externalNotifyDo) Having(conds ...gen.Condition) *externalNotifyDo {
	return e.withDO(e.DO.Having(conds...))
}

func (e externalNotifyDo) Limit(limit int) *externalNotifyDo {
	return e.withDO(e.DO.Limit(limit))
}

func (e externalNotifyDo) Offset(offset int) *externalNotifyDo {
	return e.withDO(e.DO.Offset(offset))
}

func (e externalNotifyDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *externalNotifyDo {
	return e.withDO(e.DO.Scopes(funcs...))
}

func (e externalNotifyDo) Unscoped() *externalNotifyDo {
	return e.withDO(e.DO.Unscoped())
}

func (e externalNotifyDo) Create(values ...*model.ExternalNotify) error {
	if len(values) == 0 {
		return nil
	}
	return e.DO.Create(values)
}

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

func (e externalNotifyDo) First() (*model.ExternalNotify, error) {
	if result, err := e.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.ExternalNotify), nil
	}
}

func (e externalNotifyDo) Take() (*model.ExternalNotify, error) {
	if result, err := e.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.ExternalNotify), nil
	}
}

func (e externalNotifyDo) Last() (*model.ExternalNotify, error) {
	if result, err := e.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.ExternalNotify), nil
	}
}

func (e externalNotifyDo) Find() ([]*model.ExternalNotify, error) {
	result, err := e.DO.Find()
	return result.([]*model.ExternalNotify), err
}

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

func (e externalNotifyDo) Attrs(attrs ...field.AssignExpr) *externalNotifyDo {
	return e.withDO(e.DO.Attrs(attrs...))
}

func (e externalNotifyDo) Assign(attrs ...field.AssignExpr) *externalNotifyDo {
	return e.withDO(e.DO.Assign(attrs...))
}

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

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

func (e externalNotifyDo) FirstOrInit() (*model.ExternalNotify, error) {
	if result, err := e.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.ExternalNotify), nil
	}
}

func (e externalNotifyDo) FirstOrCreate() (*model.ExternalNotify, error) {
	if result, err := e.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.ExternalNotify), nil
	}
}

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

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

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

func (e externalNotifyDo) Scan(result interface{}) (err error) {
	return e.DO.Scan(result)
}

func (e externalNotifyDo) Delete(models ...*model.ExternalNotify) (result gen.ResultInfo, err error) {
	return e.DO.Delete(models)
}

func (e *externalNotifyDo) withDO(do gen.Dao) *externalNotifyDo {
	e.DO = *do.(*gen.DO)
	return e
}
