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

	_systemTitleEffects.systemTitleEffectsDo.UseDB(db, opts...)
	_systemTitleEffects.systemTitleEffectsDo.UseModel(&model.SystemTitleEffects{})

	tableName := _systemTitleEffects.systemTitleEffectsDo.TableName()
	_systemTitleEffects.ALL = field.NewAsterisk(tableName)
	_systemTitleEffects.ID = field.NewInt64(tableName, "id")
	_systemTitleEffects.TitleID = field.NewInt64(tableName, "title_id")
	_systemTitleEffects.EffectType = field.NewInt32(tableName, "effect_type")
	_systemTitleEffects.ParamsJSON = field.NewString(tableName, "params_json")
	_systemTitleEffects.StackingStrategy = field.NewInt32(tableName, "stacking_strategy")
	_systemTitleEffects.CapValueX1000 = field.NewInt32(tableName, "cap_value_x1000")
	_systemTitleEffects.ScopesJSON = field.NewString(tableName, "scopes_json")
	_systemTitleEffects.Sort = field.NewInt32(tableName, "sort")
	_systemTitleEffects.Status = field.NewInt32(tableName, "status")
	_systemTitleEffects.CreatedAt = field.NewTime(tableName, "created_at")
	_systemTitleEffects.DeletedAt = field.NewField(tableName, "deleted_at")

	_systemTitleEffects.fillFieldMap()

	return _systemTitleEffects
}

// systemTitleEffects 头衔效果配置表
type systemTitleEffects struct {
	systemTitleEffectsDo

	ALL              field.Asterisk
	ID               field.Int64  // 头衔效果配置主键ID
	TitleID          field.Int64  // 归属头衔ID（system_titles.id）
	EffectType       field.Int32  // 效果类型：1领券 2抽奖折扣 3签到倍数 4领道具卡 5概率加成 6奖品双倍概率
	ParamsJSON       field.String // 效果参数JSON（倍数、概率、折扣值、模板ID、频次等）
	StackingStrategy field.Int32  // 叠加策略：0最大值 1累加封顶 2首个匹配
	CapValueX1000    field.Int32  // 封顶值（千分比；如倍数或概率总封顶）
	ScopesJSON       field.String // 作用范围JSON（活动/期/分类等）
	Sort             field.Int32  // 效果排序（同一头衔内）
	Status           field.Int32  // 状态：1启用 0停用
	CreatedAt        field.Time   // 创建时间
	DeletedAt        field.Field

	fieldMap map[string]field.Expr
}

func (s systemTitleEffects) Table(newTableName string) *systemTitleEffects {
	s.systemTitleEffectsDo.UseTable(newTableName)
	return s.updateTableName(newTableName)
}

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

func (s *systemTitleEffects) updateTableName(table string) *systemTitleEffects {
	s.ALL = field.NewAsterisk(table)
	s.ID = field.NewInt64(table, "id")
	s.TitleID = field.NewInt64(table, "title_id")
	s.EffectType = field.NewInt32(table, "effect_type")
	s.ParamsJSON = field.NewString(table, "params_json")
	s.StackingStrategy = field.NewInt32(table, "stacking_strategy")
	s.CapValueX1000 = field.NewInt32(table, "cap_value_x1000")
	s.ScopesJSON = field.NewString(table, "scopes_json")
	s.Sort = field.NewInt32(table, "sort")
	s.Status = field.NewInt32(table, "status")
	s.CreatedAt = field.NewTime(table, "created_at")
	s.DeletedAt = field.NewField(table, "deleted_at")

	s.fillFieldMap()

	return s
}

func (s *systemTitleEffects) 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 *systemTitleEffects) fillFieldMap() {
	s.fieldMap = make(map[string]field.Expr, 11)
	s.fieldMap["id"] = s.ID
	s.fieldMap["title_id"] = s.TitleID
	s.fieldMap["effect_type"] = s.EffectType
	s.fieldMap["params_json"] = s.ParamsJSON
	s.fieldMap["stacking_strategy"] = s.StackingStrategy
	s.fieldMap["cap_value_x1000"] = s.CapValueX1000
	s.fieldMap["scopes_json"] = s.ScopesJSON
	s.fieldMap["sort"] = s.Sort
	s.fieldMap["status"] = s.Status
	s.fieldMap["created_at"] = s.CreatedAt
	s.fieldMap["deleted_at"] = s.DeletedAt
}

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

