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

	_activityRewardSettings.activityRewardSettingsDo.UseDB(db, opts...)
	_activityRewardSettings.activityRewardSettingsDo.UseModel(&model.ActivityRewardSettings{})

	tableName := _activityRewardSettings.activityRewardSettingsDo.TableName()
	_activityRewardSettings.ALL = field.NewAsterisk(tableName)
	_activityRewardSettings.ID = field.NewInt64(tableName, "id")
	_activityRewardSettings.CreatedAt = field.NewTime(tableName, "created_at")
	_activityRewardSettings.UpdatedAt = field.NewTime(tableName, "updated_at")
	_activityRewardSettings.IssueID = field.NewInt64(tableName, "issue_id")
	_activityRewardSettings.ProductID = field.NewInt64(tableName, "product_id")
	_activityRewardSettings.Weight = field.NewInt32(tableName, "weight")
	_activityRewardSettings.Quantity = field.NewInt64(tableName, "quantity")
	_activityRewardSettings.OriginalQty = field.NewInt64(tableName, "original_qty")
	_activityRewardSettings.Level = field.NewInt32(tableName, "level")
	_activityRewardSettings.Sort = field.NewInt32(tableName, "sort")
	_activityRewardSettings.IsBoss = field.NewInt32(tableName, "is_boss")
	_activityRewardSettings.DeletedAt = field.NewField(tableName, "deleted_at")
	_activityRewardSettings.MinScore = field.NewInt64(tableName, "min_score")

	_activityRewardSettings.fillFieldMap()

	return _activityRewardSettings
}

// activityRewardSettings 活动期-奖励配置
type activityRewardSettings struct {
	activityRewardSettingsDo

	ALL         field.Asterisk
	ID          field.Int64 // 主键ID
	CreatedAt   field.Time  // 创建时间
	UpdatedAt   field.Time  // 更新时间
	IssueID     field.Int64 // 期ID（activity_issues.id）
	ProductID   field.Int64 // 奖品对应商品ID（实物奖可填）
	Weight      field.Int32 // 抽中权重（越大越易中）
	Quantity    field.Int64 // 当前可发数量（扣减）
	OriginalQty field.Int64 // 初始配置数量
	Level       field.Int32 // 奖级（如1=S 2=A 3=B）
	Sort        field.Int32 // 排序
	IsBoss      field.Int32 // Boss 1 是 0 不是
	DeletedAt   field.Field
	MinScore    field.Int64 // 最低得分/碰数要求

	fieldMap map[string]field.Expr
}

func (a activityRewardSettings) Table(newTableName string) *activityRewardSettings {
	a.activityRewardSettingsDo.UseTable(newTableName)
	return a.updateTableName(newTableName)
}

func (a activityRewardSettings) As(alias string) *activityRewardSettings {
	a.activityRewardSettingsDo.DO = *(a.activityRewardSettingsDo.As(alias).(*gen.DO))
	return a.updateTableName(alias)
}

func (a *activityRewardSettings) updateTableName(table string) *activityRewardSettings {
	a.ALL = field.NewAsterisk(table)
	a.ID = field.NewInt64(table, "id")
	a.CreatedAt = field.NewTime(table, "created_at")
	a.UpdatedAt = field.NewTime(table, "updated_at")
	a.IssueID = field.NewInt64(table, "issue_id")
	a.ProductID = field.NewInt64(table, "product_id")
	a.Weight = field.NewInt32(table, "weight")
	a.Quantity = field.NewInt64(table, "quantity")
	a.OriginalQty = field.NewInt64(table, "original_qty")
	a.Level = field.NewInt32(table, "level")
	a.Sort = field.NewInt32(table, "sort")
	a.IsBoss = field.NewInt32(table, "is_boss")
	a.DeletedAt = field.NewField(table, "deleted_at")
	a.MinScore = field.NewInt64(table, "min_score")

	a.fillFieldMap()

	return a
}

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

func (a *activityRewardSettings) fillFieldMap() {
	a.fieldMap = make(map[string]field.Expr, 13)
	a.fieldMap["id"] = a.ID
	a.fieldMap["created_at"] = a.CreatedAt
	a.fieldMap["updated_at"] = a.UpdatedAt
	a.fieldMap["issue_id"] = a.IssueID
	a.fieldMap["product_id"] = a.ProductID
	a.fieldMap["weight"] = a.Weight
	a.fieldMap["quantity"] = a.Quantity
	a.fieldMap["original_qty"] = a.OriginalQty
	a.fieldMap["level"] = a.Level
	a.fieldMap["sort"] = a.Sort
	a.fieldMap["is_boss"] = a.IsBoss
	a.fieldMap["deleted_at"] = a.DeletedAt
	a.fieldMap["min_score"] = a.MinScore
}

func (a activityRewardSettings) clone(db *gorm.DB) activityRewardSettings {
	a.activityRewardSettingsDo.ReplaceConnPool(db.Statement.ConnPool)
	return a
}

func (a activityRewardSettings) replaceDB(db *gorm.DB) activityRewardSettings {
	a.activityRewardSettingsDo.ReplaceDB(db)
	return a
}

