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

import (
	"context"
	"database/sql"

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

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

	"gorm.io/plugin/dbresolver"

	"stream.api/internal/database/model"
)

func newJob(db *gorm.DB, opts ...gen.DOOption) job {
	_job := job{}

	_job.jobDo.UseDB(db, opts...)
	_job.jobDo.UseModel(&model.Job{})

	tableName := _job.jobDo.TableName()
	_job.ALL = field.NewAsterisk(tableName)
	_job.ID = field.NewString(tableName, "id")
	_job.Status = field.NewString(tableName, "status")
	_job.Priority = field.NewInt64(tableName, "priority")
	_job.InputURL = field.NewString(tableName, "input_url")
	_job.OutputURL = field.NewString(tableName, "output_url")
	_job.TotalDuration = field.NewInt64(tableName, "total_duration")
	_job.CurrentTime = field.NewInt64(tableName, "current_time")
	_job.Progress = field.NewFloat64(tableName, "progress")
	_job.AgentID = field.NewInt64(tableName, "agent_id")
	_job.Logs = field.NewString(tableName, "logs")
	_job.Config = field.NewString(tableName, "config")
	_job.Cancelled = field.NewBool(tableName, "cancelled")
	_job.RetryCount = field.NewInt64(tableName, "retry_count")
	_job.MaxRetries = field.NewInt64(tableName, "max_retries")
	_job.CreatedAt = field.NewTime(tableName, "created_at")
	_job.UpdatedAt = field.NewTime(tableName, "updated_at")
	_job.Version = field.NewInt64(tableName, "version")

	_job.fillFieldMap()

	return _job
}

type job struct {
	jobDo jobDo

	ALL           field.Asterisk
	ID            field.String
	Status        field.String
	Priority      field.Int64
	InputURL      field.String
	OutputURL     field.String
	TotalDuration field.Int64
	CurrentTime   field.Int64
	Progress      field.Float64
	AgentID       field.Int64
	Logs          field.String
	Config        field.String
	Cancelled     field.Bool
	RetryCount    field.Int64
	MaxRetries    field.Int64
	CreatedAt     field.Time
	UpdatedAt     field.Time
	Version       field.Int64

	fieldMap map[string]field.Expr
}

func (j job) Table(newTableName string) *job {
	j.jobDo.UseTable(newTableName)
	return j.updateTableName(newTableName)
}

func (j job) As(alias string) *job {
	j.jobDo.DO = *(j.jobDo.As(alias).(*gen.DO))
	return j.updateTableName(alias)
}

func (j *job) updateTableName(table string) *job {
	j.ALL = field.NewAsterisk(table)
	j.ID = field.NewString(table, "id")
	j.Status = field.NewString(table, "status")
	j.Priority = field.NewInt64(table, "priority")
	j.InputURL = field.NewString(table, "input_url")
	j.OutputURL = field.NewString(table, "output_url")
	j.TotalDuration = field.NewInt64(table, "total_duration")
	j.CurrentTime = field.NewInt64(table, "current_time")
	j.Progress = field.NewFloat64(table, "progress")
	j.AgentID = field.NewInt64(table, "agent_id")
	j.Logs = field.NewString(table, "logs")
	j.Config = field.NewString(table, "config")
	j.Cancelled = field.NewBool(table, "cancelled")
	j.RetryCount = field.NewInt64(table, "retry_count")
	j.MaxRetries = field.NewInt64(table, "max_retries")
	j.CreatedAt = field.NewTime(table, "created_at")
	j.UpdatedAt = field.NewTime(table, "updated_at")
	j.Version = field.NewInt64(table, "version")

	j.fillFieldMap()

	return j
}

func (j *job) WithContext(ctx context.Context) IJobDo { return j.jobDo.WithContext(ctx) }

func (j job) TableName() string { return j.jobDo.TableName() }

func (j job) Alias() string { return j.jobDo.Alias() }

func (j job) Columns(cols ...field.Expr) gen.Columns { return j.jobDo.Columns(cols...) }

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

func (j *job) fillFieldMap() {
	j.fieldMap = make(map[string]field.Expr, 17)
	j.fieldMap["id"] = j.ID
	j.fieldMap["status"] = j.Status
	j.fieldMap["priority"] = j.Priority
	j.fieldMap["input_url"] = j.InputURL
	j.fieldMap["output_url"] = j.OutputURL
	j.fieldMap["total_duration"] = j.TotalDuration
	j.fieldMap["current_time"] = j.CurrentTime
	j.fieldMap["progress"] = j.Progress
	j.fieldMap["agent_id"] = j.AgentID
	j.fieldMap["logs"] = j.Logs
	j.fieldMap["config"] = j.Config
	j.fieldMap["cancelled"] = j.Cancelled
	j.fieldMap["retry_count"] = j.RetryCount
	j.fieldMap["max_retries"] = j.MaxRetries
	j.fieldMap["created_at"] = j.CreatedAt
	j.fieldMap["updated_at"] = j.UpdatedAt
	j.fieldMap["version"] = j.Version
}

func (j job) clone(db *gorm.DB) job {
	j.jobDo.ReplaceConnPool(db.Statement.ConnPool)
	return j
}

