// 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 (
	"app/internal/models"
	"context"

	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"gorm.io/gorm/schema"

	"gorm.io/gen"
	"gorm.io/gen/field"

	"gorm.io/plugin/dbresolver"
)

func newTask(db *gorm.DB, opts ...gen.DOOption) task {
	_task := task{}

	_task.taskDo.UseDB(db, opts...)
	_task.taskDo.UseModel(&models.Task{})

	tableName := _task.taskDo.TableName()
	_task.ALL = field.NewAsterisk(tableName)
	_task.Id = field.NewUint(tableName, "id")
	_task.Description = field.NewString(tableName, "description")
	_task.ProductTypeId = field.NewUint(tableName, "product_type_id")
	_task.OrderId = field.NewUint(tableName, "order_id")
	_task.ProductionStart = field.NewInt64(tableName, "production_start")
	_task.CreatedAt = field.NewInt64(tableName, "created_at")
	_task.Amount = field.NewUint(tableName, "amount")
	_task.PrepTasks = taskHasManyPrepTasks{
		db: db.Session(&gorm.Session{}),

		RelationField: field.NewRelation("PrepTasks", "models.PrepTask"),
		Task: struct {
			field.RelationField
			ProductType struct {
				field.RelationField
			}
			Order struct {
				field.RelationField
				ProductType struct {
					field.RelationField
				}
				Customer struct {
					field.RelationField
					Orders struct {
						field.RelationField
					}
				}
				Tasks struct {
					field.RelationField
				}
			}
			PrepTasks struct {
				field.RelationField
			}
			Workshops struct {
				field.RelationField
				WorkAreas struct {
					field.RelationField
					Workshop struct {
						field.RelationField
					}
					PrepTasks struct {
						field.RelationField
					}
					Shifts struct {
						field.RelationField
						ProductType struct {
							field.RelationField
						}
						WorkArea struct {
							field.RelationField
						}
					}
					TeamTasks struct {
						field.RelationField
						TeamType struct {
							field.RelationField
						}
						TeamLeader struct {
							field.RelationField
							Workshop struct {
								field.RelationField
							}
							TeamTasks struct {
								field.RelationField
							}
						}
						WorkArea struct {
							field.RelationField
						}
						TeamMembers struct {
							field.RelationField
						}
					}
				}
				Workers struct {
					field.RelationField
				}
				Tasks struct {
					field.RelationField
				}
			}
		}{
			RelationField: field.NewRelation("PrepTasks.Task", "models.Task"),
			ProductType: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("PrepTasks.Task.ProductType", "models.ProductType"),
			},
			Order: struct {
				field.RelationField
				ProductType struct {
					field.RelationField
				}
				Customer struct {
					field.RelationField
					Orders struct {
						field.RelationField
					}
				}
				Tasks struct {
					field.RelationField
				}
			}{
				RelationField: field.NewRelation("PrepTasks.Task.Order", "models.Order"),
				ProductType: struct {
					field.RelationField
				}{
					RelationField: field.NewRelation("PrepTasks.Task.Order.ProductType", "models.ProductType"),
				},
				Customer: struct {
					field.RelationField
					Orders struct {
						field.RelationField
					}
				}{
					RelationField: field.NewRelation("PrepTasks.Task.Order.Customer", "models.Customer"),
					Orders: struct {
						field.RelationField
					}{
						RelationField: field.NewRelation("PrepTasks.Task.Order.Customer.Orders", "models.Order"),
					},
				},
				Tasks: struct {
					field.RelationField
				}{
					RelationField: field.NewRelation("PrepTasks.Task.Order.Tasks", "models.Task"),
				},
			},
			PrepTasks: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("PrepTasks.Task.PrepTasks", "models.PrepTask"),
			},
			Workshops: struct {
				field.RelationField
				WorkAreas struct {
					field.RelationField
					Workshop struct {
						field.RelationField
					}
					PrepTasks struct {
						field.RelationField
					}
					Shifts struct {
						field.RelationField
						ProductType struct {
							field.RelationField
						}
						WorkArea struct {
							field.RelationField
						}
					}
					TeamTasks struct {
						field.RelationField
						TeamType struct {
							field.RelationField
						}
						TeamLeader struct {
							field.RelationField
							Workshop struct {
								field.RelationField
							}
							TeamTasks struct {
								field.RelationField
							}
						}
						WorkArea struct {
							field.RelationField
						}
						TeamMembers struct {
							field.RelationField
						}
					}
				}
				Workers struct {
					field.RelationField
				}
				Tasks struct {
					field.RelationField
				}
			}{
				RelationField: field.NewRelation("PrepTasks.Task.Workshops", "models.Workshop"),
				WorkAreas: struct {
					field.RelationField
					Workshop struct {
						field.RelationField
					}
					PrepTasks struct {
						field.RelationField
					}
					Shifts struct {
						field.RelationField
						ProductType struct {
							field.RelationField
						}
						WorkArea struct {
							field.RelationField
						}
					}
					TeamTasks struct {
						field.RelationField
						TeamType struct {
							field.RelationField
						}
						TeamLeader struct {
							field.RelationField
							Workshop struct {
								field.RelationField
							}
							TeamTasks struct {
								field.RelationField
							}
						}
						WorkArea struct {
							field.RelationField
						}
						TeamMembers struct {
							field.RelationField
						}
					}
				}{
					RelationField: field.NewRelation("PrepTasks.Task.Workshops.WorkAreas", "models.WorkArea"),
					Workshop: struct {
						field.RelationField
					}{
						RelationField: field.NewRelation("PrepTasks.Task.Workshops.WorkAreas.Workshop", "models.Workshop"),
					},
					PrepTasks: struct {
						field.RelationField
					}{
						RelationField: field.NewRelation("PrepTasks.Task.Workshops.WorkAreas.PrepTasks", "models.PrepTask"),
					},
					Shifts: struct {
						field.RelationField
						ProductType struct {
							field.RelationField
						}
						WorkArea struct {
							field.RelationField
						}
					}{
						RelationField: field.NewRelation("PrepTasks.Task.Workshops.WorkAreas.Shifts", "models.Shift"),
						ProductType: struct {
							field.RelationField
						}{
							RelationField: field.NewRelation("PrepTasks.Task.Workshops.WorkAreas.Shifts.ProductType", "models.ProductType"),
						},
						WorkArea: struct {
							field.RelationField
						}{
							RelationField: field.NewRelation("PrepTasks.Task.Workshops.WorkAreas.Shifts.WorkArea", "models.WorkArea"),
						},
					},
					TeamTasks: struct {
						field.RelationField
						TeamType struct {
							field.RelationField
						}
						TeamLeader struct {
							field.RelationField
							Workshop struct {
								field.RelationField
							}
							TeamTasks struct {
								field.RelationField
							}
						}
						WorkArea struct {
							field.RelationField
						}
						TeamMembers struct {
							field.RelationField
						}
					}{
						RelationField: field.NewRelation("PrepTasks.Task.Workshops.WorkAreas.TeamTasks", "models.TeamTask"),
						TeamType: struct {
							field.RelationField
						}{
							RelationField: field.NewRelation("PrepTasks.Task.Workshops.WorkAreas.TeamTasks.TeamType", "models.TeamType"),
						},
						TeamLeader: struct {
							field.RelationField
							Workshop struct {
								field.RelationField
							}
							TeamTasks struct {
								field.RelationField
							}
						}{
							RelationField: field.NewRelation("PrepTasks.Task.Workshops.WorkAreas.TeamTasks.TeamLeader", "models.Worker"),
							Workshop: struct {
								field.RelationField
							}{
								RelationField: field.NewRelation("PrepTasks.Task.Workshops.WorkAreas.TeamTasks.TeamLeader.Workshop", "models.Workshop"),
							},
							TeamTasks: struct {
								field.RelationField
							}{
								RelationField: field.NewRelation("PrepTasks.Task.Workshops.WorkAreas.TeamTasks.TeamLeader.TeamTasks", "models.TeamTask"),
							},
						},
						WorkArea: struct {
							field.RelationField
						}{
							RelationField: field.NewRelation("PrepTasks.Task.Workshops.WorkAreas.TeamTasks.WorkArea", "models.WorkArea"),
						},
						TeamMembers: struct {
							field.RelationField
						}{
							RelationField: field.NewRelation("PrepTasks.Task.Workshops.WorkAreas.TeamTasks.TeamMembers", "models.Worker"),
						},
					},
				},
				Workers: struct {
					field.RelationField
				}{
					RelationField: field.NewRelation("PrepTasks.Task.Workshops.Workers", "models.Worker"),
				},
				Tasks: struct {
					field.RelationField
				}{
					RelationField: field.NewRelation("PrepTasks.Task.Workshops.Tasks", "models.Task"),
				},
			},
		},
		WorkArea: struct {
			field.RelationField
		}{
			RelationField: field.NewRelation("PrepTasks.WorkArea", "models.WorkArea"),
		},
	}

	_task.ProductType = taskBelongsToProductType{
		db: db.Session(&gorm.Session{}),

		RelationField: field.NewRelation("ProductType", "models.ProductType"),
	}

	_task.Order = taskBelongsToOrder{
		db: db.Session(&gorm.Session{}),

		RelationField: field.NewRelation("Order", "models.Order"),
	}

	_task.Workshops = taskManyToManyWorkshops{
		db: db.Session(&gorm.Session{}),

		RelationField: field.NewRelation("Workshops", "models.Workshop"),
	}

	_task.fillFieldMap()

	return _task
}

