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

import (
	"context"
	"database/sql"

	"gorm.io/gorm"

	"gorm.io/gen"

	"gorm.io/plugin/dbresolver"
)

var (
	Q              = new(Query)
	Merchant       *merchant
	MerchantsAPI   *merchantsAPI
	MerchantsMetum *merchantsMetum
	Order          *order
	SysConfig      *sysConfig
	SysWallet      *sysWallet
	User           *user
	Wallet         *wallet
)

func SetDefault(db *gorm.DB, opts ...gen.DOOption) {
	*Q = *Use(db, opts...)
	Merchant = &Q.Merchant
	MerchantsAPI = &Q.MerchantsAPI
	MerchantsMetum = &Q.MerchantsMetum
	Order = &Q.Order
	SysConfig = &Q.SysConfig
	SysWallet = &Q.SysWallet
	User = &Q.User
	Wallet = &Q.Wallet
}

func Use(db *gorm.DB, opts ...gen.DOOption) *Query {
	return &Query{
		db:             db,
		Merchant:       newMerchant(db, opts...),
		MerchantsAPI:   newMerchantsAPI(db, opts...),
		MerchantsMetum: newMerchantsMetum(db, opts...),
		Order:          newOrder(db, opts...),
		SysConfig:      newSysConfig(db, opts...),
		SysWallet:      newSysWallet(db, opts...),
		User:           newUser(db, opts...),
		Wallet:         newWallet(db, opts...),
	}
}

type Query struct {
	db *gorm.DB

	Merchant       merchant
	MerchantsAPI   merchantsAPI
	MerchantsMetum merchantsMetum
	Order          order
	SysConfig      sysConfig
	SysWallet      sysWallet
	User           user
	Wallet         wallet
}

func (q *Query) Available() bool { return q.db != nil }

func (q *Query) clone(db *gorm.DB) *Query {
	return &Query{
		db:             db,
		Merchant:       q.Merchant.clone(db),
		MerchantsAPI:   q.MerchantsAPI.clone(db),
		MerchantsMetum: q.MerchantsMetum.clone(db),
		Order:          q.Order.clone(db),
		SysConfig:      q.SysConfig.clone(db),
		SysWallet:      q.SysWallet.clone(db),
		User:           q.User.clone(db),
		Wallet:         q.Wallet.clone(db),
	}
}

func (q *Query) ReadDB() *Query {
	return q.ReplaceDB(q.db.Clauses(dbresolver.Read))
}

func (q *Query) WriteDB() *Query {
	return q.ReplaceDB(q.db.Clauses(dbresolver.Write))
}

func (q *Query) ReplaceDB(db *gorm.DB) *Query {
	return &Query{
		db:             db,
		Merchant:       q.Merchant.replaceDB(db),
		MerchantsAPI:   q.MerchantsAPI.replaceDB(db),
		MerchantsMetum: q.MerchantsMetum.replaceDB(db),
		Order:          q.Order.replaceDB(db),
		SysConfig:      q.SysConfig.replaceDB(db),
		SysWallet:      q.SysWallet.replaceDB(db),
		User:           q.User.replaceDB(db),
		Wallet:         q.Wallet.replaceDB(db),
	}
}

type queryCtx struct {
	Merchant       IMerchantDo
	MerchantsAPI   IMerchantsAPIDo
	MerchantsMetum IMerchantsMetumDo
	Order          IOrderDo
	SysConfig      ISysConfigDo
	SysWallet      ISysWalletDo
	User           IUserDo
	Wallet         IWalletDo
}

func (q *Query) WithContext(ctx context.Context) *queryCtx {
	return &queryCtx{
		Merchant:       q.Merchant.WithContext(ctx),
		MerchantsAPI:   q.MerchantsAPI.WithContext(ctx),
		MerchantsMetum: q.MerchantsMetum.WithContext(ctx),
		Order:          q.Order.WithContext(ctx),
		SysConfig:      q.SysConfig.WithContext(ctx),
		SysWallet:      q.SysWallet.WithContext(ctx),
		User:           q.User.WithContext(ctx),
		Wallet:         q.Wallet.WithContext(ctx),
	}
}

func (q *Query) Transaction(fc func(tx *Query) error, opts ...*sql.TxOptions) error {
	return q.db.Transaction(func(tx *gorm.DB) error { return fc(q.clone(tx)) }, opts...)
}

func (q *Query) Begin(opts ...*sql.TxOptions) *QueryTx {
	tx := q.db.Begin(opts...)
	return &QueryTx{Query: q.clone(tx), Error: tx.Error}
}

type QueryTx struct {
	*Query
	Error error
}

func (q *QueryTx) Commit() error {
	return q.db.Commit().Error
}

func (q *QueryTx) Rollback() error {
	return q.db.Rollback().Error
}

func (q *QueryTx) SavePoint(name string) error {
	return q.db.SavePoint(name).Error
}

func (q *QueryTx) RollbackTo(name string) error {
	return q.db.RollbackTo(name).Error
}
