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

	_menus.menusDo.UseDB(db, opts...)
	_menus.menusDo.UseModel(&model.Menus{})

	tableName := _menus.menusDo.TableName()
	_menus.ALL = field.NewAsterisk(tableName)
	_menus.ID = field.NewInt64(tableName, "id")
	_menus.ParentID = field.NewInt64(tableName, "parent_id")
	_menus.Path = field.NewString(tableName, "path")
	_menus.Name = field.NewString(tableName, "name")
	_menus.Component = field.NewString(tableName, "component")
	_menus.Icon = field.NewString(tableName, "icon")
	_menus.Sort = field.NewInt32(tableName, "sort")
	_menus.Status = field.NewBool(tableName, "status")
	_menus.KeepAlive = field.NewBool(tableName, "keep_alive")
	_menus.IsHide = field.NewBool(tableName, "is_hide")
	_menus.IsHideTab = field.NewBool(tableName, "is_hide_tab")
	_menus.CreatedUser = field.NewString(tableName, "created_user")
	_menus.UpdatedUser = field.NewString(tableName, "updated_user")
	_menus.CreatedAt = field.NewTime(tableName, "created_at")
	_menus.UpdatedAt = field.NewTime(tableName, "updated_at")
	_menus.DeletedAt = field.NewField(tableName, "deleted_at")

	_menus.fillFieldMap()

	return _menus
}

type menus struct {
	menusDo

	ALL         field.Asterisk
	ID          field.Int64
	ParentID    field.Int64
	Path        field.String
	Name        field.String
	Component   field.String
	Icon        field.String
	Sort        field.Int32
	Status      field.Bool
	KeepAlive   field.Bool
	IsHide      field.Bool
	IsHideTab   field.Bool
	CreatedUser field.String
	UpdatedUser field.String
	CreatedAt   field.Time
	UpdatedAt   field.Time
	DeletedAt   field.Field

	fieldMap map[string]field.Expr
}

func (m menus) Table(newTableName string) *menus {
	m.menusDo.UseTable(newTableName)
	return m.updateTableName(newTableName)
}

func (m menus) As(alias string) *menus {
	m.menusDo.DO = *(m.menusDo.As(alias).(*gen.DO))
	return m.updateTableName(alias)
}

func (m *menus) updateTableName(table string) *menus {
	m.ALL = field.NewAsterisk(table)
	m.ID = field.NewInt64(table, "id")
	m.ParentID = field.NewInt64(table, "parent_id")
	m.Path = field.NewString(table, "path")
	m.Name = field.NewString(table, "name")
	m.Component = field.NewString(table, "component")
	m.Icon = field.NewString(table, "icon")
	m.Sort = field.NewInt32(table, "sort")
	m.Status = field.NewBool(table, "status")
	m.KeepAlive = field.NewBool(table, "keep_alive")
	m.IsHide = field.NewBool(table, "is_hide")
	m.IsHideTab = field.NewBool(table, "is_hide_tab")
	m.CreatedUser = field.NewString(table, "created_user")
	m.UpdatedUser = field.NewString(table, "updated_user")
	m.CreatedAt = field.NewTime(table, "created_at")
	m.UpdatedAt = field.NewTime(table, "updated_at")
	m.DeletedAt = field.NewField(table, "deleted_at")

	m.fillFieldMap()

	return m
}

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

func (m *menus) fillFieldMap() {
	m.fieldMap = make(map[string]field.Expr, 16)
	m.fieldMap["id"] = m.ID
	m.fieldMap["parent_id"] = m.ParentID
	m.fieldMap["path"] = m.Path
	m.fieldMap["name"] = m.Name
	m.fieldMap["component"] = m.Component
	m.fieldMap["icon"] = m.Icon
	m.fieldMap["sort"] = m.Sort
	m.fieldMap["status"] = m.Status
	m.fieldMap["keep_alive"] = m.KeepAlive
	m.fieldMap["is_hide"] = m.IsHide
	m.fieldMap["is_hide_tab"] = m.IsHideTab
	m.fieldMap["created_user"] = m.CreatedUser
	m.fieldMap["updated_user"] = m.UpdatedUser
	m.fieldMap["created_at"] = m.CreatedAt
	m.fieldMap["updated_at"] = m.UpdatedAt
	m.fieldMap["deleted_at"] = m.DeletedAt
}

