// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"
	"wechat-api/ent/predicate"
	"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"
)

// SopTaskUpdate is the builder for updating SopTask entities.
type SopTaskUpdate struct {
	config
	hooks    []Hook
	mutation *SopTaskMutation
}

// Where appends a list predicates to the SopTaskUpdate builder.
func (stu *SopTaskUpdate) Where(ps ...predicate.SopTask) *SopTaskUpdate {
	stu.mutation.Where(ps...)
	return stu
}

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

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

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

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

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

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

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

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

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

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

// SetBotWxidList sets the "bot_wxid_list" field.
func (stu *SopTaskUpdate) SetBotWxidList(s []string) *SopTaskUpdate {
	stu.mutation.SetBotWxidList(s)
	return stu
}

// AppendBotWxidList appends s to the "bot_wxid_list" field.
func (stu *SopTaskUpdate) AppendBotWxidList(s []string) *SopTaskUpdate {
	stu.mutation.AppendBotWxidList(s)
	return stu
}

// ClearBotWxidList clears the value of the "bot_wxid_list" field.
func (stu *SopTaskUpdate) ClearBotWxidList() *SopTaskUpdate {
	stu.mutation.ClearBotWxidList()
	return stu
}

// SetType sets the "type" field.
func (stu *SopTaskUpdate) SetType(i int) *SopTaskUpdate {
	stu.mutation.ResetType()
	stu.mutation.SetType(i)
	return stu
}

// SetNillableType sets the "type" field if the given value is not nil.
func (stu *SopTaskUpdate) SetNillableType(i *int) *SopTaskUpdate {
	if i != nil {
		stu.SetType(*i)
	}
	return stu
}

// AddType adds i to the "type" field.
func (stu *SopTaskUpdate) AddType(i int) *SopTaskUpdate {
	stu.mutation.AddType(i)
	return stu
}

// SetPlanStartTime sets the "plan_start_time" field.
func (stu *SopTaskUpdate) SetPlanStartTime(t time.Time) *SopTaskUpdate {
	stu.mutation.SetPlanStartTime(t)
	return stu
}

// SetNillablePlanStartTime sets the "plan_start_time" field if the given value is not nil.
func (stu *SopTaskUpdate) SetNillablePlanStartTime(t *time.Time) *SopTaskUpdate {
	if t != nil {
		stu.SetPlanStartTime(*t)
	}
	return stu
}

// ClearPlanStartTime clears the value of the "plan_start_time" field.
func (stu *SopTaskUpdate) ClearPlanStartTime() *SopTaskUpdate {
	stu.mutation.ClearPlanStartTime()
	return stu
}

// SetPlanEndTime sets the "plan_end_time" field.
func (stu *SopTaskUpdate) SetPlanEndTime(t time.Time) *SopTaskUpdate {
	stu.mutation.SetPlanEndTime(t)
	return stu
}

// SetNillablePlanEndTime sets the "plan_end_time" field if the given value is not nil.
func (stu *SopTaskUpdate) SetNillablePlanEndTime(t *time.Time) *SopTaskUpdate {
	if t != nil {
		stu.SetPlanEndTime(*t)
	}
	return stu
}

// ClearPlanEndTime clears the value of the "plan_end_time" field.
func (stu *SopTaskUpdate) ClearPlanEndTime() *SopTaskUpdate {
	stu.mutation.ClearPlanEndTime()
	return stu
}

// SetCreatorID sets the "creator_id" field.
func (stu *SopTaskUpdate) SetCreatorID(s string) *SopTaskUpdate {
	stu.mutation.SetCreatorID(s)
	return stu
}

// SetNillableCreatorID sets the "creator_id" field if the given value is not nil.
func (stu *SopTaskUpdate) SetNillableCreatorID(s *string) *SopTaskUpdate {
	if s != nil {
		stu.SetCreatorID(*s)
	}
	return stu
}

