// Code generated by ent, DO NOT EDIT.

package ent

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

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

// AgentBaseCreate is the builder for creating a AgentBase entity.
type AgentBaseCreate struct {
	config
	mutation *AgentBaseMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

// SetChunkIndex sets the "chunk_index" field.
func (abc *AgentBaseCreate) SetChunkIndex(u uint64) *AgentBaseCreate {
	abc.mutation.SetChunkIndex(u)
	return abc
}

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

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

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

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

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

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

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

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

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

// SetID sets the "id" field.
func (abc *AgentBaseCreate) SetID(s string) *AgentBaseCreate {
	abc.mutation.SetID(s)
	return abc
}

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

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

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

// Save creates the AgentBase in the database.
func (abc *AgentBaseCreate) Save(ctx context.Context) (*AgentBase, error) {
	abc.defaults()
	return withHooks(ctx, abc.sqlSave, abc.mutation, abc.hooks)
}

// SaveX calls Save and panics if Save returns an error.
func (abc *AgentBaseCreate) SaveX(ctx context.Context) *AgentBase {
	v, err := abc.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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

// defaults sets the default values of the builder before save.
func (abc *AgentBaseCreate) defaults() {
	if _, ok := abc.mutation.Q(); !ok {
		v := agentbase.DefaultQ
		abc.mutation.SetQ(v)
	}
	if _, ok := abc.mutation.A(); !ok {
		v := agentbase.DefaultA
		abc.mutation.SetA(v)
	}
	if _, ok := abc.mutation.DatasetID(); !ok {
		v := agentbase.DefaultDatasetID
		abc.mutation.SetDatasetID(v)
	}
	if _, ok := abc.mutation.CollectionID(); !ok {
		v := agentbase.DefaultCollectionID
		abc.mutation.SetCollectionID(v)
	}
	if _, ok := abc.mutation.SourceName(); !ok {
		v := agentbase.DefaultSourceName
		abc.mutation.SetSourceName(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (abc *AgentBaseCreate) check() error {
	if _, ok := abc.mutation.ChunkIndex(); !ok {
		return &ValidationError{Name: "chunk_index", err: errors.New(`ent: missing required field "AgentBase.chunk_index"`)}
	}
	if v, ok := abc.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 (abc *AgentBaseCreate) sqlSave(ctx context.Context) (*AgentBase, error) {
	if err := abc.check(); err != nil {
		return nil, err
	}
	_node, _spec := abc.createSpec()
	if err := sqlgraph.CreateNode(ctx, abc.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	if _spec.ID.Value != nil {
		if id, ok := _spec.ID.Value.(string); ok {
			_node.ID = id
		} else {
			return nil, fmt.Errorf("unexpected AgentBase.ID type: %T", _spec.ID.Value)
		}
	}
	abc.mutation.id = &_node.ID
	abc.mutation.done = true
	return _node, nil
}

func (abc *AgentBaseCreate) createSpec() (*AgentBase, *sqlgraph.CreateSpec) {
	var (
		_node = &AgentBase{config: abc.config}
		_spec = sqlgraph.NewCreateSpec(agentbase.Table, sqlgraph.NewFieldSpec(agentbase.FieldID, field.TypeString))
	)
	_spec.OnConflict = abc.conflict
	if id, ok := abc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := abc.mutation.Q(); ok {
		_spec.SetField(agentbase.FieldQ, field.TypeString, value)
		_node.Q = value
	}
	if value, ok := abc.mutation.A(); ok {
		_spec.SetField(agentbase.FieldA, field.TypeString, value)
		_node.A = value
	}
	if value, ok := abc.mutation.ChunkIndex(); ok {
		_spec.SetField(agentbase.FieldChunkIndex, field.TypeUint64, value)
		_node.ChunkIndex = value
	}
	if value, ok := abc.mutation.Indexes(); ok {
		_spec.SetField(agentbase.FieldIndexes, field.TypeJSON, value)
		_node.Indexes = value
	}
	if value, ok := abc.mutation.DatasetID(); ok {
		_spec.SetField(agentbase.FieldDatasetID, field.TypeString, value)
		_node.DatasetID = value
	}
	if value, ok := abc.mutation.CollectionID(); ok {
		_spec.SetField(agentbase.FieldCollectionID, field.TypeString, value)
		_node.CollectionID = value
	}
	if value, ok := abc.mutation.SourceName(); ok {
		_spec.SetField(agentbase.FieldSourceName, field.TypeString, value)
		_node.SourceName = value
	}
	if value, ok := abc.mutation.CanWrite(); ok {
		_spec.SetField(agentbase.FieldCanWrite, field.TypeJSON, value)
		_node.CanWrite = value
	}
	if value, ok := abc.mutation.IsOwner(); ok {
		_spec.SetField(agentbase.FieldIsOwner, field.TypeJSON, value)
		_node.IsOwner = value
	}
	if nodes := abc.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 = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.AgentBase.Create().
//		SetQ(v).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.AgentBaseUpsert) {
//			SetQ(v+v).
//		}).
//		Exec(ctx)
func (abc *AgentBaseCreate) OnConflict(opts ...sql.ConflictOption) *AgentBaseUpsertOne {
	abc.conflict = opts
	return &AgentBaseUpsertOne{
		create: abc,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.AgentBase.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (abc *AgentBaseCreate) OnConflictColumns(columns ...string) *AgentBaseUpsertOne {
	abc.conflict = append(abc.conflict, sql.ConflictColumns(columns...))
	return &AgentBaseUpsertOne{
		create: abc,
	}
}

type (
	// AgentBaseUpsertOne is the builder for "upsert"-ing
	//  one AgentBase node.
	AgentBaseUpsertOne struct {
		create *AgentBaseCreate
	}

	// AgentBaseUpsert is the "OnConflict" setter.
	AgentBaseUpsert struct {
		*sql.UpdateSet
	}
)

// SetQ sets the "q" field.
func (u *AgentBaseUpsert) SetQ(v string) *AgentBaseUpsert {
	u.Set(agentbase.FieldQ, v)
	return u
}

// UpdateQ sets the "q" field to the value that was provided on create.
func (u *AgentBaseUpsert) UpdateQ() *AgentBaseUpsert {
	u.SetExcluded(agentbase.FieldQ)
	return u
}

// ClearQ clears the value of the "q" field.
func (u *AgentBaseUpsert) ClearQ() *AgentBaseUpsert {
	u.SetNull(agentbase.FieldQ)
	return u
}

// SetA sets the "a" field.
func (u *AgentBaseUpsert) SetA(v string) *AgentBaseUpsert {
	u.Set(agentbase.FieldA, v)
	return u
}

// UpdateA sets the "a" field to the value that was provided on create.
func (u *AgentBaseUpsert) UpdateA() *AgentBaseUpsert {
	u.SetExcluded(agentbase.FieldA)
	return u
}

// ClearA clears the value of the "a" field.
func (u *AgentBaseUpsert) ClearA() *AgentBaseUpsert {
	u.SetNull(agentbase.FieldA)
	return u
}

// SetChunkIndex sets the "chunk_index" field.
func (u *AgentBaseUpsert) SetChunkIndex(v uint64) *AgentBaseUpsert {
	u.Set(agentbase.FieldChunkIndex, v)
	return u
}

// UpdateChunkIndex sets the "chunk_index" field to the value that was provided on create.
func (u *AgentBaseUpsert) UpdateChunkIndex() *AgentBaseUpsert {
	u.SetExcluded(agentbase.FieldChunkIndex)
	return u
}

// AddChunkIndex adds v to the "chunk_index" field.
func (u *AgentBaseUpsert) AddChunkIndex(v uint64) *AgentBaseUpsert {
	u.Add(agentbase.FieldChunkIndex, v)
	return u
}

// SetIndexes sets the "indexes" field.
func (u *AgentBaseUpsert) SetIndexes(v []string) *AgentBaseUpsert {
	u.Set(agentbase.FieldIndexes, v)
	return u
}

// UpdateIndexes sets the "indexes" field to the value that was provided on create.
func (u *AgentBaseUpsert) UpdateIndexes() *AgentBaseUpsert {
	u.SetExcluded(agentbase.FieldIndexes)
	return u
}

// ClearIndexes clears the value of the "indexes" field.
func (u *AgentBaseUpsert) ClearIndexes() *AgentBaseUpsert {
	u.SetNull(agentbase.FieldIndexes)
	return u
}

// SetDatasetID sets the "dataset_id" field.
func (u *AgentBaseUpsert) SetDatasetID(v string) *AgentBaseUpsert {
	u.Set(agentbase.FieldDatasetID, v)
	return u
}

// UpdateDatasetID sets the "dataset_id" field to the value that was provided on create.
func (u *AgentBaseUpsert) UpdateDatasetID() *AgentBaseUpsert {
	u.SetExcluded(agentbase.FieldDatasetID)
	return u
}

// ClearDatasetID clears the value of the "dataset_id" field.
func (u *AgentBaseUpsert) ClearDatasetID() *AgentBaseUpsert {
	u.SetNull(agentbase.FieldDatasetID)
	return u
}

// SetCollectionID sets the "collection_id" field.
func (u *AgentBaseUpsert) SetCollectionID(v string) *AgentBaseUpsert {
	u.Set(agentbase.FieldCollectionID, v)
	return u
}

// UpdateCollectionID sets the "collection_id" field to the value that was provided on create.
func (u *AgentBaseUpsert) UpdateCollectionID() *AgentBaseUpsert {
	u.SetExcluded(agentbase.FieldCollectionID)
	return u
}

// ClearCollectionID clears the value of the "collection_id" field.
func (u *AgentBaseUpsert) ClearCollectionID() *AgentBaseUpsert {
	u.SetNull(agentbase.FieldCollectionID)
	return u
}

// SetSourceName sets the "source_name" field.
func (u *AgentBaseUpsert) SetSourceName(v string) *AgentBaseUpsert {
	u.Set(agentbase.FieldSourceName, v)
	return u
}

// UpdateSourceName sets the "source_name" field to the value that was provided on create.
func (u *AgentBaseUpsert) UpdateSourceName() *AgentBaseUpsert {
	u.SetExcluded(agentbase.FieldSourceName)
	return u
}

// ClearSourceName clears the value of the "source_name" field.
func (u *AgentBaseUpsert) ClearSourceName() *AgentBaseUpsert {
	u.SetNull(agentbase.FieldSourceName)
	return u
}

// SetCanWrite sets the "can_write" field.
func (u *AgentBaseUpsert) SetCanWrite(v []bool) *AgentBaseUpsert {
	u.Set(agentbase.FieldCanWrite, v)
	return u
}

// UpdateCanWrite sets the "can_write" field to the value that was provided on create.
func (u *AgentBaseUpsert) UpdateCanWrite() *AgentBaseUpsert {
	u.SetExcluded(agentbase.FieldCanWrite)
	return u
}

// ClearCanWrite clears the value of the "can_write" field.
func (u *AgentBaseUpsert) ClearCanWrite() *AgentBaseUpsert {
	u.SetNull(agentbase.FieldCanWrite)
	return u
}

// SetIsOwner sets the "is_owner" field.
func (u *AgentBaseUpsert) SetIsOwner(v []bool) *AgentBaseUpsert {
	u.Set(agentbase.FieldIsOwner, v)
	return u
}

// UpdateIsOwner sets the "is_owner" field to the value that was provided on create.
func (u *AgentBaseUpsert) UpdateIsOwner() *AgentBaseUpsert {
	u.SetExcluded(agentbase.FieldIsOwner)
	return u
}

// ClearIsOwner clears the value of the "is_owner" field.
func (u *AgentBaseUpsert) ClearIsOwner() *AgentBaseUpsert {
	u.SetNull(agentbase.FieldIsOwner)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field.
// Using this option is equivalent to using:
//
//	client.AgentBase.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(agentbase.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *AgentBaseUpsertOne) UpdateNewValues() *AgentBaseUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		if _, exists := u.create.mutation.ID(); exists {
			s.SetIgnore(agentbase.FieldID)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.AgentBase.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *AgentBaseUpsertOne) Ignore() *AgentBaseUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *AgentBaseUpsertOne) DoNothing() *AgentBaseUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the AgentBaseCreate.OnConflict
// documentation for more info.
func (u *AgentBaseUpsertOne) Update(set func(*AgentBaseUpsert)) *AgentBaseUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&AgentBaseUpsert{UpdateSet: update})
	}))
	return u
}

// SetQ sets the "q" field.
func (u *AgentBaseUpsertOne) SetQ(v string) *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.SetQ(v)
	})
}

// UpdateQ sets the "q" field to the value that was provided on create.
func (u *AgentBaseUpsertOne) UpdateQ() *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.UpdateQ()
	})
}

