// Code generated by ent, DO NOT EDIT.

package ent

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

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

// TokenUpdate is the builder for updating Token entities.
type TokenUpdate struct {
	config
	hooks    []Hook
	mutation *TokenMutation
}

// Where appends a list predicates to the TokenUpdate builder.
func (tu *TokenUpdate) Where(ps ...predicate.Token) *TokenUpdate {
	tu.mutation.Where(ps...)
	return tu
}

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

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

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

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

// SetExpireAt sets the "expire_at" field.
func (tu *TokenUpdate) SetExpireAt(t time.Time) *TokenUpdate {
	tu.mutation.SetExpireAt(t)
	return tu
}

// SetNillableExpireAt sets the "expire_at" field if the given value is not nil.
func (tu *TokenUpdate) SetNillableExpireAt(t *time.Time) *TokenUpdate {
	if t != nil {
		tu.SetExpireAt(*t)
	}
	return tu
}

// ClearExpireAt clears the value of the "expire_at" field.
func (tu *TokenUpdate) ClearExpireAt() *TokenUpdate {
	tu.mutation.ClearExpireAt()
	return tu
}

// SetToken sets the "token" field.
func (tu *TokenUpdate) SetToken(s string) *TokenUpdate {
	tu.mutation.SetToken(s)
	return tu
}

// SetNillableToken sets the "token" field if the given value is not nil.
func (tu *TokenUpdate) SetNillableToken(s *string) *TokenUpdate {
	if s != nil {
		tu.SetToken(*s)
	}
	return tu
}

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

// SetMAC sets the "mac" field.
func (tu *TokenUpdate) SetMAC(s string) *TokenUpdate {
	tu.mutation.SetMAC(s)
	return tu
}

// SetNillableMAC sets the "mac" field if the given value is not nil.
func (tu *TokenUpdate) SetNillableMAC(s *string) *TokenUpdate {
	if s != nil {
		tu.SetMAC(*s)
	}
	return tu
}

// ClearMAC clears the value of the "mac" field.
func (tu *TokenUpdate) ClearMAC() *TokenUpdate {
	tu.mutation.ClearMAC()
	return tu
}

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

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

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

// SetAgentID sets the "agent_id" field.
func (tu *TokenUpdate) SetAgentID(u uint64) *TokenUpdate {
	tu.mutation.SetAgentID(u)
	return tu
}

// SetNillableAgentID sets the "agent_id" field if the given value is not nil.
func (tu *TokenUpdate) SetNillableAgentID(u *uint64) *TokenUpdate {
	if u != nil {
		tu.SetAgentID(*u)
	}
	return tu
}

// SetCustomAgentBase sets the "custom_agent_base" field.
func (tu *TokenUpdate) SetCustomAgentBase(s string) *TokenUpdate {
	tu.mutation.SetCustomAgentBase(s)
	return tu
}

// SetNillableCustomAgentBase sets the "custom_agent_base" field if the given value is not nil.
func (tu *TokenUpdate) SetNillableCustomAgentBase(s *string) *TokenUpdate {
	if s != nil {
		tu.SetCustomAgentBase(*s)
	}
	return tu
}

// ClearCustomAgentBase clears the value of the "custom_agent_base" field.
func (tu *TokenUpdate) ClearCustomAgentBase() *TokenUpdate {
	tu.mutation.ClearCustomAgentBase()
	return tu
}

// SetCustomAgentKey sets the "custom_agent_key" field.
func (tu *TokenUpdate) SetCustomAgentKey(s string) *TokenUpdate {
	tu.mutation.SetCustomAgentKey(s)
	return tu
}

// SetNillableCustomAgentKey sets the "custom_agent_key" field if the given value is not nil.
func (tu *TokenUpdate) SetNillableCustomAgentKey(s *string) *TokenUpdate {
	if s != nil {
		tu.SetCustomAgentKey(*s)
	}
	return tu
}

// ClearCustomAgentKey clears the value of the "custom_agent_key" field.
func (tu *TokenUpdate) ClearCustomAgentKey() *TokenUpdate {
	tu.mutation.ClearCustomAgentKey()
	return tu
}

// SetOpenaiBase sets the "openai_base" field.
func (tu *TokenUpdate) SetOpenaiBase(s string) *TokenUpdate {
	tu.mutation.SetOpenaiBase(s)
	return tu
}

// SetNillableOpenaiBase sets the "openai_base" field if the given value is not nil.
func (tu *TokenUpdate) SetNillableOpenaiBase(s *string) *TokenUpdate {
	if s != nil {
		tu.SetOpenaiBase(*s)
	}
	return tu
}

// ClearOpenaiBase clears the value of the "openai_base" field.
func (tu *TokenUpdate) ClearOpenaiBase() *TokenUpdate {
	tu.mutation.ClearOpenaiBase()
	return tu
}

