// 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 dao

import (
	"context"

	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"gorm.io/gorm/schema"

	"gorm.io/gen"
	"gorm.io/gen/field"

	"gorm.io/plugin/dbresolver"

	"bindbox-game/internal/repository/mysql/model"
)

func newRoleUsers(db *gorm.DB, opts ...gen.DOOption) roleUsers {
	_roleUsers := roleUsers{}

	_roleUsers.roleUsersDo.UseDB(db, opts...)
	_roleUsers.roleUsersDo.UseModel(&model.RoleUsers{})

	tableName := _roleUsers.roleUsersDo.TableName()
	_roleUsers.ALL = field.NewAsterisk(tableName)
	_roleUsers.ID = field.NewInt64(tableName, "id")
	_roleUsers.RoleID = field.NewInt64(tableName, "role_id")
	_roleUsers.AdminID = field.NewInt32(tableName, "admin_id")
	_roleUsers.CreatedUser = field.NewString(tableName, "created_user")
	_roleUsers.UpdatedUser = field.NewString(tableName, "updated_user")
	_roleUsers.CreatedAt = field.NewTime(tableName, "created_at")
	_roleUsers.UpdatedAt = field.NewTime(tableName, "updated_at")
	_roleUsers.DeletedAt = field.NewField(tableName, "deleted_at")

	_roleUsers.fillFieldMap()

	return _roleUsers
}

type roleUsers struct {
	roleUsersDo

	ALL         field.Asterisk
	ID          field.Int64
	RoleID      field.Int64
	AdminID     field.Int32
	CreatedUser field.String
	UpdatedUser field.String
	CreatedAt   field.Time
	UpdatedAt   field.Time
	DeletedAt   field.Field

	fieldMap map[string]field.Expr
}

func (r roleUsers) Table(newTableName string) *roleUsers {
	r.roleUsersDo.UseTable(newTableName)
	return r.updateTableName(newTableName)
}

func (r roleUsers) As(alias string) *roleUsers {
	r.roleUsersDo.DO = *(r.roleUsersDo.As(alias).(*gen.DO))
	return r.updateTableName(alias)
}

func (r *roleUsers) updateTableName(table string) *roleUsers {
	r.ALL = field.NewAsterisk(table)
	r.ID = field.NewInt64(table, "id")
	r.RoleID = field.NewInt64(table, "role_id")
	r.AdminID = field.NewInt32(table, "admin_id")
	r.CreatedUser = field.NewString(table, "created_user")
	r.UpdatedUser = field.NewString(table, "updated_user")
	r.CreatedAt = field.NewTime(table, "created_at")
	r.UpdatedAt = field.NewTime(table, "updated_at")
	r.DeletedAt = field.NewField(table, "deleted_at")

	r.fillFieldMap()

	return r
}

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

func (r *roleUsers) fillFieldMap() {
	r.fieldMap = make(map[string]field.Expr, 8)
	r.fieldMap["id"] = r.ID
	r.fieldMap["role_id"] = r.RoleID
	r.fieldMap["admin_id"] = r.AdminID
	r.fieldMap["created_user"] = r.CreatedUser
	r.fieldMap["updated_user"] = r.UpdatedUser
	r.fieldMap["created_at"] = r.CreatedAt
	r.fieldMap["updated_at"] = r.UpdatedAt
	r.fieldMap["deleted_at"] = r.DeletedAt
}

func (r roleUsers) clone(db *gorm.DB) roleUsers {
	r.roleUsersDo.ReplaceConnPool(db.Statement.ConnPool)
	return r
}

func (r roleUsers) replaceDB(db *gorm.DB) roleUsers {
	r.roleUsersDo.ReplaceDB(db)
	return r
}

type roleUsersDo struct{ gen.DO }

func (r roleUsersDo) Debug() *roleUsersDo {
	return r.withDO(r.DO.Debug())
}

func (r roleUsersDo) WithContext(ctx context.Context) *roleUsersDo {
	return r.withDO(r.DO.WithContext(ctx))
}

func (r roleUsersDo) ReadDB() *roleUsersDo {
	return r.Clauses(dbresolver.Read)
}

