// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"
	"wechat-api/ent/custom_types"
	"wechat-api/ent/messagerecords"
	"wechat-api/ent/predicate"
	"wechat-api/ent/sopnode"
	"wechat-api/ent/sopstage"
	"wechat-api/ent/soptask"

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

// SopStageUpdate is the builder for updating SopStage entities.
type SopStageUpdate struct {
	config
	hooks    []Hook
	mutation *SopStageMutation
}

// Where appends a list predicates to the SopStageUpdate builder.
func (ssu *SopStageUpdate) Where(ps ...predicate.SopStage) *SopStageUpdate {
	ssu.mutation.Where(ps...)
	return ssu
}

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

// SetStatus sets the "status" field.
func (ssu *SopStageUpdate) SetStatus(u uint8) *SopStageUpdate {
	ssu.mutation.ResetStatus()
	ssu.mutation.SetStatus(u)
	return ssu
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (ssu *SopStageUpdate) SetNillableStatus(u *uint8) *SopStageUpdate {
	if u != nil {
		ssu.SetStatus(*u)
	}
	return ssu
}

// AddStatus adds u to the "status" field.
func (ssu *SopStageUpdate) AddStatus(u int8) *SopStageUpdate {
	ssu.mutation.AddStatus(u)
	return ssu
}

// ClearStatus clears the value of the "status" field.
func (ssu *SopStageUpdate) ClearStatus() *SopStageUpdate {
	ssu.mutation.ClearStatus()
	return ssu
}

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

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

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

// SetTaskID sets the "task_id" field.
func (ssu *SopStageUpdate) SetTaskID(u uint64) *SopStageUpdate {
	ssu.mutation.SetTaskID(u)
	return ssu
}

// SetNillableTaskID sets the "task_id" field if the given value is not nil.
func (ssu *SopStageUpdate) SetNillableTaskID(u *uint64) *SopStageUpdate {
	if u != nil {
		ssu.SetTaskID(*u)
	}
	return ssu
}

// SetName sets the "name" field.
func (ssu *SopStageUpdate) SetName(s string) *SopStageUpdate {
	ssu.mutation.SetName(s)
	return ssu
}

// SetNillableName sets the "name" field if the given value is not nil.
func (ssu *SopStageUpdate) SetNillableName(s *string) *SopStageUpdate {
	if s != nil {
		ssu.SetName(*s)
	}
	return ssu
}

// SetConditionType sets the "condition_type" field.
func (ssu *SopStageUpdate) SetConditionType(i int) *SopStageUpdate {
	ssu.mutation.ResetConditionType()
	ssu.mutation.SetConditionType(i)
	return ssu
}

// SetNillableConditionType sets the "condition_type" field if the given value is not nil.
func (ssu *SopStageUpdate) SetNillableConditionType(i *int) *SopStageUpdate {
	if i != nil {
		ssu.SetConditionType(*i)
	}
	return ssu
}

// AddConditionType adds i to the "condition_type" field.
func (ssu *SopStageUpdate) AddConditionType(i int) *SopStageUpdate {
	ssu.mutation.AddConditionType(i)
	return ssu
}

// SetConditionOperator sets the "condition_operator" field.
func (ssu *SopStageUpdate) SetConditionOperator(i int) *SopStageUpdate {
	ssu.mutation.ResetConditionOperator()
	ssu.mutation.SetConditionOperator(i)
	return ssu
}

// SetNillableConditionOperator sets the "condition_operator" field if the given value is not nil.
func (ssu *SopStageUpdate) SetNillableConditionOperator(i *int) *SopStageUpdate {
	if i != nil {
		ssu.SetConditionOperator(*i)
	}
	return ssu
}

// AddConditionOperator adds i to the "condition_operator" field.
func (ssu *SopStageUpdate) AddConditionOperator(i int) *SopStageUpdate {
	ssu.mutation.AddConditionOperator(i)
	return ssu
}

// SetConditionList sets the "condition_list" field.
func (ssu *SopStageUpdate) SetConditionList(ct []custom_types.Condition) *SopStageUpdate {
	ssu.mutation.SetConditionList(ct)
	return ssu
}

// AppendConditionList appends ct to the "condition_list" field.
func (ssu *SopStageUpdate) AppendConditionList(ct []custom_types.Condition) *SopStageUpdate {
	ssu.mutation.AppendConditionList(ct)
	return ssu
}

// SetActionMessage sets the "action_message" field.
func (ssu *SopStageUpdate) SetActionMessage(ct []custom_types.Action) *SopStageUpdate {
	ssu.mutation.SetActionMessage(ct)
	return ssu
}

// AppendActionMessage appends ct to the "action_message" field.
func (ssu *SopStageUpdate) AppendActionMessage(ct []custom_types.Action) *SopStageUpdate {
	ssu.mutation.AppendActionMessage(ct)
	return ssu
}

// ClearActionMessage clears the value of the "action_message" field.
func (ssu *SopStageUpdate) ClearActionMessage() *SopStageUpdate {
	ssu.mutation.ClearActionMessage()
	return ssu
}

// SetActionLabel sets the "action_label" field.
func (ssu *SopStageUpdate) SetActionLabel(u []uint64) *SopStageUpdate {
	ssu.mutation.SetActionLabel(u)
	return ssu
}

// AppendActionLabel appends u to the "action_label" field.
func (ssu *SopStageUpdate) AppendActionLabel(u []uint64) *SopStageUpdate {
	ssu.mutation.AppendActionLabel(u)
	return ssu
}

// ClearActionLabel clears the value of the "action_label" field.
func (ssu *SopStageUpdate) ClearActionLabel() *SopStageUpdate {
	ssu.mutation.ClearActionLabel()
	return ssu
}

// SetIndexSort sets the "index_sort" field.
func (ssu *SopStageUpdate) SetIndexSort(i int) *SopStageUpdate {
	ssu.mutation.ResetIndexSort()
	ssu.mutation.SetIndexSort(i)
	return ssu
}

// SetNillableIndexSort sets the "index_sort" field if the given value is not nil.
func (ssu *SopStageUpdate) SetNillableIndexSort(i *int) *SopStageUpdate {
	if i != nil {
		ssu.SetIndexSort(*i)
	}
	return ssu
}

// AddIndexSort adds i to the "index_sort" field.
func (ssu *SopStageUpdate) AddIndexSort(i int) *SopStageUpdate {
	ssu.mutation.AddIndexSort(i)
	return ssu
}

// ClearIndexSort clears the value of the "index_sort" field.
func (ssu *SopStageUpdate) ClearIndexSort() *SopStageUpdate {
	ssu.mutation.ClearIndexSort()
	return ssu
}

// SetSopTaskID sets the "sop_task" edge to the SopTask entity by ID.
func (ssu *SopStageUpdate) SetSopTaskID(id uint64) *SopStageUpdate {
	ssu.mutation.SetSopTaskID(id)
	return ssu
}

// SetSopTask sets the "sop_task" edge to the SopTask entity.
func (ssu *SopStageUpdate) SetSopTask(s *SopTask) *SopStageUpdate {
	return ssu.SetSopTaskID(s.ID)
}

// AddStageNodeIDs adds the "stage_nodes" edge to the SopNode entity by IDs.
func (ssu *SopStageUpdate) AddStageNodeIDs(ids ...uint64) *SopStageUpdate {
	ssu.mutation.AddStageNodeIDs(ids...)
	return ssu
}

// AddStageNodes adds the "stage_nodes" edges to the SopNode entity.
func (ssu *SopStageUpdate) AddStageNodes(s ...*SopNode) *SopStageUpdate {
	ids := make([]uint64, len(s))
	for i := range s {
		ids[i] = s[i].ID
	}
	return ssu.AddStageNodeIDs(ids...)
}

// AddStageMessageIDs adds the "stage_messages" edge to the MessageRecords entity by IDs.
func (ssu *SopStageUpdate) AddStageMessageIDs(ids ...uint64) *SopStageUpdate {
	ssu.mutation.AddStageMessageIDs(ids...)
	return ssu
}

// AddStageMessages adds the "stage_messages" edges to the MessageRecords entity.
func (ssu *SopStageUpdate) AddStageMessages(m ...*MessageRecords) *SopStageUpdate {
	ids := make([]uint64, len(m))
	for i := range m {
		ids[i] = m[i].ID
	}
	return ssu.AddStageMessageIDs(ids...)
}

// Mutation returns the SopStageMutation object of the builder.
func (ssu *SopStageUpdate) Mutation() *SopStageMutation {
	return ssu.mutation
}

// ClearSopTask clears the "sop_task" edge to the SopTask entity.
func (ssu *SopStageUpdate) ClearSopTask() *SopStageUpdate {
	ssu.mutation.ClearSopTask()
	return ssu
}

// ClearStageNodes clears all "stage_nodes" edges to the SopNode entity.
func (ssu *SopStageUpdate) ClearStageNodes() *SopStageUpdate {
	ssu.mutation.ClearStageNodes()
	return ssu
}

// RemoveStageNodeIDs removes the "stage_nodes" edge to SopNode entities by IDs.
func (ssu *SopStageUpdate) RemoveStageNodeIDs(ids ...uint64) *SopStageUpdate {
	ssu.mutation.RemoveStageNodeIDs(ids...)
	return ssu
}

// RemoveStageNodes removes "stage_nodes" edges to SopNode entities.
func (ssu *SopStageUpdate) RemoveStageNodes(s ...*SopNode) *SopStageUpdate {
	ids := make([]uint64, len(s))
	for i := range s {
		ids[i] = s[i].ID
	}
	return ssu.RemoveStageNodeIDs(ids...)
}

// ClearStageMessages clears all "stage_messages" edges to the MessageRecords entity.
func (ssu *SopStageUpdate) ClearStageMessages() *SopStageUpdate {
	ssu.mutation.ClearStageMessages()
	return ssu
}

// RemoveStageMessageIDs removes the "stage_messages" edge to MessageRecords entities by IDs.
func (ssu *SopStageUpdate) RemoveStageMessageIDs(ids ...uint64) *SopStageUpdate {
	ssu.mutation.RemoveStageMessageIDs(ids...)
	return ssu
}

// RemoveStageMessages removes "stage_messages" edges to MessageRecords entities.
func (ssu *SopStageUpdate) RemoveStageMessages(m ...*MessageRecords) *SopStageUpdate {
	ids := make([]uint64, len(m))
	for i := range m {
		ids[i] = m[i].ID
	}
	return ssu.RemoveStageMessageIDs(ids...)
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (ssu *SopStageUpdate) Save(ctx context.Context) (int, error) {
	if err := ssu.defaults(); err != nil {
		return 0, err
	}
	return withHooks(ctx, ssu.sqlSave, ssu.mutation, ssu.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (ssu *SopStageUpdate) defaults() error {
	if _, ok := ssu.mutation.UpdatedAt(); !ok {
		if sopstage.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized sopstage.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := sopstage.UpdateDefaultUpdatedAt()
		ssu.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (ssu *SopStageUpdate) check() error {
	if _, ok := ssu.mutation.SopTaskID(); ssu.mutation.SopTaskCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "SopStage.sop_task"`)
	}
	return nil
}

func (ssu *SopStageUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := ssu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(sopstage.Table, sopstage.Columns, sqlgraph.NewFieldSpec(sopstage.FieldID, field.TypeUint64))
	if ps := ssu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := ssu.mutation.UpdatedAt(); ok {
		_spec.SetField(sopstage.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := ssu.mutation.Status(); ok {
		_spec.SetField(sopstage.FieldStatus, field.TypeUint8, value)
	}
	if value, ok := ssu.mutation.AddedStatus(); ok {
		_spec.AddField(sopstage.FieldStatus, field.TypeUint8, value)
	}
	if ssu.mutation.StatusCleared() {
		_spec.ClearField(sopstage.FieldStatus, field.TypeUint8)
	}
	if value, ok := ssu.mutation.DeletedAt(); ok {
		_spec.SetField(sopstage.FieldDeletedAt, field.TypeTime, value)
	}
	if ssu.mutation.DeletedAtCleared() {
		_spec.ClearField(sopstage.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := ssu.mutation.Name(); ok {
		_spec.SetField(sopstage.FieldName, field.TypeString, value)
	}
	if value, ok := ssu.mutation.ConditionType(); ok {
		_spec.SetField(sopstage.FieldConditionType, field.TypeInt, value)
	}
	if value, ok := ssu.mutation.AddedConditionType(); ok {
		_spec.AddField(sopstage.FieldConditionType, field.TypeInt, value)
	}
	if value, ok := ssu.mutation.ConditionOperator(); ok {
		_spec.SetField(sopstage.FieldConditionOperator, field.TypeInt, value)
	}
	if value, ok := ssu.mutation.AddedConditionOperator(); ok {
		_spec.AddField(sopstage.FieldConditionOperator, field.TypeInt, value)
	}
	if value, ok := ssu.mutation.ConditionList(); ok {
		_spec.SetField(sopstage.FieldConditionList, field.TypeJSON, value)
	}
	if value, ok := ssu.mutation.AppendedConditionList(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, sopstage.FieldConditionList, value)
		})
	}
	if value, ok := ssu.mutation.ActionMessage(); ok {
		_spec.SetField(sopstage.FieldActionMessage, field.TypeJSON, value)
	}
	if value, ok := ssu.mutation.AppendedActionMessage(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, sopstage.FieldActionMessage, value)
		})
	}
	if ssu.mutation.ActionMessageCleared() {
		_spec.ClearField(sopstage.FieldActionMessage, field.TypeJSON)
	}
	if value, ok := ssu.mutation.ActionLabel(); ok {
		_spec.SetField(sopstage.FieldActionLabel, field.TypeJSON, value)
	}
	if value, ok := ssu.mutation.AppendedActionLabel(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, sopstage.FieldActionLabel, value)
		})
	}
	if ssu.mutation.ActionLabelCleared() {
		_spec.ClearField(sopstage.FieldActionLabel, field.TypeJSON)
	}
	if value, ok := ssu.mutation.IndexSort(); ok {
		_spec.SetField(sopstage.FieldIndexSort, field.TypeInt, value)
	}
	if value, ok := ssu.mutation.AddedIndexSort(); ok {
		_spec.AddField(sopstage.FieldIndexSort, field.TypeInt, value)
	}
	if ssu.mutation.IndexSortCleared() {
		_spec.ClearField(sopstage.FieldIndexSort, field.TypeInt)
	}
	if ssu.mutation.SopTaskCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   sopstage.SopTaskTable,
			Columns: []string{sopstage.SopTaskColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(soptask.FieldID, field.TypeUint64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := ssu.mutation.SopTaskIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   sopstage.SopTaskTable,
			Columns: []string{sopstage.SopTaskColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(soptask.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if ssu.mutation.StageNodesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   sopstage.StageNodesTable,
			Columns: []string{sopstage.StageNodesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(sopnode.FieldID, field.TypeUint64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := ssu.mutation.RemovedStageNodesIDs(); len(nodes) > 0 && !ssu.mutation.StageNodesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   sopstage.StageNodesTable,
			Columns: []string{sopstage.StageNodesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(sopnode.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := ssu.mutation.StageNodesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   sopstage.StageNodesTable,
			Columns: []string{sopstage.StageNodesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(sopnode.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if ssu.mutation.StageMessagesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   sopstage.StageMessagesTable,
			Columns: []string{sopstage.StageMessagesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(messagerecords.FieldID, field.TypeUint64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := ssu.mutation.RemovedStageMessagesIDs(); len(nodes) > 0 && !ssu.mutation.StageMessagesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   sopstage.StageMessagesTable,
			Columns: []string{sopstage.StageMessagesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(messagerecords.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := ssu.mutation.StageMessagesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   sopstage.StageMessagesTable,
			Columns: []string{sopstage.StageMessagesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(messagerecords.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, ssu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{sopstage.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	ssu.mutation.done = true
	return n, nil
}

// SopStageUpdateOne is the builder for updating a single SopStage entity.
type SopStageUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *SopStageMutation
}

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

// SetStatus sets the "status" field.
func (ssuo *SopStageUpdateOne) SetStatus(u uint8) *SopStageUpdateOne {
	ssuo.mutation.ResetStatus()
	ssuo.mutation.SetStatus(u)
	return ssuo
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (ssuo *SopStageUpdateOne) SetNillableStatus(u *uint8) *SopStageUpdateOne {
	if u != nil {
		ssuo.SetStatus(*u)
	}
	return ssuo
}

// AddStatus adds u to the "status" field.
func (ssuo *SopStageUpdateOne) AddStatus(u int8) *SopStageUpdateOne {
	ssuo.mutation.AddStatus(u)
	return ssuo
}

// ClearStatus clears the value of the "status" field.
func (ssuo *SopStageUpdateOne) ClearStatus() *SopStageUpdateOne {
	ssuo.mutation.ClearStatus()
	return ssuo
}

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

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

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

// SetTaskID sets the "task_id" field.
func (ssuo *SopStageUpdateOne) SetTaskID(u uint64) *SopStageUpdateOne {
	ssuo.mutation.SetTaskID(u)
	return ssuo
}

// SetNillableTaskID sets the "task_id" field if the given value is not nil.
func (ssuo *SopStageUpdateOne) SetNillableTaskID(u *uint64) *SopStageUpdateOne {
	if u != nil {
		ssuo.SetTaskID(*u)
	}
	return ssuo
}

// SetName sets the "name" field.
func (ssuo *SopStageUpdateOne) SetName(s string) *SopStageUpdateOne {
	ssuo.mutation.SetName(s)
	return ssuo
}

// SetNillableName sets the "name" field if the given value is not nil.
func (ssuo *SopStageUpdateOne) SetNillableName(s *string) *SopStageUpdateOne {
	if s != nil {
		ssuo.SetName(*s)
	}
	return ssuo
}

// SetConditionType sets the "condition_type" field.
func (ssuo *SopStageUpdateOne) SetConditionType(i int) *SopStageUpdateOne {
	ssuo.mutation.ResetConditionType()
	ssuo.mutation.SetConditionType(i)
	return ssuo
}

// SetNillableConditionType sets the "condition_type" field if the given value is not nil.
func (ssuo *SopStageUpdateOne) SetNillableConditionType(i *int) *SopStageUpdateOne {
	if i != nil {
		ssuo.SetConditionType(*i)
	}
	return ssuo
}

// AddConditionType adds i to the "condition_type" field.
func (ssuo *SopStageUpdateOne) AddConditionType(i int) *SopStageUpdateOne {
	ssuo.mutation.AddConditionType(i)
	return ssuo
}

// SetConditionOperator sets the "condition_operator" field.
func (ssuo *SopStageUpdateOne) SetConditionOperator(i int) *SopStageUpdateOne {
	ssuo.mutation.ResetConditionOperator()
	ssuo.mutation.SetConditionOperator(i)
	return ssuo
}

// SetNillableConditionOperator sets the "condition_operator" field if the given value is not nil.
func (ssuo *SopStageUpdateOne) SetNillableConditionOperator(i *int) *SopStageUpdateOne {
	if i != nil {
		ssuo.SetConditionOperator(*i)
	}
	return ssuo
}

// AddConditionOperator adds i to the "condition_operator" field.
func (ssuo *SopStageUpdateOne) AddConditionOperator(i int) *SopStageUpdateOne {
	ssuo.mutation.AddConditionOperator(i)
	return ssuo
}

// SetConditionList sets the "condition_list" field.
func (ssuo *SopStageUpdateOne) SetConditionList(ct []custom_types.Condition) *SopStageUpdateOne {
	ssuo.mutation.SetConditionList(ct)
	return ssuo
}

// AppendConditionList appends ct to the "condition_list" field.
func (ssuo *SopStageUpdateOne) AppendConditionList(ct []custom_types.Condition) *SopStageUpdateOne {
	ssuo.mutation.AppendConditionList(ct)
	return ssuo
}

// SetActionMessage sets the "action_message" field.
func (ssuo *SopStageUpdateOne) SetActionMessage(ct []custom_types.Action) *SopStageUpdateOne {
	ssuo.mutation.SetActionMessage(ct)
	return ssuo
}

// AppendActionMessage appends ct to the "action_message" field.
func (ssuo *SopStageUpdateOne) AppendActionMessage(ct []custom_types.Action) *SopStageUpdateOne {
	ssuo.mutation.AppendActionMessage(ct)
	return ssuo
}

// ClearActionMessage clears the value of the "action_message" field.
func (ssuo *SopStageUpdateOne) ClearActionMessage() *SopStageUpdateOne {
	ssuo.mutation.ClearActionMessage()
	return ssuo
}

// SetActionLabel sets the "action_label" field.
func (ssuo *SopStageUpdateOne) SetActionLabel(u []uint64) *SopStageUpdateOne {
	ssuo.mutation.SetActionLabel(u)
	return ssuo
}

// AppendActionLabel appends u to the "action_label" field.
func (ssuo *SopStageUpdateOne) AppendActionLabel(u []uint64) *SopStageUpdateOne {
	ssuo.mutation.AppendActionLabel(u)
	return ssuo
}

// ClearActionLabel clears the value of the "action_label" field.
func (ssuo *SopStageUpdateOne) ClearActionLabel() *SopStageUpdateOne {
	ssuo.mutation.ClearActionLabel()
	return ssuo
}

// SetIndexSort sets the "index_sort" field.
func (ssuo *SopStageUpdateOne) SetIndexSort(i int) *SopStageUpdateOne {
	ssuo.mutation.ResetIndexSort()
	ssuo.mutation.SetIndexSort(i)
	return ssuo
}

// SetNillableIndexSort sets the "index_sort" field if the given value is not nil.
func (ssuo *SopStageUpdateOne) SetNillableIndexSort(i *int) *SopStageUpdateOne {
	if i != nil {
		ssuo.SetIndexSort(*i)
	}
	return ssuo
}

// AddIndexSort adds i to the "index_sort" field.
func (ssuo *SopStageUpdateOne) AddIndexSort(i int) *SopStageUpdateOne {
	ssuo.mutation.AddIndexSort(i)
	return ssuo
}

// ClearIndexSort clears the value of the "index_sort" field.
func (ssuo *SopStageUpdateOne) ClearIndexSort() *SopStageUpdateOne {
	ssuo.mutation.ClearIndexSort()
	return ssuo
}

// SetSopTaskID sets the "sop_task" edge to the SopTask entity by ID.
func (ssuo *SopStageUpdateOne) SetSopTaskID(id uint64) *SopStageUpdateOne {
	ssuo.mutation.SetSopTaskID(id)
	return ssuo
}

// SetSopTask sets the "sop_task" edge to the SopTask entity.
func (ssuo *SopStageUpdateOne) SetSopTask(s *SopTask) *SopStageUpdateOne {
	return ssuo.SetSopTaskID(s.ID)
}

// AddStageNodeIDs adds the "stage_nodes" edge to the SopNode entity by IDs.
func (ssuo *SopStageUpdateOne) AddStageNodeIDs(ids ...uint64) *SopStageUpdateOne {
	ssuo.mutation.AddStageNodeIDs(ids...)
	return ssuo
}

// AddStageNodes adds the "stage_nodes" edges to the SopNode entity.
func (ssuo *SopStageUpdateOne) AddStageNodes(s ...*SopNode) *SopStageUpdateOne {
	ids := make([]uint64, len(s))
	for i := range s {
		ids[i] = s[i].ID
	}
	return ssuo.AddStageNodeIDs(ids...)
}

// AddStageMessageIDs adds the "stage_messages" edge to the MessageRecords entity by IDs.
func (ssuo *SopStageUpdateOne) AddStageMessageIDs(ids ...uint64) *SopStageUpdateOne {
	ssuo.mutation.AddStageMessageIDs(ids...)
	return ssuo
}

// AddStageMessages adds the "stage_messages" edges to the MessageRecords entity.
func (ssuo *SopStageUpdateOne) AddStageMessages(m ...*MessageRecords) *SopStageUpdateOne {
	ids := make([]uint64, len(m))
	for i := range m {
		ids[i] = m[i].ID
	}
	return ssuo.AddStageMessageIDs(ids...)
}

// Mutation returns the SopStageMutation object of the builder.
func (ssuo *SopStageUpdateOne) Mutation() *SopStageMutation {
	return ssuo.mutation
}

// ClearSopTask clears the "sop_task" edge to the SopTask entity.
func (ssuo *SopStageUpdateOne) ClearSopTask() *SopStageUpdateOne {
	ssuo.mutation.ClearSopTask()
	return ssuo
}

// ClearStageNodes clears all "stage_nodes" edges to the SopNode entity.
func (ssuo *SopStageUpdateOne) ClearStageNodes() *SopStageUpdateOne {
	ssuo.mutation.ClearStageNodes()
	return ssuo
}

// RemoveStageNodeIDs removes the "stage_nodes" edge to SopNode entities by IDs.
func (ssuo *SopStageUpdateOne) RemoveStageNodeIDs(ids ...uint64) *SopStageUpdateOne {
	ssuo.mutation.RemoveStageNodeIDs(ids...)
	return ssuo
}

// RemoveStageNodes removes "stage_nodes" edges to SopNode entities.
func (ssuo *SopStageUpdateOne) RemoveStageNodes(s ...*SopNode) *SopStageUpdateOne {
	ids := make([]uint64, len(s))
	for i := range s {
		ids[i] = s[i].ID
	}
	return ssuo.RemoveStageNodeIDs(ids...)
}

// ClearStageMessages clears all "stage_messages" edges to the MessageRecords entity.
func (ssuo *SopStageUpdateOne) ClearStageMessages() *SopStageUpdateOne {
	ssuo.mutation.ClearStageMessages()
	return ssuo
}

// RemoveStageMessageIDs removes the "stage_messages" edge to MessageRecords entities by IDs.
func (ssuo *SopStageUpdateOne) RemoveStageMessageIDs(ids ...uint64) *SopStageUpdateOne {
	ssuo.mutation.RemoveStageMessageIDs(ids...)
	return ssuo
}

// RemoveStageMessages removes "stage_messages" edges to MessageRecords entities.
func (ssuo *SopStageUpdateOne) RemoveStageMessages(m ...*MessageRecords) *SopStageUpdateOne {
	ids := make([]uint64, len(m))
	for i := range m {
		ids[i] = m[i].ID
	}
	return ssuo.RemoveStageMessageIDs(ids...)
}

// Where appends a list predicates to the SopStageUpdate builder.
func (ssuo *SopStageUpdateOne) Where(ps ...predicate.SopStage) *SopStageUpdateOne {
	ssuo.mutation.Where(ps...)
	return ssuo
}

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

// Save executes the query and returns the updated SopStage entity.
func (ssuo *SopStageUpdateOne) Save(ctx context.Context) (*SopStage, error) {
	if err := ssuo.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, ssuo.sqlSave, ssuo.mutation, ssuo.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (ssuo *SopStageUpdateOne) defaults() error {
	if _, ok := ssuo.mutation.UpdatedAt(); !ok {
		if sopstage.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized sopstage.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := sopstage.UpdateDefaultUpdatedAt()
		ssuo.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (ssuo *SopStageUpdateOne) check() error {
	if _, ok := ssuo.mutation.SopTaskID(); ssuo.mutation.SopTaskCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "SopStage.sop_task"`)
	}
	return nil
}

func (ssuo *SopStageUpdateOne) sqlSave(ctx context.Context) (_node *SopStage, err error) {
	if err := ssuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(sopstage.Table, sopstage.Columns, sqlgraph.NewFieldSpec(sopstage.FieldID, field.TypeUint64))
	id, ok := ssuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "SopStage.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := ssuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, sopstage.FieldID)
		for _, f := range fields {
			if !sopstage.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != sopstage.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := ssuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := ssuo.mutation.UpdatedAt(); ok {
		_spec.SetField(sopstage.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := ssuo.mutation.Status(); ok {
		_spec.SetField(sopstage.FieldStatus, field.TypeUint8, value)
	}
	if value, ok := ssuo.mutation.AddedStatus(); ok {
		_spec.AddField(sopstage.FieldStatus, field.TypeUint8, value)
	}
	if ssuo.mutation.StatusCleared() {
		_spec.ClearField(sopstage.FieldStatus, field.TypeUint8)
	}
	if value, ok := ssuo.mutation.DeletedAt(); ok {
		_spec.SetField(sopstage.FieldDeletedAt, field.TypeTime, value)
	}
	if ssuo.mutation.DeletedAtCleared() {
		_spec.ClearField(sopstage.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := ssuo.mutation.Name(); ok {
		_spec.SetField(sopstage.FieldName, field.TypeString, value)
	}
	if value, ok := ssuo.mutation.ConditionType(); ok {
		_spec.SetField(sopstage.FieldConditionType, field.TypeInt, value)
	}
	if value, ok := ssuo.mutation.AddedConditionType(); ok {
		_spec.AddField(sopstage.FieldConditionType, field.TypeInt, value)
	}
	if value, ok := ssuo.mutation.ConditionOperator(); ok {
		_spec.SetField(sopstage.FieldConditionOperator, field.TypeInt, value)
	}
	if value, ok := ssuo.mutation.AddedConditionOperator(); ok {
		_spec.AddField(sopstage.FieldConditionOperator, field.TypeInt, value)
	}
	if value, ok := ssuo.mutation.ConditionList(); ok {
		_spec.SetField(sopstage.FieldConditionList, field.TypeJSON, value)
	}
	if value, ok := ssuo.mutation.AppendedConditionList(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, sopstage.FieldConditionList, value)
		})
	}
	if value, ok := ssuo.mutation.ActionMessage(); ok {
		_spec.SetField(sopstage.FieldActionMessage, field.TypeJSON, value)
	}
	if value, ok := ssuo.mutation.AppendedActionMessage(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, sopstage.FieldActionMessage, value)
		})
	}
	if ssuo.mutation.ActionMessageCleared() {
		_spec.ClearField(sopstage.FieldActionMessage, field.TypeJSON)
	}
	if value, ok := ssuo.mutation.ActionLabel(); ok {
		_spec.SetField(sopstage.FieldActionLabel, field.TypeJSON, value)
	}
	if value, ok := ssuo.mutation.AppendedActionLabel(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, sopstage.FieldActionLabel, value)
		})
	}
	if ssuo.mutation.ActionLabelCleared() {
		_spec.ClearField(sopstage.FieldActionLabel, field.TypeJSON)
	}
	if value, ok := ssuo.mutation.IndexSort(); ok {
		_spec.SetField(sopstage.FieldIndexSort, field.TypeInt, value)
	}
	if value, ok := ssuo.mutation.AddedIndexSort(); ok {
		_spec.AddField(sopstage.FieldIndexSort, field.TypeInt, value)
	}
	if ssuo.mutation.IndexSortCleared() {
		_spec.ClearField(sopstage.FieldIndexSort, field.TypeInt)
	}
	if ssuo.mutation.SopTaskCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   sopstage.SopTaskTable,
			Columns: []string{sopstage.SopTaskColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(soptask.FieldID, field.TypeUint64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := ssuo.mutation.SopTaskIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   sopstage.SopTaskTable,
			Columns: []string{sopstage.SopTaskColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(soptask.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if ssuo.mutation.StageNodesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   sopstage.StageNodesTable,
			Columns: []string{sopstage.StageNodesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(sopnode.FieldID, field.TypeUint64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := ssuo.mutation.RemovedStageNodesIDs(); len(nodes) > 0 && !ssuo.mutation.StageNodesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   sopstage.StageNodesTable,
			Columns: []string{sopstage.StageNodesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(sopnode.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := ssuo.mutation.StageNodesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   sopstage.StageNodesTable,
			Columns: []string{sopstage.StageNodesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(sopnode.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if ssuo.mutation.StageMessagesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   sopstage.StageMessagesTable,
			Columns: []string{sopstage.StageMessagesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(messagerecords.FieldID, field.TypeUint64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := ssuo.mutation.RemovedStageMessagesIDs(); len(nodes) > 0 && !ssuo.mutation.StageMessagesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   sopstage.StageMessagesTable,
			Columns: []string{sopstage.StageMessagesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(messagerecords.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := ssuo.mutation.StageMessagesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   sopstage.StageMessagesTable,
			Columns: []string{sopstage.StageMessagesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(messagerecords.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &SopStage{config: ssuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, ssuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{sopstage.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	ssuo.mutation.done = true
	return _node, nil
}