// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"
	"wechat-api/ent/label"
	"wechat-api/ent/labelrelationship"

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

// LabelCreate is the builder for creating a Label entity.
type LabelCreate struct {
	config
	mutation *LabelMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

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

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

// SetType sets the "type" field.
func (lc *LabelCreate) SetType(i int) *LabelCreate {
	lc.mutation.SetType(i)
	return lc
}

// SetNillableType sets the "type" field if the given value is not nil.
func (lc *LabelCreate) SetNillableType(i *int) *LabelCreate {
	if i != nil {
		lc.SetType(*i)
	}
	return lc
}

// SetName sets the "name" field.
func (lc *LabelCreate) SetName(s string) *LabelCreate {
	lc.mutation.SetName(s)
	return lc
}

// SetNillableName sets the "name" field if the given value is not nil.
func (lc *LabelCreate) SetNillableName(s *string) *LabelCreate {
	if s != nil {
		lc.SetName(*s)
	}
	return lc
}

// SetFrom sets the "from" field.
func (lc *LabelCreate) SetFrom(i int) *LabelCreate {
	lc.mutation.SetFrom(i)
	return lc
}

// SetNillableFrom sets the "from" field if the given value is not nil.
func (lc *LabelCreate) SetNillableFrom(i *int) *LabelCreate {
	if i != nil {
		lc.SetFrom(*i)
	}
	return lc
}

// SetMode sets the "mode" field.
func (lc *LabelCreate) SetMode(i int) *LabelCreate {
	lc.mutation.SetMode(i)
	return lc
}

// SetNillableMode sets the "mode" field if the given value is not nil.
func (lc *LabelCreate) SetNillableMode(i *int) *LabelCreate {
	if i != nil {
		lc.SetMode(*i)
	}
	return lc
}

// SetConditions sets the "conditions" field.
func (lc *LabelCreate) SetConditions(s string) *LabelCreate {
	lc.mutation.SetConditions(s)
	return lc
}

// SetNillableConditions sets the "conditions" field if the given value is not nil.
func (lc *LabelCreate) SetNillableConditions(s *string) *LabelCreate {
	if s != nil {
		lc.SetConditions(*s)
	}
	return lc
}

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

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

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

// AddLabelRelationshipIDs adds the "label_relationships" edge to the LabelRelationship entity by IDs.
func (lc *LabelCreate) AddLabelRelationshipIDs(ids ...uint64) *LabelCreate {
	lc.mutation.AddLabelRelationshipIDs(ids...)
	return lc
}

// AddLabelRelationships adds the "label_relationships" edges to the LabelRelationship entity.
func (lc *LabelCreate) AddLabelRelationships(l ...*LabelRelationship) *LabelCreate {
	ids := make([]uint64, len(l))
	for i := range l {
		ids[i] = l[i].ID
	}
	return lc.AddLabelRelationshipIDs(ids...)
}

// Mutation returns the LabelMutation object of the builder.
func (lc *LabelCreate) Mutation() *LabelMutation {
	return lc.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (lc *LabelCreate) defaults() {
	if _, ok := lc.mutation.CreatedAt(); !ok {
		v := label.DefaultCreatedAt()
		lc.mutation.SetCreatedAt(v)
	}
	if _, ok := lc.mutation.UpdatedAt(); !ok {
		v := label.DefaultUpdatedAt()
		lc.mutation.SetUpdatedAt(v)
	}
	if _, ok := lc.mutation.Status(); !ok {
		v := label.DefaultStatus
		lc.mutation.SetStatus(v)
	}
	if _, ok := lc.mutation.GetType(); !ok {
		v := label.DefaultType
		lc.mutation.SetType(v)
	}
	if _, ok := lc.mutation.Name(); !ok {
		v := label.DefaultName
		lc.mutation.SetName(v)
	}
	if _, ok := lc.mutation.From(); !ok {
		v := label.DefaultFrom
		lc.mutation.SetFrom(v)
	}
	if _, ok := lc.mutation.Mode(); !ok {
		v := label.DefaultMode
		lc.mutation.SetMode(v)
	}
	if _, ok := lc.mutation.Conditions(); !ok {
		v := label.DefaultConditions
		lc.mutation.SetConditions(v)
	}
	if _, ok := lc.mutation.OrganizationID(); !ok {
		v := label.DefaultOrganizationID
		lc.mutation.SetOrganizationID(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (lc *LabelCreate) check() error {
	if _, ok := lc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Label.created_at"`)}
	}
	if _, ok := lc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Label.updated_at"`)}
	}
	if _, ok := lc.mutation.GetType(); !ok {
		return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "Label.type"`)}
	}
	if _, ok := lc.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Label.name"`)}
	}
	if _, ok := lc.mutation.From(); !ok {
		return &ValidationError{Name: "from", err: errors.New(`ent: missing required field "Label.from"`)}
	}
	if _, ok := lc.mutation.Mode(); !ok {
		return &ValidationError{Name: "mode", err: errors.New(`ent: missing required field "Label.mode"`)}
	}
	return nil
}

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

func (lc *LabelCreate) createSpec() (*Label, *sqlgraph.CreateSpec) {
	var (
		_node = &Label{config: lc.config}
		_spec = sqlgraph.NewCreateSpec(label.Table, sqlgraph.NewFieldSpec(label.FieldID, field.TypeUint64))
	)
	_spec.OnConflict = lc.conflict
	if id, ok := lc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := lc.mutation.CreatedAt(); ok {
		_spec.SetField(label.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := lc.mutation.UpdatedAt(); ok {
		_spec.SetField(label.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := lc.mutation.Status(); ok {
		_spec.SetField(label.FieldStatus, field.TypeUint8, value)
		_node.Status = value
	}
	if value, ok := lc.mutation.GetType(); ok {
		_spec.SetField(label.FieldType, field.TypeInt, value)
		_node.Type = value
	}
	if value, ok := lc.mutation.Name(); ok {
		_spec.SetField(label.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := lc.mutation.From(); ok {
		_spec.SetField(label.FieldFrom, field.TypeInt, value)
		_node.From = value
	}
	if value, ok := lc.mutation.Mode(); ok {
		_spec.SetField(label.FieldMode, field.TypeInt, value)
		_node.Mode = value
	}
	if value, ok := lc.mutation.Conditions(); ok {
		_spec.SetField(label.FieldConditions, field.TypeString, value)
		_node.Conditions = value
	}
	if value, ok := lc.mutation.OrganizationID(); ok {
		_spec.SetField(label.FieldOrganizationID, field.TypeUint64, value)
		_node.OrganizationID = value
	}
	if nodes := lc.mutation.LabelRelationshipsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   label.LabelRelationshipsTable,
			Columns: []string{label.LabelRelationshipsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(labelrelationship.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

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

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

type (
	// LabelUpsertOne is the builder for "upsert"-ing
	//  one Label node.
	LabelUpsertOne struct {
		create *LabelCreate
	}

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

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

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

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

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

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

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

// SetType sets the "type" field.
func (u *LabelUpsert) SetType(v int) *LabelUpsert {
	u.Set(label.FieldType, v)
	return u
}

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

// AddType adds v to the "type" field.
func (u *LabelUpsert) AddType(v int) *LabelUpsert {
	u.Add(label.FieldType, v)
	return u
}

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

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

// SetFrom sets the "from" field.
func (u *LabelUpsert) SetFrom(v int) *LabelUpsert {
	u.Set(label.FieldFrom, v)
	return u
}

// UpdateFrom sets the "from" field to the value that was provided on create.
func (u *LabelUpsert) UpdateFrom() *LabelUpsert {
	u.SetExcluded(label.FieldFrom)
	return u
}

// AddFrom adds v to the "from" field.
func (u *LabelUpsert) AddFrom(v int) *LabelUpsert {
	u.Add(label.FieldFrom, v)
	return u
}

// SetMode sets the "mode" field.
func (u *LabelUpsert) SetMode(v int) *LabelUpsert {
	u.Set(label.FieldMode, v)
	return u
}

// UpdateMode sets the "mode" field to the value that was provided on create.
func (u *LabelUpsert) UpdateMode() *LabelUpsert {
	u.SetExcluded(label.FieldMode)
	return u
}

// AddMode adds v to the "mode" field.
func (u *LabelUpsert) AddMode(v int) *LabelUpsert {
	u.Add(label.FieldMode, v)
	return u
}

// SetConditions sets the "conditions" field.
func (u *LabelUpsert) SetConditions(v string) *LabelUpsert {
	u.Set(label.FieldConditions, v)
	return u
}

// UpdateConditions sets the "conditions" field to the value that was provided on create.
func (u *LabelUpsert) UpdateConditions() *LabelUpsert {
	u.SetExcluded(label.FieldConditions)
	return u
}

// ClearConditions clears the value of the "conditions" field.
func (u *LabelUpsert) ClearConditions() *LabelUpsert {
	u.SetNull(label.FieldConditions)
	return u
}

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

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

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

// ClearOrganizationID clears the value of the "organization_id" field.
func (u *LabelUpsert) ClearOrganizationID() *LabelUpsert {
	u.SetNull(label.FieldOrganizationID)
	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.Label.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(label.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *LabelUpsertOne) UpdateNewValues() *LabelUpsertOne {
	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(label.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(label.FieldCreatedAt)
		}
	}))
	return u
}

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

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

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

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

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

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

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

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

// SetType sets the "type" field.
func (u *LabelUpsertOne) SetType(v int) *LabelUpsertOne {
	return u.Update(func(s *LabelUpsert) {
		s.SetType(v)
	})
}

// AddType adds v to the "type" field.
func (u *LabelUpsertOne) AddType(v int) *LabelUpsertOne {
	return u.Update(func(s *LabelUpsert) {
		s.AddType(v)
	})
}

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

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

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

// SetFrom sets the "from" field.
func (u *LabelUpsertOne) SetFrom(v int) *LabelUpsertOne {
	return u.Update(func(s *LabelUpsert) {
		s.SetFrom(v)
	})
}

// AddFrom adds v to the "from" field.
func (u *LabelUpsertOne) AddFrom(v int) *LabelUpsertOne {
	return u.Update(func(s *LabelUpsert) {
		s.AddFrom(v)
	})
}

// UpdateFrom sets the "from" field to the value that was provided on create.
func (u *LabelUpsertOne) UpdateFrom() *LabelUpsertOne {
	return u.Update(func(s *LabelUpsert) {
		s.UpdateFrom()
	})
}

// SetMode sets the "mode" field.
func (u *LabelUpsertOne) SetMode(v int) *LabelUpsertOne {
	return u.Update(func(s *LabelUpsert) {
		s.SetMode(v)
	})
}

// AddMode adds v to the "mode" field.
func (u *LabelUpsertOne) AddMode(v int) *LabelUpsertOne {
	return u.Update(func(s *LabelUpsert) {
		s.AddMode(v)
	})
}

// UpdateMode sets the "mode" field to the value that was provided on create.
func (u *LabelUpsertOne) UpdateMode() *LabelUpsertOne {
	return u.Update(func(s *LabelUpsert) {
		s.UpdateMode()
	})
}

// SetConditions sets the "conditions" field.
func (u *LabelUpsertOne) SetConditions(v string) *LabelUpsertOne {
	return u.Update(func(s *LabelUpsert) {
		s.SetConditions(v)
	})
}

// UpdateConditions sets the "conditions" field to the value that was provided on create.
func (u *LabelUpsertOne) UpdateConditions() *LabelUpsertOne {
	return u.Update(func(s *LabelUpsert) {
		s.UpdateConditions()
	})
}

// ClearConditions clears the value of the "conditions" field.
func (u *LabelUpsertOne) ClearConditions() *LabelUpsertOne {
	return u.Update(func(s *LabelUpsert) {
		s.ClearConditions()
	})
}

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

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

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

// ClearOrganizationID clears the value of the "organization_id" field.
func (u *LabelUpsertOne) ClearOrganizationID() *LabelUpsertOne {
	return u.Update(func(s *LabelUpsert) {
		s.ClearOrganizationID()
	})
}

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

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

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

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

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

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

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

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

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

// LabelUpsertBulk is the builder for "upsert"-ing
// a bulk of Label nodes.
type LabelUpsertBulk struct {
	create *LabelCreateBulk
}

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

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

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

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

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

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

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

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

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

// SetType sets the "type" field.
func (u *LabelUpsertBulk) SetType(v int) *LabelUpsertBulk {
	return u.Update(func(s *LabelUpsert) {
		s.SetType(v)
	})
}

// AddType adds v to the "type" field.
func (u *LabelUpsertBulk) AddType(v int) *LabelUpsertBulk {
	return u.Update(func(s *LabelUpsert) {
		s.AddType(v)
	})
}

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

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

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

// SetFrom sets the "from" field.
func (u *LabelUpsertBulk) SetFrom(v int) *LabelUpsertBulk {
	return u.Update(func(s *LabelUpsert) {
		s.SetFrom(v)
	})
}

// AddFrom adds v to the "from" field.
func (u *LabelUpsertBulk) AddFrom(v int) *LabelUpsertBulk {
	return u.Update(func(s *LabelUpsert) {
		s.AddFrom(v)
	})
}

// UpdateFrom sets the "from" field to the value that was provided on create.
func (u *LabelUpsertBulk) UpdateFrom() *LabelUpsertBulk {
	return u.Update(func(s *LabelUpsert) {
		s.UpdateFrom()
	})
}

// SetMode sets the "mode" field.
func (u *LabelUpsertBulk) SetMode(v int) *LabelUpsertBulk {
	return u.Update(func(s *LabelUpsert) {
		s.SetMode(v)
	})
}

// AddMode adds v to the "mode" field.
func (u *LabelUpsertBulk) AddMode(v int) *LabelUpsertBulk {
	return u.Update(func(s *LabelUpsert) {
		s.AddMode(v)
	})
}

// UpdateMode sets the "mode" field to the value that was provided on create.
func (u *LabelUpsertBulk) UpdateMode() *LabelUpsertBulk {
	return u.Update(func(s *LabelUpsert) {
		s.UpdateMode()
	})
}

// SetConditions sets the "conditions" field.
func (u *LabelUpsertBulk) SetConditions(v string) *LabelUpsertBulk {
	return u.Update(func(s *LabelUpsert) {
		s.SetConditions(v)
	})
}

// UpdateConditions sets the "conditions" field to the value that was provided on create.
func (u *LabelUpsertBulk) UpdateConditions() *LabelUpsertBulk {
	return u.Update(func(s *LabelUpsert) {
		s.UpdateConditions()
	})
}

// ClearConditions clears the value of the "conditions" field.
func (u *LabelUpsertBulk) ClearConditions() *LabelUpsertBulk {
	return u.Update(func(s *LabelUpsert) {
		s.ClearConditions()
	})
}

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

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

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

// ClearOrganizationID clears the value of the "organization_id" field.
func (u *LabelUpsertBulk) ClearOrganizationID() *LabelUpsertBulk {
	return u.Update(func(s *LabelUpsert) {
		s.ClearOrganizationID()
	})
}

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

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