func (r roleUsersDo) WriteDB() *roleUsersDo {
	return r.Clauses(dbresolver.Write)
}

func (r roleUsersDo) Session(config *gorm.Session) *roleUsersDo {
	return r.withDO(r.DO.Session(config))
}

func (r roleUsersDo) Clauses(conds ...clause.Expression) *roleUsersDo {
	return r.withDO(r.DO.Clauses(conds...))
}

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

func (r roleUsersDo) Not(conds ...gen.Condition) *roleUsersDo {
	return r.withDO(r.DO.Not(conds...))
}

func (r roleUsersDo) Or(conds ...gen.Condition) *roleUsersDo {
	return r.withDO(r.DO.Or(conds...))
}

func (r roleUsersDo) Select(conds ...field.Expr) *roleUsersDo {
	return r.withDO(r.DO.Select(conds...))
}

func (r roleUsersDo) Where(conds ...gen.Condition) *roleUsersDo {
	return r.withDO(r.DO.Where(conds...))
}

func (r roleUsersDo) Order(conds ...field.Expr) *roleUsersDo {
	return r.withDO(r.DO.Order(conds...))
}

func (r roleUsersDo) Distinct(cols ...field.Expr) *roleUsersDo {
	return r.withDO(r.DO.Distinct(cols...))
}

func (r roleUsersDo) Omit(cols ...field.Expr) *roleUsersDo {
	return r.withDO(r.DO.Omit(cols...))
}

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

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

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

func (r roleUsersDo) Group(cols ...field.Expr) *roleUsersDo {
	return r.withDO(r.DO.Group(cols...))
}

func (r roleUsersDo) Having(conds ...gen.Condition) *roleUsersDo {
	return r.withDO(r.DO.Having(conds...))
}

func (r roleUsersDo) Limit(limit int) *roleUsersDo {
	return r.withDO(r.DO.Limit(limit))
}

func (r roleUsersDo) Offset(offset int) *roleUsersDo {
	return r.withDO(r.DO.Offset(offset))
}

func (r roleUsersDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *roleUsersDo {
	return r.withDO(r.DO.Scopes(funcs...))
}

func (r roleUsersDo) Unscoped() *roleUsersDo {
	return r.withDO(r.DO.Unscoped())
}

func (r roleUsersDo) Create(values ...*model.RoleUsers) error {
	if len(values) == 0 {
		return nil
	}
	return r.DO.Create(values)
}

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

func (r roleUsersDo) First() (*model.RoleUsers, error) {
	if result, err := r.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.RoleUsers), nil
	}
}

func (r roleUsersDo) Take() (*model.RoleUsers, error) {
	if result, err := r.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.RoleUsers), nil
	}
}

func (r roleUsersDo) Last() (*model.RoleUsers, error) {
	if result, err := r.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.RoleUsers), nil
	}
}

func (r roleUsersDo) Find() ([]*model.RoleUsers, error) {
	result, err := r.DO.Find()
	return result.([]*model.RoleUsers), err
}

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

func (r roleUsersDo) Attrs(attrs ...field.AssignExpr) *roleUsersDo {
	return r.withDO(r.DO.Attrs(attrs...))
}

func (r roleUsersDo) Assign(attrs ...field.AssignExpr) *roleUsersDo {
	return r.withDO(r.DO.Assign(attrs...))
}

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

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

func (r roleUsersDo) FirstOrInit() (*model.RoleUsers, error) {
	if result, err := r.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.RoleUsers), nil
	}
}

func (r roleUsersDo) FirstOrCreate() (*model.RoleUsers, error) {
	if result, err := r.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.RoleUsers), nil
	}
}

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

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

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

func (r roleUsersDo) Scan(result interface{}) (err error) {
	return r.DO.Scan(result)
}

func (r roleUsersDo) Delete(models ...*model.RoleUsers) (result gen.ResultInfo, err error) {
	return r.DO.Delete(models)
}

func (r *roleUsersDo) withDO(do gen.Dao) *roleUsersDo {
	r.DO = *do.(*gen.DO)
	return r
}
