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

	_configBackup.configBackupDo.UseDB(db, opts...)
	_configBackup.configBackupDo.UseModel(&model.ConfigBackup{})

	tableName := _configBackup.configBackupDo.TableName()
	_configBackup.ALL = field.NewAsterisk(tableName)
	_configBackup.ID = field.NewUint64(tableName, "id")
	_configBackup.CreatedAt = field.NewTime(tableName, "created_at")
	_configBackup.UpdatedAt = field.NewTime(tableName, "updated_at")
	_configBackup.DeletedAt = field.NewField(tableName, "deleted_at")
	_configBackup.Name = field.NewString(tableName, "name")
	_configBackup.FilePath = field.NewString(tableName, "filepath")
	_configBackup.Content = field.NewString(tableName, "content")

	_configBackup.fillFieldMap()

	return _configBackup
}

type configBackup struct {
	configBackupDo

	ALL       field.Asterisk
	ID        field.Uint64
	CreatedAt field.Time
	UpdatedAt field.Time
	DeletedAt field.Field
	Name      field.String
	FilePath  field.String
	Content   field.String

	fieldMap map[string]field.Expr
}

func (c configBackup) Table(newTableName string) *configBackup {
	c.configBackupDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

func (c configBackup) As(alias string) *configBackup {
	c.configBackupDo.DO = *(c.configBackupDo.As(alias).(*gen.DO))
	return c.updateTableName(alias)
}

func (c *configBackup) updateTableName(table string) *configBackup {
	c.ALL = field.NewAsterisk(table)
	c.ID = field.NewUint64(table, "id")
	c.CreatedAt = field.NewTime(table, "created_at")
	c.UpdatedAt = field.NewTime(table, "updated_at")
	c.DeletedAt = field.NewField(table, "deleted_at")
	c.Name = field.NewString(table, "name")
	c.FilePath = field.NewString(table, "filepath")
	c.Content = field.NewString(table, "content")

	c.fillFieldMap()

	return c
}

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

func (c *configBackup) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 7)
	c.fieldMap["id"] = c.ID
	c.fieldMap["created_at"] = c.CreatedAt
	c.fieldMap["updated_at"] = c.UpdatedAt
	c.fieldMap["deleted_at"] = c.DeletedAt
	c.fieldMap["name"] = c.Name
	c.fieldMap["filepath"] = c.FilePath
	c.fieldMap["content"] = c.Content
}

func (c configBackup) clone(db *gorm.DB) configBackup {
	c.configBackupDo.ReplaceConnPool(db.Statement.ConnPool)
	return c
}

func (c configBackup) replaceDB(db *gorm.DB) configBackup {
	c.configBackupDo.ReplaceDB(db)
	return c
}

type configBackupDo struct{ gen.DO }

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

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

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

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

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

	return
}

func (c configBackupDo) Debug() *configBackupDo {
	return c.withDO(c.DO.Debug())
}

func (c configBackupDo) WithContext(ctx context.Context) *configBackupDo {
	return c.withDO(c.DO.WithContext(ctx))
}

func (c configBackupDo) ReadDB() *configBackupDo {
	return c.Clauses(dbresolver.Read)
}

func (c configBackupDo) WriteDB() *configBackupDo {
	return c.Clauses(dbresolver.Write)
}

func (c configBackupDo) Session(config *gorm.Session) *configBackupDo {
	return c.withDO(c.DO.Session(config))
}

func (c configBackupDo) Clauses(conds ...clause.Expression) *configBackupDo {
	return c.withDO(c.DO.Clauses(conds...))
}

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

func (c configBackupDo) Not(conds ...gen.Condition) *configBackupDo {
	return c.withDO(c.DO.Not(conds...))
}

func (c configBackupDo) Or(conds ...gen.Condition) *configBackupDo {
	return c.withDO(c.DO.Or(conds...))
}

func (c configBackupDo) Select(conds ...field.Expr) *configBackupDo {
	return c.withDO(c.DO.Select(conds...))
}

func (c configBackupDo) Where(conds ...gen.Condition) *configBackupDo {
	return c.withDO(c.DO.Where(conds...))
}

func (c configBackupDo) Order(conds ...field.Expr) *configBackupDo {
	return c.withDO(c.DO.Order(conds...))
}

func (c configBackupDo) Distinct(cols ...field.Expr) *configBackupDo {
	return c.withDO(c.DO.Distinct(cols...))
}

func (c configBackupDo) Omit(cols ...field.Expr) *configBackupDo {
	return c.withDO(c.DO.Omit(cols...))
}

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

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

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

func (c configBackupDo) Group(cols ...field.Expr) *configBackupDo {
	return c.withDO(c.DO.Group(cols...))
}

func (c configBackupDo) Having(conds ...gen.Condition) *configBackupDo {
	return c.withDO(c.DO.Having(conds...))
}

func (c configBackupDo) Limit(limit int) *configBackupDo {
	return c.withDO(c.DO.Limit(limit))
}

func (c configBackupDo) Offset(offset int) *configBackupDo {
	return c.withDO(c.DO.Offset(offset))
}

func (c configBackupDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *configBackupDo {
	return c.withDO(c.DO.Scopes(funcs...))
}

func (c configBackupDo) Unscoped() *configBackupDo {
	return c.withDO(c.DO.Unscoped())
}

func (c configBackupDo) Create(values ...*model.ConfigBackup) error {
	if len(values) == 0 {
		return nil
	}
	return c.DO.Create(values)
}

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

func (c configBackupDo) First() (*model.ConfigBackup, error) {
	if result, err := c.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.ConfigBackup), nil
	}
}

func (c configBackupDo) Take() (*model.ConfigBackup, error) {
	if result, err := c.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.ConfigBackup), nil
	}
}

func (c configBackupDo) Last() (*model.ConfigBackup, error) {
	if result, err := c.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.ConfigBackup), nil
	}
}

func (c configBackupDo) Find() ([]*model.ConfigBackup, error) {
	result, err := c.DO.Find()
	return result.([]*model.ConfigBackup), err
}

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

func (c configBackupDo) Attrs(attrs ...field.AssignExpr) *configBackupDo {
	return c.withDO(c.DO.Attrs(attrs...))
}

func (c configBackupDo) Assign(attrs ...field.AssignExpr) *configBackupDo {
	return c.withDO(c.DO.Assign(attrs...))
}

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

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

func (c configBackupDo) FirstOrInit() (*model.ConfigBackup, error) {
	if result, err := c.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.ConfigBackup), nil
	}
}

func (c configBackupDo) FirstOrCreate() (*model.ConfigBackup, error) {
	if result, err := c.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.ConfigBackup), nil
	}
}

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

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

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

func (c configBackupDo) Scan(result interface{}) (err error) {
	return c.DO.Scan(result)
}

func (c configBackupDo) Delete(models ...*model.ConfigBackup) (result gen.ResultInfo, err error) {
	return c.DO.Delete(models)
}

func (c *configBackupDo) withDO(do gen.Dao) *configBackupDo {
	c.DO = *do.(*gen.DO)
	return c
}
