// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"
	"wechat-api/ent/allocagent"
	"wechat-api/ent/predicate"

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

// AllocAgentUpdate is the builder for updating AllocAgent entities.
type AllocAgentUpdate struct {
	config
	hooks    []Hook
	mutation *AllocAgentMutation
}

// Where appends a list predicates to the AllocAgentUpdate builder.
func (aau *AllocAgentUpdate) Where(ps ...predicate.AllocAgent) *AllocAgentUpdate {
	aau.mutation.Where(ps...)
	return aau
}

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

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

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

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

// SetUserID sets the "user_id" field.
func (aau *AllocAgentUpdate) SetUserID(s string) *AllocAgentUpdate {
	aau.mutation.SetUserID(s)
	return aau
}

// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (aau *AllocAgentUpdate) SetNillableUserID(s *string) *AllocAgentUpdate {
	if s != nil {
		aau.SetUserID(*s)
	}
	return aau
}

// ClearUserID clears the value of the "user_id" field.
func (aau *AllocAgentUpdate) ClearUserID() *AllocAgentUpdate {
	aau.mutation.ClearUserID()
	return aau
}

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

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

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

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

// SetAgents sets the "agents" field.
func (aau *AllocAgentUpdate) SetAgents(u []uint64) *AllocAgentUpdate {
	aau.mutation.SetAgents(u)
	return aau
}

// AppendAgents appends u to the "agents" field.
func (aau *AllocAgentUpdate) AppendAgents(u []uint64) *AllocAgentUpdate {
	aau.mutation.AppendAgents(u)
	return aau
}

// SetStatus sets the "status" field.
func (aau *AllocAgentUpdate) SetStatus(i int) *AllocAgentUpdate {
	aau.mutation.ResetStatus()
	aau.mutation.SetStatus(i)
	return aau
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (aau *AllocAgentUpdate) SetNillableStatus(i *int) *AllocAgentUpdate {
	if i != nil {
		aau.SetStatus(*i)
	}
	return aau
}

// AddStatus adds i to the "status" field.
func (aau *AllocAgentUpdate) AddStatus(i int) *AllocAgentUpdate {
	aau.mutation.AddStatus(i)
	return aau
}

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

// Mutation returns the AllocAgentMutation object of the builder.
func (aau *AllocAgentUpdate) Mutation() *AllocAgentMutation {
	return aau.mutation
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (aau *AllocAgentUpdate) check() error {
	if v, ok := aau.mutation.Status(); ok {
		if err := allocagent.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "AllocAgent.status": %w`, err)}
		}
	}
	return nil
}

func (aau *AllocAgentUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := aau.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(allocagent.Table, allocagent.Columns, sqlgraph.NewFieldSpec(allocagent.FieldID, field.TypeUint64))
	if ps := aau.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := aau.mutation.UpdatedAt(); ok {
		_spec.SetField(allocagent.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := aau.mutation.DeletedAt(); ok {
		_spec.SetField(allocagent.FieldDeletedAt, field.TypeTime, value)
	}
	if aau.mutation.DeletedAtCleared() {
		_spec.ClearField(allocagent.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := aau.mutation.UserID(); ok {
		_spec.SetField(allocagent.FieldUserID, field.TypeString, value)
	}
	if aau.mutation.UserIDCleared() {
		_spec.ClearField(allocagent.FieldUserID, field.TypeString)
	}
	if value, ok := aau.mutation.OrganizationID(); ok {
		_spec.SetField(allocagent.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := aau.mutation.AddedOrganizationID(); ok {
		_spec.AddField(allocagent.FieldOrganizationID, field.TypeUint64, value)
	}
	if aau.mutation.OrganizationIDCleared() {
		_spec.ClearField(allocagent.FieldOrganizationID, field.TypeUint64)
	}
	if value, ok := aau.mutation.Agents(); ok {
		_spec.SetField(allocagent.FieldAgents, field.TypeJSON, value)
	}
	if value, ok := aau.mutation.AppendedAgents(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, allocagent.FieldAgents, value)
		})
	}
	if value, ok := aau.mutation.Status(); ok {
		_spec.SetField(allocagent.FieldStatus, field.TypeInt, value)
	}
	if value, ok := aau.mutation.AddedStatus(); ok {
		_spec.AddField(allocagent.FieldStatus, field.TypeInt, value)
	}
	if aau.mutation.StatusCleared() {
		_spec.ClearField(allocagent.FieldStatus, field.TypeInt)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, aau.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{allocagent.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	aau.mutation.done = true
	return n, nil
}

// AllocAgentUpdateOne is the builder for updating a single AllocAgent entity.
type AllocAgentUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *AllocAgentMutation
}

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

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

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

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

// SetUserID sets the "user_id" field.
func (aauo *AllocAgentUpdateOne) SetUserID(s string) *AllocAgentUpdateOne {
	aauo.mutation.SetUserID(s)
	return aauo
}

// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (aauo *AllocAgentUpdateOne) SetNillableUserID(s *string) *AllocAgentUpdateOne {
	if s != nil {
		aauo.SetUserID(*s)
	}
	return aauo
}

// ClearUserID clears the value of the "user_id" field.
func (aauo *AllocAgentUpdateOne) ClearUserID() *AllocAgentUpdateOne {
	aauo.mutation.ClearUserID()
	return aauo
}

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

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

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

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

// SetAgents sets the "agents" field.
func (aauo *AllocAgentUpdateOne) SetAgents(u []uint64) *AllocAgentUpdateOne {
	aauo.mutation.SetAgents(u)
	return aauo
}

// AppendAgents appends u to the "agents" field.
func (aauo *AllocAgentUpdateOne) AppendAgents(u []uint64) *AllocAgentUpdateOne {
	aauo.mutation.AppendAgents(u)
	return aauo
}

// SetStatus sets the "status" field.
func (aauo *AllocAgentUpdateOne) SetStatus(i int) *AllocAgentUpdateOne {
	aauo.mutation.ResetStatus()
	aauo.mutation.SetStatus(i)
	return aauo
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (aauo *AllocAgentUpdateOne) SetNillableStatus(i *int) *AllocAgentUpdateOne {
	if i != nil {
		aauo.SetStatus(*i)
	}
	return aauo
}

// AddStatus adds i to the "status" field.
func (aauo *AllocAgentUpdateOne) AddStatus(i int) *AllocAgentUpdateOne {
	aauo.mutation.AddStatus(i)
	return aauo
}

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

// Mutation returns the AllocAgentMutation object of the builder.
func (aauo *AllocAgentUpdateOne) Mutation() *AllocAgentMutation {
	return aauo.mutation
}

// Where appends a list predicates to the AllocAgentUpdate builder.
func (aauo *AllocAgentUpdateOne) Where(ps ...predicate.AllocAgent) *AllocAgentUpdateOne {
	aauo.mutation.Where(ps...)
	return aauo
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (aauo *AllocAgentUpdateOne) check() error {
	if v, ok := aauo.mutation.Status(); ok {
		if err := allocagent.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "AllocAgent.status": %w`, err)}
		}
	}
	return nil
}

