// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"
	"wechat-api/ent/contactfieldtemplate"
	"wechat-api/ent/custom_types"

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

// ContactFieldTemplateCreate is the builder for creating a ContactFieldTemplate entity.
type ContactFieldTemplateCreate struct {
	config
	mutation *ContactFieldTemplateMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

// SetStatus sets the "status" field.
func (cftc *ContactFieldTemplateCreate) SetStatus(u uint8) *ContactFieldTemplateCreate {
	cftc.mutation.SetStatus(u)
	return cftc
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (cftc *ContactFieldTemplateCreate) SetNillableStatus(u *uint8) *ContactFieldTemplateCreate {
	if u != nil {
		cftc.SetStatus(*u)
	}
	return cftc
}

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

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

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

// SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
func (cftc *ContactFieldTemplateCreate) SetNillableOrganizationID(u *uint64) *ContactFieldTemplateCreate {
	if u != nil {
		cftc.SetOrganizationID(*u)
	}
	return cftc
}

// SetTemplate sets the "template" field.
func (cftc *ContactFieldTemplateCreate) SetTemplate(ctft []custom_types.ContactFieldTemplate) *ContactFieldTemplateCreate {
	cftc.mutation.SetTemplate(ctft)
	return cftc
}

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

// Mutation returns the ContactFieldTemplateMutation object of the builder.
func (cftc *ContactFieldTemplateCreate) Mutation() *ContactFieldTemplateMutation {
	return cftc.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (cftc *ContactFieldTemplateCreate) defaults() error {
	if _, ok := cftc.mutation.CreatedAt(); !ok {
		if contactfieldtemplate.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized contactfieldtemplate.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := contactfieldtemplate.DefaultCreatedAt()
		cftc.mutation.SetCreatedAt(v)
	}
	if _, ok := cftc.mutation.UpdatedAt(); !ok {
		if contactfieldtemplate.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized contactfieldtemplate.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := contactfieldtemplate.DefaultUpdatedAt()
		cftc.mutation.SetUpdatedAt(v)
	}
	if _, ok := cftc.mutation.Status(); !ok {
		v := contactfieldtemplate.DefaultStatus
		cftc.mutation.SetStatus(v)
	}
	if _, ok := cftc.mutation.OrganizationID(); !ok {
		v := contactfieldtemplate.DefaultOrganizationID
		cftc.mutation.SetOrganizationID(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (cftc *ContactFieldTemplateCreate) check() error {
	if _, ok := cftc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "ContactFieldTemplate.created_at"`)}
	}
	if _, ok := cftc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "ContactFieldTemplate.updated_at"`)}
	}
	if _, ok := cftc.mutation.OrganizationID(); !ok {
		return &ValidationError{Name: "organization_id", err: errors.New(`ent: missing required field "ContactFieldTemplate.organization_id"`)}
	}
	if _, ok := cftc.mutation.Template(); !ok {
		return &ValidationError{Name: "template", err: errors.New(`ent: missing required field "ContactFieldTemplate.template"`)}
	}
	return nil
}

func (cftc *ContactFieldTemplateCreate) sqlSave(ctx context.Context) (*ContactFieldTemplate, error) {
	if err := cftc.check(); err != nil {
		return nil, err
	}
	_node, _spec := cftc.createSpec()
	if err := sqlgraph.CreateNode(ctx, cftc.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)
	}
	cftc.mutation.id = &_node.ID
	cftc.mutation.done = true
	return _node, nil
}

func (cftc *ContactFieldTemplateCreate) createSpec() (*ContactFieldTemplate, *sqlgraph.CreateSpec) {
	var (
		_node = &ContactFieldTemplate{config: cftc.config}
		_spec = sqlgraph.NewCreateSpec(contactfieldtemplate.Table, sqlgraph.NewFieldSpec(contactfieldtemplate.FieldID, field.TypeUint64))
	)
	_spec.OnConflict = cftc.conflict
	if id, ok := cftc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := cftc.mutation.CreatedAt(); ok {
		_spec.SetField(contactfieldtemplate.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := cftc.mutation.UpdatedAt(); ok {
		_spec.SetField(contactfieldtemplate.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := cftc.mutation.Status(); ok {
		_spec.SetField(contactfieldtemplate.FieldStatus, field.TypeUint8, value)
		_node.Status = value
	}
	if value, ok := cftc.mutation.DeletedAt(); ok {
		_spec.SetField(contactfieldtemplate.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = value
	}
	if value, ok := cftc.mutation.OrganizationID(); ok {
		_spec.SetField(contactfieldtemplate.FieldOrganizationID, field.TypeUint64, value)
		_node.OrganizationID = value
	}
	if value, ok := cftc.mutation.Template(); ok {
		_spec.SetField(contactfieldtemplate.FieldTemplate, field.TypeJSON, value)
		_node.Template = value
	}
	return _node, _spec
}

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

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

type (
	// ContactFieldTemplateUpsertOne is the builder for "upsert"-ing
	//  one ContactFieldTemplate node.
	ContactFieldTemplateUpsertOne struct {
		create *ContactFieldTemplateCreate
	}

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

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

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

// SetStatus sets the "status" field.
func (u *ContactFieldTemplateUpsert) SetStatus(v uint8) *ContactFieldTemplateUpsert {
	u.Set(contactfieldtemplate.FieldStatus, v)
	return u
}

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

// AddStatus adds v to the "status" field.
func (u *ContactFieldTemplateUpsert) AddStatus(v uint8) *ContactFieldTemplateUpsert {
	u.Add(contactfieldtemplate.FieldStatus, v)
	return u
}

// ClearStatus clears the value of the "status" field.
func (u *ContactFieldTemplateUpsert) ClearStatus() *ContactFieldTemplateUpsert {
	u.SetNull(contactfieldtemplate.FieldStatus)
	return u
}

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

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

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

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

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

// AddOrganizationID adds v to the "organization_id" field.
func (u *ContactFieldTemplateUpsert) AddOrganizationID(v uint64) *ContactFieldTemplateUpsert {
	u.Add(contactfieldtemplate.FieldOrganizationID, v)
	return u
}

// SetTemplate sets the "template" field.
func (u *ContactFieldTemplateUpsert) SetTemplate(v []custom_types.ContactFieldTemplate) *ContactFieldTemplateUpsert {
	u.Set(contactfieldtemplate.FieldTemplate, v)
	return u
}

// UpdateTemplate sets the "template" field to the value that was provided on create.
func (u *ContactFieldTemplateUpsert) UpdateTemplate() *ContactFieldTemplateUpsert {
	u.SetExcluded(contactfieldtemplate.FieldTemplate)
	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.ContactFieldTemplate.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(contactfieldtemplate.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *ContactFieldTemplateUpsertOne) UpdateNewValues() *ContactFieldTemplateUpsertOne {
	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(contactfieldtemplate.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(contactfieldtemplate.FieldCreatedAt)
		}
	}))
	return u
}

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

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

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

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

// SetStatus sets the "status" field.
func (u *ContactFieldTemplateUpsertOne) SetStatus(v uint8) *ContactFieldTemplateUpsertOne {
	return u.Update(func(s *ContactFieldTemplateUpsert) {
		s.SetStatus(v)
	})
}

// AddStatus adds v to the "status" field.
func (u *ContactFieldTemplateUpsertOne) AddStatus(v uint8) *ContactFieldTemplateUpsertOne {
	return u.Update(func(s *ContactFieldTemplateUpsert) {
		s.AddStatus(v)
	})
}

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

// ClearStatus clears the value of the "status" field.
func (u *ContactFieldTemplateUpsertOne) ClearStatus() *ContactFieldTemplateUpsertOne {
	return u.Update(func(s *ContactFieldTemplateUpsert) {
		s.ClearStatus()
	})
}

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

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

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

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

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

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

// SetTemplate sets the "template" field.
func (u *ContactFieldTemplateUpsertOne) SetTemplate(v []custom_types.ContactFieldTemplate) *ContactFieldTemplateUpsertOne {
	return u.Update(func(s *ContactFieldTemplateUpsert) {
		s.SetTemplate(v)
	})
}

// UpdateTemplate sets the "template" field to the value that was provided on create.
func (u *ContactFieldTemplateUpsertOne) UpdateTemplate() *ContactFieldTemplateUpsertOne {
	return u.Update(func(s *ContactFieldTemplateUpsert) {
		s.UpdateTemplate()
	})
}

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

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

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

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

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

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

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

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

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

// ContactFieldTemplateUpsertBulk is the builder for "upsert"-ing
// a bulk of ContactFieldTemplate nodes.
type ContactFieldTemplateUpsertBulk struct {
	create *ContactFieldTemplateCreateBulk
}

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

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

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

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

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

// SetStatus sets the "status" field.
func (u *ContactFieldTemplateUpsertBulk) SetStatus(v uint8) *ContactFieldTemplateUpsertBulk {
	return u.Update(func(s *ContactFieldTemplateUpsert) {
		s.SetStatus(v)
	})
}

// AddStatus adds v to the "status" field.
func (u *ContactFieldTemplateUpsertBulk) AddStatus(v uint8) *ContactFieldTemplateUpsertBulk {
	return u.Update(func(s *ContactFieldTemplateUpsert) {
		s.AddStatus(v)
	})
}

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

// ClearStatus clears the value of the "status" field.
func (u *ContactFieldTemplateUpsertBulk) ClearStatus() *ContactFieldTemplateUpsertBulk {
	return u.Update(func(s *ContactFieldTemplateUpsert) {
		s.ClearStatus()
	})
}

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

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

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

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

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

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

// SetTemplate sets the "template" field.
func (u *ContactFieldTemplateUpsertBulk) SetTemplate(v []custom_types.ContactFieldTemplate) *ContactFieldTemplateUpsertBulk {
	return u.Update(func(s *ContactFieldTemplateUpsert) {
		s.SetTemplate(v)
	})
}

// UpdateTemplate sets the "template" field to the value that was provided on create.
func (u *ContactFieldTemplateUpsertBulk) UpdateTemplate() *ContactFieldTemplateUpsertBulk {
	return u.Update(func(s *ContactFieldTemplateUpsert) {
		s.UpdateTemplate()
	})
}

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

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