type task struct {
	taskDo

	ALL             field.Asterisk
	Id              field.Uint
	Description     field.String
	ProductTypeId   field.Uint
	OrderId         field.Uint
	ProductionStart field.Int64
	CreatedAt       field.Int64
	Amount          field.Uint
	PrepTasks       taskHasManyPrepTasks

	ProductType taskBelongsToProductType

	Order taskBelongsToOrder

	Workshops taskManyToManyWorkshops

	fieldMap map[string]field.Expr
}

func (t task) Table(newTableName string) *task {
	t.taskDo.UseTable(newTableName)
	return t.updateTableName(newTableName)
}

func (t task) As(alias string) *task {
	t.taskDo.DO = *(t.taskDo.As(alias).(*gen.DO))
	return t.updateTableName(alias)
}

func (t *task) updateTableName(table string) *task {
	t.ALL = field.NewAsterisk(table)
	t.Id = field.NewUint(table, "id")
	t.Description = field.NewString(table, "description")
	t.ProductTypeId = field.NewUint(table, "product_type_id")
	t.OrderId = field.NewUint(table, "order_id")
	t.ProductionStart = field.NewInt64(table, "production_start")
	t.CreatedAt = field.NewInt64(table, "created_at")
	t.Amount = field.NewUint(table, "amount")

	t.fillFieldMap()

	return t
}