func (aauo *AllocAgentUpdateOne) sqlSave(ctx context.Context) (_node *AllocAgent, err error) {
	if err := aauo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(allocagent.Table, allocagent.Columns, sqlgraph.NewFieldSpec(allocagent.FieldID, field.TypeUint64))
	id, ok := aauo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "AllocAgent.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := aauo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, allocagent.FieldID)
		for _, f := range fields {
			if !allocagent.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != allocagent.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := aauo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := aauo.mutation.UpdatedAt(); ok {
		_spec.SetField(allocagent.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := aauo.mutation.DeletedAt(); ok {
		_spec.SetField(allocagent.FieldDeletedAt, field.TypeTime, value)
	}
	if aauo.mutation.DeletedAtCleared() {
		_spec.ClearField(allocagent.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := aauo.mutation.UserID(); ok {
		_spec.SetField(allocagent.FieldUserID, field.TypeString, value)
	}
	if aauo.mutation.UserIDCleared() {
		_spec.ClearField(allocagent.FieldUserID, field.TypeString)
	}
	if value, ok := aauo.mutation.OrganizationID(); ok {
		_spec.SetField(allocagent.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := aauo.mutation.AddedOrganizationID(); ok {
		_spec.AddField(allocagent.FieldOrganizationID, field.TypeUint64, value)
	}
	if aauo.mutation.OrganizationIDCleared() {
		_spec.ClearField(allocagent.FieldOrganizationID, field.TypeUint64)
	}
	if value, ok := aauo.mutation.Agents(); ok {
		_spec.SetField(allocagent.FieldAgents, field.TypeJSON, value)
	}
	if value, ok := aauo.mutation.AppendedAgents(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, allocagent.FieldAgents, value)
		})
	}
	if value, ok := aauo.mutation.Status(); ok {
		_spec.SetField(allocagent.FieldStatus, field.TypeInt, value)
	}
	if value, ok := aauo.mutation.AddedStatus(); ok {
		_spec.AddField(allocagent.FieldStatus, field.TypeInt, value)
	}
	if aauo.mutation.StatusCleared() {
		_spec.ClearField(allocagent.FieldStatus, field.TypeInt)
	}
	_node = &AllocAgent{config: aauo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, aauo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{allocagent.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	aauo.mutation.done = true
	return _node, nil
}
