// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"
	"wechat-api/ent/employee"
	"wechat-api/ent/tutorial"

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

// TutorialCreate is the builder for creating a Tutorial entity.
type TutorialCreate struct {
	config
	mutation *TutorialMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetCreatedAt sets the "created_at" field.
func (tc *TutorialCreate) SetCreatedAt(t time.Time) *TutorialCreate {
	tc.mutation.SetCreatedAt(t)
	return tc
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (tc *TutorialCreate) SetNillableCreatedAt(t *time.Time) *TutorialCreate {
	if t != nil {
		tc.SetCreatedAt(*t)
	}
	return tc
}

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

// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (tc *TutorialCreate) SetNillableUpdatedAt(t *time.Time) *TutorialCreate {
	if t != nil {
		tc.SetUpdatedAt(*t)
	}
	return tc
}

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

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

// SetEmployeeID sets the "employee_id" field.
func (tc *TutorialCreate) SetEmployeeID(u uint64) *TutorialCreate {
	tc.mutation.SetEmployeeID(u)
	return tc
}

// SetIndex sets the "index" field.
func (tc *TutorialCreate) SetIndex(i int) *TutorialCreate {
	tc.mutation.SetIndex(i)
	return tc
}

// SetTitle sets the "title" field.
func (tc *TutorialCreate) SetTitle(s string) *TutorialCreate {
	tc.mutation.SetTitle(s)
	return tc
}

// SetContent sets the "content" field.
func (tc *TutorialCreate) SetContent(s string) *TutorialCreate {
	tc.mutation.SetContent(s)
	return tc
}

// SetOrganizationID sets the "organization_id" field.
func (tc *TutorialCreate) SetOrganizationID(u uint64) *TutorialCreate {
	tc.mutation.SetOrganizationID(u)
	return tc
}

// SetID sets the "id" field.
func (tc *TutorialCreate) SetID(u uint64) *TutorialCreate {
	tc.mutation.SetID(u)
	return tc
}

// SetEmployee sets the "employee" edge to the Employee entity.
func (tc *TutorialCreate) SetEmployee(e *Employee) *TutorialCreate {
	return tc.SetEmployeeID(e.ID)
}

// Mutation returns the TutorialMutation object of the builder.
func (tc *TutorialCreate) Mutation() *TutorialMutation {
	return tc.mutation
}

// Save creates the Tutorial in the database.
func (tc *TutorialCreate) Save(ctx context.Context) (*Tutorial, error) {
	if err := tc.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, tc.sqlSave, tc.mutation, tc.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (tc *TutorialCreate) defaults() error {
	if _, ok := tc.mutation.CreatedAt(); !ok {
		if tutorial.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized tutorial.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := tutorial.DefaultCreatedAt()
		tc.mutation.SetCreatedAt(v)
	}
	if _, ok := tc.mutation.UpdatedAt(); !ok {
		if tutorial.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized tutorial.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := tutorial.DefaultUpdatedAt()
		tc.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (tc *TutorialCreate) check() error {
	if _, ok := tc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Tutorial.created_at"`)}
	}
	if _, ok := tc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Tutorial.updated_at"`)}
	}
	if _, ok := tc.mutation.EmployeeID(); !ok {
		return &ValidationError{Name: "employee_id", err: errors.New(`ent: missing required field "Tutorial.employee_id"`)}
	}
	if v, ok := tc.mutation.EmployeeID(); ok {
		if err := tutorial.EmployeeIDValidator(v); err != nil {
			return &ValidationError{Name: "employee_id", err: fmt.Errorf(`ent: validator failed for field "Tutorial.employee_id": %w`, err)}
		}
	}
	if _, ok := tc.mutation.Index(); !ok {
		return &ValidationError{Name: "index", err: errors.New(`ent: missing required field "Tutorial.index"`)}
	}
	if _, ok := tc.mutation.Title(); !ok {
		return &ValidationError{Name: "title", err: errors.New(`ent: missing required field "Tutorial.title"`)}
	}
	if _, ok := tc.mutation.Content(); !ok {
		return &ValidationError{Name: "content", err: errors.New(`ent: missing required field "Tutorial.content"`)}
	}
	if _, ok := tc.mutation.OrganizationID(); !ok {
		return &ValidationError{Name: "organization_id", err: errors.New(`ent: missing required field "Tutorial.organization_id"`)}
	}
	if v, ok := tc.mutation.OrganizationID(); ok {
		if err := tutorial.OrganizationIDValidator(v); err != nil {
			return &ValidationError{Name: "organization_id", err: fmt.Errorf(`ent: validator failed for field "Tutorial.organization_id": %w`, err)}
		}
	}
	if _, ok := tc.mutation.EmployeeID(); !ok {
		return &ValidationError{Name: "employee", err: errors.New(`ent: missing required edge "Tutorial.employee"`)}
	}
	return nil
}

func (tc *TutorialCreate) sqlSave(ctx context.Context) (*Tutorial, error) {
	if err := tc.check(); err != nil {
		return nil, err
	}
	_node, _spec := tc.createSpec()
	if err := sqlgraph.CreateNode(ctx, tc.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	if _spec.ID.Value != _node.ID {
		id := _spec.ID.Value.(int64)
		_node.ID = uint64(id)
	}
	tc.mutation.id = &_node.ID
	tc.mutation.done = true
	return _node, nil
}

func (tc *TutorialCreate) createSpec() (*Tutorial, *sqlgraph.CreateSpec) {
	var (
		_node = &Tutorial{config: tc.config}
		_spec = sqlgraph.NewCreateSpec(tutorial.Table, sqlgraph.NewFieldSpec(tutorial.FieldID, field.TypeUint64))
	)
	_spec.OnConflict = tc.conflict
	if id, ok := tc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := tc.mutation.CreatedAt(); ok {
		_spec.SetField(tutorial.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := tc.mutation.UpdatedAt(); ok {
		_spec.SetField(tutorial.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := tc.mutation.DeletedAt(); ok {
		_spec.SetField(tutorial.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = value
	}
	if value, ok := tc.mutation.Index(); ok {
		_spec.SetField(tutorial.FieldIndex, field.TypeInt, value)
		_node.Index = value
	}
	if value, ok := tc.mutation.Title(); ok {
		_spec.SetField(tutorial.FieldTitle, field.TypeString, value)
		_node.Title = value
	}
	if value, ok := tc.mutation.Content(); ok {
		_spec.SetField(tutorial.FieldContent, field.TypeString, value)
		_node.Content = value
	}
	if value, ok := tc.mutation.OrganizationID(); ok {
		_spec.SetField(tutorial.FieldOrganizationID, field.TypeUint64, value)
		_node.OrganizationID = value
	}
	if nodes := tc.mutation.EmployeeIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   tutorial.EmployeeTable,
			Columns: []string{tutorial.EmployeeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(employee.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.EmployeeID = nodes[0]
		_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.Tutorial.Create().
//		SetCreatedAt(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.TutorialUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (tc *TutorialCreate) OnConflict(opts ...sql.ConflictOption) *TutorialUpsertOne {
	tc.conflict = opts
	return &TutorialUpsertOne{
		create: tc,
	}
}

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

type (
	// TutorialUpsertOne is the builder for "upsert"-ing
	//  one Tutorial node.
	TutorialUpsertOne struct {
		create *TutorialCreate
	}

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

// SetUpdatedAt sets the "updated_at" field.
func (u *TutorialUpsert) SetUpdatedAt(v time.Time) *TutorialUpsert {
	u.Set(tutorial.FieldUpdatedAt, v)
	return u
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *TutorialUpsert) UpdateUpdatedAt() *TutorialUpsert {
	u.SetExcluded(tutorial.FieldUpdatedAt)
	return u
}

// SetDeletedAt sets the "deleted_at" field.
func (u *TutorialUpsert) SetDeletedAt(v time.Time) *TutorialUpsert {
	u.Set(tutorial.FieldDeletedAt, v)
	return u
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *TutorialUpsert) UpdateDeletedAt() *TutorialUpsert {
	u.SetExcluded(tutorial.FieldDeletedAt)
	return u
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *TutorialUpsert) ClearDeletedAt() *TutorialUpsert {
	u.SetNull(tutorial.FieldDeletedAt)
	return u
}

// SetEmployeeID sets the "employee_id" field.
func (u *TutorialUpsert) SetEmployeeID(v uint64) *TutorialUpsert {
	u.Set(tutorial.FieldEmployeeID, v)
	return u
}

// UpdateEmployeeID sets the "employee_id" field to the value that was provided on create.
func (u *TutorialUpsert) UpdateEmployeeID() *TutorialUpsert {
	u.SetExcluded(tutorial.FieldEmployeeID)
	return u
}

// SetIndex sets the "index" field.
func (u *TutorialUpsert) SetIndex(v int) *TutorialUpsert {
	u.Set(tutorial.FieldIndex, v)
	return u
}

// UpdateIndex sets the "index" field to the value that was provided on create.
func (u *TutorialUpsert) UpdateIndex() *TutorialUpsert {
	u.SetExcluded(tutorial.FieldIndex)
	return u
}

// AddIndex adds v to the "index" field.
func (u *TutorialUpsert) AddIndex(v int) *TutorialUpsert {
	u.Add(tutorial.FieldIndex, v)
	return u
}

// SetTitle sets the "title" field.
func (u *TutorialUpsert) SetTitle(v string) *TutorialUpsert {
	u.Set(tutorial.FieldTitle, v)
	return u
}

// UpdateTitle sets the "title" field to the value that was provided on create.
func (u *TutorialUpsert) UpdateTitle() *TutorialUpsert {
	u.SetExcluded(tutorial.FieldTitle)
	return u
}

// SetContent sets the "content" field.
func (u *TutorialUpsert) SetContent(v string) *TutorialUpsert {
	u.Set(tutorial.FieldContent, v)
	return u
}

// UpdateContent sets the "content" field to the value that was provided on create.
func (u *TutorialUpsert) UpdateContent() *TutorialUpsert {
	u.SetExcluded(tutorial.FieldContent)
	return u
}

// SetOrganizationID sets the "organization_id" field.
func (u *TutorialUpsert) SetOrganizationID(v uint64) *TutorialUpsert {
	u.Set(tutorial.FieldOrganizationID, v)
	return u
}

// UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
func (u *TutorialUpsert) UpdateOrganizationID() *TutorialUpsert {
	u.SetExcluded(tutorial.FieldOrganizationID)
	return u
}

// AddOrganizationID adds v to the "organization_id" field.
func (u *TutorialUpsert) AddOrganizationID(v uint64) *TutorialUpsert {
	u.Add(tutorial.FieldOrganizationID, v)
	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.Tutorial.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(tutorial.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *TutorialUpsertOne) UpdateNewValues() *TutorialUpsertOne {
	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(tutorial.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(tutorial.FieldCreatedAt)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.Tutorial.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *TutorialUpsertOne) Ignore() *TutorialUpsertOne {
	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 *TutorialUpsertOne) DoNothing() *TutorialUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

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

// SetUpdatedAt sets the "updated_at" field.
func (u *TutorialUpsertOne) SetUpdatedAt(v time.Time) *TutorialUpsertOne {
	return u.Update(func(s *TutorialUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *TutorialUpsertOne) UpdateUpdatedAt() *TutorialUpsertOne {
	return u.Update(func(s *TutorialUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetDeletedAt sets the "deleted_at" field.
func (u *TutorialUpsertOne) SetDeletedAt(v time.Time) *TutorialUpsertOne {
	return u.Update(func(s *TutorialUpsert) {
		s.SetDeletedAt(v)
	})
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *TutorialUpsertOne) UpdateDeletedAt() *TutorialUpsertOne {
	return u.Update(func(s *TutorialUpsert) {
		s.UpdateDeletedAt()
	})
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *TutorialUpsertOne) ClearDeletedAt() *TutorialUpsertOne {
	return u.Update(func(s *TutorialUpsert) {
		s.ClearDeletedAt()
	})
}

// SetEmployeeID sets the "employee_id" field.
func (u *TutorialUpsertOne) SetEmployeeID(v uint64) *TutorialUpsertOne {
	return u.Update(func(s *TutorialUpsert) {
		s.SetEmployeeID(v)
	})
}

// UpdateEmployeeID sets the "employee_id" field to the value that was provided on create.
func (u *TutorialUpsertOne) UpdateEmployeeID() *TutorialUpsertOne {
	return u.Update(func(s *TutorialUpsert) {
		s.UpdateEmployeeID()
	})
}

// SetIndex sets the "index" field.
func (u *TutorialUpsertOne) SetIndex(v int) *TutorialUpsertOne {
	return u.Update(func(s *TutorialUpsert) {
		s.SetIndex(v)
	})
}

// AddIndex adds v to the "index" field.
func (u *TutorialUpsertOne) AddIndex(v int) *TutorialUpsertOne {
	return u.Update(func(s *TutorialUpsert) {
		s.AddIndex(v)
	})
}

// UpdateIndex sets the "index" field to the value that was provided on create.
func (u *TutorialUpsertOne) UpdateIndex() *TutorialUpsertOne {
	return u.Update(func(s *TutorialUpsert) {
		s.UpdateIndex()
	})
}

// SetTitle sets the "title" field.
func (u *TutorialUpsertOne) SetTitle(v string) *TutorialUpsertOne {
	return u.Update(func(s *TutorialUpsert) {
		s.SetTitle(v)
	})
}

// UpdateTitle sets the "title" field to the value that was provided on create.
func (u *TutorialUpsertOne) UpdateTitle() *TutorialUpsertOne {
	return u.Update(func(s *TutorialUpsert) {
		s.UpdateTitle()
	})
}

// SetContent sets the "content" field.
func (u *TutorialUpsertOne) SetContent(v string) *TutorialUpsertOne {
	return u.Update(func(s *TutorialUpsert) {
		s.SetContent(v)
	})
}

// UpdateContent sets the "content" field to the value that was provided on create.
func (u *TutorialUpsertOne) UpdateContent() *TutorialUpsertOne {
	return u.Update(func(s *TutorialUpsert) {
		s.UpdateContent()
	})
}

// SetOrganizationID sets the "organization_id" field.
func (u *TutorialUpsertOne) SetOrganizationID(v uint64) *TutorialUpsertOne {
	return u.Update(func(s *TutorialUpsert) {
		s.SetOrganizationID(v)
	})
}

// AddOrganizationID adds v to the "organization_id" field.
func (u *TutorialUpsertOne) AddOrganizationID(v uint64) *TutorialUpsertOne {
	return u.Update(func(s *TutorialUpsert) {
		s.AddOrganizationID(v)
	})
}

// UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
func (u *TutorialUpsertOne) UpdateOrganizationID() *TutorialUpsertOne {
	return u.Update(func(s *TutorialUpsert) {
		s.UpdateOrganizationID()
	})
}

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

// ExecX is like Exec, but panics if an error occurs.
func (u *TutorialUpsertOne) 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 *TutorialUpsertOne) ID(ctx context.Context) (id uint64, err error) {
	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 *TutorialUpsertOne) IDX(ctx context.Context) uint64 {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

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

// Save creates the Tutorial entities in the database.
func (tcb *TutorialCreateBulk) Save(ctx context.Context) ([]*Tutorial, error) {
	if tcb.err != nil {
		return nil, tcb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(tcb.builders))
	nodes := make([]*Tutorial, len(tcb.builders))
	mutators := make([]Mutator, len(tcb.builders))
	for i := range tcb.builders {
		func(i int, root context.Context) {
			builder := tcb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*TutorialMutation)
				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, tcb.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					spec.OnConflict = tcb.conflict
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, tcb.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
				if specs[i].ID.Value != nil && nodes[i].ID == 0 {
					id := specs[i].ID.Value.(int64)
					nodes[i].ID = uint64(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, tcb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

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

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.Tutorial.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.TutorialUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (tcb *TutorialCreateBulk) OnConflict(opts ...sql.ConflictOption) *TutorialUpsertBulk {
	tcb.conflict = opts
	return &TutorialUpsertBulk{
		create: tcb,
	}
}

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

// TutorialUpsertBulk is the builder for "upsert"-ing
// a bulk of Tutorial nodes.
type TutorialUpsertBulk struct {
	create *TutorialCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.Tutorial.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(tutorial.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *TutorialUpsertBulk) UpdateNewValues() *TutorialUpsertBulk {
	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(tutorial.FieldID)
			}
			if _, exists := b.mutation.CreatedAt(); exists {
				s.SetIgnore(tutorial.FieldCreatedAt)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.Tutorial.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *TutorialUpsertBulk) Ignore() *TutorialUpsertBulk {
	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 *TutorialUpsertBulk) DoNothing() *TutorialUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

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

// SetUpdatedAt sets the "updated_at" field.
func (u *TutorialUpsertBulk) SetUpdatedAt(v time.Time) *TutorialUpsertBulk {
	return u.Update(func(s *TutorialUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *TutorialUpsertBulk) UpdateUpdatedAt() *TutorialUpsertBulk {
	return u.Update(func(s *TutorialUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetDeletedAt sets the "deleted_at" field.
func (u *TutorialUpsertBulk) SetDeletedAt(v time.Time) *TutorialUpsertBulk {
	return u.Update(func(s *TutorialUpsert) {
		s.SetDeletedAt(v)
	})
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *TutorialUpsertBulk) UpdateDeletedAt() *TutorialUpsertBulk {
	return u.Update(func(s *TutorialUpsert) {
		s.UpdateDeletedAt()
	})
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *TutorialUpsertBulk) ClearDeletedAt() *TutorialUpsertBulk {
	return u.Update(func(s *TutorialUpsert) {
		s.ClearDeletedAt()
	})
}

// SetEmployeeID sets the "employee_id" field.
func (u *TutorialUpsertBulk) SetEmployeeID(v uint64) *TutorialUpsertBulk {
	return u.Update(func(s *TutorialUpsert) {
		s.SetEmployeeID(v)
	})
}

// UpdateEmployeeID sets the "employee_id" field to the value that was provided on create.
func (u *TutorialUpsertBulk) UpdateEmployeeID() *TutorialUpsertBulk {
	return u.Update(func(s *TutorialUpsert) {
		s.UpdateEmployeeID()
	})
}

// SetIndex sets the "index" field.
func (u *TutorialUpsertBulk) SetIndex(v int) *TutorialUpsertBulk {
	return u.Update(func(s *TutorialUpsert) {
		s.SetIndex(v)
	})
}

// AddIndex adds v to the "index" field.
func (u *TutorialUpsertBulk) AddIndex(v int) *TutorialUpsertBulk {
	return u.Update(func(s *TutorialUpsert) {
		s.AddIndex(v)
	})
}

// UpdateIndex sets the "index" field to the value that was provided on create.
func (u *TutorialUpsertBulk) UpdateIndex() *TutorialUpsertBulk {
	return u.Update(func(s *TutorialUpsert) {
		s.UpdateIndex()
	})
}

// SetTitle sets the "title" field.
func (u *TutorialUpsertBulk) SetTitle(v string) *TutorialUpsertBulk {
	return u.Update(func(s *TutorialUpsert) {
		s.SetTitle(v)
	})
}

// UpdateTitle sets the "title" field to the value that was provided on create.
func (u *TutorialUpsertBulk) UpdateTitle() *TutorialUpsertBulk {
	return u.Update(func(s *TutorialUpsert) {
		s.UpdateTitle()
	})
}

// SetContent sets the "content" field.
func (u *TutorialUpsertBulk) SetContent(v string) *TutorialUpsertBulk {
	return u.Update(func(s *TutorialUpsert) {
		s.SetContent(v)
	})
}

// UpdateContent sets the "content" field to the value that was provided on create.
func (u *TutorialUpsertBulk) UpdateContent() *TutorialUpsertBulk {
	return u.Update(func(s *TutorialUpsert) {
		s.UpdateContent()
	})
}

// SetOrganizationID sets the "organization_id" field.
func (u *TutorialUpsertBulk) SetOrganizationID(v uint64) *TutorialUpsertBulk {
	return u.Update(func(s *TutorialUpsert) {
		s.SetOrganizationID(v)
	})
}

// AddOrganizationID adds v to the "organization_id" field.
func (u *TutorialUpsertBulk) AddOrganizationID(v uint64) *TutorialUpsertBulk {
	return u.Update(func(s *TutorialUpsert) {
		s.AddOrganizationID(v)
	})
}

// UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
func (u *TutorialUpsertBulk) UpdateOrganizationID() *TutorialUpsertBulk {
	return u.Update(func(s *TutorialUpsert) {
		s.UpdateOrganizationID()
	})
}

// Exec executes the query.
func (u *TutorialUpsertBulk) 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 TutorialCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for TutorialCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

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