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

	_autoBackup.autoBackupDo.UseDB(db, opts...)
	_autoBackup.autoBackupDo.UseModel(&model.AutoBackup{})

	tableName := _autoBackup.autoBackupDo.TableName()
	_autoBackup.ALL = field.NewAsterisk(tableName)
	_autoBackup.ID = field.NewUint64(tableName, "id")
	_autoBackup.CreatedAt = field.NewTime(tableName, "created_at")
	_autoBackup.UpdatedAt = field.NewTime(tableName, "updated_at")
	_autoBackup.DeletedAt = field.NewField(tableName, "deleted_at")
	_autoBackup.Name = field.NewString(tableName, "name")
	_autoBackup.BackupType = field.NewString(tableName, "backup_type")
	_autoBackup.StorageType = field.NewString(tableName, "storage_type")
	_autoBackup.BackupPath = field.NewString(tableName, "backup_path")
	_autoBackup.StoragePath = field.NewString(tableName, "storage_path")
	_autoBackup.CronExpression = field.NewString(tableName, "cron_expression")
	_autoBackup.Enabled = field.NewBool(tableName, "enabled")
	_autoBackup.LastBackupTime = field.NewTime(tableName, "last_backup_time")
	_autoBackup.LastBackupStatus = field.NewString(tableName, "last_backup_status")
	_autoBackup.LastBackupError = field.NewString(tableName, "last_backup_error")
	_autoBackup.S3Endpoint = field.NewString(tableName, "s3_endpoint")
	_autoBackup.S3AccessKeyID = field.NewString(tableName, "s3_access_key_id")
	_autoBackup.S3SecretAccessKey = field.NewString(tableName, "s3_secret_access_key")
	_autoBackup.S3Bucket = field.NewString(tableName, "s3_bucket")
	_autoBackup.S3Region = field.NewString(tableName, "s3_region")

	_autoBackup.fillFieldMap()

	return _autoBackup
}

type autoBackup struct {
	autoBackupDo

	ALL               field.Asterisk
	ID                field.Uint64
	CreatedAt         field.Time
	UpdatedAt         field.Time
	DeletedAt         field.Field
	Name              field.String // Backup task name
	BackupType        field.String // Type of backup
	StorageType       field.String // Storage type (local/s3)
	BackupPath        field.String // Custom directory path for backup
	StoragePath       field.String // Storage destination path
	CronExpression    field.String // Cron expression for scheduling
	Enabled           field.Bool   // Whether the backup task is enabled
	LastBackupTime    field.Time   // Last backup execution time
	LastBackupStatus  field.String // Status of last backup
	LastBackupError   field.String // Error message from last backup if failed
	S3Endpoint        field.String // S3 endpoint URL
	S3AccessKeyID     field.String // S3 access key ID
	S3SecretAccessKey field.String // S3 secret access key
	S3Bucket          field.String // S3 bucket name
	S3Region          field.String // S3 region

	fieldMap map[string]field.Expr
}

func (a autoBackup) Table(newTableName string) *autoBackup {
	a.autoBackupDo.UseTable(newTableName)
	return a.updateTableName(newTableName)
}

func (a autoBackup) As(alias string) *autoBackup {
	a.autoBackupDo.DO = *(a.autoBackupDo.As(alias).(*gen.DO))
	return a.updateTableName(alias)
}

func (a *autoBackup) updateTableName(table string) *autoBackup {
	a.ALL = field.NewAsterisk(table)
	a.ID = field.NewUint64(table, "id")
	a.CreatedAt = field.NewTime(table, "created_at")
	a.UpdatedAt = field.NewTime(table, "updated_at")
	a.DeletedAt = field.NewField(table, "deleted_at")
	a.Name = field.NewString(table, "name")
	a.BackupType = field.NewString(table, "backup_type")
	a.StorageType = field.NewString(table, "storage_type")
	a.BackupPath = field.NewString(table, "backup_path")
	a.StoragePath = field.NewString(table, "storage_path")
	a.CronExpression = field.NewString(table, "cron_expression")
	a.Enabled = field.NewBool(table, "enabled")
	a.LastBackupTime = field.NewTime(table, "last_backup_time")
	a.LastBackupStatus = field.NewString(table, "last_backup_status")
	a.LastBackupError = field.NewString(table, "last_backup_error")
	a.S3Endpoint = field.NewString(table, "s3_endpoint")
	a.S3AccessKeyID = field.NewString(table, "s3_access_key_id")
	a.S3SecretAccessKey = field.NewString(table, "s3_secret_access_key")
	a.S3Bucket = field.NewString(table, "s3_bucket")
	a.S3Region = field.NewString(table, "s3_region")

	a.fillFieldMap()

	return a
}

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

func (a *autoBackup) fillFieldMap() {
	a.fieldMap = make(map[string]field.Expr, 19)
	a.fieldMap["id"] = a.ID
	a.fieldMap["created_at"] = a.CreatedAt
	a.fieldMap["updated_at"] = a.UpdatedAt
	a.fieldMap["deleted_at"] = a.DeletedAt
	a.fieldMap["name"] = a.Name
	a.fieldMap["backup_type"] = a.BackupType
	a.fieldMap["storage_type"] = a.StorageType
	a.fieldMap["backup_path"] = a.BackupPath
	a.fieldMap["storage_path"] = a.StoragePath
	a.fieldMap["cron_expression"] = a.CronExpression
	a.fieldMap["enabled"] = a.Enabled
	a.fieldMap["last_backup_time"] = a.LastBackupTime
	a.fieldMap["last_backup_status"] = a.LastBackupStatus
	a.fieldMap["last_backup_error"] = a.LastBackupError
	a.fieldMap["s3_endpoint"] = a.S3Endpoint
	a.fieldMap["s3_access_key_id"] = a.S3AccessKeyID
	a.fieldMap["s3_secret_access_key"] = a.S3SecretAccessKey
	a.fieldMap["s3_bucket"] = a.S3Bucket
	a.fieldMap["s3_region"] = a.S3Region
}

