// 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"

	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"gorm.io/gorm/schema"

	"gorm.io/gen"
	"gorm.io/gen/field"

	"gorm.io/plugin/dbresolver"

	"gadmin/internal/gorm/model"
)

func newCache(db *gorm.DB, opts ...gen.DOOption) cache {
	_cache := cache{}

	_cache.cacheDo.UseDB(db, opts...)
	_cache.cacheDo.UseModel(&model.Cache{})

	tableName := _cache.cacheDo.TableName()
	_cache.ALL = field.NewAsterisk(tableName)
	_cache.ID = field.NewInt64(tableName, "id")
	_cache.Key = field.NewString(tableName, "key")
	_cache.Value = field.NewString(tableName, "value")
	_cache.ExpiAt = field.NewInt64(tableName, "expi_at")
	_cache.CreatedAt = field.NewTime(tableName, "created_at")

	_cache.fillFieldMap()

	return _cache
}

type cache struct {
	cacheDo

	ALL       field.Asterisk
	ID        field.Int64
	Key       field.String
	Value     field.String
	ExpiAt    field.Int64 // 过期时间
	CreatedAt field.Time  // 创建时间

	fieldMap map[string]field.Expr
}

func (c cache) Table(newTableName string) *cache {
	c.cacheDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

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

func (c *cache) updateTableName(table string) *cache {
	c.ALL = field.NewAsterisk(table)
	c.ID = field.NewInt64(table, "id")
	c.Key = field.NewString(table, "key")
	c.Value = field.NewString(table, "value")
	c.ExpiAt = field.NewInt64(table, "expi_at")
	c.CreatedAt = field.NewTime(table, "created_at")

	c.fillFieldMap()

	return c
}

func (c *cache) 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 *cache) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 5)
	c.fieldMap["id"] = c.ID
	c.fieldMap["key"] = c.Key
	c.fieldMap["value"] = c.Value
	c.fieldMap["expi_at"] = c.ExpiAt
	c.fieldMap["created_at"] = c.CreatedAt
}

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

func (c cache) replaceDB(db *gorm.DB) cache {
	c.cacheDo.ReplaceDB(db)
	return c
}

type cacheDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

func (c cacheDo) Exists(subquery interface{ UnderlyingDB() *gorm.DB }) *cacheDo {
	return c.Where(field.CompareSubQuery(field.ExistsOp, nil, subquery.UnderlyingDB()))
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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