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

	_acmeUser.acmeUserDo.UseDB(db, opts...)
	_acmeUser.acmeUserDo.UseModel(&model.AcmeUser{})

	tableName := _acmeUser.acmeUserDo.TableName()
	_acmeUser.ALL = field.NewAsterisk(tableName)
	_acmeUser.ID = field.NewInt(tableName, "id")
	_acmeUser.CreatedAt = field.NewTime(tableName, "created_at")
	_acmeUser.UpdatedAt = field.NewTime(tableName, "updated_at")
	_acmeUser.DeletedAt = field.NewField(tableName, "deleted_at")
	_acmeUser.Name = field.NewString(tableName, "name")
	_acmeUser.Email = field.NewString(tableName, "email")
	_acmeUser.CADir = field.NewString(tableName, "ca_dir")
	_acmeUser.Registration = field.NewField(tableName, "registration")
	_acmeUser.Key = field.NewField(tableName, "key")

	_acmeUser.fillFieldMap()

	return _acmeUser
}

type acmeUser struct {
	acmeUserDo

	ALL          field.Asterisk
	ID           field.Int
	CreatedAt    field.Time
	UpdatedAt    field.Time
	DeletedAt    field.Field
	Name         field.String
	Email        field.String
	CADir        field.String
	Registration field.Field
	Key          field.Field

	fieldMap map[string]field.Expr
}

func (a acmeUser) Table(newTableName string) *acmeUser {
	a.acmeUserDo.UseTable(newTableName)
	return a.updateTableName(newTableName)
}

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

func (a *acmeUser) updateTableName(table string) *acmeUser {
	a.ALL = field.NewAsterisk(table)
	a.ID = field.NewInt(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.Email = field.NewString(table, "email")
	a.CADir = field.NewString(table, "ca_dir")
	a.Registration = field.NewField(table, "registration")
	a.Key = field.NewField(table, "key")

	a.fillFieldMap()

	return a
}

func (a *acmeUser) 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 *acmeUser) fillFieldMap() {
	a.fieldMap = make(map[string]field.Expr, 9)
	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["email"] = a.Email
	a.fieldMap["ca_dir"] = a.CADir
	a.fieldMap["registration"] = a.Registration
	a.fieldMap["key"] = a.Key
}

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

func (a acmeUser) replaceDB(db *gorm.DB) acmeUser {
	a.acmeUserDo.ReplaceDB(db)
	return a
}

type acmeUserDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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