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

import (
	"context"

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

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

	"gorm.io/plugin/dbresolver"

	"github.com/bitmagnet-io/bitmagnet/internal/model"
)

func newKeyValue(db *gorm.DB, opts ...gen.DOOption) keyValue {
	_keyValue := keyValue{}

	_keyValue.keyValueDo.UseDB(db, opts...)
	_keyValue.keyValueDo.UseModel(&model.KeyValue{})

	tableName := _keyValue.keyValueDo.TableName()
	_keyValue.ALL = field.NewAsterisk(tableName)
	_keyValue.Key = field.NewString(tableName, "key")
	_keyValue.Value = field.NewString(tableName, "value")
	_keyValue.CreatedAt = field.NewTime(tableName, "created_at")
	_keyValue.UpdatedAt = field.NewTime(tableName, "updated_at")

	_keyValue.fillFieldMap()

	return _keyValue
}

type keyValue struct {
	keyValueDo

	ALL       field.Asterisk
	Key       field.String
	Value     field.String
	CreatedAt field.Time
	UpdatedAt field.Time

	fieldMap map[string]field.Expr
}

func (k keyValue) Table(newTableName string) *keyValue {
	k.keyValueDo.UseTable(newTableName)
	return k.updateTableName(newTableName)
}

func (k keyValue) As(alias string) *keyValue {
	k.keyValueDo.DO = *(k.keyValueDo.As(alias).(*gen.DO))
	return k.updateTableName(alias)
}

func (k *keyValue) updateTableName(table string) *keyValue {
	k.ALL = field.NewAsterisk(table)
	k.Key = field.NewString(table, "key")
	k.Value = field.NewString(table, "value")
	k.CreatedAt = field.NewTime(table, "created_at")
	k.UpdatedAt = field.NewTime(table, "updated_at")

	k.fillFieldMap()

	return k
}

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

func (k *keyValue) fillFieldMap() {
	k.fieldMap = make(map[string]field.Expr, 4)
	k.fieldMap["key"] = k.Key
	k.fieldMap["value"] = k.Value
	k.fieldMap["created_at"] = k.CreatedAt
	k.fieldMap["updated_at"] = k.UpdatedAt
}

func (k keyValue) clone(db *gorm.DB) keyValue {
	k.keyValueDo.ReplaceConnPool(db.Statement.ConnPool)
	return k
}

func (k keyValue) replaceDB(db *gorm.DB) keyValue {
	k.keyValueDo.ReplaceDB(db)
	return k
}

type keyValueDo struct{ gen.DO }

