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

import (
	"context"
	"database/sql"

	"gorm.io/gorm"

	"gorm.io/gen"

	"gorm.io/plugin/dbresolver"
)

var (
	Q                      = new(Query)
	Admin                  *admin
	AppKeyword             *appKeyword
	AppKeywordReply        *appKeywordReply
	AppMessageLog          *appMessageLog
	AppUser                *appUser
	LogOperation           *logOperation
	LogRequest             *logRequest
	MiniProgram            *miniProgram
	MiniProgramAccessToken *miniProgramAccessToken
)

func SetDefault(db *gorm.DB, opts ...gen.DOOption) {
	*Q = *Use(db, opts...)
	Admin = &Q.Admin
	AppKeyword = &Q.AppKeyword
	AppKeywordReply = &Q.AppKeywordReply
	AppMessageLog = &Q.AppMessageLog
	AppUser = &Q.AppUser
	LogOperation = &Q.LogOperation
	LogRequest = &Q.LogRequest
	MiniProgram = &Q.MiniProgram
	MiniProgramAccessToken = &Q.MiniProgramAccessToken
}

func Use(db *gorm.DB, opts ...gen.DOOption) *Query {
	return &Query{
		db:                     db,
		Admin:                  newAdmin(db, opts...),
		AppKeyword:             newAppKeyword(db, opts...),
		AppKeywordReply:        newAppKeywordReply(db, opts...),
		AppMessageLog:          newAppMessageLog(db, opts...),
		AppUser:                newAppUser(db, opts...),
		LogOperation:           newLogOperation(db, opts...),
		LogRequest:             newLogRequest(db, opts...),
		MiniProgram:            newMiniProgram(db, opts...),
		MiniProgramAccessToken: newMiniProgramAccessToken(db, opts...),
	}
}

type Query struct {
	db *gorm.DB

	Admin                  admin
	AppKeyword             appKeyword
	AppKeywordReply        appKeywordReply
	AppMessageLog          appMessageLog
	AppUser                appUser
	LogOperation           logOperation
	LogRequest             logRequest
	MiniProgram            miniProgram
	MiniProgramAccessToken miniProgramAccessToken
}

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

func (q *Query) clone(db *gorm.DB) *Query {
	return &Query{
		db:                     db,
		Admin:                  q.Admin.clone(db),
		AppKeyword:             q.AppKeyword.clone(db),
		AppKeywordReply:        q.AppKeywordReply.clone(db),
		AppMessageLog:          q.AppMessageLog.clone(db),
		AppUser:                q.AppUser.clone(db),
		LogOperation:           q.LogOperation.clone(db),
		LogRequest:             q.LogRequest.clone(db),
		MiniProgram:            q.MiniProgram.clone(db),
		MiniProgramAccessToken: q.MiniProgramAccessToken.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,
		Admin:                  q.Admin.replaceDB(db),
		AppKeyword:             q.AppKeyword.replaceDB(db),
		AppKeywordReply:        q.AppKeywordReply.replaceDB(db),
		AppMessageLog:          q.AppMessageLog.replaceDB(db),
		AppUser:                q.AppUser.replaceDB(db),
		LogOperation:           q.LogOperation.replaceDB(db),
		LogRequest:             q.LogRequest.replaceDB(db),
		MiniProgram:            q.MiniProgram.replaceDB(db),
		MiniProgramAccessToken: q.MiniProgramAccessToken.replaceDB(db),
	}
}

type queryCtx struct {
	Admin                  *adminDo
	AppKeyword             *appKeywordDo
	AppKeywordReply        *appKeywordReplyDo
	AppMessageLog          *appMessageLogDo
	AppUser                *appUserDo
	LogOperation           *logOperationDo
	LogRequest             *logRequestDo
	MiniProgram            *miniProgramDo
	MiniProgramAccessToken *miniProgramAccessTokenDo
}

func (q *Query) WithContext(ctx context.Context) *queryCtx {
	return &queryCtx{
		Admin:                  q.Admin.WithContext(ctx),
		AppKeyword:             q.AppKeyword.WithContext(ctx),
		AppKeywordReply:        q.AppKeywordReply.WithContext(ctx),
		AppMessageLog:          q.AppMessageLog.WithContext(ctx),
		AppUser:                q.AppUser.WithContext(ctx),
		LogOperation:           q.LogOperation.WithContext(ctx),
		LogRequest:             q.LogRequest.WithContext(ctx),
		MiniProgram:            q.MiniProgram.WithContext(ctx),
		MiniProgramAccessToken: q.MiniProgramAccessToken.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
}
