// 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/ch3nnn/webstack-go/internal/dal/model"
)

func newStCategory(db *gorm.DB, opts ...gen.DOOption) stCategory {
	_stCategory := stCategory{}

	_stCategory.stCategoryDo.UseDB(db, opts...)
	_stCategory.stCategoryDo.UseModel(&model.StCategory{})

	tableName := _stCategory.stCategoryDo.TableName()
	_stCategory.ALL = field.NewAsterisk(tableName)
	_stCategory.ID = field.NewInt(tableName, "id")
	_stCategory.ParentID = field.NewInt(tableName, "parent_id")
	_stCategory.Sort = field.NewInt(tableName, "sort")
	_stCategory.Title = field.NewString(tableName, "title")
	_stCategory.Icon = field.NewString(tableName, "icon")
	_stCategory.Level = field.NewInt32(tableName, "level")
	_stCategory.IsUsed = field.NewBool(tableName, "is_used")
	_stCategory.CreatedAt = field.NewTime(tableName, "created_at")
	_stCategory.UpdatedAt = field.NewTime(tableName, "updated_at")
	_stCategory.DeletedAt = field.NewTime(tableName, "deleted_at")

	_stCategory.fillFieldMap()

	return _stCategory
}

type stCategory struct {
	stCategoryDo stCategoryDo

	ALL       field.Asterisk
	ID        field.Int
	ParentID  field.Int
	Sort      field.Int
	Title     field.String
	Icon      field.String
	Level     field.Int32
	IsUsed    field.Bool
	CreatedAt field.Time
	UpdatedAt field.Time
	DeletedAt field.Time

	fieldMap map[string]field.Expr
}

func (s stCategory) Table(newTableName string) *stCategory {
	s.stCategoryDo.UseTable(newTableName)
	return s.updateTableName(newTableName)
}

func (s stCategory) As(alias string) *stCategory {
	s.stCategoryDo.DO = *(s.stCategoryDo.As(alias).(*gen.DO))
	return s.updateTableName(alias)
}

func (s *stCategory) updateTableName(table string) *stCategory {
	s.ALL = field.NewAsterisk(table)
	s.ID = field.NewInt(table, "id")
	s.ParentID = field.NewInt(table, "parent_id")
	s.Sort = field.NewInt(table, "sort")
	s.Title = field.NewString(table, "title")
	s.Icon = field.NewString(table, "icon")
	s.Level = field.NewInt32(table, "level")
	s.IsUsed = field.NewBool(table, "is_used")
	s.CreatedAt = field.NewTime(table, "created_at")
	s.UpdatedAt = field.NewTime(table, "updated_at")
	s.DeletedAt = field.NewTime(table, "deleted_at")

	s.fillFieldMap()

	return s
}

func (s *stCategory) WithContext(ctx context.Context) IStCategoryDo {
	return s.stCategoryDo.WithContext(ctx)
}

func (s stCategory) TableName() string { return s.stCategoryDo.TableName() }

func (s stCategory) Alias() string { return s.stCategoryDo.Alias() }

func (s stCategory) Columns(cols ...field.Expr) gen.Columns { return s.stCategoryDo.Columns(cols...) }

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

func (s *stCategory) fillFieldMap() {
	s.fieldMap = make(map[string]field.Expr, 10)
	s.fieldMap["id"] = s.ID
	s.fieldMap["parent_id"] = s.ParentID
	s.fieldMap["sort"] = s.Sort
	s.fieldMap["title"] = s.Title
	s.fieldMap["icon"] = s.Icon
	s.fieldMap["level"] = s.Level
	s.fieldMap["is_used"] = s.IsUsed
	s.fieldMap["created_at"] = s.CreatedAt
	s.fieldMap["updated_at"] = s.UpdatedAt
	s.fieldMap["deleted_at"] = s.DeletedAt
}

