// Code generated by entc, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

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

	"github.com/pingcap/test-infra/common/model"
	"github.com/pingcap/test-infra/services/tcms/service/store/v2/ent/resourceduration"
)

// ResourceDurationCreate is the builder for creating a ResourceDuration entity.
type ResourceDurationCreate struct {
	config
	mutation *ResourceDurationMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetCreatedAt sets the "createdAt" field.
func (rdc *ResourceDurationCreate) SetCreatedAt(t time.Time) *ResourceDurationCreate {
	rdc.mutation.SetCreatedAt(t)
	return rdc
}

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

// SetUpdatedAt sets the "updatedAt" field.
func (rdc *ResourceDurationCreate) SetUpdatedAt(t time.Time) *ResourceDurationCreate {
	rdc.mutation.SetUpdatedAt(t)
	return rdc
}

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

// SetPlanExecutionID sets the "planExecutionID" field.
func (rdc *ResourceDurationCreate) SetPlanExecutionID(m model.ID) *ResourceDurationCreate {
	rdc.mutation.SetPlanExecutionID(m)
	return rdc
}

// SetType sets the "type" field.
func (rdc *ResourceDurationCreate) SetType(mdt model.ResourceDurationType) *ResourceDurationCreate {
	rdc.mutation.SetType(mdt)
	return rdc
}

// SetNillableType sets the "type" field if the given value is not nil.
func (rdc *ResourceDurationCreate) SetNillableType(mdt *model.ResourceDurationType) *ResourceDurationCreate {
	if mdt != nil {
		rdc.SetType(*mdt)
	}
	return rdc
}

// SetCpuDuration sets the "cpuDuration" field.
func (rdc *ResourceDurationCreate) SetCpuDuration(u uint64) *ResourceDurationCreate {
	rdc.mutation.SetCpuDuration(u)
	return rdc
}

// SetMemoryDuration sets the "memoryDuration" field.
func (rdc *ResourceDurationCreate) SetMemoryDuration(u uint64) *ResourceDurationCreate {
	rdc.mutation.SetMemoryDuration(u)
	return rdc
}

// SetID sets the "id" field.
func (rdc *ResourceDurationCreate) SetID(m model.ID) *ResourceDurationCreate {
	rdc.mutation.SetID(m)
	return rdc
}

// Mutation returns the ResourceDurationMutation object of the builder.
func (rdc *ResourceDurationCreate) Mutation() *ResourceDurationMutation {
	return rdc.mutation
}

// Save creates the ResourceDuration in the database.
func (rdc *ResourceDurationCreate) Save(ctx context.Context) (*ResourceDuration, error) {
	var (
		err  error
		node *ResourceDuration
	)
	rdc.defaults()
	if len(rdc.hooks) == 0 {
		if err = rdc.check(); err != nil {
			return nil, err
		}
		node, err = rdc.sqlSave(ctx)
	} else {
		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
			mutation, ok := m.(*ResourceDurationMutation)
			if !ok {
				return nil, fmt.Errorf("unexpected mutation type %T", m)
			}
			if err = rdc.check(); err != nil {
				return nil, err
			}
			rdc.mutation = mutation
			if node, err = rdc.sqlSave(ctx); err != nil {
				return nil, err
			}
			mutation.id = &node.ID
			mutation.done = true
			return node, err
		})
		for i := len(rdc.hooks) - 1; i >= 0; i-- {
			if rdc.hooks[i] == nil {
				return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
			}
			mut = rdc.hooks[i](mut)
		}
		if _, err := mut.Mutate(ctx, rdc.mutation); err != nil {
			return nil, err
		}
	}
	return node, err
}

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

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

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

