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

import (
	"context"
	"database/sql"

	"gorm.io/gorm"

	"gorm.io/gen"

	"gorm.io/plugin/dbresolver"
)

var (
	Q           = new(Query)
	Customer    *customer
	Order       *order
	PrepTask    *prepTask
	ProductType *productType
	Shift       *shift
	Task        *task
	TeamTask    *teamTask
	TeamType    *teamType
	WorkArea    *workArea
	Worker      *worker
	Workshop    *workshop
)

func SetDefault(db *gorm.DB, opts ...gen.DOOption) {
	*Q = *Use(db, opts...)
	Customer = &Q.Customer
	Order = &Q.Order
	PrepTask = &Q.PrepTask
	ProductType = &Q.ProductType
	Shift = &Q.Shift
	Task = &Q.Task
	TeamTask = &Q.TeamTask
	TeamType = &Q.TeamType
	WorkArea = &Q.WorkArea
	Worker = &Q.Worker
	Workshop = &Q.Workshop
}

func Use(db *gorm.DB, opts ...gen.DOOption) *Query {
	return &Query{
		db:          db,
		Customer:    newCustomer(db, opts...),
		Order:       newOrder(db, opts...),
		PrepTask:    newPrepTask(db, opts...),
		ProductType: newProductType(db, opts...),
		Shift:       newShift(db, opts...),
		Task:        newTask(db, opts...),
		TeamTask:    newTeamTask(db, opts...),
		TeamType:    newTeamType(db, opts...),
		WorkArea:    newWorkArea(db, opts...),
		Worker:      newWorker(db, opts...),
		Workshop:    newWorkshop(db, opts...),
	}
}

type Query struct {
	db *gorm.DB

	Customer    customer
	Order       order
	PrepTask    prepTask
	ProductType productType
	Shift       shift
	Task        task
	TeamTask    teamTask
	TeamType    teamType
	WorkArea    workArea
	Worker      worker
	Workshop    workshop
}

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

func (q *Query) clone(db *gorm.DB) *Query {
	return &Query{
		db:          db,
		Customer:    q.Customer.clone(db),
		Order:       q.Order.clone(db),
		PrepTask:    q.PrepTask.clone(db),
		ProductType: q.ProductType.clone(db),
		Shift:       q.Shift.clone(db),
		Task:        q.Task.clone(db),
		TeamTask:    q.TeamTask.clone(db),
		TeamType:    q.TeamType.clone(db),
		WorkArea:    q.WorkArea.clone(db),
		Worker:      q.Worker.clone(db),
		Workshop:    q.Workshop.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,
		Customer:    q.Customer.replaceDB(db),
		Order:       q.Order.replaceDB(db),
		PrepTask:    q.PrepTask.replaceDB(db),
		ProductType: q.ProductType.replaceDB(db),
		Shift:       q.Shift.replaceDB(db),
		Task:        q.Task.replaceDB(db),
		TeamTask:    q.TeamTask.replaceDB(db),
		TeamType:    q.TeamType.replaceDB(db),
		WorkArea:    q.WorkArea.replaceDB(db),
		Worker:      q.Worker.replaceDB(db),
		Workshop:    q.Workshop.replaceDB(db),
	}
}

type queryCtx struct {
	Customer    ICustomerDo
	Order       IOrderDo
	PrepTask    IPrepTaskDo
	ProductType IProductTypeDo
	Shift       IShiftDo
	Task        ITaskDo
	TeamTask    ITeamTaskDo
	TeamType    ITeamTypeDo
	WorkArea    IWorkAreaDo
	Worker      IWorkerDo
	Workshop    IWorkshopDo
}

func (q *Query) WithContext(ctx context.Context) *queryCtx {
	return &queryCtx{
		Customer:    q.Customer.WithContext(ctx),
		Order:       q.Order.WithContext(ctx),
		PrepTask:    q.PrepTask.WithContext(ctx),
		ProductType: q.ProductType.WithContext(ctx),
		Shift:       q.Shift.WithContext(ctx),
		Task:        q.Task.WithContext(ctx),
		TeamTask:    q.TeamTask.WithContext(ctx),
		TeamType:    q.TeamType.WithContext(ctx),
		WorkArea:    q.WorkArea.WithContext(ctx),
		Worker:      q.Worker.WithContext(ctx),
		Workshop:    q.Workshop.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
}
