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

	_authToken.authTokenDo.UseDB(db, opts...)
	_authToken.authTokenDo.UseModel(&model.AuthToken{})

	tableName := _authToken.authTokenDo.TableName()
	_authToken.ALL = field.NewAsterisk(tableName)
	_authToken.UserID = field.NewInt(tableName, "user_id")
	_authToken.Token = field.NewString(tableName, "token")
	_authToken.ExpiredAt = field.NewInt64(tableName, "expired_at")

	_authToken.fillFieldMap()

	return _authToken
}

type authToken struct {
	authTokenDo

	ALL       field.Asterisk
	UserID    field.Int
	Token     field.String
	ExpiredAt field.Int64

	fieldMap map[string]field.Expr
}

func (a authToken) Table(newTableName string) *authToken {
	a.authTokenDo.UseTable(newTableName)
	return a.updateTableName(newTableName)
}

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

func (a *authToken) updateTableName(table string) *authToken {
	a.ALL = field.NewAsterisk(table)
	a.UserID = field.NewInt(table, "user_id")
	a.Token = field.NewString(table, "token")
	a.ExpiredAt = field.NewInt64(table, "expired_at")

	a.fillFieldMap()

	return a
}

func (a *authToken) 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 *authToken) fillFieldMap() {
	a.fieldMap = make(map[string]field.Expr, 3)
	a.fieldMap["user_id"] = a.UserID
	a.fieldMap["token"] = a.Token
	a.fieldMap["expired_at"] = a.ExpiredAt
}

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

func (a authToken) replaceDB(db *gorm.DB) authToken {
	a.authTokenDo.ReplaceDB(db)
	return a
}

type authTokenDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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