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

	_adminRolePermission.adminRolePermissionDo.UseDB(db, opts...)
	_adminRolePermission.adminRolePermissionDo.UseModel(&model.AdminRolePermission{})

	tableName := _adminRolePermission.adminRolePermissionDo.TableName()
	_adminRolePermission.ALL = field.NewAsterisk(tableName)
	_adminRolePermission.ID = field.NewInt32(tableName, "id")
	_adminRolePermission.RoleID = field.NewInt32(tableName, "role_id")
	_adminRolePermission.SystemID = field.NewInt32(tableName, "system_id")
	_adminRolePermission.PermissionID = field.NewInt32(tableName, "permission_id")

	_adminRolePermission.fillFieldMap()

	return _adminRolePermission
}

type adminRolePermission struct {
	adminRolePermissionDo

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

	fieldMap map[string]field.Expr
}

func (a adminRolePermission) Table(newTableName string) *adminRolePermission {
	a.adminRolePermissionDo.UseTable(newTableName)
	return a.updateTableName(newTableName)
}

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

func (a *adminRolePermission) updateTableName(table string) *adminRolePermission {
	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.PermissionID = field.NewInt32(table, "permission_id")

	a.fillFieldMap()

	return a
}

func (a *adminRolePermission) WithContext(ctx context.Context) *adminRolePermissionDo {
	return a.adminRolePermissionDo.WithContext(ctx)
}

func (a adminRolePermission) TableName() string { return a.adminRolePermissionDo.TableName() }

func (a adminRolePermission) Alias() string { return a.adminRolePermissionDo.Alias() }

func (a adminRolePermission) Columns(cols ...field.Expr) gen.Columns {
	return a.adminRolePermissionDo.Columns(cols...)
}

func (a *adminRolePermission) 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 *adminRolePermission) 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["permission_id"] = a.PermissionID
}

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

func (a adminRolePermission) replaceDB(db *gorm.DB) adminRolePermission {
	a.adminRolePermissionDo.ReplaceDB(db)
	return a
}

type adminRolePermissionDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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