// ClearCreatorID clears the value of the "creator_id" field.
func (stu *SopTaskUpdate) ClearCreatorID() *SopTaskUpdate {
	stu.mutation.ClearCreatorID()
	return stu
}

// SetOrganizationID sets the "organization_id" field.
func (stu *SopTaskUpdate) SetOrganizationID(u uint64) *SopTaskUpdate {
	stu.mutation.ResetOrganizationID()
	stu.mutation.SetOrganizationID(u)
	return stu
}

// SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
func (stu *SopTaskUpdate) SetNillableOrganizationID(u *uint64) *SopTaskUpdate {
	if u != nil {
		stu.SetOrganizationID(*u)
	}
	return stu
}

// AddOrganizationID adds u to the "organization_id" field.
func (stu *SopTaskUpdate) AddOrganizationID(u int64) *SopTaskUpdate {
	stu.mutation.AddOrganizationID(u)
	return stu
}

// ClearOrganizationID clears the value of the "organization_id" field.
func (stu *SopTaskUpdate) ClearOrganizationID() *SopTaskUpdate {
	stu.mutation.ClearOrganizationID()
	return stu
}

// SetToken sets the "token" field.
func (stu *SopTaskUpdate) SetToken(s []string) *SopTaskUpdate {
	stu.mutation.SetToken(s)
	return stu
}

// AppendToken appends s to the "token" field.
func (stu *SopTaskUpdate) AppendToken(s []string) *SopTaskUpdate {
	stu.mutation.AppendToken(s)
	return stu
}

// ClearToken clears the value of the "token" field.
func (stu *SopTaskUpdate) ClearToken() *SopTaskUpdate {
	stu.mutation.ClearToken()
	return stu
}

// AddTaskStageIDs adds the "task_stages" edge to the SopStage entity by IDs.
func (stu *SopTaskUpdate) AddTaskStageIDs(ids ...uint64) *SopTaskUpdate {
	stu.mutation.AddTaskStageIDs(ids...)
	return stu
}

// AddTaskStages adds the "task_stages" edges to the SopStage entity.
func (stu *SopTaskUpdate) AddTaskStages(s ...*SopStage) *SopTaskUpdate {
	ids := make([]uint64, len(s))
	for i := range s {
		ids[i] = s[i].ID
	}
	return stu.AddTaskStageIDs(ids...)
}

// Mutation returns the SopTaskMutation object of the builder.
func (stu *SopTaskUpdate) Mutation() *SopTaskMutation {
	return stu.mutation
}

// ClearTaskStages clears all "task_stages" edges to the SopStage entity.
func (stu *SopTaskUpdate) ClearTaskStages() *SopTaskUpdate {
	stu.mutation.ClearTaskStages()
	return stu
}

// RemoveTaskStageIDs removes the "task_stages" edge to SopStage entities by IDs.
func (stu *SopTaskUpdate) RemoveTaskStageIDs(ids ...uint64) *SopTaskUpdate {
	stu.mutation.RemoveTaskStageIDs(ids...)
	return stu
}

