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

	_userGamePasses.userGamePassesDo.UseDB(db, opts...)
	_userGamePasses.userGamePassesDo.UseModel(&model.UserGamePasses{})

	tableName := _userGamePasses.userGamePassesDo.TableName()
	_userGamePasses.ALL = field.NewAsterisk(tableName)
	_userGamePasses.ID = field.NewInt64(tableName, "id")
	_userGamePasses.CreatedAt = field.NewTime(tableName, "created_at")
	_userGamePasses.UpdatedAt = field.NewTime(tableName, "updated_at")
	_userGamePasses.UserID = field.NewInt64(tableName, "user_id")
	_userGamePasses.ActivityID = field.NewInt64(tableName, "activity_id")
	_userGamePasses.Remaining = field.NewInt32(tableName, "remaining")
	_userGamePasses.TotalGranted = field.NewInt32(tableName, "total_granted")
	_userGamePasses.TotalUsed = field.NewInt32(tableName, "total_used")
	_userGamePasses.ExpiredAt = field.NewTime(tableName, "expired_at")
	_userGamePasses.Source = field.NewString(tableName, "source")
	_userGamePasses.Remark = field.NewString(tableName, "remark")

	_userGamePasses.fillFieldMap()

	return _userGamePasses
}

// userGamePasses 用户游戏次数卡
type userGamePasses struct {
	userGamePassesDo

	ALL          field.Asterisk
	ID           field.Int64  // 主键ID
	CreatedAt    field.Time   // 创建时间
	UpdatedAt    field.Time   // 更新时间
	UserID       field.Int64  // 用户ID
	ActivityID   field.Int64  // 指定活动ID，NULL表示全局通用
	Remaining    field.Int32  // 剩余次数
	TotalGranted field.Int32  // 累计发放次数
	TotalUsed    field.Int32  // 累计使用次数
	ExpiredAt    field.Time   // 过期时间，NULL表示永不过期
	Source       field.String // 来源: admin/purchase/task
	Remark       field.String // 备注

	fieldMap map[string]field.Expr
}

func (u userGamePasses) Table(newTableName string) *userGamePasses {
	u.userGamePassesDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

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

func (u *userGamePasses) updateTableName(table string) *userGamePasses {
	u.ALL = field.NewAsterisk(table)
	u.ID = field.NewInt64(table, "id")
	u.CreatedAt = field.NewTime(table, "created_at")
	u.UpdatedAt = field.NewTime(table, "updated_at")
	u.UserID = field.NewInt64(table, "user_id")
	u.ActivityID = field.NewInt64(table, "activity_id")
	u.Remaining = field.NewInt32(table, "remaining")
	u.TotalGranted = field.NewInt32(table, "total_granted")
	u.TotalUsed = field.NewInt32(table, "total_used")
	u.ExpiredAt = field.NewTime(table, "expired_at")
	u.Source = field.NewString(table, "source")
	u.Remark = field.NewString(table, "remark")

	u.fillFieldMap()

	return u
}

func (u *userGamePasses) 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 *userGamePasses) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 11)
	u.fieldMap["id"] = u.ID
	u.fieldMap["created_at"] = u.CreatedAt
	u.fieldMap["updated_at"] = u.UpdatedAt
	u.fieldMap["user_id"] = u.UserID
	u.fieldMap["activity_id"] = u.ActivityID
	u.fieldMap["remaining"] = u.Remaining
	u.fieldMap["total_granted"] = u.TotalGranted
	u.fieldMap["total_used"] = u.TotalUsed
	u.fieldMap["expired_at"] = u.ExpiredAt
	u.fieldMap["source"] = u.Source
	u.fieldMap["remark"] = u.Remark
}

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

func (u userGamePasses) replaceDB(db *gorm.DB) userGamePasses {
	u.userGamePassesDo.ReplaceDB(db)
	return u
}

type userGamePassesDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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