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

	_userTitleEffectClaims.userTitleEffectClaimsDo.UseDB(db, opts...)
	_userTitleEffectClaims.userTitleEffectClaimsDo.UseModel(&model.UserTitleEffectClaims{})

	tableName := _userTitleEffectClaims.userTitleEffectClaimsDo.TableName()
	_userTitleEffectClaims.ALL = field.NewAsterisk(tableName)
	_userTitleEffectClaims.ID = field.NewInt64(tableName, "id")
	_userTitleEffectClaims.UserID = field.NewInt64(tableName, "user_id")
	_userTitleEffectClaims.TitleID = field.NewInt64(tableName, "title_id")
	_userTitleEffectClaims.EffectType = field.NewInt32(tableName, "effect_type")
	_userTitleEffectClaims.TargetTemplateID = field.NewInt64(tableName, "target_template_id")
	_userTitleEffectClaims.PeriodKey = field.NewString(tableName, "period_key")
	_userTitleEffectClaims.ClaimCount = field.NewInt32(tableName, "claim_count")
	_userTitleEffectClaims.LastClaimAt = field.NewTime(tableName, "last_claim_at")
	_userTitleEffectClaims.CreatedAt = field.NewTime(tableName, "created_at")

	_userTitleEffectClaims.fillFieldMap()

	return _userTitleEffectClaims
}

// userTitleEffectClaims 领取型权益限流表
type userTitleEffectClaims struct {
	userTitleEffectClaimsDo

	ALL              field.Asterisk
	ID               field.Int64  // 头衔领取型权益限流主键ID
	UserID           field.Int64  // 用户ID（users.id）
	TitleID          field.Int64  // 头衔ID（system_titles.id）
	EffectType       field.Int32  // 效果类型（与system_title_effects.effect_type一致）
	TargetTemplateID field.Int64  // 目标模板ID（券模板/卡模板等）
	PeriodKey        field.String // 周期键（如每日YYYYMMDD、每周YYYYWW、每月YYYYMM）
	ClaimCount       field.Int32  // 当期已领取次数
	LastClaimAt      field.Time   // 最近领取时间
	CreatedAt        field.Time   // 创建时间

	fieldMap map[string]field.Expr
}

func (u userTitleEffectClaims) Table(newTableName string) *userTitleEffectClaims {
	u.userTitleEffectClaimsDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

func (u userTitleEffectClaims) As(alias string) *userTitleEffectClaims {
	u.userTitleEffectClaimsDo.DO = *(u.userTitleEffectClaimsDo.As(alias).(*gen.DO))
	return u.updateTableName(alias)
}

func (u *userTitleEffectClaims) updateTableName(table string) *userTitleEffectClaims {
	u.ALL = field.NewAsterisk(table)
	u.ID = field.NewInt64(table, "id")
	u.UserID = field.NewInt64(table, "user_id")
	u.TitleID = field.NewInt64(table, "title_id")
	u.EffectType = field.NewInt32(table, "effect_type")
	u.TargetTemplateID = field.NewInt64(table, "target_template_id")
	u.PeriodKey = field.NewString(table, "period_key")
	u.ClaimCount = field.NewInt32(table, "claim_count")
	u.LastClaimAt = field.NewTime(table, "last_claim_at")
	u.CreatedAt = field.NewTime(table, "created_at")

	u.fillFieldMap()

	return u
}

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

func (u *userTitleEffectClaims) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 9)
	u.fieldMap["id"] = u.ID
	u.fieldMap["user_id"] = u.UserID
	u.fieldMap["title_id"] = u.TitleID
	u.fieldMap["effect_type"] = u.EffectType
	u.fieldMap["target_template_id"] = u.TargetTemplateID
	u.fieldMap["period_key"] = u.PeriodKey
	u.fieldMap["claim_count"] = u.ClaimCount
	u.fieldMap["last_claim_at"] = u.LastClaimAt
	u.fieldMap["created_at"] = u.CreatedAt
}

func (u userTitleEffectClaims) clone(db *gorm.DB) userTitleEffectClaims {
	u.userTitleEffectClaimsDo.ReplaceConnPool(db.Statement.ConnPool)
	return u
}

func (u userTitleEffectClaims) replaceDB(db *gorm.DB) userTitleEffectClaims {
	u.userTitleEffectClaimsDo.ReplaceDB(db)
	return u
}

type userTitleEffectClaimsDo struct{ gen.DO }

func (u userTitleEffectClaimsDo) Debug() *userTitleEffectClaimsDo {
	return u.withDO(u.DO.Debug())
}

func (u userTitleEffectClaimsDo) WithContext(ctx context.Context) *userTitleEffectClaimsDo {
	return u.withDO(u.DO.WithContext(ctx))
}

func (u userTitleEffectClaimsDo) ReadDB() *userTitleEffectClaimsDo {
	return u.Clauses(dbresolver.Read)
}

