// 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"
	"database/sql"

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

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

	"gorm.io/plugin/dbresolver"

	"stream.api/internal/database/model"
)

func newPayment(db *gorm.DB, opts ...gen.DOOption) payment {
	_payment := payment{}

	_payment.paymentDo.UseDB(db, opts...)
	_payment.paymentDo.UseModel(&model.Payment{})

	tableName := _payment.paymentDo.TableName()
	_payment.ALL = field.NewAsterisk(tableName)
	_payment.ID = field.NewString(tableName, "id")
	_payment.UserID = field.NewString(tableName, "user_id")
	_payment.PlanID = field.NewString(tableName, "plan_id")
	_payment.Amount = field.NewFloat64(tableName, "amount")
	_payment.Currency = field.NewString(tableName, "currency")
	_payment.Status = field.NewString(tableName, "status")
	_payment.Provider = field.NewString(tableName, "provider")
	_payment.TransactionID = field.NewString(tableName, "transaction_id")
	_payment.CreatedAt = field.NewTime(tableName, "created_at")
	_payment.UpdatedAt = field.NewTime(tableName, "updated_at")
	_payment.Version = field.NewInt64(tableName, "version")

	_payment.fillFieldMap()

	return _payment
}

type payment struct {
	paymentDo paymentDo

	ALL           field.Asterisk
	ID            field.String
	UserID        field.String
	PlanID        field.String
	Amount        field.Float64
	Currency      field.String
	Status        field.String
	Provider      field.String
	TransactionID field.String
	CreatedAt     field.Time
	UpdatedAt     field.Time
	Version       field.Int64

	fieldMap map[string]field.Expr
}

func (p payment) Table(newTableName string) *payment {
	p.paymentDo.UseTable(newTableName)
	return p.updateTableName(newTableName)
}

func (p payment) As(alias string) *payment {
	p.paymentDo.DO = *(p.paymentDo.As(alias).(*gen.DO))
	return p.updateTableName(alias)
}

func (p *payment) updateTableName(table string) *payment {
	p.ALL = field.NewAsterisk(table)
	p.ID = field.NewString(table, "id")
	p.UserID = field.NewString(table, "user_id")
	p.PlanID = field.NewString(table, "plan_id")
	p.Amount = field.NewFloat64(table, "amount")
	p.Currency = field.NewString(table, "currency")
	p.Status = field.NewString(table, "status")
	p.Provider = field.NewString(table, "provider")
	p.TransactionID = field.NewString(table, "transaction_id")
	p.CreatedAt = field.NewTime(table, "created_at")
	p.UpdatedAt = field.NewTime(table, "updated_at")
	p.Version = field.NewInt64(table, "version")

	p.fillFieldMap()

	return p
}

func (p *payment) WithContext(ctx context.Context) IPaymentDo { return p.paymentDo.WithContext(ctx) }

func (p payment) TableName() string { return p.paymentDo.TableName() }

func (p payment) Alias() string { return p.paymentDo.Alias() }

func (p payment) Columns(cols ...field.Expr) gen.Columns { return p.paymentDo.Columns(cols...) }

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

func (p *payment) fillFieldMap() {
	p.fieldMap = make(map[string]field.Expr, 11)
	p.fieldMap["id"] = p.ID
	p.fieldMap["user_id"] = p.UserID
	p.fieldMap["plan_id"] = p.PlanID
	p.fieldMap["amount"] = p.Amount
	p.fieldMap["currency"] = p.Currency
	p.fieldMap["status"] = p.Status
	p.fieldMap["provider"] = p.Provider
	p.fieldMap["transaction_id"] = p.TransactionID
	p.fieldMap["created_at"] = p.CreatedAt
	p.fieldMap["updated_at"] = p.UpdatedAt
	p.fieldMap["version"] = p.Version
}

func (p payment) clone(db *gorm.DB) payment {
	p.paymentDo.ReplaceConnPool(db.Statement.ConnPool)
	return p
}

func (p payment) replaceDB(db *gorm.DB) payment {
	p.paymentDo.ReplaceDB(db)
	return p
}

type paymentDo struct{ gen.DO }

