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

	_productCategories.productCategoriesDo.UseDB(db, opts...)
	_productCategories.productCategoriesDo.UseModel(&model.ProductCategories{})

	tableName := _productCategories.productCategoriesDo.TableName()
	_productCategories.ALL = field.NewAsterisk(tableName)
	_productCategories.ID = field.NewInt64(tableName, "id")
	_productCategories.CreatedAt = field.NewTime(tableName, "created_at")
	_productCategories.UpdatedAt = field.NewTime(tableName, "updated_at")
	_productCategories.Name = field.NewString(tableName, "name")
	_productCategories.ParentID = field.NewInt64(tableName, "parent_id")
	_productCategories.Status = field.NewInt32(tableName, "status")
	_productCategories.DeletedAt = field.NewField(tableName, "deleted_at")

	_productCategories.fillFieldMap()

	return _productCategories
}

// productCategories 商品分类
type productCategories struct {
	productCategoriesDo

	ALL       field.Asterisk
	ID        field.Int64  // 主键ID
	CreatedAt field.Time   // 创建时间
	UpdatedAt field.Time   // 更新时间
	Name      field.String // 分类名称
	ParentID  field.Int64  // 父分类ID（可空）
	Status    field.Int32  // 状态：1启用 2禁用
	DeletedAt field.Field

	fieldMap map[string]field.Expr
}

func (p productCategories) Table(newTableName string) *productCategories {
	p.productCategoriesDo.UseTable(newTableName)
	return p.updateTableName(newTableName)
}

func (p productCategories) As(alias string) *productCategories {
	p.productCategoriesDo.DO = *(p.productCategoriesDo.As(alias).(*gen.DO))
	return p.updateTableName(alias)
}

func (p *productCategories) updateTableName(table string) *productCategories {
	p.ALL = field.NewAsterisk(table)
	p.ID = field.NewInt64(table, "id")
	p.CreatedAt = field.NewTime(table, "created_at")
	p.UpdatedAt = field.NewTime(table, "updated_at")
	p.Name = field.NewString(table, "name")
	p.ParentID = field.NewInt64(table, "parent_id")
	p.Status = field.NewInt32(table, "status")
	p.DeletedAt = field.NewField(table, "deleted_at")

	p.fillFieldMap()

	return p
}

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

func (p *productCategories) fillFieldMap() {
	p.fieldMap = make(map[string]field.Expr, 7)
	p.fieldMap["id"] = p.ID
	p.fieldMap["created_at"] = p.CreatedAt
	p.fieldMap["updated_at"] = p.UpdatedAt
	p.fieldMap["name"] = p.Name
	p.fieldMap["parent_id"] = p.ParentID
	p.fieldMap["status"] = p.Status
	p.fieldMap["deleted_at"] = p.DeletedAt
}

func (p productCategories) clone(db *gorm.DB) productCategories {
	p.productCategoriesDo.ReplaceConnPool(db.Statement.ConnPool)
	return p
}

func (p productCategories) replaceDB(db *gorm.DB) productCategories {
	p.productCategoriesDo.ReplaceDB(db)
	return p
}

type productCategoriesDo struct{ gen.DO }

func (p productCategoriesDo) Debug() *productCategoriesDo {
	return p.withDO(p.DO.Debug())
}

func (p productCategoriesDo) WithContext(ctx context.Context) *productCategoriesDo {
	return p.withDO(p.DO.WithContext(ctx))
}

func (p productCategoriesDo) ReadDB() *productCategoriesDo {
	return p.Clauses(dbresolver.Read)
}

func (p productCategoriesDo) WriteDB() *productCategoriesDo {
	return p.Clauses(dbresolver.Write)
}

func (p productCategoriesDo) Session(config *gorm.Session) *productCategoriesDo {
	return p.withDO(p.DO.Session(config))
}

func (p productCategoriesDo) Clauses(conds ...clause.Expression) *productCategoriesDo {
	return p.withDO(p.DO.Clauses(conds...))
}

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

func (p productCategoriesDo) Not(conds ...gen.Condition) *productCategoriesDo {
	return p.withDO(p.DO.Not(conds...))
}

func (p productCategoriesDo) Or(conds ...gen.Condition) *productCategoriesDo {
	return p.withDO(p.DO.Or(conds...))
}

func (p productCategoriesDo) Select(conds ...field.Expr) *productCategoriesDo {
	return p.withDO(p.DO.Select(conds...))
}

func (p productCategoriesDo) Where(conds ...gen.Condition) *productCategoriesDo {
	return p.withDO(p.DO.Where(conds...))
}

func (p productCategoriesDo) Order(conds ...field.Expr) *productCategoriesDo {
	return p.withDO(p.DO.Order(conds...))
}

func (p productCategoriesDo) Distinct(cols ...field.Expr) *productCategoriesDo {
	return p.withDO(p.DO.Distinct(cols...))
}

func (p productCategoriesDo) Omit(cols ...field.Expr) *productCategoriesDo {
	return p.withDO(p.DO.Omit(cols...))
}

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

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

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

func (p productCategoriesDo) Group(cols ...field.Expr) *productCategoriesDo {
	return p.withDO(p.DO.Group(cols...))
}

func (p productCategoriesDo) Having(conds ...gen.Condition) *productCategoriesDo {
	return p.withDO(p.DO.Having(conds...))
}

func (p productCategoriesDo) Limit(limit int) *productCategoriesDo {
	return p.withDO(p.DO.Limit(limit))
}

func (p productCategoriesDo) Offset(offset int) *productCategoriesDo {
	return p.withDO(p.DO.Offset(offset))
}

func (p productCategoriesDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *productCategoriesDo {
	return p.withDO(p.DO.Scopes(funcs...))
}

func (p productCategoriesDo) Unscoped() *productCategoriesDo {
	return p.withDO(p.DO.Unscoped())
}

func (p productCategoriesDo) Create(values ...*model.ProductCategories) error {
	if len(values) == 0 {
		return nil
	}
	return p.DO.Create(values)
}

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

func (p productCategoriesDo) First() (*model.ProductCategories, error) {
	if result, err := p.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.ProductCategories), nil
	}
}

func (p productCategoriesDo) Take() (*model.ProductCategories, error) {
	if result, err := p.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.ProductCategories), nil
	}
}

func (p productCategoriesDo) Last() (*model.ProductCategories, error) {
	if result, err := p.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.ProductCategories), nil
	}
}

func (p productCategoriesDo) Find() ([]*model.ProductCategories, error) {
	result, err := p.DO.Find()
	return result.([]*model.ProductCategories), err
}

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

func (p productCategoriesDo) Attrs(attrs ...field.AssignExpr) *productCategoriesDo {
	return p.withDO(p.DO.Attrs(attrs...))
}

func (p productCategoriesDo) Assign(attrs ...field.AssignExpr) *productCategoriesDo {
	return p.withDO(p.DO.Assign(attrs...))
}

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

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

func (p productCategoriesDo) FirstOrInit() (*model.ProductCategories, error) {
	if result, err := p.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.ProductCategories), nil
	}
}

func (p productCategoriesDo) FirstOrCreate() (*model.ProductCategories, error) {
	if result, err := p.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.ProductCategories), nil
	}
}

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

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

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

func (p productCategoriesDo) Scan(result interface{}) (err error) {
	return p.DO.Scan(result)
}

func (p productCategoriesDo) Delete(models ...*model.ProductCategories) (result gen.ResultInfo, err error) {
	return p.DO.Delete(models)
}

func (p *productCategoriesDo) withDO(do gen.Dao) *productCategoriesDo {
	p.DO = *do.(*gen.DO)
	return p
}