// SetOpenaiKey sets the "openai_key" field.
func (tu *TokenUpdate) SetOpenaiKey(s string) *TokenUpdate {
	tu.mutation.SetOpenaiKey(s)
	return tu
}

// SetNillableOpenaiKey sets the "openai_key" field if the given value is not nil.
func (tu *TokenUpdate) SetNillableOpenaiKey(s *string) *TokenUpdate {
	if s != nil {
		tu.SetOpenaiKey(*s)
	}
	return tu
}

// ClearOpenaiKey clears the value of the "openai_key" field.
func (tu *TokenUpdate) ClearOpenaiKey() *TokenUpdate {
	tu.mutation.ClearOpenaiKey()
	return tu
}

// SetAgent sets the "agent" edge to the Agent entity.
func (tu *TokenUpdate) SetAgent(a *Agent) *TokenUpdate {
	return tu.SetAgentID(a.ID)
}

// Mutation returns the TokenMutation object of the builder.
func (tu *TokenUpdate) Mutation() *TokenMutation {
	return tu.mutation
}

// ClearAgent clears the "agent" edge to the Agent entity.
func (tu *TokenUpdate) ClearAgent() *TokenUpdate {
	tu.mutation.ClearAgent()
	return tu
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (tu *TokenUpdate) check() error {
	if _, ok := tu.mutation.AgentID(); tu.mutation.AgentCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "Token.agent"`)
	}
	return nil
}

func (tu *TokenUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := tu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(token.Table, token.Columns, sqlgraph.NewFieldSpec(token.FieldID, field.TypeUint64))
	if ps := tu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tu.mutation.UpdatedAt(); ok {
		_spec.SetField(token.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := tu.mutation.DeletedAt(); ok {
		_spec.SetField(token.FieldDeletedAt, field.TypeTime, value)
	}
	if tu.mutation.DeletedAtCleared() {
		_spec.ClearField(token.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := tu.mutation.ExpireAt(); ok {
		_spec.SetField(token.FieldExpireAt, field.TypeTime, value)
	}
	if tu.mutation.ExpireAtCleared() {
		_spec.ClearField(token.FieldExpireAt, field.TypeTime)
	}
	if value, ok := tu.mutation.Token(); ok {
		_spec.SetField(token.FieldToken, field.TypeString, value)
	}
	if tu.mutation.TokenCleared() {
		_spec.ClearField(token.FieldToken, field.TypeString)
	}
	if value, ok := tu.mutation.MAC(); ok {
		_spec.SetField(token.FieldMAC, field.TypeString, value)
	}
	if tu.mutation.MACCleared() {
		_spec.ClearField(token.FieldMAC, field.TypeString)
	}
	if value, ok := tu.mutation.OrganizationID(); ok {
		_spec.SetField(token.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := tu.mutation.AddedOrganizationID(); ok {
		_spec.AddField(token.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := tu.mutation.CustomAgentBase(); ok {
		_spec.SetField(token.FieldCustomAgentBase, field.TypeString, value)
	}
	if tu.mutation.CustomAgentBaseCleared() {
		_spec.ClearField(token.FieldCustomAgentBase, field.TypeString)
	}
	if value, ok := tu.mutation.CustomAgentKey(); ok {
		_spec.SetField(token.FieldCustomAgentKey, field.TypeString, value)
	}
	if tu.mutation.CustomAgentKeyCleared() {
		_spec.ClearField(token.FieldCustomAgentKey, field.TypeString)
	}
	if value, ok := tu.mutation.OpenaiBase(); ok {
		_spec.SetField(token.FieldOpenaiBase, field.TypeString, value)
	}
	if tu.mutation.OpenaiBaseCleared() {
		_spec.ClearField(token.FieldOpenaiBase, field.TypeString)
	}
	if value, ok := tu.mutation.OpenaiKey(); ok {
		_spec.SetField(token.FieldOpenaiKey, field.TypeString, value)
	}
	if tu.mutation.OpenaiKeyCleared() {
		_spec.ClearField(token.FieldOpenaiKey, field.TypeString)
	}
	if tu.mutation.AgentCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   token.AgentTable,
			Columns: []string{token.AgentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tu.mutation.AgentIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   token.AgentTable,
			Columns: []string{token.AgentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(agent.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, tu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{token.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	tu.mutation.done = true
	return n, nil
}

// TokenUpdateOne is the builder for updating a single Token entity.
type TokenUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *TokenMutation
}

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

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

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

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

// SetExpireAt sets the "expire_at" field.
func (tuo *TokenUpdateOne) SetExpireAt(t time.Time) *TokenUpdateOne {
	tuo.mutation.SetExpireAt(t)
	return tuo
}

// SetNillableExpireAt sets the "expire_at" field if the given value is not nil.
func (tuo *TokenUpdateOne) SetNillableExpireAt(t *time.Time) *TokenUpdateOne {
	if t != nil {
		tuo.SetExpireAt(*t)
	}
	return tuo
}

// ClearExpireAt clears the value of the "expire_at" field.
func (tuo *TokenUpdateOne) ClearExpireAt() *TokenUpdateOne {
	tuo.mutation.ClearExpireAt()
	return tuo
}

// SetToken sets the "token" field.
func (tuo *TokenUpdateOne) SetToken(s string) *TokenUpdateOne {
	tuo.mutation.SetToken(s)
	return tuo
}

// SetNillableToken sets the "token" field if the given value is not nil.
func (tuo *TokenUpdateOne) SetNillableToken(s *string) *TokenUpdateOne {
	if s != nil {
		tuo.SetToken(*s)
	}
	return tuo
}

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

// SetMAC sets the "mac" field.
func (tuo *TokenUpdateOne) SetMAC(s string) *TokenUpdateOne {
	tuo.mutation.SetMAC(s)
	return tuo
}

// SetNillableMAC sets the "mac" field if the given value is not nil.
func (tuo *TokenUpdateOne) SetNillableMAC(s *string) *TokenUpdateOne {
	if s != nil {
		tuo.SetMAC(*s)
	}
	return tuo
}

// ClearMAC clears the value of the "mac" field.
func (tuo *TokenUpdateOne) ClearMAC() *TokenUpdateOne {
	tuo.mutation.ClearMAC()
	return tuo
}

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

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

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

// SetAgentID sets the "agent_id" field.
func (tuo *TokenUpdateOne) SetAgentID(u uint64) *TokenUpdateOne {
	tuo.mutation.SetAgentID(u)
	return tuo
}

// SetNillableAgentID sets the "agent_id" field if the given value is not nil.
func (tuo *TokenUpdateOne) SetNillableAgentID(u *uint64) *TokenUpdateOne {
	if u != nil {
		tuo.SetAgentID(*u)
	}
	return tuo
}

// SetCustomAgentBase sets the "custom_agent_base" field.
func (tuo *TokenUpdateOne) SetCustomAgentBase(s string) *TokenUpdateOne {
	tuo.mutation.SetCustomAgentBase(s)
	return tuo
}

// SetNillableCustomAgentBase sets the "custom_agent_base" field if the given value is not nil.
func (tuo *TokenUpdateOne) SetNillableCustomAgentBase(s *string) *TokenUpdateOne {
	if s != nil {
		tuo.SetCustomAgentBase(*s)
	}
	return tuo
}

// ClearCustomAgentBase clears the value of the "custom_agent_base" field.
func (tuo *TokenUpdateOne) ClearCustomAgentBase() *TokenUpdateOne {
	tuo.mutation.ClearCustomAgentBase()
	return tuo
}

// SetCustomAgentKey sets the "custom_agent_key" field.
func (tuo *TokenUpdateOne) SetCustomAgentKey(s string) *TokenUpdateOne {
	tuo.mutation.SetCustomAgentKey(s)
	return tuo
}

// SetNillableCustomAgentKey sets the "custom_agent_key" field if the given value is not nil.
func (tuo *TokenUpdateOne) SetNillableCustomAgentKey(s *string) *TokenUpdateOne {
	if s != nil {
		tuo.SetCustomAgentKey(*s)
	}
	return tuo
}

// ClearCustomAgentKey clears the value of the "custom_agent_key" field.
func (tuo *TokenUpdateOne) ClearCustomAgentKey() *TokenUpdateOne {
	tuo.mutation.ClearCustomAgentKey()
	return tuo
}

// SetOpenaiBase sets the "openai_base" field.
func (tuo *TokenUpdateOne) SetOpenaiBase(s string) *TokenUpdateOne {
	tuo.mutation.SetOpenaiBase(s)
	return tuo
}

// SetNillableOpenaiBase sets the "openai_base" field if the given value is not nil.
func (tuo *TokenUpdateOne) SetNillableOpenaiBase(s *string) *TokenUpdateOne {
	if s != nil {
		tuo.SetOpenaiBase(*s)
	}
	return tuo
}

// ClearOpenaiBase clears the value of the "openai_base" field.
func (tuo *TokenUpdateOne) ClearOpenaiBase() *TokenUpdateOne {
	tuo.mutation.ClearOpenaiBase()
	return tuo
}

// SetOpenaiKey sets the "openai_key" field.
func (tuo *TokenUpdateOne) SetOpenaiKey(s string) *TokenUpdateOne {
	tuo.mutation.SetOpenaiKey(s)
	return tuo
}

// SetNillableOpenaiKey sets the "openai_key" field if the given value is not nil.
func (tuo *TokenUpdateOne) SetNillableOpenaiKey(s *string) *TokenUpdateOne {
	if s != nil {
		tuo.SetOpenaiKey(*s)
	}
	return tuo
}

// ClearOpenaiKey clears the value of the "openai_key" field.
func (tuo *TokenUpdateOne) ClearOpenaiKey() *TokenUpdateOne {
	tuo.mutation.ClearOpenaiKey()
	return tuo
}

// SetAgent sets the "agent" edge to the Agent entity.
func (tuo *TokenUpdateOne) SetAgent(a *Agent) *TokenUpdateOne {
	return tuo.SetAgentID(a.ID)
}

// Mutation returns the TokenMutation object of the builder.
func (tuo *TokenUpdateOne) Mutation() *TokenMutation {
	return tuo.mutation
}

// ClearAgent clears the "agent" edge to the Agent entity.
func (tuo *TokenUpdateOne) ClearAgent() *TokenUpdateOne {
	tuo.mutation.ClearAgent()
	return tuo
}

// Where appends a list predicates to the TokenUpdate builder.
func (tuo *TokenUpdateOne) Where(ps ...predicate.Token) *TokenUpdateOne {
	tuo.mutation.Where(ps...)
	return tuo
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (tuo *TokenUpdateOne) check() error {
	if _, ok := tuo.mutation.AgentID(); tuo.mutation.AgentCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "Token.agent"`)
	}
	return nil
}