// ClearQ clears the value of the "q" field.
func (u *AgentBaseUpsertOne) ClearQ() *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.ClearQ()
	})
}

// SetA sets the "a" field.
func (u *AgentBaseUpsertOne) SetA(v string) *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.SetA(v)
	})
}

// UpdateA sets the "a" field to the value that was provided on create.
func (u *AgentBaseUpsertOne) UpdateA() *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.UpdateA()
	})
}

// ClearA clears the value of the "a" field.
func (u *AgentBaseUpsertOne) ClearA() *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.ClearA()
	})
}

// SetChunkIndex sets the "chunk_index" field.
func (u *AgentBaseUpsertOne) SetChunkIndex(v uint64) *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.SetChunkIndex(v)
	})
}

// AddChunkIndex adds v to the "chunk_index" field.
func (u *AgentBaseUpsertOne) AddChunkIndex(v uint64) *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.AddChunkIndex(v)
	})
}

// UpdateChunkIndex sets the "chunk_index" field to the value that was provided on create.
func (u *AgentBaseUpsertOne) UpdateChunkIndex() *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.UpdateChunkIndex()
	})
}

// SetIndexes sets the "indexes" field.
func (u *AgentBaseUpsertOne) SetIndexes(v []string) *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.SetIndexes(v)
	})
}