// RemoveTaskStages removes "task_stages" edges to SopStage entities.
func (stu *SopTaskUpdate) RemoveTaskStages(s ...*SopStage) *SopTaskUpdate {
	ids := make([]uint64, len(s))
	for i := range s {
		ids[i] = s[i].ID
	}
	return stu.RemoveTaskStageIDs(ids...)
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (stu *SopTaskUpdate) check() error {
	if v, ok := stu.mutation.Name(); ok {
		if err := soptask.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "SopTask.name": %w`, err)}
		}
	}
	return nil
}

func (stu *SopTaskUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := stu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(soptask.Table, soptask.Columns, sqlgraph.NewFieldSpec(soptask.FieldID, field.TypeUint64))
	if ps := stu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := stu.mutation.UpdatedAt(); ok {
		_spec.SetField(soptask.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := stu.mutation.Status(); ok {
		_spec.SetField(soptask.FieldStatus, field.TypeUint8, value)
	}
	if value, ok := stu.mutation.AddedStatus(); ok {
		_spec.AddField(soptask.FieldStatus, field.TypeUint8, value)
	}
	if stu.mutation.StatusCleared() {
		_spec.ClearField(soptask.FieldStatus, field.TypeUint8)
	}
	if value, ok := stu.mutation.DeletedAt(); ok {
		_spec.SetField(soptask.FieldDeletedAt, field.TypeTime, value)
	}
	if stu.mutation.DeletedAtCleared() {
		_spec.ClearField(soptask.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := stu.mutation.Name(); ok {
		_spec.SetField(soptask.FieldName, field.TypeString, value)
	}
	if value, ok := stu.mutation.BotWxidList(); ok {
		_spec.SetField(soptask.FieldBotWxidList, field.TypeJSON, value)
	}
	if value, ok := stu.mutation.AppendedBotWxidList(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, soptask.FieldBotWxidList, value)
		})
	}
	if stu.mutation.BotWxidListCleared() {
		_spec.ClearField(soptask.FieldBotWxidList, field.TypeJSON)
	}
	if value, ok := stu.mutation.GetType(); ok {
		_spec.SetField(soptask.FieldType, field.TypeInt, value)
	}
	if value, ok := stu.mutation.AddedType(); ok {
		_spec.AddField(soptask.FieldType, field.TypeInt, value)
	}
	if value, ok := stu.mutation.PlanStartTime(); ok {
		_spec.SetField(soptask.FieldPlanStartTime, field.TypeTime, value)
	}
	if stu.mutation.PlanStartTimeCleared() {
		_spec.ClearField(soptask.FieldPlanStartTime, field.TypeTime)
	}
	if value, ok := stu.mutation.PlanEndTime(); ok {
		_spec.SetField(soptask.FieldPlanEndTime, field.TypeTime, value)
	}
	if stu.mutation.PlanEndTimeCleared() {
		_spec.ClearField(soptask.FieldPlanEndTime, field.TypeTime)
	}
	if value, ok := stu.mutation.CreatorID(); ok {
		_spec.SetField(soptask.FieldCreatorID, field.TypeString, value)
	}
	if stu.mutation.CreatorIDCleared() {
		_spec.ClearField(soptask.FieldCreatorID, field.TypeString)
	}
	if value, ok := stu.mutation.OrganizationID(); ok {
		_spec.SetField(soptask.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := stu.mutation.AddedOrganizationID(); ok {
		_spec.AddField(soptask.FieldOrganizationID, field.TypeUint64, value)
	}
	if stu.mutation.OrganizationIDCleared() {
		_spec.ClearField(soptask.FieldOrganizationID, field.TypeUint64)
	}
	if value, ok := stu.mutation.Token(); ok {
		_spec.SetField(soptask.FieldToken, field.TypeJSON, value)
	}
	if value, ok := stu.mutation.AppendedToken(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, soptask.FieldToken, value)
		})
	}
	if stu.mutation.TokenCleared() {
		_spec.ClearField(soptask.FieldToken, field.TypeJSON)
	}
	if stu.mutation.TaskStagesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   soptask.TaskStagesTable,
			Columns: []string{soptask.TaskStagesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(sopstage.FieldID, field.TypeUint64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := stu.mutation.RemovedTaskStagesIDs(); len(nodes) > 0 && !stu.mutation.TaskStagesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   soptask.TaskStagesTable,
			Columns: []string{soptask.TaskStagesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(sopstage.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 := stu.mutation.TaskStagesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   soptask.TaskStagesTable,
			Columns: []string{soptask.TaskStagesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(sopstage.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, stu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{soptask.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	stu.mutation.done = true
	return n, nil
}

// SopTaskUpdateOne is the builder for updating a single SopTask entity.
type SopTaskUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *SopTaskMutation
}

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

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

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

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

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

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

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

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

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

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

// SetBotWxidList sets the "bot_wxid_list" field.
func (stuo *SopTaskUpdateOne) SetBotWxidList(s []string) *SopTaskUpdateOne {
	stuo.mutation.SetBotWxidList(s)
	return stuo
}

// AppendBotWxidList appends s to the "bot_wxid_list" field.
func (stuo *SopTaskUpdateOne) AppendBotWxidList(s []string) *SopTaskUpdateOne {
	stuo.mutation.AppendBotWxidList(s)
	return stuo
}

// ClearBotWxidList clears the value of the "bot_wxid_list" field.
func (stuo *SopTaskUpdateOne) ClearBotWxidList() *SopTaskUpdateOne {
	stuo.mutation.ClearBotWxidList()
	return stuo
}

// SetType sets the "type" field.
func (stuo *SopTaskUpdateOne) SetType(i int) *SopTaskUpdateOne {
	stuo.mutation.ResetType()
	stuo.mutation.SetType(i)
	return stuo
}

// SetNillableType sets the "type" field if the given value is not nil.
func (stuo *SopTaskUpdateOne) SetNillableType(i *int) *SopTaskUpdateOne {
	if i != nil {
		stuo.SetType(*i)
	}
	return stuo
}

// AddType adds i to the "type" field.
func (stuo *SopTaskUpdateOne) AddType(i int) *SopTaskUpdateOne {
	stuo.mutation.AddType(i)
	return stuo
}

// SetPlanStartTime sets the "plan_start_time" field.
func (stuo *SopTaskUpdateOne) SetPlanStartTime(t time.Time) *SopTaskUpdateOne {
	stuo.mutation.SetPlanStartTime(t)
	return stuo
}

// SetNillablePlanStartTime sets the "plan_start_time" field if the given value is not nil.
func (stuo *SopTaskUpdateOne) SetNillablePlanStartTime(t *time.Time) *SopTaskUpdateOne {
	if t != nil {
		stuo.SetPlanStartTime(*t)
	}
	return stuo
}

// ClearPlanStartTime clears the value of the "plan_start_time" field.
func (stuo *SopTaskUpdateOne) ClearPlanStartTime() *SopTaskUpdateOne {
	stuo.mutation.ClearPlanStartTime()
	return stuo
}

// SetPlanEndTime sets the "plan_end_time" field.
func (stuo *SopTaskUpdateOne) SetPlanEndTime(t time.Time) *SopTaskUpdateOne {
	stuo.mutation.SetPlanEndTime(t)
	return stuo
}

// SetNillablePlanEndTime sets the "plan_end_time" field if the given value is not nil.
func (stuo *SopTaskUpdateOne) SetNillablePlanEndTime(t *time.Time) *SopTaskUpdateOne {
	if t != nil {
		stuo.SetPlanEndTime(*t)
	}
	return stuo
}

// ClearPlanEndTime clears the value of the "plan_end_time" field.
func (stuo *SopTaskUpdateOne) ClearPlanEndTime() *SopTaskUpdateOne {
	stuo.mutation.ClearPlanEndTime()
	return stuo
}

// SetCreatorID sets the "creator_id" field.
func (stuo *SopTaskUpdateOne) SetCreatorID(s string) *SopTaskUpdateOne {
	stuo.mutation.SetCreatorID(s)
	return stuo
}

// SetNillableCreatorID sets the "creator_id" field if the given value is not nil.
func (stuo *SopTaskUpdateOne) SetNillableCreatorID(s *string) *SopTaskUpdateOne {
	if s != nil {
		stuo.SetCreatorID(*s)
	}
	return stuo
}

// ClearCreatorID clears the value of the "creator_id" field.
func (stuo *SopTaskUpdateOne) ClearCreatorID() *SopTaskUpdateOne {
	stuo.mutation.ClearCreatorID()
	return stuo
}

// SetOrganizationID sets the "organization_id" field.
func (stuo *SopTaskUpdateOne) SetOrganizationID(u uint64) *SopTaskUpdateOne {
	stuo.mutation.ResetOrganizationID()
	stuo.mutation.SetOrganizationID(u)
	return stuo
}

// SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
func (stuo *SopTaskUpdateOne) SetNillableOrganizationID(u *uint64) *SopTaskUpdateOne {
	if u != nil {
		stuo.SetOrganizationID(*u)
	}
	return stuo
}

// AddOrganizationID adds u to the "organization_id" field.
func (stuo *SopTaskUpdateOne) AddOrganizationID(u int64) *SopTaskUpdateOne {
	stuo.mutation.AddOrganizationID(u)
	return stuo
}

// ClearOrganizationID clears the value of the "organization_id" field.
func (stuo *SopTaskUpdateOne) ClearOrganizationID() *SopTaskUpdateOne {
	stuo.mutation.ClearOrganizationID()
	return stuo
}

// SetToken sets the "token" field.
func (stuo *SopTaskUpdateOne) SetToken(s []string) *SopTaskUpdateOne {
	stuo.mutation.SetToken(s)
	return stuo
}

// AppendToken appends s to the "token" field.
func (stuo *SopTaskUpdateOne) AppendToken(s []string) *SopTaskUpdateOne {
	stuo.mutation.AppendToken(s)
	return stuo
}

// ClearToken clears the value of the "token" field.
func (stuo *SopTaskUpdateOne) ClearToken() *SopTaskUpdateOne {
	stuo.mutation.ClearToken()
	return stuo
}

// AddTaskStageIDs adds the "task_stages" edge to the SopStage entity by IDs.
func (stuo *SopTaskUpdateOne) AddTaskStageIDs(ids ...uint64) *SopTaskUpdateOne {
	stuo.mutation.AddTaskStageIDs(ids...)
	return stuo
}

// AddTaskStages adds the "task_stages" edges to the SopStage entity.
func (stuo *SopTaskUpdateOne) AddTaskStages(s ...*SopStage) *SopTaskUpdateOne {
	ids := make([]uint64, len(s))
	for i := range s {
		ids[i] = s[i].ID
	}
	return stuo.AddTaskStageIDs(ids...)
}

// Mutation returns the SopTaskMutation object of the builder.
func (stuo *SopTaskUpdateOne) Mutation() *SopTaskMutation {
	return stuo.mutation
}

// ClearTaskStages clears all "task_stages" edges to the SopStage entity.
func (stuo *SopTaskUpdateOne) ClearTaskStages() *SopTaskUpdateOne {
	stuo.mutation.ClearTaskStages()
	return stuo
}

// RemoveTaskStageIDs removes the "task_stages" edge to SopStage entities by IDs.
func (stuo *SopTaskUpdateOne) RemoveTaskStageIDs(ids ...uint64) *SopTaskUpdateOne {
	stuo.mutation.RemoveTaskStageIDs(ids...)
	return stuo
}

// RemoveTaskStages removes "task_stages" edges to SopStage entities.
func (stuo *SopTaskUpdateOne) RemoveTaskStages(s ...*SopStage) *SopTaskUpdateOne {
	ids := make([]uint64, len(s))
	for i := range s {
		ids[i] = s[i].ID
	}
	return stuo.RemoveTaskStageIDs(ids...)
}

// Where appends a list predicates to the SopTaskUpdate builder.
func (stuo *SopTaskUpdateOne) Where(ps ...predicate.SopTask) *SopTaskUpdateOne {
	stuo.mutation.Where(ps...)
	return stuo
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (stuo *SopTaskUpdateOne) check() error {
	if v, ok := stuo.mutation.Name(); ok {
		if err := soptask.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "SopTask.name": %w`, err)}
		}
	}
	return nil
}