func (tuo *TokenUpdateOne) sqlSave(ctx context.Context) (_node *Token, err error) {
	if err := tuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(token.Table, token.Columns, sqlgraph.NewFieldSpec(token.FieldID, field.TypeUint64))
	id, ok := tuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Token.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := tuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, token.FieldID)
		for _, f := range fields {
			if !token.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != token.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := tuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tuo.mutation.UpdatedAt(); ok {
		_spec.SetField(token.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := tuo.mutation.DeletedAt(); ok {
		_spec.SetField(token.FieldDeletedAt, field.TypeTime, value)
	}
	if tuo.mutation.DeletedAtCleared() {
		_spec.ClearField(token.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := tuo.mutation.ExpireAt(); ok {
		_spec.SetField(token.FieldExpireAt, field.TypeTime, value)
	}
	if tuo.mutation.ExpireAtCleared() {
		_spec.ClearField(token.FieldExpireAt, field.TypeTime)
	}
	if value, ok := tuo.mutation.Token(); ok {
		_spec.SetField(token.FieldToken, field.TypeString, value)
	}
	if tuo.mutation.TokenCleared() {
		_spec.ClearField(token.FieldToken, field.TypeString)
	}
	if value, ok := tuo.mutation.MAC(); ok {
		_spec.SetField(token.FieldMAC, field.TypeString, value)
	}
	if tuo.mutation.MACCleared() {
		_spec.ClearField(token.FieldMAC, field.TypeString)
	}
	if value, ok := tuo.mutation.OrganizationID(); ok {
		_spec.SetField(token.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := tuo.mutation.AddedOrganizationID(); ok {
		_spec.AddField(token.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := tuo.mutation.CustomAgentBase(); ok {
		_spec.SetField(token.FieldCustomAgentBase, field.TypeString, value)
	}
	if tuo.mutation.CustomAgentBaseCleared() {
		_spec.ClearField(token.FieldCustomAgentBase, field.TypeString)
	}
	if value, ok := tuo.mutation.CustomAgentKey(); ok {
		_spec.SetField(token.FieldCustomAgentKey, field.TypeString, value)
	}
	if tuo.mutation.CustomAgentKeyCleared() {
		_spec.ClearField(token.FieldCustomAgentKey, field.TypeString)
	}
	if value, ok := tuo.mutation.OpenaiBase(); ok {
		_spec.SetField(token.FieldOpenaiBase, field.TypeString, value)
	}
	if tuo.mutation.OpenaiBaseCleared() {
		_spec.ClearField(token.FieldOpenaiBase, field.TypeString)
	}
	if value, ok := tuo.mutation.OpenaiKey(); ok {
		_spec.SetField(token.FieldOpenaiKey, field.TypeString, value)
	}
	if tuo.mutation.OpenaiKeyCleared() {
		_spec.ClearField(token.FieldOpenaiKey, field.TypeString)
	}
	if tuo.mutation.AgentCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   token.AgentTable,
			Columns: []string{token.AgentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tuo.mutation.AgentIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   token.AgentTable,
			Columns: []string{token.AgentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Token{config: tuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, tuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{token.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	tuo.mutation.done = true
	return _node, nil
}