// UpdateIndexes sets the "indexes" field to the value that was provided on create.
func (u *AgentBaseUpsertOne) UpdateIndexes() *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.UpdateIndexes()
	})
}

// ClearIndexes clears the value of the "indexes" field.
func (u *AgentBaseUpsertOne) ClearIndexes() *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.ClearIndexes()
	})
}

// SetDatasetID sets the "dataset_id" field.
func (u *AgentBaseUpsertOne) SetDatasetID(v string) *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.SetDatasetID(v)
	})
}

// UpdateDatasetID sets the "dataset_id" field to the value that was provided on create.
func (u *AgentBaseUpsertOne) UpdateDatasetID() *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.UpdateDatasetID()
	})
}

// ClearDatasetID clears the value of the "dataset_id" field.
func (u *AgentBaseUpsertOne) ClearDatasetID() *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.ClearDatasetID()
	})
}

// SetCollectionID sets the "collection_id" field.
func (u *AgentBaseUpsertOne) SetCollectionID(v string) *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.SetCollectionID(v)
	})
}

// UpdateCollectionID sets the "collection_id" field to the value that was provided on create.
func (u *AgentBaseUpsertOne) UpdateCollectionID() *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.UpdateCollectionID()
	})
}

// ClearCollectionID clears the value of the "collection_id" field.
func (u *AgentBaseUpsertOne) ClearCollectionID() *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.ClearCollectionID()
	})
}