func (s systemTitleEffects) replaceDB(db *gorm.DB) systemTitleEffects {
	s.systemTitleEffectsDo.ReplaceDB(db)
	return s
}

type systemTitleEffectsDo struct{ gen.DO }

func (s systemTitleEffectsDo) Debug() *systemTitleEffectsDo {
	return s.withDO(s.DO.Debug())
}

func (s systemTitleEffectsDo) WithContext(ctx context.Context) *systemTitleEffectsDo {
	return s.withDO(s.DO.WithContext(ctx))
}

func (s systemTitleEffectsDo) ReadDB() *systemTitleEffectsDo {
	return s.Clauses(dbresolver.Read)
}

func (s systemTitleEffectsDo) WriteDB() *systemTitleEffectsDo {
	return s.Clauses(dbresolver.Write)
}

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

func (s systemTitleEffectsDo) Clauses(conds ...clause.Expression) *systemTitleEffectsDo {
	return s.withDO(s.DO.Clauses(conds...))
}

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

func (s systemTitleEffectsDo) Not(conds ...gen.Condition) *systemTitleEffectsDo {
	return s.withDO(s.DO.Not(conds...))
}

func (s systemTitleEffectsDo) Or(conds ...gen.Condition) *systemTitleEffectsDo {
	return s.withDO(s.DO.Or(conds...))
}

func (s systemTitleEffectsDo) Select(conds ...field.Expr) *systemTitleEffectsDo {
	return s.withDO(s.DO.Select(conds...))
}

func (s systemTitleEffectsDo) Where(conds ...gen.Condition) *systemTitleEffectsDo {
	return s.withDO(s.DO.Where(conds...))
}

func (s systemTitleEffectsDo) Order(conds ...field.Expr) *systemTitleEffectsDo {
	return s.withDO(s.DO.Order(conds...))
}

func (s systemTitleEffectsDo) Distinct(cols ...field.Expr) *systemTitleEffectsDo {
	return s.withDO(s.DO.Distinct(cols...))
}

func (s systemTitleEffectsDo) Omit(cols ...field.Expr) *systemTitleEffectsDo {
	return s.withDO(s.DO.Omit(cols...))
}

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

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

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

func (s systemTitleEffectsDo) Group(cols ...field.Expr) *systemTitleEffectsDo {
	return s.withDO(s.DO.Group(cols...))
}

func (s systemTitleEffectsDo) Having(conds ...gen.Condition) *systemTitleEffectsDo {
	return s.withDO(s.DO.Having(conds...))
}

func (s systemTitleEffectsDo) Limit(limit int) *systemTitleEffectsDo {
	return s.withDO(s.DO.Limit(limit))
}

func (s systemTitleEffectsDo) Offset(offset int) *systemTitleEffectsDo {
	return s.withDO(s.DO.Offset(offset))
}

func (s systemTitleEffectsDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *systemTitleEffectsDo {
	return s.withDO(s.DO.Scopes(funcs...))
}

func (s systemTitleEffectsDo) Unscoped() *systemTitleEffectsDo {
	return s.withDO(s.DO.Unscoped())
}

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

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

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

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

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

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

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

func (s systemTitleEffectsDo) Attrs(attrs ...field.AssignExpr) *systemTitleEffectsDo {
	return s.withDO(s.DO.Attrs(attrs...))
}

func (s systemTitleEffectsDo) Assign(attrs ...field.AssignExpr) *systemTitleEffectsDo {
	return s.withDO(s.DO.Assign(attrs...))
}

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

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

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

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

func (s systemTitleEffectsDo) FindByPage(offset int, limit int) (result []*model.SystemTitleEffects, 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 systemTitleEffectsDo) 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 systemTitleEffectsDo) Scan(result interface{}) (err error) {
	return s.DO.Scan(result)
}

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

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