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

	_adminPermission.adminPermissionDo.UseDB(db, opts...)
	_adminPermission.adminPermissionDo.UseModel(&model.AdminPermission{})

	tableName := _adminPermission.adminPermissionDo.TableName()
	_adminPermission.ALL = field.NewAsterisk(tableName)
	_adminPermission.ID = field.NewInt32(tableName, "id")
	_adminPermission.Name = field.NewString(tableName, "name")
	_adminPermission.Apis = field.NewString(tableName, "apis")

	_adminPermission.fillFieldMap()

	return _adminPermission
}

type adminPermission struct {
	adminPermissionDo adminPermissionDo

	ALL  field.Asterisk
	ID   field.Int32
	Name field.String
	Apis field.String

	fieldMap map[string]field.Expr
}

func (a adminPermission) Table(newTableName string) *adminPermission {
	a.adminPermissionDo.UseTable(newTableName)
	return a.updateTableName(newTableName)
}

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

func (a *adminPermission) updateTableName(table string) *adminPermission {
	a.ALL = field.NewAsterisk(table)
	a.ID = field.NewInt32(table, "id")
	a.Name = field.NewString(table, "name")
	a.Apis = field.NewString(table, "apis")

	a.fillFieldMap()

	return a
}

func (a *adminPermission) WithContext(ctx context.Context) *adminPermissionDo {
	return a.adminPermissionDo.WithContext(ctx)
}

func (a adminPermission) TableName() string { return a.adminPermissionDo.TableName() }

func (a adminPermission) Alias() string { return a.adminPermissionDo.Alias() }

func (a adminPermission) Columns(cols ...field.Expr) gen.Columns {
	return a.adminPermissionDo.Columns(cols...)
}

func (a *adminPermission) 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 *adminPermission) fillFieldMap() {
	a.fieldMap = make(map[string]field.Expr, 3)
	a.fieldMap["id"] = a.ID
	a.fieldMap["name"] = a.Name
	a.fieldMap["apis"] = a.Apis
}

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

func (a adminPermission) replaceDB(db *gorm.DB) adminPermission {
	a.adminPermissionDo.ReplaceDB(db)
	return a
}

type adminPermissionDo struct{ gen.DO }

func (a adminPermissionDo) Debug() *adminPermissionDo {
	return a.withDO(a.DO.Debug())
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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