func (t *task) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
	_f, ok := t.fieldMap[fieldName]
	if !ok || _f == nil {
		return nil, false
	}
	_oe, ok := _f.(field.OrderExpr)
	return _oe, ok
}

func (t *task) fillFieldMap() {
	t.fieldMap = make(map[string]field.Expr, 11)
	t.fieldMap["id"] = t.Id
	t.fieldMap["description"] = t.Description
	t.fieldMap["product_type_id"] = t.ProductTypeId
	t.fieldMap["order_id"] = t.OrderId
	t.fieldMap["production_start"] = t.ProductionStart
	t.fieldMap["created_at"] = t.CreatedAt
	t.fieldMap["amount"] = t.Amount

}

func (t task) clone(db *gorm.DB) task {
	t.taskDo.ReplaceConnPool(db.Statement.ConnPool)
	return t
}

func (t task) replaceDB(db *gorm.DB) task {
	t.taskDo.ReplaceDB(db)
	return t
}

type taskHasManyPrepTasks struct {
	db *gorm.DB

	field.RelationField

	Task struct {
		field.RelationField
		ProductType struct {
			field.RelationField
		}
		Order struct {
			field.RelationField
			ProductType struct {
				field.RelationField
			}
			Customer struct {
				field.RelationField
				Orders struct {
					field.RelationField
				}
			}
			Tasks struct {
				field.RelationField
			}
		}
		PrepTasks struct {
			field.RelationField
		}
		Workshops struct {
			field.RelationField
			WorkAreas struct {
				field.RelationField
				Workshop struct {
					field.RelationField
				}
				PrepTasks struct {
					field.RelationField
				}
				Shifts struct {
					field.RelationField
					ProductType struct {
						field.RelationField
					}
					WorkArea struct {
						field.RelationField
					}
				}
				TeamTasks struct {
					field.RelationField
					TeamType struct {
						field.RelationField
					}
					TeamLeader struct {
						field.RelationField
						Workshop struct {
							field.RelationField
						}
						TeamTasks struct {
							field.RelationField
						}
					}
					WorkArea struct {
						field.RelationField
					}
					TeamMembers struct {
						field.RelationField
					}
				}
			}
			Workers struct {
				field.RelationField
			}
			Tasks struct {
				field.RelationField
			}
		}
	}
	WorkArea struct {
		field.RelationField
	}
}