func (s stCategory) clone(db *gorm.DB) stCategory {
	s.stCategoryDo.ReplaceConnPool(db.Statement.ConnPool)
	return s
}

func (s stCategory) replaceDB(db *gorm.DB) stCategory {
	s.stCategoryDo.ReplaceDB(db)
	return s
}

type stCategoryDo struct{ gen.DO }

type IStCategoryDo interface {
	gen.SubQuery
	Debug() IStCategoryDo
	WithContext(ctx context.Context) IStCategoryDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() IStCategoryDo
	WriteDB() IStCategoryDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) IStCategoryDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) IStCategoryDo
	Not(conds ...gen.Condition) IStCategoryDo
	Or(conds ...gen.Condition) IStCategoryDo
	Select(conds ...field.Expr) IStCategoryDo
	Where(conds ...gen.Condition) IStCategoryDo
	Order(conds ...field.Expr) IStCategoryDo
	Distinct(cols ...field.Expr) IStCategoryDo
	Omit(cols ...field.Expr) IStCategoryDo
	Join(table schema.Tabler, on ...field.Expr) IStCategoryDo
	LeftJoin(table schema.Tabler, on ...field.Expr) IStCategoryDo
	RightJoin(table schema.Tabler, on ...field.Expr) IStCategoryDo
	Group(cols ...field.Expr) IStCategoryDo
	Having(conds ...gen.Condition) IStCategoryDo
	Limit(limit int) IStCategoryDo
	Offset(offset int) IStCategoryDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) IStCategoryDo
	Unscoped() IStCategoryDo
	Create(values ...*model.StCategory) error
	CreateInBatches(values []*model.StCategory, batchSize int) error
	Save(values ...*model.StCategory) error
	First() (*model.StCategory, error)
	Take() (*model.StCategory, error)
	Last() (*model.StCategory, error)
	Find() ([]*model.StCategory, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.StCategory, err error)
	FindInBatches(result *[]*model.StCategory, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.StCategory) (info gen.ResultInfo, err error)
	Update(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	Updates(value interface{}) (info gen.ResultInfo, err error)
	UpdateColumn(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateColumnSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	UpdateColumns(value interface{}) (info gen.ResultInfo, err error)
	UpdateFrom(q gen.SubQuery) gen.Dao
	Attrs(attrs ...field.AssignExpr) IStCategoryDo
	Assign(attrs ...field.AssignExpr) IStCategoryDo
	Joins(fields ...field.RelationField) IStCategoryDo
	Preload(fields ...field.RelationField) IStCategoryDo
	FirstOrInit() (*model.StCategory, error)
	FirstOrCreate() (*model.StCategory, error)
	FindByPage(offset int, limit int) (result []*model.StCategory, count int64, err error)
	ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
	Scan(result interface{}) (err error)
	Returning(value interface{}, columns ...string) IStCategoryDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (s stCategoryDo) Debug() IStCategoryDo {
	return s.withDO(s.DO.Debug())
}

func (s stCategoryDo) WithContext(ctx context.Context) IStCategoryDo {
	return s.withDO(s.DO.WithContext(ctx))
}

func (s stCategoryDo) ReadDB() IStCategoryDo {
	return s.Clauses(dbresolver.Read)
}

func (s stCategoryDo) WriteDB() IStCategoryDo {
	return s.Clauses(dbresolver.Write)
}

func (s stCategoryDo) Session(config *gorm.Session) IStCategoryDo {
	return s.withDO(s.DO.Session(config))
}

func (s stCategoryDo) Clauses(conds ...clause.Expression) IStCategoryDo {
	return s.withDO(s.DO.Clauses(conds...))
}

func (s stCategoryDo) Returning(value interface{}, columns ...string) IStCategoryDo {
	return s.withDO(s.DO.Returning(value, columns...))
}

func (s stCategoryDo) Not(conds ...gen.Condition) IStCategoryDo {
	return s.withDO(s.DO.Not(conds...))
}

func (s stCategoryDo) Or(conds ...gen.Condition) IStCategoryDo {
	return s.withDO(s.DO.Or(conds...))
}

func (s stCategoryDo) Select(conds ...field.Expr) IStCategoryDo {
	return s.withDO(s.DO.Select(conds...))
}

func (s stCategoryDo) Where(conds ...gen.Condition) IStCategoryDo {
	return s.withDO(s.DO.Where(conds...))
}

func (s stCategoryDo) Order(conds ...field.Expr) IStCategoryDo {
	return s.withDO(s.DO.Order(conds...))
}

func (s stCategoryDo) Distinct(cols ...field.Expr) IStCategoryDo {
	return s.withDO(s.DO.Distinct(cols...))
}

func (s stCategoryDo) Omit(cols ...field.Expr) IStCategoryDo {
	return s.withDO(s.DO.Omit(cols...))
}

func (s stCategoryDo) Join(table schema.Tabler, on ...field.Expr) IStCategoryDo {
	return s.withDO(s.DO.Join(table, on...))
}

func (s stCategoryDo) LeftJoin(table schema.Tabler, on ...field.Expr) IStCategoryDo {
	return s.withDO(s.DO.LeftJoin(table, on...))
}

func (s stCategoryDo) RightJoin(table schema.Tabler, on ...field.Expr) IStCategoryDo {
	return s.withDO(s.DO.RightJoin(table, on...))
}

func (s stCategoryDo) Group(cols ...field.Expr) IStCategoryDo {
	return s.withDO(s.DO.Group(cols...))
}

func (s stCategoryDo) Having(conds ...gen.Condition) IStCategoryDo {
	return s.withDO(s.DO.Having(conds...))
}

func (s stCategoryDo) Limit(limit int) IStCategoryDo {
	return s.withDO(s.DO.Limit(limit))
}

func (s stCategoryDo) Offset(offset int) IStCategoryDo {
	return s.withDO(s.DO.Offset(offset))
}

func (s stCategoryDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IStCategoryDo {
	return s.withDO(s.DO.Scopes(funcs...))
}

func (s stCategoryDo) Unscoped() IStCategoryDo {
	return s.withDO(s.DO.Unscoped())
}

func (s stCategoryDo) Create(values ...*model.StCategory) error {
	if len(values) == 0 {
		return nil
	}
	return s.DO.Create(values)
}

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

func (s stCategoryDo) First() (*model.StCategory, error) {
	if result, err := s.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.StCategory), nil
	}
}

func (s stCategoryDo) Take() (*model.StCategory, error) {
	if result, err := s.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.StCategory), nil
	}
}

