// Code generated by go.ipao.vip/gen. DO NOT EDIT.
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
// Code generated by go.ipao.vip/gen. DO NOT EDIT.

package models

import (
	"context"
	"database/sql"

	"gorm.io/gorm"

	"go.ipao.vip/gen"

	"gorm.io/plugin/dbresolver"
)

var (
	Q                      = new(Query)
	CommentQuery           *commentQuery
	ContentQuery           *contentQuery
	ContentAccessQuery     *contentAccessQuery
	ContentAssetQuery      *contentAssetQuery
	ContentPriceQuery      *contentPriceQuery
	CouponQuery            *couponQuery
	MediaAssetQuery        *mediaAssetQuery
	NotificationQuery      *notificationQuery
	OrderQuery             *orderQuery
	OrderItemQuery         *orderItemQuery
	PayoutAccountQuery     *payoutAccountQuery
	TenantQuery            *tenantQuery
	TenantInviteQuery      *tenantInviteQuery
	TenantJoinRequestQuery *tenantJoinRequestQuery
	TenantLedgerQuery      *tenantLedgerQuery
	TenantUserQuery        *tenantUserQuery
	UserQuery              *userQuery
	UserCommentActionQuery *userCommentActionQuery
	UserContentActionQuery *userContentActionQuery
	UserCouponQuery        *userCouponQuery
)

func SetDefault(db *gorm.DB, opts ...gen.DOOption) {
	*Q = *Use(db, opts...)
	CommentQuery = &Q.Comment
	ContentQuery = &Q.Content
	ContentAccessQuery = &Q.ContentAccess
	ContentAssetQuery = &Q.ContentAsset
	ContentPriceQuery = &Q.ContentPrice
	CouponQuery = &Q.Coupon
	MediaAssetQuery = &Q.MediaAsset
	NotificationQuery = &Q.Notification
	OrderQuery = &Q.Order
	OrderItemQuery = &Q.OrderItem
	PayoutAccountQuery = &Q.PayoutAccount
	TenantQuery = &Q.Tenant
	TenantInviteQuery = &Q.TenantInvite
	TenantJoinRequestQuery = &Q.TenantJoinRequest
	TenantLedgerQuery = &Q.TenantLedger
	TenantUserQuery = &Q.TenantUser
	UserQuery = &Q.User
	UserCommentActionQuery = &Q.UserCommentAction
	UserContentActionQuery = &Q.UserContentAction
	UserCouponQuery = &Q.UserCoupon
}

func Use(db *gorm.DB, opts ...gen.DOOption) *Query {
	return &Query{
		db:                db,
		Comment:           newComment(db, opts...),
		Content:           newContent(db, opts...),
		ContentAccess:     newContentAccess(db, opts...),
		ContentAsset:      newContentAsset(db, opts...),
		ContentPrice:      newContentPrice(db, opts...),
		Coupon:            newCoupon(db, opts...),
		MediaAsset:        newMediaAsset(db, opts...),
		Notification:      newNotification(db, opts...),
		Order:             newOrder(db, opts...),
		OrderItem:         newOrderItem(db, opts...),
		PayoutAccount:     newPayoutAccount(db, opts...),
		Tenant:            newTenant(db, opts...),
		TenantInvite:      newTenantInvite(db, opts...),
		TenantJoinRequest: newTenantJoinRequest(db, opts...),
		TenantLedger:      newTenantLedger(db, opts...),
		TenantUser:        newTenantUser(db, opts...),
		User:              newUser(db, opts...),
		UserCommentAction: newUserCommentAction(db, opts...),
		UserContentAction: newUserContentAction(db, opts...),
		UserCoupon:        newUserCoupon(db, opts...),
	}
}

type Query struct {
	db *gorm.DB

	Comment           commentQuery
	Content           contentQuery
	ContentAccess     contentAccessQuery
	ContentAsset      contentAssetQuery
	ContentPrice      contentPriceQuery
	Coupon            couponQuery
	MediaAsset        mediaAssetQuery
	Notification      notificationQuery
	Order             orderQuery
	OrderItem         orderItemQuery
	PayoutAccount     payoutAccountQuery
	Tenant            tenantQuery
	TenantInvite      tenantInviteQuery
	TenantJoinRequest tenantJoinRequestQuery
	TenantLedger      tenantLedgerQuery
	TenantUser        tenantUserQuery
	User              userQuery
	UserCommentAction userCommentActionQuery
	UserContentAction userContentActionQuery
	UserCoupon        userCouponQuery
}

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

