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

	_adminRoleMenu.adminRoleMenuDo.UseDB(db, opts...)
	_adminRoleMenu.adminRoleMenuDo.UseModel(&model.AdminRoleMenu{})

	tableName := _adminRoleMenu.adminRoleMenuDo.TableName()
	_adminRoleMenu.ALL = field.NewAsterisk(tableName)
	_adminRoleMenu.ID = field.NewInt32(tableName, "id")
	_adminRoleMenu.RoleID = field.NewInt32(tableName, "role_id")
	_adminRoleMenu.SystemID = field.NewInt32(tableName, "system_id")
	_adminRoleMenu.PageID = field.NewInt32(tableName, "page_id")

	_adminRoleMenu.fillFieldMap()

	return _adminRoleMenu
}

type adminRoleMenu struct {
	adminRoleMenuDo

	ALL      field.Asterisk
	ID       field.Int32
	RoleID   field.Int32
	SystemID field.Int32
	PageID   field.Int32

	fieldMap map[string]field.Expr
}

func (a adminRoleMenu) Table(newTableName string) *adminRoleMenu {
	a.adminRoleMenuDo.UseTable(newTableName)
	return a.updateTableName(newTableName)
}

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

func (a *adminRoleMenu) updateTableName(table string) *adminRoleMenu {
	a.ALL = field.NewAsterisk(table)
	a.ID = field.NewInt32(table, "id")
	a.RoleID = field.NewInt32(table, "role_id")
	a.SystemID = field.NewInt32(table, "system_id")
	a.PageID = field.NewInt32(table, "page_id")

	a.fillFieldMap()

	return a
}

func (a *adminRoleMenu) WithContext(ctx context.Context) *adminRoleMenuDo {
	return a.adminRoleMenuDo.WithContext(ctx)
}

func (a adminRoleMenu) TableName() string { return a.adminRoleMenuDo.TableName() }

func (a adminRoleMenu) Alias() string { return a.adminRoleMenuDo.Alias() }

func (a adminRoleMenu) Columns(cols ...field.Expr) gen.Columns {
	return a.adminRoleMenuDo.Columns(cols...)
}

func (a *adminRoleMenu) 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 *adminRoleMenu) fillFieldMap() {
	a.fieldMap = make(map[string]field.Expr, 4)
	a.fieldMap["id"] = a.ID
	a.fieldMap["role_id"] = a.RoleID
	a.fieldMap["system_id"] = a.SystemID
	a.fieldMap["page_id"] = a.PageID
}

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

func (a adminRoleMenu) replaceDB(db *gorm.DB) adminRoleMenu {
	a.adminRoleMenuDo.ReplaceDB(db)
	return a
}

type adminRoleMenuDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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