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

	_userGameTickets.userGameTicketsDo.UseDB(db, opts...)
	_userGameTickets.userGameTicketsDo.UseModel(&model.UserGameTickets{})

	tableName := _userGameTickets.userGameTicketsDo.TableName()
	_userGameTickets.ALL = field.NewAsterisk(tableName)
	_userGameTickets.ID = field.NewInt64(tableName, "id")
	_userGameTickets.CreatedAt = field.NewTime(tableName, "created_at")
	_userGameTickets.UpdatedAt = field.NewTime(tableName, "updated_at")
	_userGameTickets.UserID = field.NewInt64(tableName, "user_id")
	_userGameTickets.GameCode = field.NewString(tableName, "game_code")
	_userGameTickets.Available = field.NewInt32(tableName, "available")
	_userGameTickets.TotalEarned = field.NewInt32(tableName, "total_earned")
	_userGameTickets.TotalUsed = field.NewInt32(tableName, "total_used")

	_userGameTickets.fillFieldMap()

	return _userGameTickets
}

// userGameTickets 用户游戏资格
type userGameTickets struct {
	userGameTicketsDo

	ALL         field.Asterisk
	ID          field.Int64
	CreatedAt   field.Time
	UpdatedAt   field.Time
	UserID      field.Int64  // 用户ID
	GameCode    field.String // 游戏代码
	Available   field.Int32  // 可用次数
	TotalEarned field.Int32  // 累计获得
	TotalUsed   field.Int32  // 累计使用

	fieldMap map[string]field.Expr
}

func (u userGameTickets) Table(newTableName string) *userGameTickets {
	u.userGameTicketsDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

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

func (u *userGameTickets) updateTableName(table string) *userGameTickets {
	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.GameCode = field.NewString(table, "game_code")
	u.Available = field.NewInt32(table, "available")
	u.TotalEarned = field.NewInt32(table, "total_earned")
	u.TotalUsed = field.NewInt32(table, "total_used")

	u.fillFieldMap()

	return u
}

func (u *userGameTickets) 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 *userGameTickets) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 8)
	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["game_code"] = u.GameCode
	u.fieldMap["available"] = u.Available
	u.fieldMap["total_earned"] = u.TotalEarned
	u.fieldMap["total_used"] = u.TotalUsed
}

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

func (u userGameTickets) replaceDB(db *gorm.DB) userGameTickets {
	u.userGameTicketsDo.ReplaceDB(db)
	return u
}

type userGameTicketsDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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