func (u userTitleEffectClaimsDo) WriteDB() *userTitleEffectClaimsDo {
	return u.Clauses(dbresolver.Write)
}

func (u userTitleEffectClaimsDo) Session(config *gorm.Session) *userTitleEffectClaimsDo {
	return u.withDO(u.DO.Session(config))
}

func (u userTitleEffectClaimsDo) Clauses(conds ...clause.Expression) *userTitleEffectClaimsDo {
	return u.withDO(u.DO.Clauses(conds...))
}

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

func (u userTitleEffectClaimsDo) Not(conds ...gen.Condition) *userTitleEffectClaimsDo {
	return u.withDO(u.DO.Not(conds...))
}

func (u userTitleEffectClaimsDo) Or(conds ...gen.Condition) *userTitleEffectClaimsDo {
	return u.withDO(u.DO.Or(conds...))
}

func (u userTitleEffectClaimsDo) Select(conds ...field.Expr) *userTitleEffectClaimsDo {
	return u.withDO(u.DO.Select(conds...))
}

func (u userTitleEffectClaimsDo) Where(conds ...gen.Condition) *userTitleEffectClaimsDo {
	return u.withDO(u.DO.Where(conds...))
}

func (u userTitleEffectClaimsDo) Order(conds ...field.Expr) *userTitleEffectClaimsDo {
	return u.withDO(u.DO.Order(conds...))
}

func (u userTitleEffectClaimsDo) Distinct(cols ...field.Expr) *userTitleEffectClaimsDo {
	return u.withDO(u.DO.Distinct(cols...))
}

func (u userTitleEffectClaimsDo) Omit(cols ...field.Expr) *userTitleEffectClaimsDo {
	return u.withDO(u.DO.Omit(cols...))
}

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

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

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

func (u userTitleEffectClaimsDo) Group(cols ...field.Expr) *userTitleEffectClaimsDo {
	return u.withDO(u.DO.Group(cols...))
}

func (u userTitleEffectClaimsDo) Having(conds ...gen.Condition) *userTitleEffectClaimsDo {
	return u.withDO(u.DO.Having(conds...))
}

func (u userTitleEffectClaimsDo) Limit(limit int) *userTitleEffectClaimsDo {
	return u.withDO(u.DO.Limit(limit))
}

func (u userTitleEffectClaimsDo) Offset(offset int) *userTitleEffectClaimsDo {
	return u.withDO(u.DO.Offset(offset))
}

func (u userTitleEffectClaimsDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *userTitleEffectClaimsDo {
	return u.withDO(u.DO.Scopes(funcs...))
}

func (u userTitleEffectClaimsDo) Unscoped() *userTitleEffectClaimsDo {
	return u.withDO(u.DO.Unscoped())
}

func (u userTitleEffectClaimsDo) Create(values ...*model.UserTitleEffectClaims) error {
	if len(values) == 0 {
		return nil
	}
	return u.DO.Create(values)
}

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

func (u userTitleEffectClaimsDo) First() (*model.UserTitleEffectClaims, error) {
	if result, err := u.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.UserTitleEffectClaims), nil
	}
}

func (u userTitleEffectClaimsDo) Take() (*model.UserTitleEffectClaims, error) {
	if result, err := u.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.UserTitleEffectClaims), nil
	}
}

func (u userTitleEffectClaimsDo) Last() (*model.UserTitleEffectClaims, error) {
	if result, err := u.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.UserTitleEffectClaims), nil
	}
}

func (u userTitleEffectClaimsDo) Find() ([]*model.UserTitleEffectClaims, error) {
	result, err := u.DO.Find()
	return result.([]*model.UserTitleEffectClaims), err
}

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

func (u userTitleEffectClaimsDo) Attrs(attrs ...field.AssignExpr) *userTitleEffectClaimsDo {
	return u.withDO(u.DO.Attrs(attrs...))
}

func (u userTitleEffectClaimsDo) Assign(attrs ...field.AssignExpr) *userTitleEffectClaimsDo {
	return u.withDO(u.DO.Assign(attrs...))
}

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

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

func (u userTitleEffectClaimsDo) FirstOrInit() (*model.UserTitleEffectClaims, error) {
	if result, err := u.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.UserTitleEffectClaims), nil
	}
}

func (u userTitleEffectClaimsDo) FirstOrCreate() (*model.UserTitleEffectClaims, error) {
	if result, err := u.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.UserTitleEffectClaims), nil
	}
}

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

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

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

func (u userTitleEffectClaimsDo) Scan(result interface{}) (err error) {
	return u.DO.Scan(result)
}

func (u userTitleEffectClaimsDo) Delete(models ...*model.UserTitleEffectClaims) (result gen.ResultInfo, err error) {
	return u.DO.Delete(models)
}

func (u *userTitleEffectClaimsDo) withDO(do gen.Dao) *userTitleEffectClaimsDo {
	u.DO = *do.(*gen.DO)
	return u
}