func (stuo *SopTaskUpdateOne) sqlSave(ctx context.Context) (_node *SopTask, err error) {
	if err := stuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(soptask.Table, soptask.Columns, sqlgraph.NewFieldSpec(soptask.FieldID, field.TypeUint64))
	id, ok := stuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "SopTask.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := stuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, soptask.FieldID)
		for _, f := range fields {
			if !soptask.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != soptask.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := stuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := stuo.mutation.UpdatedAt(); ok {
		_spec.SetField(soptask.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := stuo.mutation.Status(); ok {
		_spec.SetField(soptask.FieldStatus, field.TypeUint8, value)
	}
	if value, ok := stuo.mutation.AddedStatus(); ok {
		_spec.AddField(soptask.FieldStatus, field.TypeUint8, value)
	}
	if stuo.mutation.StatusCleared() {
		_spec.ClearField(soptask.FieldStatus, field.TypeUint8)
	}
	if value, ok := stuo.mutation.DeletedAt(); ok {
		_spec.SetField(soptask.FieldDeletedAt, field.TypeTime, value)
	}
	if stuo.mutation.DeletedAtCleared() {
		_spec.ClearField(soptask.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := stuo.mutation.Name(); ok {
		_spec.SetField(soptask.FieldName, field.TypeString, value)
	}
	if value, ok := stuo.mutation.BotWxidList(); ok {
		_spec.SetField(soptask.FieldBotWxidList, field.TypeJSON, value)
	}
	if value, ok := stuo.mutation.AppendedBotWxidList(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, soptask.FieldBotWxidList, value)
		})
	}
	if stuo.mutation.BotWxidListCleared() {
		_spec.ClearField(soptask.FieldBotWxidList, field.TypeJSON)
	}
	if value, ok := stuo.mutation.GetType(); ok {
		_spec.SetField(soptask.FieldType, field.TypeInt, value)
	}
	if value, ok := stuo.mutation.AddedType(); ok {
		_spec.AddField(soptask.FieldType, field.TypeInt, value)
	}
	if value, ok := stuo.mutation.PlanStartTime(); ok {
		_spec.SetField(soptask.FieldPlanStartTime, field.TypeTime, value)
	}
	if stuo.mutation.PlanStartTimeCleared() {
		_spec.ClearField(soptask.FieldPlanStartTime, field.TypeTime)
	}
	if value, ok := stuo.mutation.PlanEndTime(); ok {
		_spec.SetField(soptask.FieldPlanEndTime, field.TypeTime, value)
	}
	if stuo.mutation.PlanEndTimeCleared() {
		_spec.ClearField(soptask.FieldPlanEndTime, field.TypeTime)
	}
	if value, ok := stuo.mutation.CreatorID(); ok {
		_spec.SetField(soptask.FieldCreatorID, field.TypeString, value)
	}
	if stuo.mutation.CreatorIDCleared() {
		_spec.ClearField(soptask.FieldCreatorID, field.TypeString)
	}
	if value, ok := stuo.mutation.OrganizationID(); ok {
		_spec.SetField(soptask.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := stuo.mutation.AddedOrganizationID(); ok {
		_spec.AddField(soptask.FieldOrganizationID, field.TypeUint64, value)
	}
	if stuo.mutation.OrganizationIDCleared() {
		_spec.ClearField(soptask.FieldOrganizationID, field.TypeUint64)
	}
	if value, ok := stuo.mutation.Token(); ok {
		_spec.SetField(soptask.FieldToken, field.TypeJSON, value)
	}
	if value, ok := stuo.mutation.AppendedToken(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, soptask.FieldToken, value)
		})
	}
	if stuo.mutation.TokenCleared() {
		_spec.ClearField(soptask.FieldToken, field.TypeJSON)
	}
	if stuo.mutation.TaskStagesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   soptask.TaskStagesTable,
			Columns: []string{soptask.TaskStagesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(sopstage.FieldID, field.TypeUint64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := stuo.mutation.RemovedTaskStagesIDs(); len(nodes) > 0 && !stuo.mutation.TaskStagesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   soptask.TaskStagesTable,
			Columns: []string{soptask.TaskStagesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(sopstage.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 := stuo.mutation.TaskStagesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   soptask.TaskStagesTable,
			Columns: []string{soptask.TaskStagesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(sopstage.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &SopTask{config: stuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, stuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{soptask.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	stuo.mutation.done = true
	return _node, nil
}