func (a taskHasManyPrepTasks) Where(conds ...field.Expr) *taskHasManyPrepTasks {
	if len(conds) == 0 {
		return &a
	}

	exprs := make([]clause.Expression, 0, len(conds))
	for _, cond := range conds {
		exprs = append(exprs, cond.BeCond().(clause.Expression))
	}
	a.db = a.db.Clauses(clause.Where{Exprs: exprs})
	return &a
}

func (a taskHasManyPrepTasks) WithContext(ctx context.Context) *taskHasManyPrepTasks {
	a.db = a.db.WithContext(ctx)
	return &a
}

func (a taskHasManyPrepTasks) Session(session *gorm.Session) *taskHasManyPrepTasks {
	a.db = a.db.Session(session)
	return &a
}

func (a taskHasManyPrepTasks) Model(m *models.Task) *taskHasManyPrepTasksTx {
	return &taskHasManyPrepTasksTx{a.db.Model(m).Association(a.Name())}
}

type taskHasManyPrepTasksTx struct{ tx *gorm.Association }

func (a taskHasManyPrepTasksTx) Find() (result []*models.PrepTask, err error) {
	return result, a.tx.Find(&result)
}

func (a taskHasManyPrepTasksTx) Append(values ...*models.PrepTask) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Append(targetValues...)
}

func (a taskHasManyPrepTasksTx) Replace(values ...*models.PrepTask) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Replace(targetValues...)
}

func (a taskHasManyPrepTasksTx) Delete(values ...*models.PrepTask) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Delete(targetValues...)
}

func (a taskHasManyPrepTasksTx) Clear() error {
	return a.tx.Clear()
}

func (a taskHasManyPrepTasksTx) Count() int64 {
	return a.tx.Count()
}

type taskBelongsToProductType struct {
	db *gorm.DB

	field.RelationField
}

func (a taskBelongsToProductType) Where(conds ...field.Expr) *taskBelongsToProductType {
	if len(conds) == 0 {
		return &a
	}

	exprs := make([]clause.Expression, 0, len(conds))
	for _, cond := range conds {
		exprs = append(exprs, cond.BeCond().(clause.Expression))
	}
	a.db = a.db.Clauses(clause.Where{Exprs: exprs})
	return &a
}

func (a taskBelongsToProductType) WithContext(ctx context.Context) *taskBelongsToProductType {
	a.db = a.db.WithContext(ctx)
	return &a
}

func (a taskBelongsToProductType) Session(session *gorm.Session) *taskBelongsToProductType {
	a.db = a.db.Session(session)
	return &a
}

func (a taskBelongsToProductType) Model(m *models.Task) *taskBelongsToProductTypeTx {
	return &taskBelongsToProductTypeTx{a.db.Model(m).Association(a.Name())}
}

type taskBelongsToProductTypeTx struct{ tx *gorm.Association }

func (a taskBelongsToProductTypeTx) Find() (result *models.ProductType, err error) {
	return result, a.tx.Find(&result)
}

func (a taskBelongsToProductTypeTx) Append(values ...*models.ProductType) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Append(targetValues...)
}

func (a taskBelongsToProductTypeTx) Replace(values ...*models.ProductType) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Replace(targetValues...)
}

func (a taskBelongsToProductTypeTx) Delete(values ...*models.ProductType) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Delete(targetValues...)
}

func (a taskBelongsToProductTypeTx) Clear() error {
	return a.tx.Clear()
}

func (a taskBelongsToProductTypeTx) Count() int64 {
	return a.tx.Count()
}

type taskBelongsToOrder struct {
	db *gorm.DB

	field.RelationField
}

func (a taskBelongsToOrder) Where(conds ...field.Expr) *taskBelongsToOrder {
	if len(conds) == 0 {
		return &a
	}

	exprs := make([]clause.Expression, 0, len(conds))
	for _, cond := range conds {
		exprs = append(exprs, cond.BeCond().(clause.Expression))
	}
	a.db = a.db.Clauses(clause.Where{Exprs: exprs})
	return &a
}

