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

	_taskCenterTaskTiers.taskCenterTaskTiersDo.UseDB(db, opts...)
	_taskCenterTaskTiers.taskCenterTaskTiersDo.UseModel(&model.TaskCenterTaskTiers{})

	tableName := _taskCenterTaskTiers.taskCenterTaskTiersDo.TableName()
	_taskCenterTaskTiers.ALL = field.NewAsterisk(tableName)
	_taskCenterTaskTiers.ID = field.NewInt64(tableName, "id")
	_taskCenterTaskTiers.TaskID = field.NewInt64(tableName, "task_id")
	_taskCenterTaskTiers.Metric = field.NewString(tableName, "metric")
	_taskCenterTaskTiers.Operator = field.NewString(tableName, "operator")
	_taskCenterTaskTiers.Threshold = field.NewInt64(tableName, "threshold")
	_taskCenterTaskTiers.Window = field.NewString(tableName, "window")
	_taskCenterTaskTiers.Repeatable = field.NewInt32(tableName, "repeatable")
	_taskCenterTaskTiers.Priority = field.NewInt32(tableName, "priority")
	_taskCenterTaskTiers.CreatedAt = field.NewTime(tableName, "created_at")
	_taskCenterTaskTiers.UpdatedAt = field.NewTime(tableName, "updated_at")
	_taskCenterTaskTiers.ExtraParams = field.NewString(tableName, "extra_params")
	_taskCenterTaskTiers.ActivityID = field.NewInt32(tableName, "activity_id")

	_taskCenterTaskTiers.fillFieldMap()

	return _taskCenterTaskTiers
}

// taskCenterTaskTiers 任务中心-档位配置
type taskCenterTaskTiers struct {
	taskCenterTaskTiersDo

	ALL         field.Asterisk
	ID          field.Int64  // 主键ID
	TaskID      field.Int64  // 关联任务ID（task_center_tasks.id）
	Metric      field.String // 指标：first_order|order_count|invite_count
	Operator    field.String // 比较符：>= 或 ==
	Threshold   field.Int64  // 阈值（数量或布尔首单）
	Window      field.String // 时间窗口：activity_period|since_registration
	Repeatable  field.Int32  // 是否每档一次：0否 1是
	Priority    field.Int32  // 匹配优先级（数值越小越先匹配）
	CreatedAt   field.Time   // 创建时间
	UpdatedAt   field.Time   // 更新时间
	ExtraParams field.String // 额外参数配置(如消费门槛)
	ActivityID  field.Int32  // 活动ID

	fieldMap map[string]field.Expr
}

func (t taskCenterTaskTiers) Table(newTableName string) *taskCenterTaskTiers {
	t.taskCenterTaskTiersDo.UseTable(newTableName)
	return t.updateTableName(newTableName)
}

func (t taskCenterTaskTiers) As(alias string) *taskCenterTaskTiers {
	t.taskCenterTaskTiersDo.DO = *(t.taskCenterTaskTiersDo.As(alias).(*gen.DO))
	return t.updateTableName(alias)
}

func (t *taskCenterTaskTiers) updateTableName(table string) *taskCenterTaskTiers {
	t.ALL = field.NewAsterisk(table)
	t.ID = field.NewInt64(table, "id")
	t.TaskID = field.NewInt64(table, "task_id")
	t.Metric = field.NewString(table, "metric")
	t.Operator = field.NewString(table, "operator")
	t.Threshold = field.NewInt64(table, "threshold")
	t.Window = field.NewString(table, "window")
	t.Repeatable = field.NewInt32(table, "repeatable")
	t.Priority = field.NewInt32(table, "priority")
	t.CreatedAt = field.NewTime(table, "created_at")
	t.UpdatedAt = field.NewTime(table, "updated_at")
	t.ExtraParams = field.NewString(table, "extra_params")
	t.ActivityID = field.NewInt32(table, "activity_id")

	t.fillFieldMap()

	return t
}

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

func (t *taskCenterTaskTiers) fillFieldMap() {
	t.fieldMap = make(map[string]field.Expr, 12)
	t.fieldMap["id"] = t.ID
	t.fieldMap["task_id"] = t.TaskID
	t.fieldMap["metric"] = t.Metric
	t.fieldMap["operator"] = t.Operator
	t.fieldMap["threshold"] = t.Threshold
	t.fieldMap["window"] = t.Window
	t.fieldMap["repeatable"] = t.Repeatable
	t.fieldMap["priority"] = t.Priority
	t.fieldMap["created_at"] = t.CreatedAt
	t.fieldMap["updated_at"] = t.UpdatedAt
	t.fieldMap["extra_params"] = t.ExtraParams
	t.fieldMap["activity_id"] = t.ActivityID
}

func (t taskCenterTaskTiers) clone(db *gorm.DB) taskCenterTaskTiers {
	t.taskCenterTaskTiersDo.ReplaceConnPool(db.Statement.ConnPool)
	return t
}

func (t taskCenterTaskTiers) replaceDB(db *gorm.DB) taskCenterTaskTiers {
	t.taskCenterTaskTiersDo.ReplaceDB(db)
	return t
}

type taskCenterTaskTiersDo struct{ gen.DO }

func (t taskCenterTaskTiersDo) Debug() *taskCenterTaskTiersDo {
	return t.withDO(t.DO.Debug())
}