func (m menus) clone(db *gorm.DB) menus {
	m.menusDo.ReplaceConnPool(db.Statement.ConnPool)
	return m
}

func (m menus) replaceDB(db *gorm.DB) menus {
	m.menusDo.ReplaceDB(db)
	return m
}

type menusDo struct{ gen.DO }

func (m menusDo) Debug() *menusDo {
	return m.withDO(m.DO.Debug())
}

func (m menusDo) WithContext(ctx context.Context) *menusDo {
	return m.withDO(m.DO.WithContext(ctx))
}

func (m menusDo) ReadDB() *menusDo {
	return m.Clauses(dbresolver.Read)
}

func (m menusDo) WriteDB() *menusDo {
	return m.Clauses(dbresolver.Write)
}

func (m menusDo) Session(config *gorm.Session) *menusDo {
	return m.withDO(m.DO.Session(config))
}

func (m menusDo) Clauses(conds ...clause.Expression) *menusDo {
	return m.withDO(m.DO.Clauses(conds...))
}

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

func (m menusDo) Not(conds ...gen.Condition) *menusDo {
	return m.withDO(m.DO.Not(conds...))
}

func (m menusDo) Or(conds ...gen.Condition) *menusDo {
	return m.withDO(m.DO.Or(conds...))
}

func (m menusDo) Select(conds ...field.Expr) *menusDo {
	return m.withDO(m.DO.Select(conds...))
}

func (m menusDo) Where(conds ...gen.Condition) *menusDo {
	return m.withDO(m.DO.Where(conds...))
}

func (m menusDo) Order(conds ...field.Expr) *menusDo {
	return m.withDO(m.DO.Order(conds...))
}

func (m menusDo) Distinct(cols ...field.Expr) *menusDo {
	return m.withDO(m.DO.Distinct(cols...))
}

func (m menusDo) Omit(cols ...field.Expr) *menusDo {
	return m.withDO(m.DO.Omit(cols...))
}

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

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

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

func (m menusDo) Group(cols ...field.Expr) *menusDo {
	return m.withDO(m.DO.Group(cols...))
}

func (m menusDo) Having(conds ...gen.Condition) *menusDo {
	return m.withDO(m.DO.Having(conds...))
}

func (m menusDo) Limit(limit int) *menusDo {
	return m.withDO(m.DO.Limit(limit))
}

func (m menusDo) Offset(offset int) *menusDo {
	return m.withDO(m.DO.Offset(offset))
}

func (m menusDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *menusDo {
	return m.withDO(m.DO.Scopes(funcs...))
}

func (m menusDo) Unscoped() *menusDo {
	return m.withDO(m.DO.Unscoped())
}

func (m menusDo) Create(values ...*model.Menus) error {
	if len(values) == 0 {
		return nil
	}
	return m.DO.Create(values)
}

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

func (m menusDo) First() (*model.Menus, error) {
	if result, err := m.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Menus), nil
	}
}

func (m menusDo) Take() (*model.Menus, error) {
	if result, err := m.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Menus), nil
	}
}

func (m menusDo) Last() (*model.Menus, error) {
	if result, err := m.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Menus), nil
	}
}

func (m menusDo) Find() ([]*model.Menus, error) {
	result, err := m.DO.Find()
	return result.([]*model.Menus), err
}

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

func (m menusDo) Attrs(attrs ...field.AssignExpr) *menusDo {
	return m.withDO(m.DO.Attrs(attrs...))
}

func (m menusDo) Assign(attrs ...field.AssignExpr) *menusDo {
	return m.withDO(m.DO.Assign(attrs...))
}

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

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

func (m menusDo) FirstOrInit() (*model.Menus, error) {
	if result, err := m.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Menus), nil
	}
}

func (m menusDo) FirstOrCreate() (*model.Menus, error) {
	if result, err := m.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Menus), nil
	}
}

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

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

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

func (m menusDo) Scan(result interface{}) (err error) {
	return m.DO.Scan(result)
}

func (m menusDo) Delete(models ...*model.Menus) (result gen.ResultInfo, err error) {
	return m.DO.Delete(models)
}

func (m *menusDo) withDO(do gen.Dao) *menusDo {
	m.DO = *do.(*gen.DO)
	return m
}
