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

	_planSubscription.planSubscriptionDo.UseDB(db, opts...)
	_planSubscription.planSubscriptionDo.UseModel(&model.PlanSubscription{})

	tableName := _planSubscription.planSubscriptionDo.TableName()
	_planSubscription.ALL = field.NewAsterisk(tableName)
	_planSubscription.ID = field.NewString(tableName, "id")
	_planSubscription.UserID = field.NewString(tableName, "user_id")
	_planSubscription.PaymentID = field.NewString(tableName, "payment_id")
	_planSubscription.PlanID = field.NewString(tableName, "plan_id")
	_planSubscription.TermMonths = field.NewInt32(tableName, "term_months")
	_planSubscription.PaymentMethod = field.NewString(tableName, "payment_method")
	_planSubscription.WalletAmount = field.NewFloat64(tableName, "wallet_amount")
	_planSubscription.TopupAmount = field.NewFloat64(tableName, "topup_amount")
	_planSubscription.StartedAt = field.NewTime(tableName, "started_at")
	_planSubscription.ExpiresAt = field.NewTime(tableName, "expires_at")
	_planSubscription.Reminder7DSentAt = field.NewTime(tableName, "reminder_7d_sent_at")
	_planSubscription.Reminder3DSentAt = field.NewTime(tableName, "reminder_3d_sent_at")
	_planSubscription.Reminder1DSentAt = field.NewTime(tableName, "reminder_1d_sent_at")
	_planSubscription.CreatedAt = field.NewTime(tableName, "created_at")
	_planSubscription.UpdatedAt = field.NewTime(tableName, "updated_at")
	_planSubscription.Version = field.NewInt64(tableName, "version")

	_planSubscription.fillFieldMap()

	return _planSubscription
}

type planSubscription struct {
	planSubscriptionDo planSubscriptionDo

	ALL              field.Asterisk
	ID               field.String
	UserID           field.String
	PaymentID        field.String
	PlanID           field.String
	TermMonths       field.Int32
	PaymentMethod    field.String
	WalletAmount     field.Float64
	TopupAmount      field.Float64
	StartedAt        field.Time
	ExpiresAt        field.Time
	Reminder7DSentAt field.Time
	Reminder3DSentAt field.Time
	Reminder1DSentAt field.Time
	CreatedAt        field.Time
	UpdatedAt        field.Time
	Version          field.Int64

	fieldMap map[string]field.Expr
}

func (p planSubscription) Table(newTableName string) *planSubscription {
	p.planSubscriptionDo.UseTable(newTableName)
	return p.updateTableName(newTableName)
}

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

func (p *planSubscription) updateTableName(table string) *planSubscription {
	p.ALL = field.NewAsterisk(table)
	p.ID = field.NewString(table, "id")
	p.UserID = field.NewString(table, "user_id")
	p.PaymentID = field.NewString(table, "payment_id")
	p.PlanID = field.NewString(table, "plan_id")
	p.TermMonths = field.NewInt32(table, "term_months")
	p.PaymentMethod = field.NewString(table, "payment_method")
	p.WalletAmount = field.NewFloat64(table, "wallet_amount")
	p.TopupAmount = field.NewFloat64(table, "topup_amount")
	p.StartedAt = field.NewTime(table, "started_at")
	p.ExpiresAt = field.NewTime(table, "expires_at")
	p.Reminder7DSentAt = field.NewTime(table, "reminder_7d_sent_at")
	p.Reminder3DSentAt = field.NewTime(table, "reminder_3d_sent_at")
	p.Reminder1DSentAt = field.NewTime(table, "reminder_1d_sent_at")
	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 *planSubscription) WithContext(ctx context.Context) IPlanSubscriptionDo {
	return p.planSubscriptionDo.WithContext(ctx)
}

func (p planSubscription) TableName() string { return p.planSubscriptionDo.TableName() }

func (p planSubscription) Alias() string { return p.planSubscriptionDo.Alias() }

func (p planSubscription) Columns(cols ...field.Expr) gen.Columns {
	return p.planSubscriptionDo.Columns(cols...)
}

func (p *planSubscription) 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 *planSubscription) fillFieldMap() {
	p.fieldMap = make(map[string]field.Expr, 16)
	p.fieldMap["id"] = p.ID
	p.fieldMap["user_id"] = p.UserID
	p.fieldMap["payment_id"] = p.PaymentID
	p.fieldMap["plan_id"] = p.PlanID
	p.fieldMap["term_months"] = p.TermMonths
	p.fieldMap["payment_method"] = p.PaymentMethod
	p.fieldMap["wallet_amount"] = p.WalletAmount
	p.fieldMap["topup_amount"] = p.TopupAmount
	p.fieldMap["started_at"] = p.StartedAt
	p.fieldMap["expires_at"] = p.ExpiresAt
	p.fieldMap["reminder_7d_sent_at"] = p.Reminder7DSentAt
	p.fieldMap["reminder_3d_sent_at"] = p.Reminder3DSentAt
	p.fieldMap["reminder_1d_sent_at"] = p.Reminder1DSentAt
	p.fieldMap["created_at"] = p.CreatedAt
	p.fieldMap["updated_at"] = p.UpdatedAt
	p.fieldMap["version"] = p.Version
}

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

func (p planSubscription) replaceDB(db *gorm.DB) planSubscription {
	p.planSubscriptionDo.ReplaceDB(db)
	return p
}

type planSubscriptionDo struct{ gen.DO }

