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

	"github.com/bangumi/server/dal/dao"
)

func newUserGroup(db *gorm.DB, opts ...gen.DOOption) userGroup {
	_userGroup := userGroup{}

	_userGroup.userGroupDo.UseDB(db, opts...)
	_userGroup.userGroupDo.UseModel(&dao.UserGroup{})

	tableName := _userGroup.userGroupDo.TableName()
	_userGroup.ALL = field.NewAsterisk(tableName)
	_userGroup.ID = field.NewUint8(tableName, "usr_grp_id")
	_userGroup.Name = field.NewString(tableName, "usr_grp_name")
	_userGroup.Perm = field.NewBytes(tableName, "usr_grp_perm")
	_userGroup.Dateline = field.NewUint32(tableName, "usr_grp_dateline")

	_userGroup.fillFieldMap()

	return _userGroup
}

type userGroup struct {
	userGroupDo userGroupDo

	ALL      field.Asterisk
	ID       field.Uint8
	Name     field.String
	Perm     field.Bytes
	Dateline field.Uint32

	fieldMap map[string]field.Expr
}

func (u userGroup) Table(newTableName string) *userGroup {
	u.userGroupDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

func (u userGroup) As(alias string) *userGroup {
	u.userGroupDo.DO = *(u.userGroupDo.As(alias).(*gen.DO))
	return u.updateTableName(alias)
}

func (u *userGroup) updateTableName(table string) *userGroup {
	u.ALL = field.NewAsterisk(table)
	u.ID = field.NewUint8(table, "usr_grp_id")
	u.Name = field.NewString(table, "usr_grp_name")
	u.Perm = field.NewBytes(table, "usr_grp_perm")
	u.Dateline = field.NewUint32(table, "usr_grp_dateline")

	u.fillFieldMap()

	return u
}

func (u *userGroup) WithContext(ctx context.Context) *userGroupDo {
	return u.userGroupDo.WithContext(ctx)
}

func (u userGroup) TableName() string { return u.userGroupDo.TableName() }

func (u userGroup) Alias() string { return u.userGroupDo.Alias() }

func (u userGroup) Columns(cols ...field.Expr) gen.Columns { return u.userGroupDo.Columns(cols...) }

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

func (u *userGroup) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 4)
	u.fieldMap["usr_grp_id"] = u.ID
	u.fieldMap["usr_grp_name"] = u.Name
	u.fieldMap["usr_grp_perm"] = u.Perm
	u.fieldMap["usr_grp_dateline"] = u.Dateline
}

func (u userGroup) clone(db *gorm.DB) userGroup {
	u.userGroupDo.ReplaceConnPool(db.Statement.ConnPool)
	return u
}

func (u userGroup) replaceDB(db *gorm.DB) userGroup {
	u.userGroupDo.ReplaceDB(db)
	return u
}

type userGroupDo struct{ gen.DO }

func (u userGroupDo) Debug() *userGroupDo {
	return u.withDO(u.DO.Debug())
}

func (u userGroupDo) WithContext(ctx context.Context) *userGroupDo {
	return u.withDO(u.DO.WithContext(ctx))
}

func (u userGroupDo) ReadDB() *userGroupDo {
	return u.Clauses(dbresolver.Read)
}

func (u userGroupDo) WriteDB() *userGroupDo {
	return u.Clauses(dbresolver.Write)
}

func (u userGroupDo) Session(config *gorm.Session) *userGroupDo {
	return u.withDO(u.DO.Session(config))
}

func (u userGroupDo) Clauses(conds ...clause.Expression) *userGroupDo {
	return u.withDO(u.DO.Clauses(conds...))
}

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

func (u userGroupDo) Not(conds ...gen.Condition) *userGroupDo {
	return u.withDO(u.DO.Not(conds...))
}

func (u userGroupDo) Or(conds ...gen.Condition) *userGroupDo {
	return u.withDO(u.DO.Or(conds...))
}

func (u userGroupDo) Select(conds ...field.Expr) *userGroupDo {
	return u.withDO(u.DO.Select(conds...))
}

func (u userGroupDo) Where(conds ...gen.Condition) *userGroupDo {
	return u.withDO(u.DO.Where(conds...))
}

func (u userGroupDo) Order(conds ...field.Expr) *userGroupDo {
	return u.withDO(u.DO.Order(conds...))
}

func (u userGroupDo) Distinct(cols ...field.Expr) *userGroupDo {
	return u.withDO(u.DO.Distinct(cols...))
}

func (u userGroupDo) Omit(cols ...field.Expr) *userGroupDo {
	return u.withDO(u.DO.Omit(cols...))
}

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

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

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

func (u userGroupDo) Group(cols ...field.Expr) *userGroupDo {
	return u.withDO(u.DO.Group(cols...))
}

func (u userGroupDo) Having(conds ...gen.Condition) *userGroupDo {
	return u.withDO(u.DO.Having(conds...))
}

func (u userGroupDo) Limit(limit int) *userGroupDo {
	return u.withDO(u.DO.Limit(limit))
}

func (u userGroupDo) Offset(offset int) *userGroupDo {
	return u.withDO(u.DO.Offset(offset))
}

func (u userGroupDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *userGroupDo {
	return u.withDO(u.DO.Scopes(funcs...))
}

func (u userGroupDo) Unscoped() *userGroupDo {
	return u.withDO(u.DO.Unscoped())
}

func (u userGroupDo) Create(values ...*dao.UserGroup) error {
	if len(values) == 0 {
		return nil
	}
	return u.DO.Create(values)
}

func (u userGroupDo) CreateInBatches(values []*dao.UserGroup, batchSize int) error {
	return u.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 (u userGroupDo) Save(values ...*dao.UserGroup) error {
	if len(values) == 0 {
		return nil
	}
	return u.DO.Save(values)
}

func (u userGroupDo) First() (*dao.UserGroup, error) {
	if result, err := u.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*dao.UserGroup), nil
	}
}

func (u userGroupDo) Take() (*dao.UserGroup, error) {
	if result, err := u.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*dao.UserGroup), nil
	}
}

func (u userGroupDo) Last() (*dao.UserGroup, error) {
	if result, err := u.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*dao.UserGroup), nil
	}
}

func (u userGroupDo) Find() ([]*dao.UserGroup, error) {
	result, err := u.DO.Find()
	return result.([]*dao.UserGroup), err
}

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

func (u userGroupDo) Attrs(attrs ...field.AssignExpr) *userGroupDo {
	return u.withDO(u.DO.Attrs(attrs...))
}

func (u userGroupDo) Assign(attrs ...field.AssignExpr) *userGroupDo {
	return u.withDO(u.DO.Assign(attrs...))
}

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

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

func (u userGroupDo) FirstOrInit() (*dao.UserGroup, error) {
	if result, err := u.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*dao.UserGroup), nil
	}
}

func (u userGroupDo) FirstOrCreate() (*dao.UserGroup, error) {
	if result, err := u.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*dao.UserGroup), nil
	}
}

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

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

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

func (u userGroupDo) Scan(result interface{}) (err error) {
	return u.DO.Scan(result)
}

func (u userGroupDo) Delete(models ...*dao.UserGroup) (result gen.ResultInfo, err error) {
	return u.DO.Delete(models)
}

func (u *userGroupDo) withDO(do gen.Dao) *userGroupDo {
	u.DO = *do.(*gen.DO)
	return u
}
