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

	_redeemPeripheralsReceived.redeemPeripheralsReceivedDo.UseDB(db, opts...)
	_redeemPeripheralsReceived.redeemPeripheralsReceivedDo.UseModel(&model.RedeemPeripheralsReceived{})

	tableName := _redeemPeripheralsReceived.redeemPeripheralsReceivedDo.TableName()
	_redeemPeripheralsReceived.ALL = field.NewAsterisk(tableName)
	_redeemPeripheralsReceived.ID = field.NewInt64(tableName, "id")
	_redeemPeripheralsReceived.Sn = field.NewString(tableName, "sn")
	_redeemPeripheralsReceived.Cid = field.NewInt64(tableName, "cid")
	_redeemPeripheralsReceived.Openid = field.NewString(tableName, "openid")
	_redeemPeripheralsReceived.CreatedAt = field.NewInt64(tableName, "created_at")

	_redeemPeripheralsReceived.fillFieldMap()

	return _redeemPeripheralsReceived
}

type redeemPeripheralsReceived struct {
	redeemPeripheralsReceivedDo

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

	fieldMap map[string]field.Expr
}

func (r redeemPeripheralsReceived) Table(newTableName string) *redeemPeripheralsReceived {
	r.redeemPeripheralsReceivedDo.UseTable(newTableName)
	return r.updateTableName(newTableName)
}

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

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

	r.fillFieldMap()

	return r
}

func (r *redeemPeripheralsReceived) 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 *redeemPeripheralsReceived) fillFieldMap() {
	r.fieldMap = make(map[string]field.Expr, 5)
	r.fieldMap["id"] = r.ID
	r.fieldMap["sn"] = r.Sn
	r.fieldMap["cid"] = r.Cid
	r.fieldMap["openid"] = r.Openid
	r.fieldMap["created_at"] = r.CreatedAt
}

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

func (r redeemPeripheralsReceived) replaceDB(db *gorm.DB) redeemPeripheralsReceived {
	r.redeemPeripheralsReceivedDo.ReplaceDB(db)
	return r
}

type redeemPeripheralsReceivedDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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