// 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 query

import (
	"context"

	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"gorm.io/gorm/schema"

	"gorm.io/gen"
	"gorm.io/gen/field"

	"gorm.io/plugin/dbresolver"

	"gadmin/internal/gorm/model"
)

func newRedeemReceived(db *gorm.DB, opts ...gen.DOOption) redeemReceived {
	_redeemReceived := redeemReceived{}

	_redeemReceived.redeemReceivedDo.UseDB(db, opts...)
	_redeemReceived.redeemReceivedDo.UseModel(&model.RedeemReceived{})

	tableName := _redeemReceived.redeemReceivedDo.TableName()
	_redeemReceived.ALL = field.NewAsterisk(tableName)
	_redeemReceived.ID = field.NewInt64(tableName, "id")
	_redeemReceived.Cid = field.NewInt64(tableName, "cid")
	_redeemReceived.Playerid = field.NewInt64(tableName, "playerid")
	_redeemReceived.Openid = field.NewString(tableName, "openid")
	_redeemReceived.CreatedAt = field.NewInt64(tableName, "created_at")

	_redeemReceived.fillFieldMap()

	return _redeemReceived
}

type redeemReceived struct {
	redeemReceivedDo

	ALL       field.Asterisk
	ID        field.Int64
	Cid       field.Int64  // 兑换码ID
	Playerid  field.Int64  // 玩家ID
	Openid    field.String // openid
	CreatedAt field.Int64  // 领取时间

	fieldMap map[string]field.Expr
}

func (r redeemReceived) Table(newTableName string) *redeemReceived {
	r.redeemReceivedDo.UseTable(newTableName)
	return r.updateTableName(newTableName)
}

func (r redeemReceived) As(alias string) *redeemReceived {
	r.redeemReceivedDo.DO = *(r.redeemReceivedDo.As(alias).(*gen.DO))
	return r.updateTableName(alias)
}

func (r *redeemReceived) updateTableName(table string) *redeemReceived {
	r.ALL = field.NewAsterisk(table)
	r.ID = field.NewInt64(table, "id")
	r.Cid = field.NewInt64(table, "cid")
	r.Playerid = field.NewInt64(table, "playerid")
	r.Openid = field.NewString(table, "openid")
	r.CreatedAt = field.NewInt64(table, "created_at")

	r.fillFieldMap()

	return r
}

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

func (r *redeemReceived) fillFieldMap() {
	r.fieldMap = make(map[string]field.Expr, 5)
	r.fieldMap["id"] = r.ID
	r.fieldMap["cid"] = r.Cid
	r.fieldMap["playerid"] = r.Playerid
	r.fieldMap["openid"] = r.Openid
	r.fieldMap["created_at"] = r.CreatedAt
}

func (r redeemReceived) clone(db *gorm.DB) redeemReceived {
	r.redeemReceivedDo.ReplaceConnPool(db.Statement.ConnPool)
	return r
}

func (r redeemReceived) replaceDB(db *gorm.DB) redeemReceived {
	r.redeemReceivedDo.ReplaceDB(db)
	return r
}

type redeemReceivedDo struct{ gen.DO }

func (r redeemReceivedDo) Debug() *redeemReceivedDo {
	return r.withDO(r.DO.Debug())
}

func (r redeemReceivedDo) WithContext(ctx context.Context) *redeemReceivedDo {
	return r.withDO(r.DO.WithContext(ctx))
}

func (r redeemReceivedDo) ReadDB() *redeemReceivedDo {
	return r.Clauses(dbresolver.Read)
}

func (r redeemReceivedDo) WriteDB() *redeemReceivedDo {
	return r.Clauses(dbresolver.Write)
}

func (r redeemReceivedDo) Session(config *gorm.Session) *redeemReceivedDo {
	return r.withDO(r.DO.Session(config))
}

func (r redeemReceivedDo) Clauses(conds ...clause.Expression) *redeemReceivedDo {
	return r.withDO(r.DO.Clauses(conds...))
}

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