// SetSourceName sets the "source_name" field.
func (u *AgentBaseUpsertOne) SetSourceName(v string) *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.SetSourceName(v)
	})
}

// UpdateSourceName sets the "source_name" field to the value that was provided on create.
func (u *AgentBaseUpsertOne) UpdateSourceName() *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.UpdateSourceName()
	})
}

// ClearSourceName clears the value of the "source_name" field.
func (u *AgentBaseUpsertOne) ClearSourceName() *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.ClearSourceName()
	})
}

// SetCanWrite sets the "can_write" field.
func (u *AgentBaseUpsertOne) SetCanWrite(v []bool) *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.SetCanWrite(v)
	})
}

// UpdateCanWrite sets the "can_write" field to the value that was provided on create.
func (u *AgentBaseUpsertOne) UpdateCanWrite() *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.UpdateCanWrite()
	})
}

// ClearCanWrite clears the value of the "can_write" field.
func (u *AgentBaseUpsertOne) ClearCanWrite() *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.ClearCanWrite()
	})
}

// SetIsOwner sets the "is_owner" field.
func (u *AgentBaseUpsertOne) SetIsOwner(v []bool) *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.SetIsOwner(v)
	})
}

// UpdateIsOwner sets the "is_owner" field to the value that was provided on create.
func (u *AgentBaseUpsertOne) UpdateIsOwner() *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.UpdateIsOwner()
	})
}

// ClearIsOwner clears the value of the "is_owner" field.
func (u *AgentBaseUpsertOne) ClearIsOwner() *AgentBaseUpsertOne {
	return u.Update(func(s *AgentBaseUpsert) {
		s.ClearIsOwner()
	})
}

// Exec executes the query.
func (u *AgentBaseUpsertOne) Exec(ctx context.Context) error {
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for AgentBaseCreate.OnConflict")
	}
	return u.create.Exec(ctx)
}

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

// Exec executes the UPSERT query and returns the inserted/updated ID.
func (u *AgentBaseUpsertOne) ID(ctx context.Context) (id string, err error) {
	if u.create.driver.Dialect() == dialect.MySQL {
		// In case of "ON CONFLICT", there is no way to get back non-numeric ID
		// fields from the database since MySQL does not support the RETURNING clause.
		return id, errors.New("ent: AgentBaseUpsertOne.ID is not supported by MySQL driver. Use AgentBaseUpsertOne.Exec instead")
	}
	node, err := u.create.Save(ctx)
	if err != nil {
		return id, err
	}
	return node.ID, nil
}

