// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"wechat-api/ent/agentbase"
	"wechat-api/ent/predicate"
	"wechat-api/ent/wx"

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

// AgentBaseUpdate is the builder for updating AgentBase entities.
type AgentBaseUpdate struct {
	config
	hooks    []Hook
	mutation *AgentBaseMutation
}

// Where appends a list predicates to the AgentBaseUpdate builder.
func (abu *AgentBaseUpdate) Where(ps ...predicate.AgentBase) *AgentBaseUpdate {
	abu.mutation.Where(ps...)
	return abu
}

// SetQ sets the "q" field.
func (abu *AgentBaseUpdate) SetQ(s string) *AgentBaseUpdate {
	abu.mutation.SetQ(s)
	return abu
}

// SetNillableQ sets the "q" field if the given value is not nil.
func (abu *AgentBaseUpdate) SetNillableQ(s *string) *AgentBaseUpdate {
	if s != nil {
		abu.SetQ(*s)
	}
	return abu
}

// ClearQ clears the value of the "q" field.
func (abu *AgentBaseUpdate) ClearQ() *AgentBaseUpdate {
	abu.mutation.ClearQ()
	return abu
}

// SetA sets the "a" field.
func (abu *AgentBaseUpdate) SetA(s string) *AgentBaseUpdate {
	abu.mutation.SetA(s)
	return abu
}

// SetNillableA sets the "a" field if the given value is not nil.
func (abu *AgentBaseUpdate) SetNillableA(s *string) *AgentBaseUpdate {
	if s != nil {
		abu.SetA(*s)
	}
	return abu
}

// ClearA clears the value of the "a" field.
func (abu *AgentBaseUpdate) ClearA() *AgentBaseUpdate {
	abu.mutation.ClearA()
	return abu
}

// SetChunkIndex sets the "chunk_index" field.
func (abu *AgentBaseUpdate) SetChunkIndex(u uint64) *AgentBaseUpdate {
	abu.mutation.ResetChunkIndex()
	abu.mutation.SetChunkIndex(u)
	return abu
}

// SetNillableChunkIndex sets the "chunk_index" field if the given value is not nil.
func (abu *AgentBaseUpdate) SetNillableChunkIndex(u *uint64) *AgentBaseUpdate {
	if u != nil {
		abu.SetChunkIndex(*u)
	}
	return abu
}

// AddChunkIndex adds u to the "chunk_index" field.
func (abu *AgentBaseUpdate) AddChunkIndex(u int64) *AgentBaseUpdate {
	abu.mutation.AddChunkIndex(u)
	return abu
}

// SetIndexes sets the "indexes" field.
func (abu *AgentBaseUpdate) SetIndexes(s []string) *AgentBaseUpdate {
	abu.mutation.SetIndexes(s)
	return abu
}

// AppendIndexes appends s to the "indexes" field.
func (abu *AgentBaseUpdate) AppendIndexes(s []string) *AgentBaseUpdate {
	abu.mutation.AppendIndexes(s)
	return abu
}

// ClearIndexes clears the value of the "indexes" field.
func (abu *AgentBaseUpdate) ClearIndexes() *AgentBaseUpdate {
	abu.mutation.ClearIndexes()
	return abu
}

// SetDatasetID sets the "dataset_id" field.
func (abu *AgentBaseUpdate) SetDatasetID(s string) *AgentBaseUpdate {
	abu.mutation.SetDatasetID(s)
	return abu
}

// SetNillableDatasetID sets the "dataset_id" field if the given value is not nil.
func (abu *AgentBaseUpdate) SetNillableDatasetID(s *string) *AgentBaseUpdate {
	if s != nil {
		abu.SetDatasetID(*s)
	}
	return abu
}

// ClearDatasetID clears the value of the "dataset_id" field.
func (abu *AgentBaseUpdate) ClearDatasetID() *AgentBaseUpdate {
	abu.mutation.ClearDatasetID()
	return abu
}

// SetCollectionID sets the "collection_id" field.
func (abu *AgentBaseUpdate) SetCollectionID(s string) *AgentBaseUpdate {
	abu.mutation.SetCollectionID(s)
	return abu
}

// SetNillableCollectionID sets the "collection_id" field if the given value is not nil.
func (abu *AgentBaseUpdate) SetNillableCollectionID(s *string) *AgentBaseUpdate {
	if s != nil {
		abu.SetCollectionID(*s)
	}
	return abu
}