type activityRewardSettingsDo struct{ gen.DO }

func (a activityRewardSettingsDo) Debug() *activityRewardSettingsDo {
	return a.withDO(a.DO.Debug())
}

func (a activityRewardSettingsDo) WithContext(ctx context.Context) *activityRewardSettingsDo {
	return a.withDO(a.DO.WithContext(ctx))
}

func (a activityRewardSettingsDo) ReadDB() *activityRewardSettingsDo {
	return a.Clauses(dbresolver.Read)
}

func (a activityRewardSettingsDo) WriteDB() *activityRewardSettingsDo {
	return a.Clauses(dbresolver.Write)
}

func (a activityRewardSettingsDo) Session(config *gorm.Session) *activityRewardSettingsDo {
	return a.withDO(a.DO.Session(config))
}

func (a activityRewardSettingsDo) Clauses(conds ...clause.Expression) *activityRewardSettingsDo {
	return a.withDO(a.DO.Clauses(conds...))
}

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

func (a activityRewardSettingsDo) Not(conds ...gen.Condition) *activityRewardSettingsDo {
	return a.withDO(a.DO.Not(conds...))
}

func (a activityRewardSettingsDo) Or(conds ...gen.Condition) *activityRewardSettingsDo {
	return a.withDO(a.DO.Or(conds...))
}

func (a activityRewardSettingsDo) Select(conds ...field.Expr) *activityRewardSettingsDo {
	return a.withDO(a.DO.Select(conds...))
}

func (a activityRewardSettingsDo) Where(conds ...gen.Condition) *activityRewardSettingsDo {
	return a.withDO(a.DO.Where(conds...))
}

func (a activityRewardSettingsDo) Order(conds ...field.Expr) *activityRewardSettingsDo {
	return a.withDO(a.DO.Order(conds...))
}

func (a activityRewardSettingsDo) Distinct(cols ...field.Expr) *activityRewardSettingsDo {
	return a.withDO(a.DO.Distinct(cols...))
}

func (a activityRewardSettingsDo) Omit(cols ...field.Expr) *activityRewardSettingsDo {
	return a.withDO(a.DO.Omit(cols...))
}

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

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

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

func (a activityRewardSettingsDo) Group(cols ...field.Expr) *activityRewardSettingsDo {
	return a.withDO(a.DO.Group(cols...))
}

func (a activityRewardSettingsDo) Having(conds ...gen.Condition) *activityRewardSettingsDo {
	return a.withDO(a.DO.Having(conds...))
}

func (a activityRewardSettingsDo) Limit(limit int) *activityRewardSettingsDo {
	return a.withDO(a.DO.Limit(limit))
}

func (a activityRewardSettingsDo) Offset(offset int) *activityRewardSettingsDo {
	return a.withDO(a.DO.Offset(offset))
}

func (a activityRewardSettingsDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *activityRewardSettingsDo {
	return a.withDO(a.DO.Scopes(funcs...))
}

func (a activityRewardSettingsDo) Unscoped() *activityRewardSettingsDo {
	return a.withDO(a.DO.Unscoped())
}

func (a activityRewardSettingsDo) Create(values ...*model.ActivityRewardSettings) error {
	if len(values) == 0 {
		return nil
	}
	return a.DO.Create(values)
}

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

func (a activityRewardSettingsDo) First() (*model.ActivityRewardSettings, error) {
	if result, err := a.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.ActivityRewardSettings), nil
	}
}

func (a activityRewardSettingsDo) Take() (*model.ActivityRewardSettings, error) {
	if result, err := a.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.ActivityRewardSettings), nil
	}
}

func (a activityRewardSettingsDo) Last() (*model.ActivityRewardSettings, error) {
	if result, err := a.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.ActivityRewardSettings), nil
	}
}

func (a activityRewardSettingsDo) Find() ([]*model.ActivityRewardSettings, error) {
	result, err := a.DO.Find()
	return result.([]*model.ActivityRewardSettings), err
}

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

func (a activityRewardSettingsDo) Attrs(attrs ...field.AssignExpr) *activityRewardSettingsDo {
	return a.withDO(a.DO.Attrs(attrs...))
}

func (a activityRewardSettingsDo) Assign(attrs ...field.AssignExpr) *activityRewardSettingsDo {
	return a.withDO(a.DO.Assign(attrs...))
}

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

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

func (a activityRewardSettingsDo) FirstOrInit() (*model.ActivityRewardSettings, error) {
	if result, err := a.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.ActivityRewardSettings), nil
	}
}

func (a activityRewardSettingsDo) FirstOrCreate() (*model.ActivityRewardSettings, error) {
	if result, err := a.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.ActivityRewardSettings), nil
	}
}

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

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

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

func (a activityRewardSettingsDo) Scan(result interface{}) (err error) {
	return a.DO.Scan(result)
}

func (a activityRewardSettingsDo) Delete(models ...*model.ActivityRewardSettings) (result gen.ResultInfo, err error) {
	return a.DO.Delete(models)
}

func (a *activityRewardSettingsDo) withDO(do gen.Dao) *activityRewardSettingsDo {
	a.DO = *do.(*gen.DO)
	return a
}