func (a autoBackup) clone(db *gorm.DB) autoBackup {
	a.autoBackupDo.ReplaceConnPool(db.Statement.ConnPool)
	return a
}

func (a autoBackup) replaceDB(db *gorm.DB) autoBackup {
	a.autoBackupDo.ReplaceDB(db)
	return a
}

type autoBackupDo struct{ gen.DO }

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

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

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

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

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

	return
}

func (a autoBackupDo) Debug() *autoBackupDo {
	return a.withDO(a.DO.Debug())
}

func (a autoBackupDo) WithContext(ctx context.Context) *autoBackupDo {
	return a.withDO(a.DO.WithContext(ctx))
}

func (a autoBackupDo) ReadDB() *autoBackupDo {
	return a.Clauses(dbresolver.Read)
}

func (a autoBackupDo) WriteDB() *autoBackupDo {
	return a.Clauses(dbresolver.Write)
}

func (a autoBackupDo) Session(config *gorm.Session) *autoBackupDo {
	return a.withDO(a.DO.Session(config))
}

func (a autoBackupDo) Clauses(conds ...clause.Expression) *autoBackupDo {
	return a.withDO(a.DO.Clauses(conds...))
}

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

func (a autoBackupDo) Not(conds ...gen.Condition) *autoBackupDo {
	return a.withDO(a.DO.Not(conds...))
}

func (a autoBackupDo) Or(conds ...gen.Condition) *autoBackupDo {
	return a.withDO(a.DO.Or(conds...))
}

func (a autoBackupDo) Select(conds ...field.Expr) *autoBackupDo {
	return a.withDO(a.DO.Select(conds...))
}

func (a autoBackupDo) Where(conds ...gen.Condition) *autoBackupDo {
	return a.withDO(a.DO.Where(conds...))
}

func (a autoBackupDo) Order(conds ...field.Expr) *autoBackupDo {
	return a.withDO(a.DO.Order(conds...))
}

func (a autoBackupDo) Distinct(cols ...field.Expr) *autoBackupDo {
	return a.withDO(a.DO.Distinct(cols...))
}

func (a autoBackupDo) Omit(cols ...field.Expr) *autoBackupDo {
	return a.withDO(a.DO.Omit(cols...))
}

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

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

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

func (a autoBackupDo) Group(cols ...field.Expr) *autoBackupDo {
	return a.withDO(a.DO.Group(cols...))
}

func (a autoBackupDo) Having(conds ...gen.Condition) *autoBackupDo {
	return a.withDO(a.DO.Having(conds...))
}

func (a autoBackupDo) Limit(limit int) *autoBackupDo {
	return a.withDO(a.DO.Limit(limit))
}

func (a autoBackupDo) Offset(offset int) *autoBackupDo {
	return a.withDO(a.DO.Offset(offset))
}

func (a autoBackupDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *autoBackupDo {
	return a.withDO(a.DO.Scopes(funcs...))
}

func (a autoBackupDo) Unscoped() *autoBackupDo {
	return a.withDO(a.DO.Unscoped())
}

func (a autoBackupDo) Create(values ...*model.AutoBackup) error {
	if len(values) == 0 {
		return nil
	}
	return a.DO.Create(values)
}

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

func (a autoBackupDo) First() (*model.AutoBackup, error) {
	if result, err := a.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.AutoBackup), nil
	}
}

func (a autoBackupDo) Take() (*model.AutoBackup, error) {
	if result, err := a.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.AutoBackup), nil
	}
}

func (a autoBackupDo) Last() (*model.AutoBackup, error) {
	if result, err := a.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.AutoBackup), nil
	}
}

func (a autoBackupDo) Find() ([]*model.AutoBackup, error) {
	result, err := a.DO.Find()
	return result.([]*model.AutoBackup), err
}

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

func (a autoBackupDo) Attrs(attrs ...field.AssignExpr) *autoBackupDo {
	return a.withDO(a.DO.Attrs(attrs...))
}

func (a autoBackupDo) Assign(attrs ...field.AssignExpr) *autoBackupDo {
	return a.withDO(a.DO.Assign(attrs...))
}

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

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

func (a autoBackupDo) FirstOrInit() (*model.AutoBackup, error) {
	if result, err := a.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.AutoBackup), nil
	}
}

func (a autoBackupDo) FirstOrCreate() (*model.AutoBackup, error) {
	if result, err := a.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.AutoBackup), nil
	}
}

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

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

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

func (a autoBackupDo) Scan(result interface{}) (err error) {
	return a.DO.Scan(result)
}

func (a autoBackupDo) Delete(models ...*model.AutoBackup) (result gen.ResultInfo, err error) {
	return a.DO.Delete(models)
}

func (a *autoBackupDo) withDO(do gen.Dao) *autoBackupDo {
	a.DO = *do.(*gen.DO)
	return a
}