// ClearCollectionID clears the value of the "collection_id" field.
func (abu *AgentBaseUpdate) ClearCollectionID() *AgentBaseUpdate {
	abu.mutation.ClearCollectionID()
	return abu
}

// SetSourceName sets the "source_name" field.
func (abu *AgentBaseUpdate) SetSourceName(s string) *AgentBaseUpdate {
	abu.mutation.SetSourceName(s)
	return abu
}

// SetNillableSourceName sets the "source_name" field if the given value is not nil.
func (abu *AgentBaseUpdate) SetNillableSourceName(s *string) *AgentBaseUpdate {
	if s != nil {
		abu.SetSourceName(*s)
	}
	return abu
}

// ClearSourceName clears the value of the "source_name" field.
func (abu *AgentBaseUpdate) ClearSourceName() *AgentBaseUpdate {
	abu.mutation.ClearSourceName()
	return abu
}

// SetCanWrite sets the "can_write" field.
func (abu *AgentBaseUpdate) SetCanWrite(b []bool) *AgentBaseUpdate {
	abu.mutation.SetCanWrite(b)
	return abu
}

// AppendCanWrite appends b to the "can_write" field.
func (abu *AgentBaseUpdate) AppendCanWrite(b []bool) *AgentBaseUpdate {
	abu.mutation.AppendCanWrite(b)
	return abu
}

// ClearCanWrite clears the value of the "can_write" field.
func (abu *AgentBaseUpdate) ClearCanWrite() *AgentBaseUpdate {
	abu.mutation.ClearCanWrite()
	return abu
}

// SetIsOwner sets the "is_owner" field.
func (abu *AgentBaseUpdate) SetIsOwner(b []bool) *AgentBaseUpdate {
	abu.mutation.SetIsOwner(b)
	return abu
}

// AppendIsOwner appends b to the "is_owner" field.
func (abu *AgentBaseUpdate) AppendIsOwner(b []bool) *AgentBaseUpdate {
	abu.mutation.AppendIsOwner(b)
	return abu
}

// ClearIsOwner clears the value of the "is_owner" field.
func (abu *AgentBaseUpdate) ClearIsOwner() *AgentBaseUpdate {
	abu.mutation.ClearIsOwner()
	return abu
}

// AddWxAgentIDs adds the "wx_agent" edge to the Wx entity by IDs.
func (abu *AgentBaseUpdate) AddWxAgentIDs(ids ...uint64) *AgentBaseUpdate {
	abu.mutation.AddWxAgentIDs(ids...)
	return abu
}

// AddWxAgent adds the "wx_agent" edges to the Wx entity.
func (abu *AgentBaseUpdate) AddWxAgent(w ...*Wx) *AgentBaseUpdate {
	ids := make([]uint64, len(w))
	for i := range w {
		ids[i] = w[i].ID
	}
	return abu.AddWxAgentIDs(ids...)
}

// Mutation returns the AgentBaseMutation object of the builder.
func (abu *AgentBaseUpdate) Mutation() *AgentBaseMutation {
	return abu.mutation
}

// ClearWxAgent clears all "wx_agent" edges to the Wx entity.
func (abu *AgentBaseUpdate) ClearWxAgent() *AgentBaseUpdate {
	abu.mutation.ClearWxAgent()
	return abu
}

// RemoveWxAgentIDs removes the "wx_agent" edge to Wx entities by IDs.
func (abu *AgentBaseUpdate) RemoveWxAgentIDs(ids ...uint64) *AgentBaseUpdate {
	abu.mutation.RemoveWxAgentIDs(ids...)
	return abu
}

// RemoveWxAgent removes "wx_agent" edges to Wx entities.
func (abu *AgentBaseUpdate) RemoveWxAgent(w ...*Wx) *AgentBaseUpdate {
	ids := make([]uint64, len(w))
	for i := range w {
		ids[i] = w[i].ID
	}
	return abu.RemoveWxAgentIDs(ids...)
}

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (abu *AgentBaseUpdate) check() error {
	if v, ok := abu.mutation.ChunkIndex(); ok {
		if err := agentbase.ChunkIndexValidator(v); err != nil {
			return &ValidationError{Name: "chunk_index", err: fmt.Errorf(`ent: validator failed for field "AgentBase.chunk_index": %w`, err)}
		}
	}
	return nil
}

