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

	_userInvites.userInvitesDo.UseDB(db, opts...)
	_userInvites.userInvitesDo.UseModel(&model.UserInvites{})

	tableName := _userInvites.userInvitesDo.TableName()
	_userInvites.ALL = field.NewAsterisk(tableName)
	_userInvites.ID = field.NewInt64(tableName, "id")
	_userInvites.InviterID = field.NewInt64(tableName, "inviter_id")
	_userInvites.InviteeID = field.NewInt64(tableName, "invitee_id")
	_userInvites.InviteCode = field.NewString(tableName, "invite_code")
	_userInvites.RewardPoints = field.NewInt64(tableName, "reward_points")
	_userInvites.RewardedAt = field.NewTime(tableName, "rewarded_at")
	_userInvites.CreatedAt = field.NewTime(tableName, "created_at")
	_userInvites.UpdatedAt = field.NewTime(tableName, "updated_at")
	_userInvites.DeletedAt = field.NewField(tableName, "deleted_at")
	_userInvites.IsEffective = field.NewBool(tableName, "is_effective")
	_userInvites.AccumulatedAmount = field.NewInt64(tableName, "accumulated_amount")

	_userInvites.fillFieldMap()

	return _userInvites
}

// userInvites 用户邀请关系表
type userInvites struct {
	userInvitesDo

	ALL               field.Asterisk
	ID                field.Int64  // 主键ID
	InviterID         field.Int64  // 邀请人用户ID
	InviteeID         field.Int64  // 被邀请用户ID
	InviteCode        field.String // 邀请时使用的邀请码
	RewardPoints      field.Int64  // 发放的积分数量（用于审计）
	RewardedAt        field.Time   // 奖励发放时间
	CreatedAt         field.Time   // 创建时间
	UpdatedAt         field.Time   // 更新时间
	DeletedAt         field.Field  // 删除时间（软删）
	IsEffective       field.Bool   // 是否为有效邀请
	AccumulatedAmount field.Int64  // 被邀请人累计消费金额(分)

	fieldMap map[string]field.Expr
}

func (u userInvites) Table(newTableName string) *userInvites {
	u.userInvitesDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

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

func (u *userInvites) updateTableName(table string) *userInvites {
	u.ALL = field.NewAsterisk(table)
	u.ID = field.NewInt64(table, "id")
	u.InviterID = field.NewInt64(table, "inviter_id")
	u.InviteeID = field.NewInt64(table, "invitee_id")
	u.InviteCode = field.NewString(table, "invite_code")
	u.RewardPoints = field.NewInt64(table, "reward_points")
	u.RewardedAt = field.NewTime(table, "rewarded_at")
	u.CreatedAt = field.NewTime(table, "created_at")
	u.UpdatedAt = field.NewTime(table, "updated_at")
	u.DeletedAt = field.NewField(table, "deleted_at")
	u.IsEffective = field.NewBool(table, "is_effective")
	u.AccumulatedAmount = field.NewInt64(table, "accumulated_amount")

	u.fillFieldMap()

	return u
}

func (u *userInvites) 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 *userInvites) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 11)
	u.fieldMap["id"] = u.ID
	u.fieldMap["inviter_id"] = u.InviterID
	u.fieldMap["invitee_id"] = u.InviteeID
	u.fieldMap["invite_code"] = u.InviteCode
	u.fieldMap["reward_points"] = u.RewardPoints
	u.fieldMap["rewarded_at"] = u.RewardedAt
	u.fieldMap["created_at"] = u.CreatedAt
	u.fieldMap["updated_at"] = u.UpdatedAt
	u.fieldMap["deleted_at"] = u.DeletedAt
	u.fieldMap["is_effective"] = u.IsEffective
	u.fieldMap["accumulated_amount"] = u.AccumulatedAmount
}

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

func (u userInvites) replaceDB(db *gorm.DB) userInvites {
	u.userInvitesDo.ReplaceDB(db)
	return u
}

type userInvitesDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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