func (r redeemReceivedDo) Not(conds ...gen.Condition) *redeemReceivedDo {
	return r.withDO(r.DO.Not(conds...))
}

func (r redeemReceivedDo) Or(conds ...gen.Condition) *redeemReceivedDo {
	return r.withDO(r.DO.Or(conds...))
}

func (r redeemReceivedDo) Select(conds ...field.Expr) *redeemReceivedDo {
	return r.withDO(r.DO.Select(conds...))
}

func (r redeemReceivedDo) Where(conds ...gen.Condition) *redeemReceivedDo {
	return r.withDO(r.DO.Where(conds...))
}

func (r redeemReceivedDo) Exists(subquery interface{ UnderlyingDB() *gorm.DB }) *redeemReceivedDo {
	return r.Where(field.CompareSubQuery(field.ExistsOp, nil, subquery.UnderlyingDB()))
}

func (r redeemReceivedDo) Order(conds ...field.Expr) *redeemReceivedDo {
	return r.withDO(r.DO.Order(conds...))
}

func (r redeemReceivedDo) Distinct(cols ...field.Expr) *redeemReceivedDo {
	return r.withDO(r.DO.Distinct(cols...))
}

func (r redeemReceivedDo) Omit(cols ...field.Expr) *redeemReceivedDo {
	return r.withDO(r.DO.Omit(cols...))
}

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

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

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

func (r redeemReceivedDo) Group(cols ...field.Expr) *redeemReceivedDo {
	return r.withDO(r.DO.Group(cols...))
}

func (r redeemReceivedDo) Having(conds ...gen.Condition) *redeemReceivedDo {
	return r.withDO(r.DO.Having(conds...))
}

func (r redeemReceivedDo) Limit(limit int) *redeemReceivedDo {
	return r.withDO(r.DO.Limit(limit))
}

func (r redeemReceivedDo) Offset(offset int) *redeemReceivedDo {
	return r.withDO(r.DO.Offset(offset))
}

func (r redeemReceivedDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *redeemReceivedDo {
	return r.withDO(r.DO.Scopes(funcs...))
}

func (r redeemReceivedDo) Unscoped() *redeemReceivedDo {
	return r.withDO(r.DO.Unscoped())
}

func (r redeemReceivedDo) Create(values ...*model.RedeemReceived) error {
	if len(values) == 0 {
		return nil
	}
	return r.DO.Create(values)
}

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

func (r redeemReceivedDo) First() (*model.RedeemReceived, error) {
	if result, err := r.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.RedeemReceived), nil
	}
}

func (r redeemReceivedDo) Take() (*model.RedeemReceived, error) {
	if result, err := r.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.RedeemReceived), nil
	}
}

func (r redeemReceivedDo) Last() (*model.RedeemReceived, error) {
	if result, err := r.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.RedeemReceived), nil
	}
}

func (r redeemReceivedDo) Find() ([]*model.RedeemReceived, error) {
	result, err := r.DO.Find()
	return result.([]*model.RedeemReceived), err
}

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

func (r redeemReceivedDo) Attrs(attrs ...field.AssignExpr) *redeemReceivedDo {
	return r.withDO(r.DO.Attrs(attrs...))
}

func (r redeemReceivedDo) Assign(attrs ...field.AssignExpr) *redeemReceivedDo {
	return r.withDO(r.DO.Assign(attrs...))
}

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

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

func (r redeemReceivedDo) FirstOrInit() (*model.RedeemReceived, error) {
	if result, err := r.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.RedeemReceived), nil
	}
}

func (r redeemReceivedDo) FirstOrCreate() (*model.RedeemReceived, error) {
	if result, err := r.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.RedeemReceived), nil
	}
}

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

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

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

func (r redeemReceivedDo) Scan(result interface{}) (err error) {
	return r.DO.Scan(result)
}

func (r redeemReceivedDo) Delete(models ...*model.RedeemReceived) (result gen.ResultInfo, err error) {
	return r.DO.Delete(models)
}

func (r *redeemReceivedDo) withDO(do gen.Dao) *redeemReceivedDo {
	r.DO = *do.(*gen.DO)
	return r
}