func (s stCategoryDo) Last() (*model.StCategory, error) {
	if result, err := s.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.StCategory), nil
	}
}

func (s stCategoryDo) Find() ([]*model.StCategory, error) {
	result, err := s.DO.Find()
	return result.([]*model.StCategory), err
}

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

func (s stCategoryDo) Attrs(attrs ...field.AssignExpr) IStCategoryDo {
	return s.withDO(s.DO.Attrs(attrs...))
}

func (s stCategoryDo) Assign(attrs ...field.AssignExpr) IStCategoryDo {
	return s.withDO(s.DO.Assign(attrs...))
}

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

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

func (s stCategoryDo) FirstOrInit() (*model.StCategory, error) {
	if result, err := s.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.StCategory), nil
	}
}

func (s stCategoryDo) FirstOrCreate() (*model.StCategory, error) {
	if result, err := s.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.StCategory), nil
	}
}

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

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

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

func (s stCategoryDo) Scan(result interface{}) (err error) {
	return s.DO.Scan(result)
}

func (s stCategoryDo) Delete(models ...*model.StCategory) (result gen.ResultInfo, err error) {
	return s.DO.Delete(models)
}

func (s *stCategoryDo) withDO(do gen.Dao) *stCategoryDo {
	s.DO = *do.(*gen.DO)
	return s
}