// defaults sets the default values of the builder before save.
func (rdc *ResourceDurationCreate) defaults() {
	if _, ok := rdc.mutation.CreatedAt(); !ok {
		v := resourceduration.DefaultCreatedAt()
		rdc.mutation.SetCreatedAt(v)
	}
	if _, ok := rdc.mutation.UpdatedAt(); !ok {
		v := resourceduration.DefaultUpdatedAt()
		rdc.mutation.SetUpdatedAt(v)
	}
	if _, ok := rdc.mutation.GetType(); !ok {
		v := resourceduration.DefaultType
		rdc.mutation.SetType(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (rdc *ResourceDurationCreate) check() error {
	if _, ok := rdc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "createdAt", err: errors.New(`ent: missing required field "ResourceDuration.createdAt"`)}
	}
	if _, ok := rdc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updatedAt", err: errors.New(`ent: missing required field "ResourceDuration.updatedAt"`)}
	}
	if _, ok := rdc.mutation.PlanExecutionID(); !ok {
		return &ValidationError{Name: "planExecutionID", err: errors.New(`ent: missing required field "ResourceDuration.planExecutionID"`)}
	}
	if v, ok := rdc.mutation.PlanExecutionID(); ok {
		if err := resourceduration.PlanExecutionIDValidator(uint64(v)); err != nil {
			return &ValidationError{Name: "planExecutionID", err: fmt.Errorf(`ent: validator failed for field "ResourceDuration.planExecutionID": %w`, err)}
		}
	}
	if _, ok := rdc.mutation.GetType(); !ok {
		return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "ResourceDuration.type"`)}
	}
	if v, ok := rdc.mutation.GetType(); ok {
		if err := resourceduration.TypeValidator(v); err != nil {
			return &ValidationError{Name: "type", err: fmt.Errorf(`ent: validator failed for field "ResourceDuration.type": %w`, err)}
		}
	}
	if _, ok := rdc.mutation.CpuDuration(); !ok {
		return &ValidationError{Name: "cpuDuration", err: errors.New(`ent: missing required field "ResourceDuration.cpuDuration"`)}
	}
	if _, ok := rdc.mutation.MemoryDuration(); !ok {
		return &ValidationError{Name: "memoryDuration", err: errors.New(`ent: missing required field "ResourceDuration.memoryDuration"`)}
	}
	return nil
}

func (rdc *ResourceDurationCreate) sqlSave(ctx context.Context) (*ResourceDuration, error) {
	_node, _spec := rdc.createSpec()
	if err := sqlgraph.CreateNode(ctx, rdc.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{err.Error(), err}
		}
		return nil, err
	}
	if _spec.ID.Value != _node.ID {
		id := _spec.ID.Value.(int64)
		_node.ID = model.ID(id)
	}
	return _node, nil
}

func (rdc *ResourceDurationCreate) createSpec() (*ResourceDuration, *sqlgraph.CreateSpec) {
	var (
		_node = &ResourceDuration{config: rdc.config}
		_spec = &sqlgraph.CreateSpec{
			Table: resourceduration.Table,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeUint64,
				Column: resourceduration.FieldID,
			},
		}
	)
	_spec.Schema = rdc.schemaConfig.ResourceDuration
	_spec.OnConflict = rdc.conflict
	if id, ok := rdc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := rdc.mutation.CreatedAt(); ok {
		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
			Type:   field.TypeTime,
			Value:  value,
			Column: resourceduration.FieldCreatedAt,
		})
		_node.CreatedAt = value
	}
	if value, ok := rdc.mutation.UpdatedAt(); ok {
		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
			Type:   field.TypeTime,
			Value:  value,
			Column: resourceduration.FieldUpdatedAt,
		})
		_node.UpdatedAt = value
	}
	if value, ok := rdc.mutation.PlanExecutionID(); ok {
		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
			Type:   field.TypeUint64,
			Value:  value,
			Column: resourceduration.FieldPlanExecutionID,
		})
		_node.PlanExecutionID = value
	}
	if value, ok := rdc.mutation.GetType(); ok {
		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
			Type:   field.TypeEnum,
			Value:  value,
			Column: resourceduration.FieldType,
		})
		_node.Type = value
	}
	if value, ok := rdc.mutation.CpuDuration(); ok {
		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
			Type:   field.TypeUint64,
			Value:  value,
			Column: resourceduration.FieldCpuDuration,
		})
		_node.CpuDuration = value
	}
	if value, ok := rdc.mutation.MemoryDuration(); ok {
		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
			Type:   field.TypeUint64,
			Value:  value,
			Column: resourceduration.FieldMemoryDuration,
		})
		_node.MemoryDuration = value
	}
	return _node, _spec
}

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

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

type (
	// ResourceDurationUpsertOne is the builder for "upsert"-ing
	//  one ResourceDuration node.
	ResourceDurationUpsertOne struct {
		create *ResourceDurationCreate
	}

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

// SetCreatedAt sets the "createdAt" field.
func (u *ResourceDurationUpsert) SetCreatedAt(v time.Time) *ResourceDurationUpsert {
	u.Set(resourceduration.FieldCreatedAt, v)
	return u
}

// UpdateCreatedAt sets the "createdAt" field to the value that was provided on create.
func (u *ResourceDurationUpsert) UpdateCreatedAt() *ResourceDurationUpsert {
	u.SetExcluded(resourceduration.FieldCreatedAt)
	return u
}

// SetUpdatedAt sets the "updatedAt" field.
func (u *ResourceDurationUpsert) SetUpdatedAt(v time.Time) *ResourceDurationUpsert {
	u.Set(resourceduration.FieldUpdatedAt, v)
	return u
}

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

// SetPlanExecutionID sets the "planExecutionID" field.
func (u *ResourceDurationUpsert) SetPlanExecutionID(v model.ID) *ResourceDurationUpsert {
	u.Set(resourceduration.FieldPlanExecutionID, v)
	return u
}

// UpdatePlanExecutionID sets the "planExecutionID" field to the value that was provided on create.
func (u *ResourceDurationUpsert) UpdatePlanExecutionID() *ResourceDurationUpsert {
	u.SetExcluded(resourceduration.FieldPlanExecutionID)
	return u
}

// AddPlanExecutionID adds v to the "planExecutionID" field.
func (u *ResourceDurationUpsert) AddPlanExecutionID(v model.ID) *ResourceDurationUpsert {
	u.Add(resourceduration.FieldPlanExecutionID, v)
	return u
}

// SetType sets the "type" field.
func (u *ResourceDurationUpsert) SetType(v model.ResourceDurationType) *ResourceDurationUpsert {
	u.Set(resourceduration.FieldType, v)
	return u
}

// UpdateType sets the "type" field to the value that was provided on create.
func (u *ResourceDurationUpsert) UpdateType() *ResourceDurationUpsert {
	u.SetExcluded(resourceduration.FieldType)
	return u
}

// SetCpuDuration sets the "cpuDuration" field.
func (u *ResourceDurationUpsert) SetCpuDuration(v uint64) *ResourceDurationUpsert {
	u.Set(resourceduration.FieldCpuDuration, v)
	return u
}

// UpdateCpuDuration sets the "cpuDuration" field to the value that was provided on create.
func (u *ResourceDurationUpsert) UpdateCpuDuration() *ResourceDurationUpsert {
	u.SetExcluded(resourceduration.FieldCpuDuration)
	return u
}

// AddCpuDuration adds v to the "cpuDuration" field.
func (u *ResourceDurationUpsert) AddCpuDuration(v uint64) *ResourceDurationUpsert {
	u.Add(resourceduration.FieldCpuDuration, v)
	return u
}

// SetMemoryDuration sets the "memoryDuration" field.
func (u *ResourceDurationUpsert) SetMemoryDuration(v uint64) *ResourceDurationUpsert {
	u.Set(resourceduration.FieldMemoryDuration, v)
	return u
}

// UpdateMemoryDuration sets the "memoryDuration" field to the value that was provided on create.
func (u *ResourceDurationUpsert) UpdateMemoryDuration() *ResourceDurationUpsert {
	u.SetExcluded(resourceduration.FieldMemoryDuration)
	return u
}

// AddMemoryDuration adds v to the "memoryDuration" field.
func (u *ResourceDurationUpsert) AddMemoryDuration(v uint64) *ResourceDurationUpsert {
	u.Add(resourceduration.FieldMemoryDuration, 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.ResourceDuration.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(resourceduration.FieldID)
//			}),
//		).
//		Exec(ctx)
//
func (u *ResourceDurationUpsertOne) UpdateNewValues() *ResourceDurationUpsertOne {
	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(resourceduration.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(resourceduration.FieldCreatedAt)
		}
		if _, exists := u.create.mutation.UpdatedAt(); exists {
			s.SetIgnore(resourceduration.FieldUpdatedAt)
		}
	}))
	return u
}

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

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

// SetCreatedAt sets the "createdAt" field.
func (u *ResourceDurationUpsertOne) SetCreatedAt(v time.Time) *ResourceDurationUpsertOne {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.SetCreatedAt(v)
	})
}

// UpdateCreatedAt sets the "createdAt" field to the value that was provided on create.
func (u *ResourceDurationUpsertOne) UpdateCreatedAt() *ResourceDurationUpsertOne {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.UpdateCreatedAt()
	})
}

// SetUpdatedAt sets the "updatedAt" field.
func (u *ResourceDurationUpsertOne) SetUpdatedAt(v time.Time) *ResourceDurationUpsertOne {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.SetUpdatedAt(v)
	})
}

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

// SetPlanExecutionID sets the "planExecutionID" field.
func (u *ResourceDurationUpsertOne) SetPlanExecutionID(v model.ID) *ResourceDurationUpsertOne {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.SetPlanExecutionID(v)
	})
}

// AddPlanExecutionID adds v to the "planExecutionID" field.
func (u *ResourceDurationUpsertOne) AddPlanExecutionID(v model.ID) *ResourceDurationUpsertOne {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.AddPlanExecutionID(v)
	})
}

// UpdatePlanExecutionID sets the "planExecutionID" field to the value that was provided on create.
func (u *ResourceDurationUpsertOne) UpdatePlanExecutionID() *ResourceDurationUpsertOne {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.UpdatePlanExecutionID()
	})
}

// SetType sets the "type" field.
func (u *ResourceDurationUpsertOne) SetType(v model.ResourceDurationType) *ResourceDurationUpsertOne {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.SetType(v)
	})
}

// UpdateType sets the "type" field to the value that was provided on create.
func (u *ResourceDurationUpsertOne) UpdateType() *ResourceDurationUpsertOne {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.UpdateType()
	})
}

// SetCpuDuration sets the "cpuDuration" field.
func (u *ResourceDurationUpsertOne) SetCpuDuration(v uint64) *ResourceDurationUpsertOne {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.SetCpuDuration(v)
	})
}

// AddCpuDuration adds v to the "cpuDuration" field.
func (u *ResourceDurationUpsertOne) AddCpuDuration(v uint64) *ResourceDurationUpsertOne {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.AddCpuDuration(v)
	})
}

// UpdateCpuDuration sets the "cpuDuration" field to the value that was provided on create.
func (u *ResourceDurationUpsertOne) UpdateCpuDuration() *ResourceDurationUpsertOne {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.UpdateCpuDuration()
	})
}

// SetMemoryDuration sets the "memoryDuration" field.
func (u *ResourceDurationUpsertOne) SetMemoryDuration(v uint64) *ResourceDurationUpsertOne {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.SetMemoryDuration(v)
	})
}

// AddMemoryDuration adds v to the "memoryDuration" field.
func (u *ResourceDurationUpsertOne) AddMemoryDuration(v uint64) *ResourceDurationUpsertOne {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.AddMemoryDuration(v)
	})
}

// UpdateMemoryDuration sets the "memoryDuration" field to the value that was provided on create.
func (u *ResourceDurationUpsertOne) UpdateMemoryDuration() *ResourceDurationUpsertOne {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.UpdateMemoryDuration()
	})
}

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

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

// ResourceDurationCreateBulk is the builder for creating many ResourceDuration entities in bulk.
type ResourceDurationCreateBulk struct {
	config
	builders []*ResourceDurationCreate
	conflict []sql.ConflictOption
}

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

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

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

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

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

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

// ResourceDurationUpsertBulk is the builder for "upsert"-ing
// a bulk of ResourceDuration nodes.
type ResourceDurationUpsertBulk struct {
	create *ResourceDurationCreateBulk
}

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

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

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

// SetCreatedAt sets the "createdAt" field.
func (u *ResourceDurationUpsertBulk) SetCreatedAt(v time.Time) *ResourceDurationUpsertBulk {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.SetCreatedAt(v)
	})
}

// UpdateCreatedAt sets the "createdAt" field to the value that was provided on create.
func (u *ResourceDurationUpsertBulk) UpdateCreatedAt() *ResourceDurationUpsertBulk {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.UpdateCreatedAt()
	})
}

// SetUpdatedAt sets the "updatedAt" field.
func (u *ResourceDurationUpsertBulk) SetUpdatedAt(v time.Time) *ResourceDurationUpsertBulk {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.SetUpdatedAt(v)
	})
}

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

// SetPlanExecutionID sets the "planExecutionID" field.
func (u *ResourceDurationUpsertBulk) SetPlanExecutionID(v model.ID) *ResourceDurationUpsertBulk {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.SetPlanExecutionID(v)
	})
}

// AddPlanExecutionID adds v to the "planExecutionID" field.
func (u *ResourceDurationUpsertBulk) AddPlanExecutionID(v model.ID) *ResourceDurationUpsertBulk {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.AddPlanExecutionID(v)
	})
}

// UpdatePlanExecutionID sets the "planExecutionID" field to the value that was provided on create.
func (u *ResourceDurationUpsertBulk) UpdatePlanExecutionID() *ResourceDurationUpsertBulk {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.UpdatePlanExecutionID()
	})
}

// SetType sets the "type" field.
func (u *ResourceDurationUpsertBulk) SetType(v model.ResourceDurationType) *ResourceDurationUpsertBulk {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.SetType(v)
	})
}

// UpdateType sets the "type" field to the value that was provided on create.
func (u *ResourceDurationUpsertBulk) UpdateType() *ResourceDurationUpsertBulk {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.UpdateType()
	})
}

// SetCpuDuration sets the "cpuDuration" field.
func (u *ResourceDurationUpsertBulk) SetCpuDuration(v uint64) *ResourceDurationUpsertBulk {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.SetCpuDuration(v)
	})
}

// AddCpuDuration adds v to the "cpuDuration" field.
func (u *ResourceDurationUpsertBulk) AddCpuDuration(v uint64) *ResourceDurationUpsertBulk {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.AddCpuDuration(v)
	})
}

// UpdateCpuDuration sets the "cpuDuration" field to the value that was provided on create.
func (u *ResourceDurationUpsertBulk) UpdateCpuDuration() *ResourceDurationUpsertBulk {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.UpdateCpuDuration()
	})
}

// SetMemoryDuration sets the "memoryDuration" field.
func (u *ResourceDurationUpsertBulk) SetMemoryDuration(v uint64) *ResourceDurationUpsertBulk {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.SetMemoryDuration(v)
	})
}

// AddMemoryDuration adds v to the "memoryDuration" field.
func (u *ResourceDurationUpsertBulk) AddMemoryDuration(v uint64) *ResourceDurationUpsertBulk {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.AddMemoryDuration(v)
	})
}

// UpdateMemoryDuration sets the "memoryDuration" field to the value that was provided on create.
func (u *ResourceDurationUpsertBulk) UpdateMemoryDuration() *ResourceDurationUpsertBulk {
	return u.Update(func(s *ResourceDurationUpsert) {
		s.UpdateMemoryDuration()
	})
}

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

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