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

	_redeemCode.redeemCodeDo.UseDB(db, opts...)
	_redeemCode.redeemCodeDo.UseModel(&model.RedeemCode{})

	tableName := _redeemCode.redeemCodeDo.TableName()
	_redeemCode.ALL = field.NewAsterisk(tableName)
	_redeemCode.ID = field.NewInt64(tableName, "id")
	_redeemCode.Sn = field.NewString(tableName, "sn")
	_redeemCode.Name = field.NewString(tableName, "name")
	_redeemCode.Cdk = field.NewString(tableName, "cdk")
	_redeemCode.ServerIds = field.NewString(tableName, "server_ids")
	_redeemCode.TypeID = field.NewInt64(tableName, "type_id")
	_redeemCode.Openid = field.NewString(tableName, "openid")
	_redeemCode.Times = field.NewInt64(tableName, "times")
	_redeemCode.ExpiredAt = field.NewInt32(tableName, "expired_at")
	_redeemCode.Award = field.NewString(tableName, "award")
	_redeemCode.Date = field.NewString(tableName, "date")
	_redeemCode.Operator = field.NewString(tableName, "operator")
	_redeemCode.CreatedAt = field.NewInt64(tableName, "created_at")
	_redeemCode.Status = field.NewInt32(tableName, "status")

	_redeemCode.fillFieldMap()

	return _redeemCode
}

type redeemCode struct {
	redeemCodeDo

	ALL       field.Asterisk
	ID        field.Int64
	Sn        field.String // 批次编号
	Name      field.String // 批次名称
	Cdk       field.String // 兑换码
	ServerIds field.String // 服务器
	TypeID    field.Int64  // 类型ID，1全部玩家，2指定玩家
	Openid    field.String // openid，多个用,隔开
	Times     field.Int64  // 可用次数
	ExpiredAt field.Int32  // 过期时间
	Award     field.String // 奖励配置
	Date      field.String // 生成日期
	Operator  field.String // 操作人
	CreatedAt field.Int64  // 生成时间
	Status    field.Int32  // 是否生效 1是 2否

	fieldMap map[string]field.Expr
}

func (r redeemCode) Table(newTableName string) *redeemCode {
	r.redeemCodeDo.UseTable(newTableName)
	return r.updateTableName(newTableName)
}

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

func (r *redeemCode) updateTableName(table string) *redeemCode {
	r.ALL = field.NewAsterisk(table)
	r.ID = field.NewInt64(table, "id")
	r.Sn = field.NewString(table, "sn")
	r.Name = field.NewString(table, "name")
	r.Cdk = field.NewString(table, "cdk")
	r.ServerIds = field.NewString(table, "server_ids")
	r.TypeID = field.NewInt64(table, "type_id")
	r.Openid = field.NewString(table, "openid")
	r.Times = field.NewInt64(table, "times")
	r.ExpiredAt = field.NewInt32(table, "expired_at")
	r.Award = field.NewString(table, "award")
	r.Date = field.NewString(table, "date")
	r.Operator = field.NewString(table, "operator")
	r.CreatedAt = field.NewInt64(table, "created_at")
	r.Status = field.NewInt32(table, "status")

	r.fillFieldMap()

	return r
}

func (r *redeemCode) 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 *redeemCode) fillFieldMap() {
	r.fieldMap = make(map[string]field.Expr, 14)
	r.fieldMap["id"] = r.ID
	r.fieldMap["sn"] = r.Sn
	r.fieldMap["name"] = r.Name
	r.fieldMap["cdk"] = r.Cdk
	r.fieldMap["server_ids"] = r.ServerIds
	r.fieldMap["type_id"] = r.TypeID
	r.fieldMap["openid"] = r.Openid
	r.fieldMap["times"] = r.Times
	r.fieldMap["expired_at"] = r.ExpiredAt
	r.fieldMap["award"] = r.Award
	r.fieldMap["date"] = r.Date
	r.fieldMap["operator"] = r.Operator
	r.fieldMap["created_at"] = r.CreatedAt
	r.fieldMap["status"] = r.Status
}

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

func (r redeemCode) replaceDB(db *gorm.DB) redeemCode {
	r.redeemCodeDo.ReplaceDB(db)
	return r
}

type redeemCodeDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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