// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"galileo/ent/job"
	"galileo/ent/predicate"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// JobUpdate is the builder for updating Job entities.
type JobUpdate struct {
	config
	hooks    []Hook
	mutation *JobMutation
}

// Where appends a list predicates to the JobUpdate builder.
func (ju *JobUpdate) Where(ps ...predicate.Job) *JobUpdate {
	ju.mutation.Where(ps...)
	return ju
}

// SetPayload sets the "payload" field.
func (ju *JobUpdate) SetPayload(b []byte) *JobUpdate {
	ju.mutation.SetPayload(b)
	return ju
}

// ClearPayload clears the value of the "payload" field.
func (ju *JobUpdate) ClearPayload() *JobUpdate {
	ju.mutation.ClearPayload()
	return ju
}

// SetType sets the "type" field.
func (ju *JobUpdate) SetType(s string) *JobUpdate {
	ju.mutation.SetType(s)
	return ju
}

// SetNillableType sets the "type" field if the given value is not nil.
func (ju *JobUpdate) SetNillableType(s *string) *JobUpdate {
	if s != nil {
		ju.SetType(*s)
	}
	return ju
}

// ClearType clears the value of the "type" field.
func (ju *JobUpdate) ClearType() *JobUpdate {
	ju.mutation.ClearType()
	return ju
}

// SetUpdatedAt sets the "updated_at" field.
func (ju *JobUpdate) SetUpdatedAt(t time.Time) *JobUpdate {
	ju.mutation.SetUpdatedAt(t)
	return ju
}

// ClearUpdatedAt clears the value of the "updated_at" field.
func (ju *JobUpdate) ClearUpdatedAt() *JobUpdate {
	ju.mutation.ClearUpdatedAt()
	return ju
}

// SetWorker sets the "worker" field.
func (ju *JobUpdate) SetWorker(u uint32) *JobUpdate {
	ju.mutation.ResetWorker()
	ju.mutation.SetWorker(u)
	return ju
}

// AddWorker adds u to the "worker" field.
func (ju *JobUpdate) AddWorker(u int32) *JobUpdate {
	ju.mutation.AddWorker(u)
	return ju
}