func (a taskBelongsToOrder) WithContext(ctx context.Context) *taskBelongsToOrder {
	a.db = a.db.WithContext(ctx)
	return &a
}

func (a taskBelongsToOrder) Session(session *gorm.Session) *taskBelongsToOrder {
	a.db = a.db.Session(session)
	return &a
}

func (a taskBelongsToOrder) Model(m *models.Task) *taskBelongsToOrderTx {
	return &taskBelongsToOrderTx{a.db.Model(m).Association(a.Name())}
}

type taskBelongsToOrderTx struct{ tx *gorm.Association }

func (a taskBelongsToOrderTx) Find() (result *models.Order, err error) {
	return result, a.tx.Find(&result)
}

func (a taskBelongsToOrderTx) Append(values ...*models.Order) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Append(targetValues...)
}

func (a taskBelongsToOrderTx) Replace(values ...*models.Order) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Replace(targetValues...)
}

func (a taskBelongsToOrderTx) Delete(values ...*models.Order) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Delete(targetValues...)
}

func (a taskBelongsToOrderTx) Clear() error {
	return a.tx.Clear()
}

func (a taskBelongsToOrderTx) Count() int64 {
	return a.tx.Count()
}

type taskManyToManyWorkshops struct {
	db *gorm.DB

	field.RelationField
}

func (a taskManyToManyWorkshops) Where(conds ...field.Expr) *taskManyToManyWorkshops {
	if len(conds) == 0 {
		return &a
	}

	exprs := make([]clause.Expression, 0, len(conds))
	for _, cond := range conds {
		exprs = append(exprs, cond.BeCond().(clause.Expression))
	}
	a.db = a.db.Clauses(clause.Where{Exprs: exprs})
	return &a
}

func (a taskManyToManyWorkshops) WithContext(ctx context.Context) *taskManyToManyWorkshops {
	a.db = a.db.WithContext(ctx)
	return &a
}

func (a taskManyToManyWorkshops) Session(session *gorm.Session) *taskManyToManyWorkshops {
	a.db = a.db.Session(session)
	return &a
}

func (a taskManyToManyWorkshops) Model(m *models.Task) *taskManyToManyWorkshopsTx {
	return &taskManyToManyWorkshopsTx{a.db.Model(m).Association(a.Name())}
}

type taskManyToManyWorkshopsTx struct{ tx *gorm.Association }

func (a taskManyToManyWorkshopsTx) Find() (result []*models.Workshop, err error) {
	return result, a.tx.Find(&result)
}

func (a taskManyToManyWorkshopsTx) Append(values ...*models.Workshop) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Append(targetValues...)
}

func (a taskManyToManyWorkshopsTx) Replace(values ...*models.Workshop) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Replace(targetValues...)
}

func (a taskManyToManyWorkshopsTx) Delete(values ...*models.Workshop) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Delete(targetValues...)
}

func (a taskManyToManyWorkshopsTx) Clear() error {
	return a.tx.Clear()
}

func (a taskManyToManyWorkshopsTx) Count() int64 {
	return a.tx.Count()
}

type taskDo struct{ gen.DO }

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

func (t taskDo) Debug() ITaskDo {
	return t.withDO(t.DO.Debug())
}

func (t taskDo) WithContext(ctx context.Context) ITaskDo {
	return t.withDO(t.DO.WithContext(ctx))
}

func (t taskDo) ReadDB() ITaskDo {
	return t.Clauses(dbresolver.Read)
}

func (t taskDo) WriteDB() ITaskDo {
	return t.Clauses(dbresolver.Write)
}

func (t taskDo) Session(config *gorm.Session) ITaskDo {
	return t.withDO(t.DO.Session(config))
}

func (t taskDo) Clauses(conds ...clause.Expression) ITaskDo {
	return t.withDO(t.DO.Clauses(conds...))
}

func (t taskDo) Returning(value interface{}, columns ...string) ITaskDo {
	return t.withDO(t.DO.Returning(value, columns...))
}

func (t taskDo) Not(conds ...gen.Condition) ITaskDo {
	return t.withDO(t.DO.Not(conds...))
}

func (t taskDo) Or(conds ...gen.Condition) ITaskDo {
	return t.withDO(t.DO.Or(conds...))
}

func (t taskDo) Select(conds ...field.Expr) ITaskDo {
	return t.withDO(t.DO.Select(conds...))
}

