// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/anzhiyu-c/anheyu-app/ent/link"
	"github.com/anzhiyu-c/anheyu-app/ent/linkcategory"
	"github.com/anzhiyu-c/anheyu-app/ent/linktag"
)

// LinkCreate is the builder for creating a Link entity.
type LinkCreate struct {
	config
	mutation *LinkMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetName sets the "name" field.
func (_c *LinkCreate) SetName(v string) *LinkCreate {
	_c.mutation.SetName(v)
	return _c
}

// SetURL sets the "url" field.
func (_c *LinkCreate) SetURL(v string) *LinkCreate {
	_c.mutation.SetURL(v)
	return _c
}

// SetLogo sets the "logo" field.
func (_c *LinkCreate) SetLogo(v string) *LinkCreate {
	_c.mutation.SetLogo(v)
	return _c
}

// SetNillableLogo sets the "logo" field if the given value is not nil.
func (_c *LinkCreate) SetNillableLogo(v *string) *LinkCreate {
	if v != nil {
		_c.SetLogo(*v)
	}
	return _c
}

// SetDescription sets the "description" field.
func (_c *LinkCreate) SetDescription(v string) *LinkCreate {
	_c.mutation.SetDescription(v)
	return _c
}

// SetNillableDescription sets the "description" field if the given value is not nil.
func (_c *LinkCreate) SetNillableDescription(v *string) *LinkCreate {
	if v != nil {
		_c.SetDescription(*v)
	}
	return _c
}

// SetStatus sets the "status" field.
func (_c *LinkCreate) SetStatus(v link.Status) *LinkCreate {
	_c.mutation.SetStatus(v)
	return _c
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (_c *LinkCreate) SetNillableStatus(v *link.Status) *LinkCreate {
	if v != nil {
		_c.SetStatus(*v)
	}
	return _c
}

// SetSiteshot sets the "siteshot" field.
func (_c *LinkCreate) SetSiteshot(v string) *LinkCreate {
	_c.mutation.SetSiteshot(v)
	return _c
}

// SetNillableSiteshot sets the "siteshot" field if the given value is not nil.
func (_c *LinkCreate) SetNillableSiteshot(v *string) *LinkCreate {
	if v != nil {
		_c.SetSiteshot(*v)
	}
	return _c
}

// SetSortOrder sets the "sort_order" field.
func (_c *LinkCreate) SetSortOrder(v int) *LinkCreate {
	_c.mutation.SetSortOrder(v)
	return _c
}

// SetNillableSortOrder sets the "sort_order" field if the given value is not nil.
func (_c *LinkCreate) SetNillableSortOrder(v *int) *LinkCreate {
	if v != nil {
		_c.SetSortOrder(*v)
	}
	return _c
}

// SetSkipHealthCheck sets the "skip_health_check" field.
func (_c *LinkCreate) SetSkipHealthCheck(v bool) *LinkCreate {
	_c.mutation.SetSkipHealthCheck(v)
	return _c
}

// SetNillableSkipHealthCheck sets the "skip_health_check" field if the given value is not nil.
func (_c *LinkCreate) SetNillableSkipHealthCheck(v *bool) *LinkCreate {
	if v != nil {
		_c.SetSkipHealthCheck(*v)
	}
	return _c
}

// SetCategoryID sets the "category" edge to the LinkCategory entity by ID.
func (_c *LinkCreate) SetCategoryID(id int) *LinkCreate {
	_c.mutation.SetCategoryID(id)
	return _c
}

// SetCategory sets the "category" edge to the LinkCategory entity.
func (_c *LinkCreate) SetCategory(v *LinkCategory) *LinkCreate {
	return _c.SetCategoryID(v.ID)
}

// AddTagIDs adds the "tags" edge to the LinkTag entity by IDs.
func (_c *LinkCreate) AddTagIDs(ids ...int) *LinkCreate {
	_c.mutation.AddTagIDs(ids...)
	return _c
}

// AddTags adds the "tags" edges to the LinkTag entity.
func (_c *LinkCreate) AddTags(v ...*LinkTag) *LinkCreate {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _c.AddTagIDs(ids...)
}

// Mutation returns the LinkMutation object of the builder.
func (_c *LinkCreate) Mutation() *LinkMutation {
	return _c.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (_c *LinkCreate) defaults() {
	if _, ok := _c.mutation.Status(); !ok {
		v := link.DefaultStatus
		_c.mutation.SetStatus(v)
	}
	if _, ok := _c.mutation.SortOrder(); !ok {
		v := link.DefaultSortOrder
		_c.mutation.SetSortOrder(v)
	}
	if _, ok := _c.mutation.SkipHealthCheck(); !ok {
		v := link.DefaultSkipHealthCheck
		_c.mutation.SetSkipHealthCheck(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *LinkCreate) check() error {
	if _, ok := _c.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Link.name"`)}
	}
	if v, ok := _c.mutation.Name(); ok {
		if err := link.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Link.name": %w`, err)}
		}
	}
	if _, ok := _c.mutation.URL(); !ok {
		return &ValidationError{Name: "url", err: errors.New(`ent: missing required field "Link.url"`)}
	}
	if v, ok := _c.mutation.URL(); ok {
		if err := link.URLValidator(v); err != nil {
			return &ValidationError{Name: "url", err: fmt.Errorf(`ent: validator failed for field "Link.url": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "Link.status"`)}
	}
	if v, ok := _c.mutation.Status(); ok {
		if err := link.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Link.status": %w`, err)}
		}
	}
	if _, ok := _c.mutation.SortOrder(); !ok {
		return &ValidationError{Name: "sort_order", err: errors.New(`ent: missing required field "Link.sort_order"`)}
	}
	if _, ok := _c.mutation.SkipHealthCheck(); !ok {
		return &ValidationError{Name: "skip_health_check", err: errors.New(`ent: missing required field "Link.skip_health_check"`)}
	}
	if len(_c.mutation.CategoryIDs()) == 0 {
		return &ValidationError{Name: "category", err: errors.New(`ent: missing required edge "Link.category"`)}
	}
	return nil
}

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

func (_c *LinkCreate) createSpec() (*Link, *sqlgraph.CreateSpec) {
	var (
		_node = &Link{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(link.Table, sqlgraph.NewFieldSpec(link.FieldID, field.TypeInt))
	)
	_spec.OnConflict = _c.conflict
	if value, ok := _c.mutation.Name(); ok {
		_spec.SetField(link.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := _c.mutation.URL(); ok {
		_spec.SetField(link.FieldURL, field.TypeString, value)
		_node.URL = value
	}
	if value, ok := _c.mutation.Logo(); ok {
		_spec.SetField(link.FieldLogo, field.TypeString, value)
		_node.Logo = value
	}
	if value, ok := _c.mutation.Description(); ok {
		_spec.SetField(link.FieldDescription, field.TypeString, value)
		_node.Description = value
	}
	if value, ok := _c.mutation.Status(); ok {
		_spec.SetField(link.FieldStatus, field.TypeEnum, value)
		_node.Status = value
	}
	if value, ok := _c.mutation.Siteshot(); ok {
		_spec.SetField(link.FieldSiteshot, field.TypeString, value)
		_node.Siteshot = value
	}
	if value, ok := _c.mutation.SortOrder(); ok {
		_spec.SetField(link.FieldSortOrder, field.TypeInt, value)
		_node.SortOrder = value
	}
	if value, ok := _c.mutation.SkipHealthCheck(); ok {
		_spec.SetField(link.FieldSkipHealthCheck, field.TypeBool, value)
		_node.SkipHealthCheck = value
	}
	if nodes := _c.mutation.CategoryIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   link.CategoryTable,
			Columns: []string{link.CategoryColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(linkcategory.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.link_category_links = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := _c.mutation.TagsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: false,
			Table:   link.TagsTable,
			Columns: link.TagsPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(linktag.FieldID, field.TypeInt),
			},
		}
		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.Link.Create().
//		SetName(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.LinkUpsert) {
//			SetName(v+v).
//		}).
//		Exec(ctx)
func (_c *LinkCreate) OnConflict(opts ...sql.ConflictOption) *LinkUpsertOne {
	_c.conflict = opts
	return &LinkUpsertOne{
		create: _c,
	}
}

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

type (
	// LinkUpsertOne is the builder for "upsert"-ing
	//  one Link node.
	LinkUpsertOne struct {
		create *LinkCreate
	}

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

// SetName sets the "name" field.
func (u *LinkUpsert) SetName(v string) *LinkUpsert {
	u.Set(link.FieldName, v)
	return u
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *LinkUpsert) UpdateName() *LinkUpsert {
	u.SetExcluded(link.FieldName)
	return u
}

// SetURL sets the "url" field.
func (u *LinkUpsert) SetURL(v string) *LinkUpsert {
	u.Set(link.FieldURL, v)
	return u
}

// UpdateURL sets the "url" field to the value that was provided on create.
func (u *LinkUpsert) UpdateURL() *LinkUpsert {
	u.SetExcluded(link.FieldURL)
	return u
}

// SetLogo sets the "logo" field.
func (u *LinkUpsert) SetLogo(v string) *LinkUpsert {
	u.Set(link.FieldLogo, v)
	return u
}

// UpdateLogo sets the "logo" field to the value that was provided on create.
func (u *LinkUpsert) UpdateLogo() *LinkUpsert {
	u.SetExcluded(link.FieldLogo)
	return u
}

// ClearLogo clears the value of the "logo" field.
func (u *LinkUpsert) ClearLogo() *LinkUpsert {
	u.SetNull(link.FieldLogo)
	return u
}

// SetDescription sets the "description" field.
func (u *LinkUpsert) SetDescription(v string) *LinkUpsert {
	u.Set(link.FieldDescription, v)
	return u
}

// UpdateDescription sets the "description" field to the value that was provided on create.
func (u *LinkUpsert) UpdateDescription() *LinkUpsert {
	u.SetExcluded(link.FieldDescription)
	return u
}

// ClearDescription clears the value of the "description" field.
func (u *LinkUpsert) ClearDescription() *LinkUpsert {
	u.SetNull(link.FieldDescription)
	return u
}

// SetStatus sets the "status" field.
func (u *LinkUpsert) SetStatus(v link.Status) *LinkUpsert {
	u.Set(link.FieldStatus, v)
	return u
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *LinkUpsert) UpdateStatus() *LinkUpsert {
	u.SetExcluded(link.FieldStatus)
	return u
}

// SetSiteshot sets the "siteshot" field.
func (u *LinkUpsert) SetSiteshot(v string) *LinkUpsert {
	u.Set(link.FieldSiteshot, v)
	return u
}

// UpdateSiteshot sets the "siteshot" field to the value that was provided on create.
func (u *LinkUpsert) UpdateSiteshot() *LinkUpsert {
	u.SetExcluded(link.FieldSiteshot)
	return u
}

// ClearSiteshot clears the value of the "siteshot" field.
func (u *LinkUpsert) ClearSiteshot() *LinkUpsert {
	u.SetNull(link.FieldSiteshot)
	return u
}

// SetSortOrder sets the "sort_order" field.
func (u *LinkUpsert) SetSortOrder(v int) *LinkUpsert {
	u.Set(link.FieldSortOrder, v)
	return u
}

// UpdateSortOrder sets the "sort_order" field to the value that was provided on create.
func (u *LinkUpsert) UpdateSortOrder() *LinkUpsert {
	u.SetExcluded(link.FieldSortOrder)
	return u
}

// AddSortOrder adds v to the "sort_order" field.
func (u *LinkUpsert) AddSortOrder(v int) *LinkUpsert {
	u.Add(link.FieldSortOrder, v)
	return u
}

// SetSkipHealthCheck sets the "skip_health_check" field.
func (u *LinkUpsert) SetSkipHealthCheck(v bool) *LinkUpsert {
	u.Set(link.FieldSkipHealthCheck, v)
	return u
}

// UpdateSkipHealthCheck sets the "skip_health_check" field to the value that was provided on create.
func (u *LinkUpsert) UpdateSkipHealthCheck() *LinkUpsert {
	u.SetExcluded(link.FieldSkipHealthCheck)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create.
// Using this option is equivalent to using:
//
//	client.Link.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *LinkUpsertOne) UpdateNewValues() *LinkUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	return u
}

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

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

// SetName sets the "name" field.
func (u *LinkUpsertOne) SetName(v string) *LinkUpsertOne {
	return u.Update(func(s *LinkUpsert) {
		s.SetName(v)
	})
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *LinkUpsertOne) UpdateName() *LinkUpsertOne {
	return u.Update(func(s *LinkUpsert) {
		s.UpdateName()
	})
}

// SetURL sets the "url" field.
func (u *LinkUpsertOne) SetURL(v string) *LinkUpsertOne {
	return u.Update(func(s *LinkUpsert) {
		s.SetURL(v)
	})
}

// UpdateURL sets the "url" field to the value that was provided on create.
func (u *LinkUpsertOne) UpdateURL() *LinkUpsertOne {
	return u.Update(func(s *LinkUpsert) {
		s.UpdateURL()
	})
}

// SetLogo sets the "logo" field.
func (u *LinkUpsertOne) SetLogo(v string) *LinkUpsertOne {
	return u.Update(func(s *LinkUpsert) {
		s.SetLogo(v)
	})
}

// UpdateLogo sets the "logo" field to the value that was provided on create.
func (u *LinkUpsertOne) UpdateLogo() *LinkUpsertOne {
	return u.Update(func(s *LinkUpsert) {
		s.UpdateLogo()
	})
}

// ClearLogo clears the value of the "logo" field.
func (u *LinkUpsertOne) ClearLogo() *LinkUpsertOne {
	return u.Update(func(s *LinkUpsert) {
		s.ClearLogo()
	})
}

// SetDescription sets the "description" field.
func (u *LinkUpsertOne) SetDescription(v string) *LinkUpsertOne {
	return u.Update(func(s *LinkUpsert) {
		s.SetDescription(v)
	})
}

// UpdateDescription sets the "description" field to the value that was provided on create.
func (u *LinkUpsertOne) UpdateDescription() *LinkUpsertOne {
	return u.Update(func(s *LinkUpsert) {
		s.UpdateDescription()
	})
}

// ClearDescription clears the value of the "description" field.
func (u *LinkUpsertOne) ClearDescription() *LinkUpsertOne {
	return u.Update(func(s *LinkUpsert) {
		s.ClearDescription()
	})
}

// SetStatus sets the "status" field.
func (u *LinkUpsertOne) SetStatus(v link.Status) *LinkUpsertOne {
	return u.Update(func(s *LinkUpsert) {
		s.SetStatus(v)
	})
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *LinkUpsertOne) UpdateStatus() *LinkUpsertOne {
	return u.Update(func(s *LinkUpsert) {
		s.UpdateStatus()
	})
}

// SetSiteshot sets the "siteshot" field.
func (u *LinkUpsertOne) SetSiteshot(v string) *LinkUpsertOne {
	return u.Update(func(s *LinkUpsert) {
		s.SetSiteshot(v)
	})
}

// UpdateSiteshot sets the "siteshot" field to the value that was provided on create.
func (u *LinkUpsertOne) UpdateSiteshot() *LinkUpsertOne {
	return u.Update(func(s *LinkUpsert) {
		s.UpdateSiteshot()
	})
}

// ClearSiteshot clears the value of the "siteshot" field.
func (u *LinkUpsertOne) ClearSiteshot() *LinkUpsertOne {
	return u.Update(func(s *LinkUpsert) {
		s.ClearSiteshot()
	})
}

// SetSortOrder sets the "sort_order" field.
func (u *LinkUpsertOne) SetSortOrder(v int) *LinkUpsertOne {
	return u.Update(func(s *LinkUpsert) {
		s.SetSortOrder(v)
	})
}

// AddSortOrder adds v to the "sort_order" field.
func (u *LinkUpsertOne) AddSortOrder(v int) *LinkUpsertOne {
	return u.Update(func(s *LinkUpsert) {
		s.AddSortOrder(v)
	})
}

// UpdateSortOrder sets the "sort_order" field to the value that was provided on create.
func (u *LinkUpsertOne) UpdateSortOrder() *LinkUpsertOne {
	return u.Update(func(s *LinkUpsert) {
		s.UpdateSortOrder()
	})
}

// SetSkipHealthCheck sets the "skip_health_check" field.
func (u *LinkUpsertOne) SetSkipHealthCheck(v bool) *LinkUpsertOne {
	return u.Update(func(s *LinkUpsert) {
		s.SetSkipHealthCheck(v)
	})
}

// UpdateSkipHealthCheck sets the "skip_health_check" field to the value that was provided on create.
func (u *LinkUpsertOne) UpdateSkipHealthCheck() *LinkUpsertOne {
	return u.Update(func(s *LinkUpsert) {
		s.UpdateSkipHealthCheck()
	})
}

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

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

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

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

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

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.Link.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.LinkUpsert) {
//			SetName(v+v).
//		}).
//		Exec(ctx)
func (_c *LinkCreateBulk) OnConflict(opts ...sql.ConflictOption) *LinkUpsertBulk {
	_c.conflict = opts
	return &LinkUpsertBulk{
		create: _c,
	}
}

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

// LinkUpsertBulk is the builder for "upsert"-ing
// a bulk of Link nodes.
type LinkUpsertBulk struct {
	create *LinkCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.Link.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *LinkUpsertBulk) UpdateNewValues() *LinkUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	return u
}

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

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

// SetName sets the "name" field.
func (u *LinkUpsertBulk) SetName(v string) *LinkUpsertBulk {
	return u.Update(func(s *LinkUpsert) {
		s.SetName(v)
	})
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *LinkUpsertBulk) UpdateName() *LinkUpsertBulk {
	return u.Update(func(s *LinkUpsert) {
		s.UpdateName()
	})
}

// SetURL sets the "url" field.
func (u *LinkUpsertBulk) SetURL(v string) *LinkUpsertBulk {
	return u.Update(func(s *LinkUpsert) {
		s.SetURL(v)
	})
}

// UpdateURL sets the "url" field to the value that was provided on create.
func (u *LinkUpsertBulk) UpdateURL() *LinkUpsertBulk {
	return u.Update(func(s *LinkUpsert) {
		s.UpdateURL()
	})
}

// SetLogo sets the "logo" field.
func (u *LinkUpsertBulk) SetLogo(v string) *LinkUpsertBulk {
	return u.Update(func(s *LinkUpsert) {
		s.SetLogo(v)
	})
}

// UpdateLogo sets the "logo" field to the value that was provided on create.
func (u *LinkUpsertBulk) UpdateLogo() *LinkUpsertBulk {
	return u.Update(func(s *LinkUpsert) {
		s.UpdateLogo()
	})
}

// ClearLogo clears the value of the "logo" field.
func (u *LinkUpsertBulk) ClearLogo() *LinkUpsertBulk {
	return u.Update(func(s *LinkUpsert) {
		s.ClearLogo()
	})
}

// SetDescription sets the "description" field.
func (u *LinkUpsertBulk) SetDescription(v string) *LinkUpsertBulk {
	return u.Update(func(s *LinkUpsert) {
		s.SetDescription(v)
	})
}

// UpdateDescription sets the "description" field to the value that was provided on create.
func (u *LinkUpsertBulk) UpdateDescription() *LinkUpsertBulk {
	return u.Update(func(s *LinkUpsert) {
		s.UpdateDescription()
	})
}

// ClearDescription clears the value of the "description" field.
func (u *LinkUpsertBulk) ClearDescription() *LinkUpsertBulk {
	return u.Update(func(s *LinkUpsert) {
		s.ClearDescription()
	})
}

// SetStatus sets the "status" field.
func (u *LinkUpsertBulk) SetStatus(v link.Status) *LinkUpsertBulk {
	return u.Update(func(s *LinkUpsert) {
		s.SetStatus(v)
	})
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *LinkUpsertBulk) UpdateStatus() *LinkUpsertBulk {
	return u.Update(func(s *LinkUpsert) {
		s.UpdateStatus()
	})
}

// SetSiteshot sets the "siteshot" field.
func (u *LinkUpsertBulk) SetSiteshot(v string) *LinkUpsertBulk {
	return u.Update(func(s *LinkUpsert) {
		s.SetSiteshot(v)
	})
}

// UpdateSiteshot sets the "siteshot" field to the value that was provided on create.
func (u *LinkUpsertBulk) UpdateSiteshot() *LinkUpsertBulk {
	return u.Update(func(s *LinkUpsert) {
		s.UpdateSiteshot()
	})
}

// ClearSiteshot clears the value of the "siteshot" field.
func (u *LinkUpsertBulk) ClearSiteshot() *LinkUpsertBulk {
	return u.Update(func(s *LinkUpsert) {
		s.ClearSiteshot()
	})
}

// SetSortOrder sets the "sort_order" field.
func (u *LinkUpsertBulk) SetSortOrder(v int) *LinkUpsertBulk {
	return u.Update(func(s *LinkUpsert) {
		s.SetSortOrder(v)
	})
}

// AddSortOrder adds v to the "sort_order" field.
func (u *LinkUpsertBulk) AddSortOrder(v int) *LinkUpsertBulk {
	return u.Update(func(s *LinkUpsert) {
		s.AddSortOrder(v)
	})
}

// UpdateSortOrder sets the "sort_order" field to the value that was provided on create.
func (u *LinkUpsertBulk) UpdateSortOrder() *LinkUpsertBulk {
	return u.Update(func(s *LinkUpsert) {
		s.UpdateSortOrder()
	})
}

// SetSkipHealthCheck sets the "skip_health_check" field.
func (u *LinkUpsertBulk) SetSkipHealthCheck(v bool) *LinkUpsertBulk {
	return u.Update(func(s *LinkUpsert) {
		s.SetSkipHealthCheck(v)
	})
}

// UpdateSkipHealthCheck sets the "skip_health_check" field to the value that was provided on create.
func (u *LinkUpsertBulk) UpdateSkipHealthCheck() *LinkUpsertBulk {
	return u.Update(func(s *LinkUpsert) {
		s.UpdateSkipHealthCheck()
	})
}

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

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