func (j job) replaceDB(db *gorm.DB) job {
	j.jobDo.ReplaceDB(db)
	return j
}

type jobDo struct{ gen.DO }

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

func (j jobDo) Debug() IJobDo {
	return j.withDO(j.DO.Debug())
}

func (j jobDo) WithContext(ctx context.Context) IJobDo {
	return j.withDO(j.DO.WithContext(ctx))
}

func (j jobDo) ReadDB() IJobDo {
	return j.Clauses(dbresolver.Read)
}

func (j jobDo) WriteDB() IJobDo {
	return j.Clauses(dbresolver.Write)
}

func (j jobDo) Session(config *gorm.Session) IJobDo {
	return j.withDO(j.DO.Session(config))
}

func (j jobDo) Clauses(conds ...clause.Expression) IJobDo {
	return j.withDO(j.DO.Clauses(conds...))
}

func (j jobDo) Returning(value interface{}, columns ...string) IJobDo {
	return j.withDO(j.DO.Returning(value, columns...))
}

func (j jobDo) Not(conds ...gen.Condition) IJobDo {
	return j.withDO(j.DO.Not(conds...))
}

func (j jobDo) Or(conds ...gen.Condition) IJobDo {
	return j.withDO(j.DO.Or(conds...))
}

func (j jobDo) Select(conds ...field.Expr) IJobDo {
	return j.withDO(j.DO.Select(conds...))
}

func (j jobDo) Where(conds ...gen.Condition) IJobDo {
	return j.withDO(j.DO.Where(conds...))
}

func (j jobDo) Order(conds ...field.Expr) IJobDo {
	return j.withDO(j.DO.Order(conds...))
}

func (j jobDo) Distinct(cols ...field.Expr) IJobDo {
	return j.withDO(j.DO.Distinct(cols...))
}

func (j jobDo) Omit(cols ...field.Expr) IJobDo {
	return j.withDO(j.DO.Omit(cols...))
}

func (j jobDo) Join(table schema.Tabler, on ...field.Expr) IJobDo {
	return j.withDO(j.DO.Join(table, on...))
}

func (j jobDo) LeftJoin(table schema.Tabler, on ...field.Expr) IJobDo {
	return j.withDO(j.DO.LeftJoin(table, on...))
}

func (j jobDo) RightJoin(table schema.Tabler, on ...field.Expr) IJobDo {
	return j.withDO(j.DO.RightJoin(table, on...))
}

func (j jobDo) Group(cols ...field.Expr) IJobDo {
	return j.withDO(j.DO.Group(cols...))
}

func (j jobDo) Having(conds ...gen.Condition) IJobDo {
	return j.withDO(j.DO.Having(conds...))
}

func (j jobDo) Limit(limit int) IJobDo {
	return j.withDO(j.DO.Limit(limit))
}

func (j jobDo) Offset(offset int) IJobDo {
	return j.withDO(j.DO.Offset(offset))
}

func (j jobDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IJobDo {
	return j.withDO(j.DO.Scopes(funcs...))
}

func (j jobDo) Unscoped() IJobDo {
	return j.withDO(j.DO.Unscoped())
}

func (j jobDo) Create(values ...*model.Job) error {
	if len(values) == 0 {
		return nil
	}
	return j.DO.Create(values)
}

func (j jobDo) CreateInBatches(values []*model.Job, batchSize int) error {
	return j.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 (j jobDo) Save(values ...*model.Job) error {
	if len(values) == 0 {
		return nil
	}
	return j.DO.Save(values)
}

func (j jobDo) First() (*model.Job, error) {
	if result, err := j.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Job), nil
	}
}

func (j jobDo) Take() (*model.Job, error) {
	if result, err := j.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Job), nil
	}
}

func (j jobDo) Last() (*model.Job, error) {
	if result, err := j.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Job), nil
	}
}

func (j jobDo) Find() ([]*model.Job, error) {
	result, err := j.DO.Find()
	return result.([]*model.Job), err
}

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

func (j jobDo) Attrs(attrs ...field.AssignExpr) IJobDo {
	return j.withDO(j.DO.Attrs(attrs...))
}

func (j jobDo) Assign(attrs ...field.AssignExpr) IJobDo {
	return j.withDO(j.DO.Assign(attrs...))
}

func (j jobDo) Joins(fields ...field.RelationField) IJobDo {
	for _, _f := range fields {
		j = *j.withDO(j.DO.Joins(_f))
	}
	return &j
}

func (j jobDo) Preload(fields ...field.RelationField) IJobDo {
	for _, _f := range fields {
		j = *j.withDO(j.DO.Preload(_f))
	}
	return &j
}

func (j jobDo) FirstOrInit() (*model.Job, error) {
	if result, err := j.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Job), nil
	}
}

func (j jobDo) FirstOrCreate() (*model.Job, error) {
	if result, err := j.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Job), nil
	}
}

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

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

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

func (j jobDo) Scan(result interface{}) (err error) {
	return j.DO.Scan(result)
}

func (j jobDo) Delete(models ...*model.Job) (result gen.ResultInfo, err error) {
	return j.DO.Delete(models)
}

func (j *jobDo) withDO(do gen.Dao) *jobDo {
	j.DO = *do.(*gen.DO)
	return j
}