func (t taskDo) Where(conds ...gen.Condition) ITaskDo {
	return t.withDO(t.DO.Where(conds...))
}

func (t taskDo) Order(conds ...field.Expr) ITaskDo {
	return t.withDO(t.DO.Order(conds...))
}

func (t taskDo) Distinct(cols ...field.Expr) ITaskDo {
	return t.withDO(t.DO.Distinct(cols...))
}

func (t taskDo) Omit(cols ...field.Expr) ITaskDo {
	return t.withDO(t.DO.Omit(cols...))
}

func (t taskDo) Join(table schema.Tabler, on ...field.Expr) ITaskDo {
	return t.withDO(t.DO.Join(table, on...))
}

func (t taskDo) LeftJoin(table schema.Tabler, on ...field.Expr) ITaskDo {
	return t.withDO(t.DO.LeftJoin(table, on...))
}

func (t taskDo) RightJoin(table schema.Tabler, on ...field.Expr) ITaskDo {
	return t.withDO(t.DO.RightJoin(table, on...))
}

func (t taskDo) Group(cols ...field.Expr) ITaskDo {
	return t.withDO(t.DO.Group(cols...))
}

func (t taskDo) Having(conds ...gen.Condition) ITaskDo {
	return t.withDO(t.DO.Having(conds...))
}

func (t taskDo) Limit(limit int) ITaskDo {
	return t.withDO(t.DO.Limit(limit))
}

func (t taskDo) Offset(offset int) ITaskDo {
	return t.withDO(t.DO.Offset(offset))
}

func (t taskDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ITaskDo {
	return t.withDO(t.DO.Scopes(funcs...))
}

func (t taskDo) Unscoped() ITaskDo {
	return t.withDO(t.DO.Unscoped())
}

func (t taskDo) Create(values ...*models.Task) error {
	if len(values) == 0 {
		return nil
	}
	return t.DO.Create(values)
}

func (t taskDo) CreateInBatches(values []*models.Task, batchSize int) error {
	return t.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 (t taskDo) Save(values ...*models.Task) error {
	if len(values) == 0 {
		return nil
	}
	return t.DO.Save(values)
}

func (t taskDo) First() (*models.Task, error) {
	if result, err := t.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*models.Task), nil
	}
}

func (t taskDo) Take() (*models.Task, error) {
	if result, err := t.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*models.Task), nil
	}
}

func (t taskDo) Last() (*models.Task, error) {
	if result, err := t.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*models.Task), nil
	}
}

func (t taskDo) Find() ([]*models.Task, error) {
	result, err := t.DO.Find()
	return result.([]*models.Task), err
}

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

func (t taskDo) Attrs(attrs ...field.AssignExpr) ITaskDo {
	return t.withDO(t.DO.Attrs(attrs...))
}

func (t taskDo) Assign(attrs ...field.AssignExpr) ITaskDo {
	return t.withDO(t.DO.Assign(attrs...))
}

func (t taskDo) Joins(fields ...field.RelationField) ITaskDo {
	for _, _f := range fields {
		t = *t.withDO(t.DO.Joins(_f))
	}
	return &t
}

func (t taskDo) Preload(fields ...field.RelationField) ITaskDo {
	for _, _f := range fields {
		t = *t.withDO(t.DO.Preload(_f))
	}
	return &t
}

func (t taskDo) FirstOrInit() (*models.Task, error) {
	if result, err := t.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*models.Task), nil
	}
}

func (t taskDo) FirstOrCreate() (*models.Task, error) {
	if result, err := t.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*models.Task), nil
	}
}

func (t taskDo) FindByPage(offset int, limit int) (result []*models.Task, count int64, err error) {
	result, err = t.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 = t.Offset(-1).Limit(-1).Count()
	return
}

func (t taskDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
	count, err = t.Count()
	if err != nil {
		return
	}

	err = t.Offset(offset).Limit(limit).Scan(result)
	return
}

func (t taskDo) Scan(result interface{}) (err error) {
	return t.DO.Scan(result)
}

func (t taskDo) Delete(models ...*models.Task) (result gen.ResultInfo, err error) {
	return t.DO.Delete(models)
}

func (t *taskDo) withDO(do gen.Dao) *taskDo {
	t.DO = *do.(*gen.DO)
	return t
}