type IPaymentDo interface {
	gen.SubQuery
	Debug() IPaymentDo
	WithContext(ctx context.Context) IPaymentDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() IPaymentDo
	WriteDB() IPaymentDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) IPaymentDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) IPaymentDo
	Not(conds ...gen.Condition) IPaymentDo
	Or(conds ...gen.Condition) IPaymentDo
	Select(conds ...field.Expr) IPaymentDo
	Where(conds ...gen.Condition) IPaymentDo
	Order(conds ...field.Expr) IPaymentDo
	Distinct(cols ...field.Expr) IPaymentDo
	Omit(cols ...field.Expr) IPaymentDo
	Join(table schema.Tabler, on ...field.Expr) IPaymentDo
	LeftJoin(table schema.Tabler, on ...field.Expr) IPaymentDo
	RightJoin(table schema.Tabler, on ...field.Expr) IPaymentDo
	Group(cols ...field.Expr) IPaymentDo
	Having(conds ...gen.Condition) IPaymentDo
	Limit(limit int) IPaymentDo
	Offset(offset int) IPaymentDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) IPaymentDo
	Unscoped() IPaymentDo
	Create(values ...*model.Payment) error
	CreateInBatches(values []*model.Payment, batchSize int) error
	Save(values ...*model.Payment) error
	First() (*model.Payment, error)
	Take() (*model.Payment, error)
	Last() (*model.Payment, error)
	Find() ([]*model.Payment, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.Payment, err error)
	FindInBatches(result *[]*model.Payment, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.Payment) (info gen.ResultInfo, err error)
	Update(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	Updates(value interface{}) (info gen.ResultInfo, err error)
	UpdateColumn(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateColumnSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	UpdateColumns(value interface{}) (info gen.ResultInfo, err error)
	UpdateFrom(q gen.SubQuery) gen.Dao
	Attrs(attrs ...field.AssignExpr) IPaymentDo
	Assign(attrs ...field.AssignExpr) IPaymentDo
	Joins(fields ...field.RelationField) IPaymentDo
	Preload(fields ...field.RelationField) IPaymentDo
	FirstOrInit() (*model.Payment, error)
	FirstOrCreate() (*model.Payment, error)
	FindByPage(offset int, limit int) (result []*model.Payment, count int64, err error)
	ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
	Rows() (*sql.Rows, error)
	Row() *sql.Row
	Scan(result interface{}) (err error)
	Returning(value interface{}, columns ...string) IPaymentDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (p paymentDo) Debug() IPaymentDo {
	return p.withDO(p.DO.Debug())
}

func (p paymentDo) WithContext(ctx context.Context) IPaymentDo {
	return p.withDO(p.DO.WithContext(ctx))
}

func (p paymentDo) ReadDB() IPaymentDo {
	return p.Clauses(dbresolver.Read)
}

func (p paymentDo) WriteDB() IPaymentDo {
	return p.Clauses(dbresolver.Write)
}

func (p paymentDo) Session(config *gorm.Session) IPaymentDo {
	return p.withDO(p.DO.Session(config))
}

func (p paymentDo) Clauses(conds ...clause.Expression) IPaymentDo {
	return p.withDO(p.DO.Clauses(conds...))
}

func (p paymentDo) Returning(value interface{}, columns ...string) IPaymentDo {
	return p.withDO(p.DO.Returning(value, columns...))
}

func (p paymentDo) Not(conds ...gen.Condition) IPaymentDo {
	return p.withDO(p.DO.Not(conds...))
}

func (p paymentDo) Or(conds ...gen.Condition) IPaymentDo {
	return p.withDO(p.DO.Or(conds...))
}

func (p paymentDo) Select(conds ...field.Expr) IPaymentDo {
	return p.withDO(p.DO.Select(conds...))
}

func (p paymentDo) Where(conds ...gen.Condition) IPaymentDo {
	return p.withDO(p.DO.Where(conds...))
}

func (p paymentDo) Order(conds ...field.Expr) IPaymentDo {
	return p.withDO(p.DO.Order(conds...))
}

func (p paymentDo) Distinct(cols ...field.Expr) IPaymentDo {
	return p.withDO(p.DO.Distinct(cols...))
}

func (p paymentDo) Omit(cols ...field.Expr) IPaymentDo {
	return p.withDO(p.DO.Omit(cols...))
}

func (p paymentDo) Join(table schema.Tabler, on ...field.Expr) IPaymentDo {
	return p.withDO(p.DO.Join(table, on...))
}

func (p paymentDo) LeftJoin(table schema.Tabler, on ...field.Expr) IPaymentDo {
	return p.withDO(p.DO.LeftJoin(table, on...))
}

func (p paymentDo) RightJoin(table schema.Tabler, on ...field.Expr) IPaymentDo {
	return p.withDO(p.DO.RightJoin(table, on...))
}

func (p paymentDo) Group(cols ...field.Expr) IPaymentDo {
	return p.withDO(p.DO.Group(cols...))
}

func (p paymentDo) Having(conds ...gen.Condition) IPaymentDo {
	return p.withDO(p.DO.Having(conds...))
}

func (p paymentDo) Limit(limit int) IPaymentDo {
	return p.withDO(p.DO.Limit(limit))
}

func (p paymentDo) Offset(offset int) IPaymentDo {
	return p.withDO(p.DO.Offset(offset))
}

func (p paymentDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IPaymentDo {
	return p.withDO(p.DO.Scopes(funcs...))
}

func (p paymentDo) Unscoped() IPaymentDo {
	return p.withDO(p.DO.Unscoped())
}

func (p paymentDo) Create(values ...*model.Payment) error {
	if len(values) == 0 {
		return nil
	}
	return p.DO.Create(values)
}

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

func (p paymentDo) First() (*model.Payment, error) {
	if result, err := p.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Payment), nil
	}
}

func (p paymentDo) Take() (*model.Payment, error) {
	if result, err := p.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Payment), nil
	}
}

func (p paymentDo) Last() (*model.Payment, error) {
	if result, err := p.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Payment), nil
	}
}

func (p paymentDo) Find() ([]*model.Payment, error) {
	result, err := p.DO.Find()
	return result.([]*model.Payment), err
}

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

func (p paymentDo) Attrs(attrs ...field.AssignExpr) IPaymentDo {
	return p.withDO(p.DO.Attrs(attrs...))
}

func (p paymentDo) Assign(attrs ...field.AssignExpr) IPaymentDo {
	return p.withDO(p.DO.Assign(attrs...))
}

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

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

func (p paymentDo) FirstOrInit() (*model.Payment, error) {
	if result, err := p.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Payment), nil
	}
}

func (p paymentDo) FirstOrCreate() (*model.Payment, error) {
	if result, err := p.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Payment), nil
	}
}

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

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

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

func (p paymentDo) Scan(result interface{}) (err error) {
	return p.DO.Scan(result)
}

func (p paymentDo) Delete(models ...*model.Payment) (result gen.ResultInfo, err error) {
	return p.DO.Delete(models)
}

func (p *paymentDo) withDO(do gen.Dao) *paymentDo {
	p.DO = *do.(*gen.DO)
	return p
}