func (abu *AgentBaseUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := abu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(agentbase.Table, agentbase.Columns, sqlgraph.NewFieldSpec(agentbase.FieldID, field.TypeString))
	if ps := abu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := abu.mutation.Q(); ok {
		_spec.SetField(agentbase.FieldQ, field.TypeString, value)
	}
	if abu.mutation.QCleared() {
		_spec.ClearField(agentbase.FieldQ, field.TypeString)
	}
	if value, ok := abu.mutation.A(); ok {
		_spec.SetField(agentbase.FieldA, field.TypeString, value)
	}
	if abu.mutation.ACleared() {
		_spec.ClearField(agentbase.FieldA, field.TypeString)
	}
	if value, ok := abu.mutation.ChunkIndex(); ok {
		_spec.SetField(agentbase.FieldChunkIndex, field.TypeUint64, value)
	}
	if value, ok := abu.mutation.AddedChunkIndex(); ok {
		_spec.AddField(agentbase.FieldChunkIndex, field.TypeUint64, value)
	}
	if value, ok := abu.mutation.Indexes(); ok {
		_spec.SetField(agentbase.FieldIndexes, field.TypeJSON, value)
	}
	if value, ok := abu.mutation.AppendedIndexes(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, agentbase.FieldIndexes, value)
		})
	}
	if abu.mutation.IndexesCleared() {
		_spec.ClearField(agentbase.FieldIndexes, field.TypeJSON)
	}
	if value, ok := abu.mutation.DatasetID(); ok {
		_spec.SetField(agentbase.FieldDatasetID, field.TypeString, value)
	}
	if abu.mutation.DatasetIDCleared() {
		_spec.ClearField(agentbase.FieldDatasetID, field.TypeString)
	}
	if value, ok := abu.mutation.CollectionID(); ok {
		_spec.SetField(agentbase.FieldCollectionID, field.TypeString, value)
	}
	if abu.mutation.CollectionIDCleared() {
		_spec.ClearField(agentbase.FieldCollectionID, field.TypeString)
	}
	if value, ok := abu.mutation.SourceName(); ok {
		_spec.SetField(agentbase.FieldSourceName, field.TypeString, value)
	}
	if abu.mutation.SourceNameCleared() {
		_spec.ClearField(agentbase.FieldSourceName, field.TypeString)
	}
	if value, ok := abu.mutation.CanWrite(); ok {
		_spec.SetField(agentbase.FieldCanWrite, field.TypeJSON, value)
	}
	if value, ok := abu.mutation.AppendedCanWrite(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, agentbase.FieldCanWrite, value)
		})
	}
	if abu.mutation.CanWriteCleared() {
		_spec.ClearField(agentbase.FieldCanWrite, field.TypeJSON)
	}
	if value, ok := abu.mutation.IsOwner(); ok {
		_spec.SetField(agentbase.FieldIsOwner, field.TypeJSON, value)
	}
	if value, ok := abu.mutation.AppendedIsOwner(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, agentbase.FieldIsOwner, value)
		})
	}
	if abu.mutation.IsOwnerCleared() {
		_spec.ClearField(agentbase.FieldIsOwner, field.TypeJSON)
	}
	if abu.mutation.WxAgentCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   agentbase.WxAgentTable,
			Columns: []string{agentbase.WxAgentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(wx.FieldID, field.TypeUint64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := abu.mutation.RemovedWxAgentIDs(); len(nodes) > 0 && !abu.mutation.WxAgentCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   agentbase.WxAgentTable,
			Columns: []string{agentbase.WxAgentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(wx.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 := abu.mutation.WxAgentIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   agentbase.WxAgentTable,
			Columns: []string{agentbase.WxAgentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(wx.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, abu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{agentbase.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	abu.mutation.done = true
	return n, nil
}

// AgentBaseUpdateOne is the builder for updating a single AgentBase entity.
type AgentBaseUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *AgentBaseMutation
}

// SetQ sets the "q" field.
func (abuo *AgentBaseUpdateOne) SetQ(s string) *AgentBaseUpdateOne {
	abuo.mutation.SetQ(s)
	return abuo
}

// SetNillableQ sets the "q" field if the given value is not nil.
func (abuo *AgentBaseUpdateOne) SetNillableQ(s *string) *AgentBaseUpdateOne {
	if s != nil {
		abuo.SetQ(*s)
	}
	return abuo
}

// ClearQ clears the value of the "q" field.
func (abuo *AgentBaseUpdateOne) ClearQ() *AgentBaseUpdateOne {
	abuo.mutation.ClearQ()
	return abuo
}

// SetA sets the "a" field.
func (abuo *AgentBaseUpdateOne) SetA(s string) *AgentBaseUpdateOne {
	abuo.mutation.SetA(s)
	return abuo
}

// SetNillableA sets the "a" field if the given value is not nil.
func (abuo *AgentBaseUpdateOne) SetNillableA(s *string) *AgentBaseUpdateOne {
	if s != nil {
		abuo.SetA(*s)
	}
	return abuo
}

// ClearA clears the value of the "a" field.
func (abuo *AgentBaseUpdateOne) ClearA() *AgentBaseUpdateOne {
	abuo.mutation.ClearA()
	return abuo
}

// SetChunkIndex sets the "chunk_index" field.
func (abuo *AgentBaseUpdateOne) SetChunkIndex(u uint64) *AgentBaseUpdateOne {
	abuo.mutation.ResetChunkIndex()
	abuo.mutation.SetChunkIndex(u)
	return abuo
}

// SetNillableChunkIndex sets the "chunk_index" field if the given value is not nil.
func (abuo *AgentBaseUpdateOne) SetNillableChunkIndex(u *uint64) *AgentBaseUpdateOne {
	if u != nil {
		abuo.SetChunkIndex(*u)
	}
	return abuo
}

// AddChunkIndex adds u to the "chunk_index" field.
func (abuo *AgentBaseUpdateOne) AddChunkIndex(u int64) *AgentBaseUpdateOne {
	abuo.mutation.AddChunkIndex(u)
	return abuo
}

// SetIndexes sets the "indexes" field.
func (abuo *AgentBaseUpdateOne) SetIndexes(s []string) *AgentBaseUpdateOne {
	abuo.mutation.SetIndexes(s)
	return abuo
}

// AppendIndexes appends s to the "indexes" field.
func (abuo *AgentBaseUpdateOne) AppendIndexes(s []string) *AgentBaseUpdateOne {
	abuo.mutation.AppendIndexes(s)
	return abuo
}

// ClearIndexes clears the value of the "indexes" field.
func (abuo *AgentBaseUpdateOne) ClearIndexes() *AgentBaseUpdateOne {
	abuo.mutation.ClearIndexes()
	return abuo
}

// SetDatasetID sets the "dataset_id" field.
func (abuo *AgentBaseUpdateOne) SetDatasetID(s string) *AgentBaseUpdateOne {
	abuo.mutation.SetDatasetID(s)
	return abuo
}

// SetNillableDatasetID sets the "dataset_id" field if the given value is not nil.
func (abuo *AgentBaseUpdateOne) SetNillableDatasetID(s *string) *AgentBaseUpdateOne {
	if s != nil {
		abuo.SetDatasetID(*s)
	}
	return abuo
}

// ClearDatasetID clears the value of the "dataset_id" field.
func (abuo *AgentBaseUpdateOne) ClearDatasetID() *AgentBaseUpdateOne {
	abuo.mutation.ClearDatasetID()
	return abuo
}

// SetCollectionID sets the "collection_id" field.
func (abuo *AgentBaseUpdateOne) SetCollectionID(s string) *AgentBaseUpdateOne {
	abuo.mutation.SetCollectionID(s)
	return abuo
}

// SetNillableCollectionID sets the "collection_id" field if the given value is not nil.
func (abuo *AgentBaseUpdateOne) SetNillableCollectionID(s *string) *AgentBaseUpdateOne {
	if s != nil {
		abuo.SetCollectionID(*s)
	}
	return abuo
}

// ClearCollectionID clears the value of the "collection_id" field.
func (abuo *AgentBaseUpdateOne) ClearCollectionID() *AgentBaseUpdateOne {
	abuo.mutation.ClearCollectionID()
	return abuo
}

// SetSourceName sets the "source_name" field.
func (abuo *AgentBaseUpdateOne) SetSourceName(s string) *AgentBaseUpdateOne {
	abuo.mutation.SetSourceName(s)
	return abuo
}

// SetNillableSourceName sets the "source_name" field if the given value is not nil.
func (abuo *AgentBaseUpdateOne) SetNillableSourceName(s *string) *AgentBaseUpdateOne {
	if s != nil {
		abuo.SetSourceName(*s)
	}
	return abuo
}

// ClearSourceName clears the value of the "source_name" field.
func (abuo *AgentBaseUpdateOne) ClearSourceName() *AgentBaseUpdateOne {
	abuo.mutation.ClearSourceName()
	return abuo
}

// SetCanWrite sets the "can_write" field.
func (abuo *AgentBaseUpdateOne) SetCanWrite(b []bool) *AgentBaseUpdateOne {
	abuo.mutation.SetCanWrite(b)
	return abuo
}

// AppendCanWrite appends b to the "can_write" field.
func (abuo *AgentBaseUpdateOne) AppendCanWrite(b []bool) *AgentBaseUpdateOne {
	abuo.mutation.AppendCanWrite(b)
	return abuo
}

// ClearCanWrite clears the value of the "can_write" field.
func (abuo *AgentBaseUpdateOne) ClearCanWrite() *AgentBaseUpdateOne {
	abuo.mutation.ClearCanWrite()
	return abuo
}

// SetIsOwner sets the "is_owner" field.
func (abuo *AgentBaseUpdateOne) SetIsOwner(b []bool) *AgentBaseUpdateOne {
	abuo.mutation.SetIsOwner(b)
	return abuo
}

// AppendIsOwner appends b to the "is_owner" field.
func (abuo *AgentBaseUpdateOne) AppendIsOwner(b []bool) *AgentBaseUpdateOne {
	abuo.mutation.AppendIsOwner(b)
	return abuo
}

// ClearIsOwner clears the value of the "is_owner" field.
func (abuo *AgentBaseUpdateOne) ClearIsOwner() *AgentBaseUpdateOne {
	abuo.mutation.ClearIsOwner()
	return abuo
}

// AddWxAgentIDs adds the "wx_agent" edge to the Wx entity by IDs.
func (abuo *AgentBaseUpdateOne) AddWxAgentIDs(ids ...uint64) *AgentBaseUpdateOne {
	abuo.mutation.AddWxAgentIDs(ids...)
	return abuo
}

// AddWxAgent adds the "wx_agent" edges to the Wx entity.
func (abuo *AgentBaseUpdateOne) AddWxAgent(w ...*Wx) *AgentBaseUpdateOne {
	ids := make([]uint64, len(w))
	for i := range w {
		ids[i] = w[i].ID
	}
	return abuo.AddWxAgentIDs(ids...)
}

// Mutation returns the AgentBaseMutation object of the builder.
func (abuo *AgentBaseUpdateOne) Mutation() *AgentBaseMutation {
	return abuo.mutation
}

// ClearWxAgent clears all "wx_agent" edges to the Wx entity.
func (abuo *AgentBaseUpdateOne) ClearWxAgent() *AgentBaseUpdateOne {
	abuo.mutation.ClearWxAgent()
	return abuo
}

// RemoveWxAgentIDs removes the "wx_agent" edge to Wx entities by IDs.
func (abuo *AgentBaseUpdateOne) RemoveWxAgentIDs(ids ...uint64) *AgentBaseUpdateOne {
	abuo.mutation.RemoveWxAgentIDs(ids...)
	return abuo
}

// RemoveWxAgent removes "wx_agent" edges to Wx entities.
func (abuo *AgentBaseUpdateOne) RemoveWxAgent(w ...*Wx) *AgentBaseUpdateOne {
	ids := make([]uint64, len(w))
	for i := range w {
		ids[i] = w[i].ID
	}
	return abuo.RemoveWxAgentIDs(ids...)
}

// Where appends a list predicates to the AgentBaseUpdate builder.
func (abuo *AgentBaseUpdateOne) Where(ps ...predicate.AgentBase) *AgentBaseUpdateOne {
	abuo.mutation.Where(ps...)
	return abuo
}

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

// Save executes the query and returns the updated AgentBase entity.
func (abuo *AgentBaseUpdateOne) Save(ctx context.Context) (*AgentBase, error) {
	return withHooks(ctx, abuo.sqlSave, abuo.mutation, abuo.hooks)
}

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

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

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

// check runs all checks and user-defined validators on the builder.
func (abuo *AgentBaseUpdateOne) check() error {
	if v, ok := abuo.mutation.ChunkIndex(); ok {
		if err := agentbase.ChunkIndexValidator(v); err != nil {
			return &ValidationError{Name: "chunk_index", err: fmt.Errorf(`ent: validator failed for field "AgentBase.chunk_index": %w`, err)}
		}
	}
	return nil
}

func (abuo *AgentBaseUpdateOne) sqlSave(ctx context.Context) (_node *AgentBase, err error) {
	if err := abuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(agentbase.Table, agentbase.Columns, sqlgraph.NewFieldSpec(agentbase.FieldID, field.TypeString))
	id, ok := abuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "AgentBase.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := abuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, agentbase.FieldID)
		for _, f := range fields {
			if !agentbase.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != agentbase.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := abuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := abuo.mutation.Q(); ok {
		_spec.SetField(agentbase.FieldQ, field.TypeString, value)
	}
	if abuo.mutation.QCleared() {
		_spec.ClearField(agentbase.FieldQ, field.TypeString)
	}
	if value, ok := abuo.mutation.A(); ok {
		_spec.SetField(agentbase.FieldA, field.TypeString, value)
	}
	if abuo.mutation.ACleared() {
		_spec.ClearField(agentbase.FieldA, field.TypeString)
	}
	if value, ok := abuo.mutation.ChunkIndex(); ok {
		_spec.SetField(agentbase.FieldChunkIndex, field.TypeUint64, value)
	}
	if value, ok := abuo.mutation.AddedChunkIndex(); ok {
		_spec.AddField(agentbase.FieldChunkIndex, field.TypeUint64, value)
	}
	if value, ok := abuo.mutation.Indexes(); ok {
		_spec.SetField(agentbase.FieldIndexes, field.TypeJSON, value)
	}
	if value, ok := abuo.mutation.AppendedIndexes(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, agentbase.FieldIndexes, value)
		})
	}
	if abuo.mutation.IndexesCleared() {
		_spec.ClearField(agentbase.FieldIndexes, field.TypeJSON)
	}
	if value, ok := abuo.mutation.DatasetID(); ok {
		_spec.SetField(agentbase.FieldDatasetID, field.TypeString, value)
	}
	if abuo.mutation.DatasetIDCleared() {
		_spec.ClearField(agentbase.FieldDatasetID, field.TypeString)
	}
	if value, ok := abuo.mutation.CollectionID(); ok {
		_spec.SetField(agentbase.FieldCollectionID, field.TypeString, value)
	}
	if abuo.mutation.CollectionIDCleared() {
		_spec.ClearField(agentbase.FieldCollectionID, field.TypeString)
	}
	if value, ok := abuo.mutation.SourceName(); ok {
		_spec.SetField(agentbase.FieldSourceName, field.TypeString, value)
	}
	if abuo.mutation.SourceNameCleared() {
		_spec.ClearField(agentbase.FieldSourceName, field.TypeString)
	}
	if value, ok := abuo.mutation.CanWrite(); ok {
		_spec.SetField(agentbase.FieldCanWrite, field.TypeJSON, value)
	}
	if value, ok := abuo.mutation.AppendedCanWrite(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, agentbase.FieldCanWrite, value)
		})
	}
	if abuo.mutation.CanWriteCleared() {
		_spec.ClearField(agentbase.FieldCanWrite, field.TypeJSON)
	}
	if value, ok := abuo.mutation.IsOwner(); ok {
		_spec.SetField(agentbase.FieldIsOwner, field.TypeJSON, value)
	}
	if value, ok := abuo.mutation.AppendedIsOwner(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, agentbase.FieldIsOwner, value)
		})
	}
	if abuo.mutation.IsOwnerCleared() {
		_spec.ClearField(agentbase.FieldIsOwner, field.TypeJSON)
	}
	if abuo.mutation.WxAgentCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   agentbase.WxAgentTable,
			Columns: []string{agentbase.WxAgentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(wx.FieldID, field.TypeUint64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := abuo.mutation.RemovedWxAgentIDs(); len(nodes) > 0 && !abuo.mutation.WxAgentCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   agentbase.WxAgentTable,
			Columns: []string{agentbase.WxAgentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(wx.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 := abuo.mutation.WxAgentIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   agentbase.WxAgentTable,
			Columns: []string{agentbase.WxAgentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(wx.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &AgentBase{config: abuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, abuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{agentbase.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	abuo.mutation.done = true
	return _node, nil
}