type IPlanSubscriptionDo interface {
	gen.SubQuery
	Debug() IPlanSubscriptionDo
	WithContext(ctx context.Context) IPlanSubscriptionDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() IPlanSubscriptionDo
	WriteDB() IPlanSubscriptionDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) IPlanSubscriptionDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) IPlanSubscriptionDo
	Not(conds ...gen.Condition) IPlanSubscriptionDo
	Or(conds ...gen.Condition) IPlanSubscriptionDo
	Select(conds ...field.Expr) IPlanSubscriptionDo
	Where(conds ...gen.Condition) IPlanSubscriptionDo
	Order(conds ...field.Expr) IPlanSubscriptionDo
	Distinct(cols ...field.Expr) IPlanSubscriptionDo
	Omit(cols ...field.Expr) IPlanSubscriptionDo
	Join(table schema.Tabler, on ...field.Expr) IPlanSubscriptionDo
	LeftJoin(table schema.Tabler, on ...field.Expr) IPlanSubscriptionDo
	RightJoin(table schema.Tabler, on ...field.Expr) IPlanSubscriptionDo
	Group(cols ...field.Expr) IPlanSubscriptionDo
	Having(conds ...gen.Condition) IPlanSubscriptionDo
	Limit(limit int) IPlanSubscriptionDo
	Offset(offset int) IPlanSubscriptionDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) IPlanSubscriptionDo
	Unscoped() IPlanSubscriptionDo
	Create(values ...*model.PlanSubscription) error
	CreateInBatches(values []*model.PlanSubscription, batchSize int) error
	Save(values ...*model.PlanSubscription) error
	First() (*model.PlanSubscription, error)
	Take() (*model.PlanSubscription, error)
	Last() (*model.PlanSubscription, error)
	Find() ([]*model.PlanSubscription, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.PlanSubscription, err error)
	FindInBatches(result *[]*model.PlanSubscription, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.PlanSubscription) (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) IPlanSubscriptionDo
	Assign(attrs ...field.AssignExpr) IPlanSubscriptionDo
	Joins(fields ...field.RelationField) IPlanSubscriptionDo
	Preload(fields ...field.RelationField) IPlanSubscriptionDo
	FirstOrInit() (*model.PlanSubscription, error)
	FirstOrCreate() (*model.PlanSubscription, error)
	FindByPage(offset int, limit int) (result []*model.PlanSubscription, 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) IPlanSubscriptionDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (p planSubscriptionDo) Debug() IPlanSubscriptionDo {
	return p.withDO(p.DO.Debug())
}

func (p planSubscriptionDo) WithContext(ctx context.Context) IPlanSubscriptionDo {
	return p.withDO(p.DO.WithContext(ctx))
}

func (p planSubscriptionDo) ReadDB() IPlanSubscriptionDo {
	return p.Clauses(dbresolver.Read)
}

func (p planSubscriptionDo) WriteDB() IPlanSubscriptionDo {
	return p.Clauses(dbresolver.Write)
}

func (p planSubscriptionDo) Session(config *gorm.Session) IPlanSubscriptionDo {
	return p.withDO(p.DO.Session(config))
}

func (p planSubscriptionDo) Clauses(conds ...clause.Expression) IPlanSubscriptionDo {
	return p.withDO(p.DO.Clauses(conds...))
}

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

func (p planSubscriptionDo) Not(conds ...gen.Condition) IPlanSubscriptionDo {
	return p.withDO(p.DO.Not(conds...))
}

func (p planSubscriptionDo) Or(conds ...gen.Condition) IPlanSubscriptionDo {
	return p.withDO(p.DO.Or(conds...))
}

func (p planSubscriptionDo) Select(conds ...field.Expr) IPlanSubscriptionDo {
	return p.withDO(p.DO.Select(conds...))
}

func (p planSubscriptionDo) Where(conds ...gen.Condition) IPlanSubscriptionDo {
	return p.withDO(p.DO.Where(conds...))
}

func (p planSubscriptionDo) Order(conds ...field.Expr) IPlanSubscriptionDo {
	return p.withDO(p.DO.Order(conds...))
}

func (p planSubscriptionDo) Distinct(cols ...field.Expr) IPlanSubscriptionDo {
	return p.withDO(p.DO.Distinct(cols...))
}

func (p planSubscriptionDo) Omit(cols ...field.Expr) IPlanSubscriptionDo {
	return p.withDO(p.DO.Omit(cols...))
}

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

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

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

func (p planSubscriptionDo) Group(cols ...field.Expr) IPlanSubscriptionDo {
	return p.withDO(p.DO.Group(cols...))
}

func (p planSubscriptionDo) Having(conds ...gen.Condition) IPlanSubscriptionDo {
	return p.withDO(p.DO.Having(conds...))
}

func (p planSubscriptionDo) Limit(limit int) IPlanSubscriptionDo {
	return p.withDO(p.DO.Limit(limit))
}

func (p planSubscriptionDo) Offset(offset int) IPlanSubscriptionDo {
	return p.withDO(p.DO.Offset(offset))
}

func (p planSubscriptionDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IPlanSubscriptionDo {
	return p.withDO(p.DO.Scopes(funcs...))
}

func (p planSubscriptionDo) Unscoped() IPlanSubscriptionDo {
	return p.withDO(p.DO.Unscoped())
}

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

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

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

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

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

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

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

func (p planSubscriptionDo) Attrs(attrs ...field.AssignExpr) IPlanSubscriptionDo {
	return p.withDO(p.DO.Attrs(attrs...))
}

func (p planSubscriptionDo) Assign(attrs ...field.AssignExpr) IPlanSubscriptionDo {
	return p.withDO(p.DO.Assign(attrs...))
}

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

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

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

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

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

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

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