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

	_config.configDo.UseDB(db, opts...)
	_config.configDo.UseModel(&model.Config{})

	tableName := _config.configDo.TableName()
	_config.ALL = field.NewAsterisk(tableName)
	_config.ID = field.NewInt64(tableName, "id")
	_config.Group_ = field.NewString(tableName, "group")
	_config.Name = field.NewString(tableName, "name")
	_config.Type = field.NewString(tableName, "type")
	_config.Key = field.NewString(tableName, "key")
	_config.Value = field.NewString(tableName, "value")
	_config.DefaultValue = field.NewString(tableName, "default_value")
	_config.Sort = field.NewInt32(tableName, "sort")
	_config.Tip = field.NewString(tableName, "tip")
	_config.IsDefault = field.NewBool(tableName, "is_default")
	_config.Status = field.NewBool(tableName, "status")
	_config.CreatedAt = field.NewTime(tableName, "created_at")
	_config.UpdatedAt = field.NewTime(tableName, "updated_at")

	_config.fillFieldMap()

	return _config
}

type config struct {
	configDo

	ALL          field.Asterisk
	ID           field.Int64  // 配置ID
	Group_       field.String // 分组
	Name         field.String // 参数名称
	Type         field.String // 类型:string,text,int,bool,array,datetime,date,file
	Key          field.String // 参数键名
	Value        field.String // 参数键值
	DefaultValue field.String // 默认值
	Sort         field.Int32  // 排序
	Tip          field.String // 变量描述
	IsDefault    field.Bool   // 是否默认
	Status       field.Bool   // 状态
	CreatedAt    field.Time   // 创建时间
	UpdatedAt    field.Time   // 更新时间

	fieldMap map[string]field.Expr
}

func (c config) Table(newTableName string) *config {
	c.configDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

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

func (c *config) updateTableName(table string) *config {
	c.ALL = field.NewAsterisk(table)
	c.ID = field.NewInt64(table, "id")
	c.Group_ = field.NewString(table, "group")
	c.Name = field.NewString(table, "name")
	c.Type = field.NewString(table, "type")
	c.Key = field.NewString(table, "key")
	c.Value = field.NewString(table, "value")
	c.DefaultValue = field.NewString(table, "default_value")
	c.Sort = field.NewInt32(table, "sort")
	c.Tip = field.NewString(table, "tip")
	c.IsDefault = field.NewBool(table, "is_default")
	c.Status = field.NewBool(table, "status")
	c.CreatedAt = field.NewTime(table, "created_at")
	c.UpdatedAt = field.NewTime(table, "updated_at")

	c.fillFieldMap()

	return c
}

func (c *config) 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 *config) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 13)
	c.fieldMap["id"] = c.ID
	c.fieldMap["group"] = c.Group_
	c.fieldMap["name"] = c.Name
	c.fieldMap["type"] = c.Type
	c.fieldMap["key"] = c.Key
	c.fieldMap["value"] = c.Value
	c.fieldMap["default_value"] = c.DefaultValue
	c.fieldMap["sort"] = c.Sort
	c.fieldMap["tip"] = c.Tip
	c.fieldMap["is_default"] = c.IsDefault
	c.fieldMap["status"] = c.Status
	c.fieldMap["created_at"] = c.CreatedAt
	c.fieldMap["updated_at"] = c.UpdatedAt
}

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

func (c config) replaceDB(db *gorm.DB) config {
	c.configDo.ReplaceDB(db)
	return c
}

type configDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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