// IDX is like ID, but panics if an error occurs.
func (u *AgentBaseUpsertOne) IDX(ctx context.Context) string {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

// AgentBaseCreateBulk is the builder for creating many AgentBase entities in bulk.
type AgentBaseCreateBulk struct {
	config
	err      error
	builders []*AgentBaseCreate
	conflict []sql.ConflictOption
}

// Save creates the AgentBase entities in the database.
func (abcb *AgentBaseCreateBulk) Save(ctx context.Context) ([]*AgentBase, error) {
	if abcb.err != nil {
		return nil, abcb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(abcb.builders))
	nodes := make([]*AgentBase, len(abcb.builders))
	mutators := make([]Mutator, len(abcb.builders))
	for i := range abcb.builders {
		func(i int, root context.Context) {
			builder := abcb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*AgentBaseMutation)
				if !ok {
					return nil, fmt.Errorf("unexpected mutation type %T", m)
				}
				if err := builder.check(); err != nil {
					return nil, err
				}
				builder.mutation = mutation
				var err error
				nodes[i], specs[i] = builder.createSpec()
				if i < len(mutators)-1 {
					_, err = mutators[i+1].Mutate(root, abcb.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					spec.OnConflict = abcb.conflict
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, abcb.driver, spec); err != nil {
						if sqlgraph.IsConstraintError(err) {
							err = &ConstraintError{msg: err.Error(), wrap: err}
						}
					}
				}
				if err != nil {
					return nil, err
				}
				mutation.id = &nodes[i].ID
				mutation.done = true
				return nodes[i], nil
			})
			for i := len(builder.hooks) - 1; i >= 0; i-- {
				mut = builder.hooks[i](mut)
			}
			mutators[i] = mut
		}(i, ctx)
	}
	if len(mutators) > 0 {
		if _, err := mutators[0].Mutate(ctx, abcb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

// SaveX is like Save, but panics if an error occurs.
func (abcb *AgentBaseCreateBulk) SaveX(ctx context.Context) []*AgentBase {
	v, err := abcb.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.AgentBase.CreateBulk(builders...).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.AgentBaseUpsert) {
//			SetQ(v+v).
//		}).
//		Exec(ctx)
func (abcb *AgentBaseCreateBulk) OnConflict(opts ...sql.ConflictOption) *AgentBaseUpsertBulk {
	abcb.conflict = opts
	return &AgentBaseUpsertBulk{
		create: abcb,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.AgentBase.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (abcb *AgentBaseCreateBulk) OnConflictColumns(columns ...string) *AgentBaseUpsertBulk {
	abcb.conflict = append(abcb.conflict, sql.ConflictColumns(columns...))
	return &AgentBaseUpsertBulk{
		create: abcb,
	}
}

// AgentBaseUpsertBulk is the builder for "upsert"-ing
// a bulk of AgentBase nodes.
type AgentBaseUpsertBulk struct {
	create *AgentBaseCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.AgentBase.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(agentbase.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *AgentBaseUpsertBulk) UpdateNewValues() *AgentBaseUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		for _, b := range u.create.builders {
			if _, exists := b.mutation.ID(); exists {
				s.SetIgnore(agentbase.FieldID)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.AgentBase.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *AgentBaseUpsertBulk) Ignore() *AgentBaseUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *AgentBaseUpsertBulk) DoNothing() *AgentBaseUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the AgentBaseCreateBulk.OnConflict
// documentation for more info.
func (u *AgentBaseUpsertBulk) Update(set func(*AgentBaseUpsert)) *AgentBaseUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&AgentBaseUpsert{UpdateSet: update})
	}))
	return u
}

// SetQ sets the "q" field.
func (u *AgentBaseUpsertBulk) SetQ(v string) *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.SetQ(v)
	})
}

// UpdateQ sets the "q" field to the value that was provided on create.
func (u *AgentBaseUpsertBulk) UpdateQ() *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.UpdateQ()
	})
}

// ClearQ clears the value of the "q" field.
func (u *AgentBaseUpsertBulk) ClearQ() *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.ClearQ()
	})
}

// SetA sets the "a" field.
func (u *AgentBaseUpsertBulk) SetA(v string) *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.SetA(v)
	})
}