// SetDeletedAt sets the "deleted_at" field.
func (ju *JobUpdate) SetDeletedAt(t time.Time) *JobUpdate {
	ju.mutation.SetDeletedAt(t)
	return ju
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (ju *JobUpdate) SetNillableDeletedAt(t *time.Time) *JobUpdate {
	if t != nil {
		ju.SetDeletedAt(*t)
	}
	return ju
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (ju *JobUpdate) ClearDeletedAt() *JobUpdate {
	ju.mutation.ClearDeletedAt()
	return ju
}

// SetDeletedBy sets the "deleted_by" field.
func (ju *JobUpdate) SetDeletedBy(u uint32) *JobUpdate {
	ju.mutation.ResetDeletedBy()
	ju.mutation.SetDeletedBy(u)
	return ju
}

// SetNillableDeletedBy sets the "deleted_by" field if the given value is not nil.
func (ju *JobUpdate) SetNillableDeletedBy(u *uint32) *JobUpdate {
	if u != nil {
		ju.SetDeletedBy(*u)
	}
	return ju
}

// AddDeletedBy adds u to the "deleted_by" field.
func (ju *JobUpdate) AddDeletedBy(u int32) *JobUpdate {
	ju.mutation.AddDeletedBy(u)
	return ju
}

// ClearDeletedBy clears the value of the "deleted_by" field.
func (ju *JobUpdate) ClearDeletedBy() *JobUpdate {
	ju.mutation.ClearDeletedBy()
	return ju
}

// SetEntryID sets the "entry_id" field.
func (ju *JobUpdate) SetEntryID(s string) *JobUpdate {
	ju.mutation.SetEntryID(s)
	return ju
}

// SetNillableEntryID sets the "entry_id" field if the given value is not nil.
func (ju *JobUpdate) SetNillableEntryID(s *string) *JobUpdate {
	if s != nil {
		ju.SetEntryID(*s)
	}
	return ju
}

// ClearEntryID clears the value of the "entry_id" field.
func (ju *JobUpdate) ClearEntryID() *JobUpdate {
	ju.mutation.ClearEntryID()
	return ju
}

// SetConfig sets the "config" field.
func (ju *JobUpdate) SetConfig(b []byte) *JobUpdate {
	ju.mutation.SetConfig(b)
	return ju
}

// ClearConfig clears the value of the "config" field.
func (ju *JobUpdate) ClearConfig() *JobUpdate {
	ju.mutation.ClearConfig()
	return ju
}

// SetTaskID sets the "task_id" field.
func (ju *JobUpdate) SetTaskID(i int64) *JobUpdate {
	ju.mutation.ResetTaskID()
	ju.mutation.SetTaskID(i)
	return ju
}

// AddTaskID adds i to the "task_id" field.
func (ju *JobUpdate) AddTaskID(i int64) *JobUpdate {
	ju.mutation.AddTaskID(i)
	return ju
}

// SetActive sets the "active" field.
func (ju *JobUpdate) SetActive(b bool) *JobUpdate {
	ju.mutation.SetActive(b)
	return ju
}

// SetNillableActive sets the "active" field if the given value is not nil.
func (ju *JobUpdate) SetNillableActive(b *bool) *JobUpdate {
	if b != nil {
		ju.SetActive(*b)
	}
	return ju
}

// Mutation returns the JobMutation object of the builder.
func (ju *JobUpdate) Mutation() *JobMutation {
	return ju.mutation
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (ju *JobUpdate) Save(ctx context.Context) (int, error) {
	ju.defaults()
	return withHooks[int, JobMutation](ctx, ju.sqlSave, ju.mutation, ju.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (ju *JobUpdate) SaveX(ctx context.Context) int {
	affected, err := ju.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

// Exec executes the query.
func (ju *JobUpdate) Exec(ctx context.Context) error {
	_, err := ju.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (ju *JobUpdate) ExecX(ctx context.Context) {
	if err := ju.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (ju *JobUpdate) defaults() {
	if _, ok := ju.mutation.UpdatedAt(); !ok && !ju.mutation.UpdatedAtCleared() {
		v := job.UpdateDefaultUpdatedAt()
		ju.mutation.SetUpdatedAt(v)
	}
}

func (ju *JobUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(job.Table, job.Columns, sqlgraph.NewFieldSpec(job.FieldID, field.TypeInt64))
	if ps := ju.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := ju.mutation.Payload(); ok {
		_spec.SetField(job.FieldPayload, field.TypeBytes, value)
	}
	if ju.mutation.PayloadCleared() {
		_spec.ClearField(job.FieldPayload, field.TypeBytes)
	}
	if value, ok := ju.mutation.GetType(); ok {
		_spec.SetField(job.FieldType, field.TypeString, value)
	}
	if ju.mutation.TypeCleared() {
		_spec.ClearField(job.FieldType, field.TypeString)
	}
	if value, ok := ju.mutation.UpdatedAt(); ok {
		_spec.SetField(job.FieldUpdatedAt, field.TypeTime, value)
	}
	if ju.mutation.UpdatedAtCleared() {
		_spec.ClearField(job.FieldUpdatedAt, field.TypeTime)
	}
	if value, ok := ju.mutation.Worker(); ok {
		_spec.SetField(job.FieldWorker, field.TypeUint32, value)
	}
	if value, ok := ju.mutation.AddedWorker(); ok {
		_spec.AddField(job.FieldWorker, field.TypeUint32, value)
	}
	if value, ok := ju.mutation.DeletedAt(); ok {
		_spec.SetField(job.FieldDeletedAt, field.TypeTime, value)
	}
	if ju.mutation.DeletedAtCleared() {
		_spec.ClearField(job.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := ju.mutation.DeletedBy(); ok {
		_spec.SetField(job.FieldDeletedBy, field.TypeUint32, value)
	}
	if value, ok := ju.mutation.AddedDeletedBy(); ok {
		_spec.AddField(job.FieldDeletedBy, field.TypeUint32, value)
	}
	if ju.mutation.DeletedByCleared() {
		_spec.ClearField(job.FieldDeletedBy, field.TypeUint32)
	}
	if value, ok := ju.mutation.EntryID(); ok {
		_spec.SetField(job.FieldEntryID, field.TypeString, value)
	}
	if ju.mutation.EntryIDCleared() {
		_spec.ClearField(job.FieldEntryID, field.TypeString)
	}
	if value, ok := ju.mutation.Config(); ok {
		_spec.SetField(job.FieldConfig, field.TypeBytes, value)
	}
	if ju.mutation.ConfigCleared() {
		_spec.ClearField(job.FieldConfig, field.TypeBytes)
	}
	if value, ok := ju.mutation.TaskID(); ok {
		_spec.SetField(job.FieldTaskID, field.TypeInt64, value)
	}
	if value, ok := ju.mutation.AddedTaskID(); ok {
		_spec.AddField(job.FieldTaskID, field.TypeInt64, value)
	}
	if value, ok := ju.mutation.Active(); ok {
		_spec.SetField(job.FieldActive, field.TypeBool, value)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, ju.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{job.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	ju.mutation.done = true
	return n, nil
}

// JobUpdateOne is the builder for updating a single Job entity.
type JobUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *JobMutation
}

// SetPayload sets the "payload" field.
func (juo *JobUpdateOne) SetPayload(b []byte) *JobUpdateOne {
	juo.mutation.SetPayload(b)
	return juo
}

// ClearPayload clears the value of the "payload" field.
func (juo *JobUpdateOne) ClearPayload() *JobUpdateOne {
	juo.mutation.ClearPayload()
	return juo
}

// SetType sets the "type" field.
func (juo *JobUpdateOne) SetType(s string) *JobUpdateOne {
	juo.mutation.SetType(s)
	return juo
}

// SetNillableType sets the "type" field if the given value is not nil.
func (juo *JobUpdateOne) SetNillableType(s *string) *JobUpdateOne {
	if s != nil {
		juo.SetType(*s)
	}
	return juo
}

// ClearType clears the value of the "type" field.
func (juo *JobUpdateOne) ClearType() *JobUpdateOne {
	juo.mutation.ClearType()
	return juo
}

// SetUpdatedAt sets the "updated_at" field.
func (juo *JobUpdateOne) SetUpdatedAt(t time.Time) *JobUpdateOne {
	juo.mutation.SetUpdatedAt(t)
	return juo
}

// ClearUpdatedAt clears the value of the "updated_at" field.
func (juo *JobUpdateOne) ClearUpdatedAt() *JobUpdateOne {
	juo.mutation.ClearUpdatedAt()
	return juo
}

// SetWorker sets the "worker" field.
func (juo *JobUpdateOne) SetWorker(u uint32) *JobUpdateOne {
	juo.mutation.ResetWorker()
	juo.mutation.SetWorker(u)
	return juo
}

// AddWorker adds u to the "worker" field.
func (juo *JobUpdateOne) AddWorker(u int32) *JobUpdateOne {
	juo.mutation.AddWorker(u)
	return juo
}

// SetDeletedAt sets the "deleted_at" field.
func (juo *JobUpdateOne) SetDeletedAt(t time.Time) *JobUpdateOne {
	juo.mutation.SetDeletedAt(t)
	return juo
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (juo *JobUpdateOne) SetNillableDeletedAt(t *time.Time) *JobUpdateOne {
	if t != nil {
		juo.SetDeletedAt(*t)
	}
	return juo
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (juo *JobUpdateOne) ClearDeletedAt() *JobUpdateOne {
	juo.mutation.ClearDeletedAt()
	return juo
}

// SetDeletedBy sets the "deleted_by" field.
func (juo *JobUpdateOne) SetDeletedBy(u uint32) *JobUpdateOne {
	juo.mutation.ResetDeletedBy()
	juo.mutation.SetDeletedBy(u)
	return juo
}

// SetNillableDeletedBy sets the "deleted_by" field if the given value is not nil.
func (juo *JobUpdateOne) SetNillableDeletedBy(u *uint32) *JobUpdateOne {
	if u != nil {
		juo.SetDeletedBy(*u)
	}
	return juo
}

// AddDeletedBy adds u to the "deleted_by" field.
func (juo *JobUpdateOne) AddDeletedBy(u int32) *JobUpdateOne {
	juo.mutation.AddDeletedBy(u)
	return juo
}

// ClearDeletedBy clears the value of the "deleted_by" field.
func (juo *JobUpdateOne) ClearDeletedBy() *JobUpdateOne {
	juo.mutation.ClearDeletedBy()
	return juo
}

// SetEntryID sets the "entry_id" field.
func (juo *JobUpdateOne) SetEntryID(s string) *JobUpdateOne {
	juo.mutation.SetEntryID(s)
	return juo
}

// SetNillableEntryID sets the "entry_id" field if the given value is not nil.
func (juo *JobUpdateOne) SetNillableEntryID(s *string) *JobUpdateOne {
	if s != nil {
		juo.SetEntryID(*s)
	}
	return juo
}

// ClearEntryID clears the value of the "entry_id" field.
func (juo *JobUpdateOne) ClearEntryID() *JobUpdateOne {
	juo.mutation.ClearEntryID()
	return juo
}

// SetConfig sets the "config" field.
func (juo *JobUpdateOne) SetConfig(b []byte) *JobUpdateOne {
	juo.mutation.SetConfig(b)
	return juo
}

// ClearConfig clears the value of the "config" field.
func (juo *JobUpdateOne) ClearConfig() *JobUpdateOne {
	juo.mutation.ClearConfig()
	return juo
}

// SetTaskID sets the "task_id" field.
func (juo *JobUpdateOne) SetTaskID(i int64) *JobUpdateOne {
	juo.mutation.ResetTaskID()
	juo.mutation.SetTaskID(i)
	return juo
}

// AddTaskID adds i to the "task_id" field.
func (juo *JobUpdateOne) AddTaskID(i int64) *JobUpdateOne {
	juo.mutation.AddTaskID(i)
	return juo
}

// SetActive sets the "active" field.
func (juo *JobUpdateOne) SetActive(b bool) *JobUpdateOne {
	juo.mutation.SetActive(b)
	return juo
}

// SetNillableActive sets the "active" field if the given value is not nil.
func (juo *JobUpdateOne) SetNillableActive(b *bool) *JobUpdateOne {
	if b != nil {
		juo.SetActive(*b)
	}
	return juo
}

// Mutation returns the JobMutation object of the builder.
func (juo *JobUpdateOne) Mutation() *JobMutation {
	return juo.mutation
}

// Where appends a list predicates to the JobUpdate builder.
func (juo *JobUpdateOne) Where(ps ...predicate.Job) *JobUpdateOne {
	juo.mutation.Where(ps...)
	return juo
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (juo *JobUpdateOne) Select(field string, fields ...string) *JobUpdateOne {
	juo.fields = append([]string{field}, fields...)
	return juo
}

// Save executes the query and returns the updated Job entity.
func (juo *JobUpdateOne) Save(ctx context.Context) (*Job, error) {
	juo.defaults()
	return withHooks[*Job, JobMutation](ctx, juo.sqlSave, juo.mutation, juo.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (juo *JobUpdateOne) SaveX(ctx context.Context) *Job {
	node, err := juo.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// Exec executes the query on the entity.
func (juo *JobUpdateOne) Exec(ctx context.Context) error {
	_, err := juo.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (juo *JobUpdateOne) ExecX(ctx context.Context) {
	if err := juo.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (juo *JobUpdateOne) defaults() {
	if _, ok := juo.mutation.UpdatedAt(); !ok && !juo.mutation.UpdatedAtCleared() {
		v := job.UpdateDefaultUpdatedAt()
		juo.mutation.SetUpdatedAt(v)
	}
}

func (juo *JobUpdateOne) sqlSave(ctx context.Context) (_node *Job, err error) {
	_spec := sqlgraph.NewUpdateSpec(job.Table, job.Columns, sqlgraph.NewFieldSpec(job.FieldID, field.TypeInt64))
	id, ok := juo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Job.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := juo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, job.FieldID)
		for _, f := range fields {
			if !job.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != job.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := juo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := juo.mutation.Payload(); ok {
		_spec.SetField(job.FieldPayload, field.TypeBytes, value)
	}
	if juo.mutation.PayloadCleared() {
		_spec.ClearField(job.FieldPayload, field.TypeBytes)
	}
	if value, ok := juo.mutation.GetType(); ok {
		_spec.SetField(job.FieldType, field.TypeString, value)
	}
	if juo.mutation.TypeCleared() {
		_spec.ClearField(job.FieldType, field.TypeString)
	}
	if value, ok := juo.mutation.UpdatedAt(); ok {
		_spec.SetField(job.FieldUpdatedAt, field.TypeTime, value)
	}
	if juo.mutation.UpdatedAtCleared() {
		_spec.ClearField(job.FieldUpdatedAt, field.TypeTime)
	}
	if value, ok := juo.mutation.Worker(); ok {
		_spec.SetField(job.FieldWorker, field.TypeUint32, value)
	}
	if value, ok := juo.mutation.AddedWorker(); ok {
		_spec.AddField(job.FieldWorker, field.TypeUint32, value)
	}
	if value, ok := juo.mutation.DeletedAt(); ok {
		_spec.SetField(job.FieldDeletedAt, field.TypeTime, value)
	}
	if juo.mutation.DeletedAtCleared() {
		_spec.ClearField(job.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := juo.mutation.DeletedBy(); ok {
		_spec.SetField(job.FieldDeletedBy, field.TypeUint32, value)
	}
	if value, ok := juo.mutation.AddedDeletedBy(); ok {
		_spec.AddField(job.FieldDeletedBy, field.TypeUint32, value)
	}
	if juo.mutation.DeletedByCleared() {
		_spec.ClearField(job.FieldDeletedBy, field.TypeUint32)
	}
	if value, ok := juo.mutation.EntryID(); ok {
		_spec.SetField(job.FieldEntryID, field.TypeString, value)
	}
	if juo.mutation.EntryIDCleared() {
		_spec.ClearField(job.FieldEntryID, field.TypeString)
	}
	if value, ok := juo.mutation.Config(); ok {
		_spec.SetField(job.FieldConfig, field.TypeBytes, value)
	}
	if juo.mutation.ConfigCleared() {
		_spec.ClearField(job.FieldConfig, field.TypeBytes)
	}
	if value, ok := juo.mutation.TaskID(); ok {
		_spec.SetField(job.FieldTaskID, field.TypeInt64, value)
	}
	if value, ok := juo.mutation.AddedTaskID(); ok {
		_spec.AddField(job.FieldTaskID, field.TypeInt64, value)
	}
	if value, ok := juo.mutation.Active(); ok {
		_spec.SetField(job.FieldActive, field.TypeBool, value)
	}
	_node = &Job{config: juo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, juo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{job.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	juo.mutation.done = true
	return _node, nil
}