func (t taskCenterTaskTiersDo) WithContext(ctx context.Context) *taskCenterTaskTiersDo {
	return t.withDO(t.DO.WithContext(ctx))
}

func (t taskCenterTaskTiersDo) ReadDB() *taskCenterTaskTiersDo {
	return t.Clauses(dbresolver.Read)
}

func (t taskCenterTaskTiersDo) WriteDB() *taskCenterTaskTiersDo {
	return t.Clauses(dbresolver.Write)
}

func (t taskCenterTaskTiersDo) Session(config *gorm.Session) *taskCenterTaskTiersDo {
	return t.withDO(t.DO.Session(config))
}

func (t taskCenterTaskTiersDo) Clauses(conds ...clause.Expression) *taskCenterTaskTiersDo {
	return t.withDO(t.DO.Clauses(conds...))
}

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

func (t taskCenterTaskTiersDo) Not(conds ...gen.Condition) *taskCenterTaskTiersDo {
	return t.withDO(t.DO.Not(conds...))
}

func (t taskCenterTaskTiersDo) Or(conds ...gen.Condition) *taskCenterTaskTiersDo {
	return t.withDO(t.DO.Or(conds...))
}

func (t taskCenterTaskTiersDo) Select(conds ...field.Expr) *taskCenterTaskTiersDo {
	return t.withDO(t.DO.Select(conds...))
}

func (t taskCenterTaskTiersDo) Where(conds ...gen.Condition) *taskCenterTaskTiersDo {
	return t.withDO(t.DO.Where(conds...))
}

func (t taskCenterTaskTiersDo) Order(conds ...field.Expr) *taskCenterTaskTiersDo {
	return t.withDO(t.DO.Order(conds...))
}

func (t taskCenterTaskTiersDo) Distinct(cols ...field.Expr) *taskCenterTaskTiersDo {
	return t.withDO(t.DO.Distinct(cols...))
}

func (t taskCenterTaskTiersDo) Omit(cols ...field.Expr) *taskCenterTaskTiersDo {
	return t.withDO(t.DO.Omit(cols...))
}

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

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

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

func (t taskCenterTaskTiersDo) Group(cols ...field.Expr) *taskCenterTaskTiersDo {
	return t.withDO(t.DO.Group(cols...))
}

func (t taskCenterTaskTiersDo) Having(conds ...gen.Condition) *taskCenterTaskTiersDo {
	return t.withDO(t.DO.Having(conds...))
}

func (t taskCenterTaskTiersDo) Limit(limit int) *taskCenterTaskTiersDo {
	return t.withDO(t.DO.Limit(limit))
}

func (t taskCenterTaskTiersDo) Offset(offset int) *taskCenterTaskTiersDo {
	return t.withDO(t.DO.Offset(offset))
}

func (t taskCenterTaskTiersDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *taskCenterTaskTiersDo {
	return t.withDO(t.DO.Scopes(funcs...))
}

func (t taskCenterTaskTiersDo) Unscoped() *taskCenterTaskTiersDo {
	return t.withDO(t.DO.Unscoped())
}

func (t taskCenterTaskTiersDo) Create(values ...*model.TaskCenterTaskTiers) error {
	if len(values) == 0 {
		return nil
	}
	return t.DO.Create(values)
}

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

func (t taskCenterTaskTiersDo) First() (*model.TaskCenterTaskTiers, error) {
	if result, err := t.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.TaskCenterTaskTiers), nil
	}
}

func (t taskCenterTaskTiersDo) Take() (*model.TaskCenterTaskTiers, error) {
	if result, err := t.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.TaskCenterTaskTiers), nil
	}
}

func (t taskCenterTaskTiersDo) Last() (*model.TaskCenterTaskTiers, error) {
	if result, err := t.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.TaskCenterTaskTiers), nil
	}
}

func (t taskCenterTaskTiersDo) Find() ([]*model.TaskCenterTaskTiers, error) {
	result, err := t.DO.Find()
	return result.([]*model.TaskCenterTaskTiers), err
}

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

func (t taskCenterTaskTiersDo) Attrs(attrs ...field.AssignExpr) *taskCenterTaskTiersDo {
	return t.withDO(t.DO.Attrs(attrs...))
}

func (t taskCenterTaskTiersDo) Assign(attrs ...field.AssignExpr) *taskCenterTaskTiersDo {
	return t.withDO(t.DO.Assign(attrs...))
}

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

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

func (t taskCenterTaskTiersDo) FirstOrInit() (*model.TaskCenterTaskTiers, error) {
	if result, err := t.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.TaskCenterTaskTiers), nil
	}
}

func (t taskCenterTaskTiersDo) FirstOrCreate() (*model.TaskCenterTaskTiers, error) {
	if result, err := t.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.TaskCenterTaskTiers), nil
	}
}

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

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

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

func (t taskCenterTaskTiersDo) Scan(result interface{}) (err error) {
	return t.DO.Scan(result)
}

func (t taskCenterTaskTiersDo) Delete(models ...*model.TaskCenterTaskTiers) (result gen.ResultInfo, err error) {
	return t.DO.Delete(models)
}

func (t *taskCenterTaskTiersDo) withDO(do gen.Dao) *taskCenterTaskTiersDo {
	t.DO = *do.(*gen.DO)
	return t
}