// UpdateA sets the "a" field to the value that was provided on create.
func (u *AgentBaseUpsertBulk) UpdateA() *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.UpdateA()
	})
}

// ClearA clears the value of the "a" field.
func (u *AgentBaseUpsertBulk) ClearA() *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.ClearA()
	})
}

// SetChunkIndex sets the "chunk_index" field.
func (u *AgentBaseUpsertBulk) SetChunkIndex(v uint64) *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.SetChunkIndex(v)
	})
}

// AddChunkIndex adds v to the "chunk_index" field.
func (u *AgentBaseUpsertBulk) AddChunkIndex(v uint64) *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.AddChunkIndex(v)
	})
}

// UpdateChunkIndex sets the "chunk_index" field to the value that was provided on create.
func (u *AgentBaseUpsertBulk) UpdateChunkIndex() *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.UpdateChunkIndex()
	})
}

// SetIndexes sets the "indexes" field.
func (u *AgentBaseUpsertBulk) SetIndexes(v []string) *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.SetIndexes(v)
	})
}

// UpdateIndexes sets the "indexes" field to the value that was provided on create.
func (u *AgentBaseUpsertBulk) UpdateIndexes() *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.UpdateIndexes()
	})
}

// ClearIndexes clears the value of the "indexes" field.
func (u *AgentBaseUpsertBulk) ClearIndexes() *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.ClearIndexes()
	})
}

// SetDatasetID sets the "dataset_id" field.
func (u *AgentBaseUpsertBulk) SetDatasetID(v string) *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.SetDatasetID(v)
	})
}

// UpdateDatasetID sets the "dataset_id" field to the value that was provided on create.
func (u *AgentBaseUpsertBulk) UpdateDatasetID() *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.UpdateDatasetID()
	})
}

// ClearDatasetID clears the value of the "dataset_id" field.
func (u *AgentBaseUpsertBulk) ClearDatasetID() *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.ClearDatasetID()
	})
}

// SetCollectionID sets the "collection_id" field.
func (u *AgentBaseUpsertBulk) SetCollectionID(v string) *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.SetCollectionID(v)
	})
}

// UpdateCollectionID sets the "collection_id" field to the value that was provided on create.
func (u *AgentBaseUpsertBulk) UpdateCollectionID() *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.UpdateCollectionID()
	})
}

// ClearCollectionID clears the value of the "collection_id" field.
func (u *AgentBaseUpsertBulk) ClearCollectionID() *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.ClearCollectionID()
	})
}

// SetSourceName sets the "source_name" field.
func (u *AgentBaseUpsertBulk) SetSourceName(v string) *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.SetSourceName(v)
	})
}

// UpdateSourceName sets the "source_name" field to the value that was provided on create.
func (u *AgentBaseUpsertBulk) UpdateSourceName() *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.UpdateSourceName()
	})
}

// ClearSourceName clears the value of the "source_name" field.
func (u *AgentBaseUpsertBulk) ClearSourceName() *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.ClearSourceName()
	})
}

// SetCanWrite sets the "can_write" field.
func (u *AgentBaseUpsertBulk) SetCanWrite(v []bool) *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.SetCanWrite(v)
	})
}

// UpdateCanWrite sets the "can_write" field to the value that was provided on create.
func (u *AgentBaseUpsertBulk) UpdateCanWrite() *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.UpdateCanWrite()
	})
}

// ClearCanWrite clears the value of the "can_write" field.
func (u *AgentBaseUpsertBulk) ClearCanWrite() *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.ClearCanWrite()
	})
}

// SetIsOwner sets the "is_owner" field.
func (u *AgentBaseUpsertBulk) SetIsOwner(v []bool) *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.SetIsOwner(v)
	})
}

// UpdateIsOwner sets the "is_owner" field to the value that was provided on create.
func (u *AgentBaseUpsertBulk) UpdateIsOwner() *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.UpdateIsOwner()
	})
}

// ClearIsOwner clears the value of the "is_owner" field.
func (u *AgentBaseUpsertBulk) ClearIsOwner() *AgentBaseUpsertBulk {
	return u.Update(func(s *AgentBaseUpsert) {
		s.ClearIsOwner()
	})
}

// Exec executes the query.
func (u *AgentBaseUpsertBulk) Exec(ctx context.Context) error {
	if u.create.err != nil {
		return u.create.err
	}
	for i, b := range u.create.builders {
		if len(b.conflict) != 0 {
			return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the AgentBaseCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for AgentBaseCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

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