type IKeyValueDo interface {
	gen.SubQuery
	Debug() IKeyValueDo
	WithContext(ctx context.Context) IKeyValueDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() IKeyValueDo
	WriteDB() IKeyValueDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) IKeyValueDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) IKeyValueDo
	Not(conds ...gen.Condition) IKeyValueDo
	Or(conds ...gen.Condition) IKeyValueDo
	Select(conds ...field.Expr) IKeyValueDo
	Where(conds ...gen.Condition) IKeyValueDo
	Order(conds ...field.Expr) IKeyValueDo
	Distinct(cols ...field.Expr) IKeyValueDo
	Omit(cols ...field.Expr) IKeyValueDo
	Join(table schema.Tabler, on ...field.Expr) IKeyValueDo
	LeftJoin(table schema.Tabler, on ...field.Expr) IKeyValueDo
	RightJoin(table schema.Tabler, on ...field.Expr) IKeyValueDo
	Group(cols ...field.Expr) IKeyValueDo
	Having(conds ...gen.Condition) IKeyValueDo
	Limit(limit int) IKeyValueDo
	Offset(offset int) IKeyValueDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) IKeyValueDo
	Unscoped() IKeyValueDo
	Create(values ...*model.KeyValue) error
	CreateInBatches(values []*model.KeyValue, batchSize int) error
	Save(values ...*model.KeyValue) error
	First() (*model.KeyValue, error)
	Take() (*model.KeyValue, error)
	Last() (*model.KeyValue, error)
	Find() ([]*model.KeyValue, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.KeyValue, err error)
	FindInBatches(result *[]*model.KeyValue, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.KeyValue) (info gen.ResultInfo, err error)
	Update(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	Updates(value interface{}) (info gen.ResultInfo, err error)
	UpdateColumn(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateColumnSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	UpdateColumns(value interface{}) (info gen.ResultInfo, err error)
	UpdateFrom(q gen.SubQuery) gen.Dao
	Attrs(attrs ...field.AssignExpr) IKeyValueDo
	Assign(attrs ...field.AssignExpr) IKeyValueDo
	Joins(fields ...field.RelationField) IKeyValueDo
	Preload(fields ...field.RelationField) IKeyValueDo
	FirstOrInit() (*model.KeyValue, error)
	FirstOrCreate() (*model.KeyValue, error)
	FindByPage(offset int, limit int) (result []*model.KeyValue, count int64, err error)
	ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
	Scan(result interface{}) (err error)
	Returning(value interface{}, columns ...string) IKeyValueDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (k keyValueDo) Debug() IKeyValueDo {
	return k.withDO(k.DO.Debug())
}

func (k keyValueDo) WithContext(ctx context.Context) IKeyValueDo {
	return k.withDO(k.DO.WithContext(ctx))
}

func (k keyValueDo) ReadDB() IKeyValueDo {
	return k.Clauses(dbresolver.Read)
}

func (k keyValueDo) WriteDB() IKeyValueDo {
	return k.Clauses(dbresolver.Write)
}

func (k keyValueDo) Session(config *gorm.Session) IKeyValueDo {
	return k.withDO(k.DO.Session(config))
}

func (k keyValueDo) Clauses(conds ...clause.Expression) IKeyValueDo {
	return k.withDO(k.DO.Clauses(conds...))
}

func (k keyValueDo) Returning(value interface{}, columns ...string) IKeyValueDo {
	return k.withDO(k.DO.Returning(value, columns...))
}

func (k keyValueDo) Not(conds ...gen.Condition) IKeyValueDo {
	return k.withDO(k.DO.Not(conds...))
}

func (k keyValueDo) Or(conds ...gen.Condition) IKeyValueDo {
	return k.withDO(k.DO.Or(conds...))
}

func (k keyValueDo) Select(conds ...field.Expr) IKeyValueDo {
	return k.withDO(k.DO.Select(conds...))
}

func (k keyValueDo) Where(conds ...gen.Condition) IKeyValueDo {
	return k.withDO(k.DO.Where(conds...))
}

func (k keyValueDo) Order(conds ...field.Expr) IKeyValueDo {
	return k.withDO(k.DO.Order(conds...))
}

func (k keyValueDo) Distinct(cols ...field.Expr) IKeyValueDo {
	return k.withDO(k.DO.Distinct(cols...))
}

func (k keyValueDo) Omit(cols ...field.Expr) IKeyValueDo {
	return k.withDO(k.DO.Omit(cols...))
}

func (k keyValueDo) Join(table schema.Tabler, on ...field.Expr) IKeyValueDo {
	return k.withDO(k.DO.Join(table, on...))
}

func (k keyValueDo) LeftJoin(table schema.Tabler, on ...field.Expr) IKeyValueDo {
	return k.withDO(k.DO.LeftJoin(table, on...))
}

func (k keyValueDo) RightJoin(table schema.Tabler, on ...field.Expr) IKeyValueDo {
	return k.withDO(k.DO.RightJoin(table, on...))
}

func (k keyValueDo) Group(cols ...field.Expr) IKeyValueDo {
	return k.withDO(k.DO.Group(cols...))
}

func (k keyValueDo) Having(conds ...gen.Condition) IKeyValueDo {
	return k.withDO(k.DO.Having(conds...))
}

func (k keyValueDo) Limit(limit int) IKeyValueDo {
	return k.withDO(k.DO.Limit(limit))
}

func (k keyValueDo) Offset(offset int) IKeyValueDo {
	return k.withDO(k.DO.Offset(offset))
}

func (k keyValueDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IKeyValueDo {
	return k.withDO(k.DO.Scopes(funcs...))
}

func (k keyValueDo) Unscoped() IKeyValueDo {
	return k.withDO(k.DO.Unscoped())
}

func (k keyValueDo) Create(values ...*model.KeyValue) error {
	if len(values) == 0 {
		return nil
	}
	return k.DO.Create(values)
}

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

func (k keyValueDo) First() (*model.KeyValue, error) {
	if result, err := k.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.KeyValue), nil
	}
}

func (k keyValueDo) Take() (*model.KeyValue, error) {
	if result, err := k.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.KeyValue), nil
	}
}

func (k keyValueDo) Last() (*model.KeyValue, error) {
	if result, err := k.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.KeyValue), nil
	}
}

func (k keyValueDo) Find() ([]*model.KeyValue, error) {
	result, err := k.DO.Find()
	return result.([]*model.KeyValue), err
}

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

func (k keyValueDo) Attrs(attrs ...field.AssignExpr) IKeyValueDo {
	return k.withDO(k.DO.Attrs(attrs...))
}

func (k keyValueDo) Assign(attrs ...field.AssignExpr) IKeyValueDo {
	return k.withDO(k.DO.Assign(attrs...))
}

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

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

func (k keyValueDo) FirstOrInit() (*model.KeyValue, error) {
	if result, err := k.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.KeyValue), nil
	}
}

func (k keyValueDo) FirstOrCreate() (*model.KeyValue, error) {
	if result, err := k.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.KeyValue), nil
	}
}

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

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

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

func (k keyValueDo) Scan(result interface{}) (err error) {
	return k.DO.Scan(result)
}

func (k keyValueDo) Delete(models ...*model.KeyValue) (result gen.ResultInfo, err error) {
	return k.DO.Delete(models)
}

func (k *keyValueDo) withDO(do gen.Dao) *keyValueDo {
	k.DO = *do.(*gen.DO)
	return k
}
