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

	_eventConfig.eventConfigDo.UseDB(db, opts...)
	_eventConfig.eventConfigDo.UseModel(&model.EventConfig{})

	tableName := _eventConfig.eventConfigDo.TableName()
	_eventConfig.ALL = field.NewAsterisk(tableName)
	_eventConfig.ID = field.NewInt32(tableName, "id")
	_eventConfig.Name = field.NewString(tableName, "name")
	_eventConfig.EventCode = field.NewInt32(tableName, "event_code")
	_eventConfig.Category = field.NewString(tableName, "category")
	_eventConfig.CreatedAt = field.NewInt32(tableName, "created_at")

	_eventConfig.fillFieldMap()

	return _eventConfig
}

type eventConfig struct {
	eventConfigDo

	ALL       field.Asterisk
	ID        field.Int32
	Name      field.String
	EventCode field.Int32
	Category  field.String
	CreatedAt field.Int32

	fieldMap map[string]field.Expr
}

func (e eventConfig) Table(newTableName string) *eventConfig {
	e.eventConfigDo.UseTable(newTableName)
	return e.updateTableName(newTableName)
}

func (e eventConfig) As(alias string) *eventConfig {
	e.eventConfigDo.DO = *(e.eventConfigDo.As(alias).(*gen.DO))
	return e.updateTableName(alias)
}

func (e *eventConfig) updateTableName(table string) *eventConfig {
	e.ALL = field.NewAsterisk(table)
	e.ID = field.NewInt32(table, "id")
	e.Name = field.NewString(table, "name")
	e.EventCode = field.NewInt32(table, "event_code")
	e.Category = field.NewString(table, "category")
	e.CreatedAt = field.NewInt32(table, "created_at")

	e.fillFieldMap()

	return e
}

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

func (e *eventConfig) fillFieldMap() {
	e.fieldMap = make(map[string]field.Expr, 5)
	e.fieldMap["id"] = e.ID
	e.fieldMap["name"] = e.Name
	e.fieldMap["event_code"] = e.EventCode
	e.fieldMap["category"] = e.Category
	e.fieldMap["created_at"] = e.CreatedAt
}

func (e eventConfig) clone(db *gorm.DB) eventConfig {
	e.eventConfigDo.ReplaceConnPool(db.Statement.ConnPool)
	return e
}

func (e eventConfig) replaceDB(db *gorm.DB) eventConfig {
	e.eventConfigDo.ReplaceDB(db)
	return e
}

type eventConfigDo struct{ gen.DO }

func (e eventConfigDo) Debug() *eventConfigDo {
	return e.withDO(e.DO.Debug())
}

func (e eventConfigDo) WithContext(ctx context.Context) *eventConfigDo {
	return e.withDO(e.DO.WithContext(ctx))
}

func (e eventConfigDo) ReadDB() *eventConfigDo {
	return e.Clauses(dbresolver.Read)
}

func (e eventConfigDo) WriteDB() *eventConfigDo {
	return e.Clauses(dbresolver.Write)
}

func (e eventConfigDo) Session(config *gorm.Session) *eventConfigDo {
	return e.withDO(e.DO.Session(config))
}

func (e eventConfigDo) Clauses(conds ...clause.Expression) *eventConfigDo {
	return e.withDO(e.DO.Clauses(conds...))
}

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

func (e eventConfigDo) Not(conds ...gen.Condition) *eventConfigDo {
	return e.withDO(e.DO.Not(conds...))
}

func (e eventConfigDo) Or(conds ...gen.Condition) *eventConfigDo {
	return e.withDO(e.DO.Or(conds...))
}

func (e eventConfigDo) Select(conds ...field.Expr) *eventConfigDo {
	return e.withDO(e.DO.Select(conds...))
}

func (e eventConfigDo) Where(conds ...gen.Condition) *eventConfigDo {
	return e.withDO(e.DO.Where(conds...))
}

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

func (e eventConfigDo) Order(conds ...field.Expr) *eventConfigDo {
	return e.withDO(e.DO.Order(conds...))
}

func (e eventConfigDo) Distinct(cols ...field.Expr) *eventConfigDo {
	return e.withDO(e.DO.Distinct(cols...))
}

func (e eventConfigDo) Omit(cols ...field.Expr) *eventConfigDo {
	return e.withDO(e.DO.Omit(cols...))
}

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

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

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

func (e eventConfigDo) Group(cols ...field.Expr) *eventConfigDo {
	return e.withDO(e.DO.Group(cols...))
}

func (e eventConfigDo) Having(conds ...gen.Condition) *eventConfigDo {
	return e.withDO(e.DO.Having(conds...))
}

func (e eventConfigDo) Limit(limit int) *eventConfigDo {
	return e.withDO(e.DO.Limit(limit))
}

func (e eventConfigDo) Offset(offset int) *eventConfigDo {
	return e.withDO(e.DO.Offset(offset))
}

func (e eventConfigDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *eventConfigDo {
	return e.withDO(e.DO.Scopes(funcs...))
}

func (e eventConfigDo) Unscoped() *eventConfigDo {
	return e.withDO(e.DO.Unscoped())
}

func (e eventConfigDo) Create(values ...*model.EventConfig) error {
	if len(values) == 0 {
		return nil
	}
	return e.DO.Create(values)
}

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

func (e eventConfigDo) First() (*model.EventConfig, error) {
	if result, err := e.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.EventConfig), nil
	}
}

func (e eventConfigDo) Take() (*model.EventConfig, error) {
	if result, err := e.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.EventConfig), nil
	}
}

func (e eventConfigDo) Last() (*model.EventConfig, error) {
	if result, err := e.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.EventConfig), nil
	}
}

func (e eventConfigDo) Find() ([]*model.EventConfig, error) {
	result, err := e.DO.Find()
	return result.([]*model.EventConfig), err
}

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

func (e eventConfigDo) Attrs(attrs ...field.AssignExpr) *eventConfigDo {
	return e.withDO(e.DO.Attrs(attrs...))
}

func (e eventConfigDo) Assign(attrs ...field.AssignExpr) *eventConfigDo {
	return e.withDO(e.DO.Assign(attrs...))
}

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

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

func (e eventConfigDo) FirstOrInit() (*model.EventConfig, error) {
	if result, err := e.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.EventConfig), nil
	}
}

func (e eventConfigDo) FirstOrCreate() (*model.EventConfig, error) {
	if result, err := e.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.EventConfig), nil
	}
}

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

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

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

func (e eventConfigDo) Scan(result interface{}) (err error) {
	return e.DO.Scan(result)
}

func (e eventConfigDo) Delete(models ...*model.EventConfig) (result gen.ResultInfo, err error) {
	return e.DO.Delete(models)
}

func (e *eventConfigDo) withDO(do gen.Dao) *eventConfigDo {
	e.DO = *do.(*gen.DO)
	return e
}