func (q *Query) clone(db *gorm.DB) *Query {
	return &Query{
		db:                db,
		Comment:           q.Comment.clone(db),
		Content:           q.Content.clone(db),
		ContentAccess:     q.ContentAccess.clone(db),
		ContentAsset:      q.ContentAsset.clone(db),
		ContentPrice:      q.ContentPrice.clone(db),
		Coupon:            q.Coupon.clone(db),
		MediaAsset:        q.MediaAsset.clone(db),
		Notification:      q.Notification.clone(db),
		Order:             q.Order.clone(db),
		OrderItem:         q.OrderItem.clone(db),
		PayoutAccount:     q.PayoutAccount.clone(db),
		Tenant:            q.Tenant.clone(db),
		TenantInvite:      q.TenantInvite.clone(db),
		TenantJoinRequest: q.TenantJoinRequest.clone(db),
		TenantLedger:      q.TenantLedger.clone(db),
		TenantUser:        q.TenantUser.clone(db),
		User:              q.User.clone(db),
		UserCommentAction: q.UserCommentAction.clone(db),
		UserContentAction: q.UserContentAction.clone(db),
		UserCoupon:        q.UserCoupon.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,
		Comment:           q.Comment.replaceDB(db),
		Content:           q.Content.replaceDB(db),
		ContentAccess:     q.ContentAccess.replaceDB(db),
		ContentAsset:      q.ContentAsset.replaceDB(db),
		ContentPrice:      q.ContentPrice.replaceDB(db),
		Coupon:            q.Coupon.replaceDB(db),
		MediaAsset:        q.MediaAsset.replaceDB(db),
		Notification:      q.Notification.replaceDB(db),
		Order:             q.Order.replaceDB(db),
		OrderItem:         q.OrderItem.replaceDB(db),
		PayoutAccount:     q.PayoutAccount.replaceDB(db),
		Tenant:            q.Tenant.replaceDB(db),
		TenantInvite:      q.TenantInvite.replaceDB(db),
		TenantJoinRequest: q.TenantJoinRequest.replaceDB(db),
		TenantLedger:      q.TenantLedger.replaceDB(db),
		TenantUser:        q.TenantUser.replaceDB(db),
		User:              q.User.replaceDB(db),
		UserCommentAction: q.UserCommentAction.replaceDB(db),
		UserContentAction: q.UserContentAction.replaceDB(db),
		UserCoupon:        q.UserCoupon.replaceDB(db),
	}
}

type queryCtx struct {
	Comment           *commentQueryDo
	Content           *contentQueryDo
	ContentAccess     *contentAccessQueryDo
	ContentAsset      *contentAssetQueryDo
	ContentPrice      *contentPriceQueryDo
	Coupon            *couponQueryDo
	MediaAsset        *mediaAssetQueryDo
	Notification      *notificationQueryDo
	Order             *orderQueryDo
	OrderItem         *orderItemQueryDo
	PayoutAccount     *payoutAccountQueryDo
	Tenant            *tenantQueryDo
	TenantInvite      *tenantInviteQueryDo
	TenantJoinRequest *tenantJoinRequestQueryDo
	TenantLedger      *tenantLedgerQueryDo
	TenantUser        *tenantUserQueryDo
	User              *userQueryDo
	UserCommentAction *userCommentActionQueryDo
	UserContentAction *userContentActionQueryDo
	UserCoupon        *userCouponQueryDo
}

func (q *Query) WithContext(ctx context.Context) *queryCtx {
	return &queryCtx{
		Comment:           q.Comment.WithContext(ctx),
		Content:           q.Content.WithContext(ctx),
		ContentAccess:     q.ContentAccess.WithContext(ctx),
		ContentAsset:      q.ContentAsset.WithContext(ctx),
		ContentPrice:      q.ContentPrice.WithContext(ctx),
		Coupon:            q.Coupon.WithContext(ctx),
		MediaAsset:        q.MediaAsset.WithContext(ctx),
		Notification:      q.Notification.WithContext(ctx),
		Order:             q.Order.WithContext(ctx),
		OrderItem:         q.OrderItem.WithContext(ctx),
		PayoutAccount:     q.PayoutAccount.WithContext(ctx),
		Tenant:            q.Tenant.WithContext(ctx),
		TenantInvite:      q.TenantInvite.WithContext(ctx),
		TenantJoinRequest: q.TenantJoinRequest.WithContext(ctx),
		TenantLedger:      q.TenantLedger.WithContext(ctx),
		TenantUser:        q.TenantUser.WithContext(ctx),
		User:              q.User.WithContext(ctx),
		UserCommentAction: q.UserCommentAction.WithContext(ctx),
		UserContentAction: q.UserContentAction.WithContext(ctx),
		UserCoupon:        q.UserCoupon.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
}
