// Code generated by ent, 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"
	"gitea.maison43.duckdns.org/gilles/matosbox/internal/data/ent/emplacement"
	"gitea.maison43.duckdns.org/gilles/matosbox/internal/data/ent/lienobjetemplacement"
	"gitea.maison43.duckdns.org/gilles/matosbox/internal/data/ent/predicate"
	"github.com/google/uuid"
)

// EmplacementUpdate is the builder for updating Emplacement entities.
type EmplacementUpdate struct {
	config
	hooks    []Hook
	mutation *EmplacementMutation
}

// Where appends a list predicates to the EmplacementUpdate builder.
func (_u *EmplacementUpdate) Where(ps ...predicate.Emplacement) *EmplacementUpdate {
	_u.mutation.Where(ps...)
	return _u
}

// SetNom sets the "nom" field.
func (_u *EmplacementUpdate) SetNom(v string) *EmplacementUpdate {
	_u.mutation.SetNom(v)
	return _u
}

// SetNillableNom sets the "nom" field if the given value is not nil.
func (_u *EmplacementUpdate) SetNillableNom(v *string) *EmplacementUpdate {
	if v != nil {
		_u.SetNom(*v)
	}
	return _u
}

// SetParentID sets the "parent_id" field.
func (_u *EmplacementUpdate) SetParentID(v uuid.UUID) *EmplacementUpdate {
	_u.mutation.SetParentID(v)
	return _u
}

// SetNillableParentID sets the "parent_id" field if the given value is not nil.
func (_u *EmplacementUpdate) SetNillableParentID(v *uuid.UUID) *EmplacementUpdate {
	if v != nil {
		_u.SetParentID(*v)
	}
	return _u
}

// ClearParentID clears the value of the "parent_id" field.
func (_u *EmplacementUpdate) ClearParentID() *EmplacementUpdate {
	_u.mutation.ClearParentID()
	return _u
}

// SetSlug sets the "slug" field.
func (_u *EmplacementUpdate) SetSlug(v string) *EmplacementUpdate {
	_u.mutation.SetSlug(v)
	return _u
}

// SetNillableSlug sets the "slug" field if the given value is not nil.
func (_u *EmplacementUpdate) SetNillableSlug(v *string) *EmplacementUpdate {
	if v != nil {
		_u.SetSlug(*v)
	}
	return _u
}

// ClearSlug clears the value of the "slug" field.
func (_u *EmplacementUpdate) ClearSlug() *EmplacementUpdate {
	_u.mutation.ClearSlug()
	return _u
}

// SetPiece sets the "piece" field.
func (_u *EmplacementUpdate) SetPiece(v string) *EmplacementUpdate {
	_u.mutation.SetPiece(v)
	return _u
}

// SetNillablePiece sets the "piece" field if the given value is not nil.
func (_u *EmplacementUpdate) SetNillablePiece(v *string) *EmplacementUpdate {
	if v != nil {
		_u.SetPiece(*v)
	}
	return _u
}

// ClearPiece clears the value of the "piece" field.
func (_u *EmplacementUpdate) ClearPiece() *EmplacementUpdate {
	_u.mutation.ClearPiece()
	return _u
}

// SetMeuble sets the "meuble" field.
func (_u *EmplacementUpdate) SetMeuble(v string) *EmplacementUpdate {
	_u.mutation.SetMeuble(v)
	return _u
}

// SetNillableMeuble sets the "meuble" field if the given value is not nil.
func (_u *EmplacementUpdate) SetNillableMeuble(v *string) *EmplacementUpdate {
	if v != nil {
		_u.SetMeuble(*v)
	}
	return _u
}

// ClearMeuble clears the value of the "meuble" field.
func (_u *EmplacementUpdate) ClearMeuble() *EmplacementUpdate {
	_u.mutation.ClearMeuble()
	return _u
}

// SetNumeroBoite sets the "numero_boite" field.
func (_u *EmplacementUpdate) SetNumeroBoite(v string) *EmplacementUpdate {
	_u.mutation.SetNumeroBoite(v)
	return _u
}

// SetNillableNumeroBoite sets the "numero_boite" field if the given value is not nil.
func (_u *EmplacementUpdate) SetNillableNumeroBoite(v *string) *EmplacementUpdate {
	if v != nil {
		_u.SetNumeroBoite(*v)
	}
	return _u
}

// ClearNumeroBoite clears the value of the "numero_boite" field.
func (_u *EmplacementUpdate) ClearNumeroBoite() *EmplacementUpdate {
	_u.mutation.ClearNumeroBoite()
	return _u
}

// SetIcone sets the "icone" field.
func (_u *EmplacementUpdate) SetIcone(v string) *EmplacementUpdate {
	_u.mutation.SetIcone(v)
	return _u
}

// SetNillableIcone sets the "icone" field if the given value is not nil.
func (_u *EmplacementUpdate) SetNillableIcone(v *string) *EmplacementUpdate {
	if v != nil {
		_u.SetIcone(*v)
	}
	return _u
}

// ClearIcone clears the value of the "icone" field.
func (_u *EmplacementUpdate) ClearIcone() *EmplacementUpdate {
	_u.mutation.ClearIcone()
	return _u
}

// SetCreatedAt sets the "created_at" field.
func (_u *EmplacementUpdate) SetCreatedAt(v time.Time) *EmplacementUpdate {
	_u.mutation.SetCreatedAt(v)
	return _u
}

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

// SetUpdatedAt sets the "updated_at" field.
func (_u *EmplacementUpdate) SetUpdatedAt(v time.Time) *EmplacementUpdate {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetParent sets the "parent" edge to the Emplacement entity.
func (_u *EmplacementUpdate) SetParent(v *Emplacement) *EmplacementUpdate {
	return _u.SetParentID(v.ID)
}

// AddEnfantIDs adds the "enfants" edge to the Emplacement entity by IDs.
func (_u *EmplacementUpdate) AddEnfantIDs(ids ...uuid.UUID) *EmplacementUpdate {
	_u.mutation.AddEnfantIDs(ids...)
	return _u
}

// AddEnfants adds the "enfants" edges to the Emplacement entity.
func (_u *EmplacementUpdate) AddEnfants(v ...*Emplacement) *EmplacementUpdate {
	ids := make([]uuid.UUID, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddEnfantIDs(ids...)
}

// AddLiensObjetIDs adds the "liens_objets" edge to the LienObjetEmplacement entity by IDs.
func (_u *EmplacementUpdate) AddLiensObjetIDs(ids ...uuid.UUID) *EmplacementUpdate {
	_u.mutation.AddLiensObjetIDs(ids...)
	return _u
}

// AddLiensObjets adds the "liens_objets" edges to the LienObjetEmplacement entity.
func (_u *EmplacementUpdate) AddLiensObjets(v ...*LienObjetEmplacement) *EmplacementUpdate {
	ids := make([]uuid.UUID, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddLiensObjetIDs(ids...)
}

// Mutation returns the EmplacementMutation object of the builder.
func (_u *EmplacementUpdate) Mutation() *EmplacementMutation {
	return _u.mutation
}

// ClearParent clears the "parent" edge to the Emplacement entity.
func (_u *EmplacementUpdate) ClearParent() *EmplacementUpdate {
	_u.mutation.ClearParent()
	return _u
}

// ClearEnfants clears all "enfants" edges to the Emplacement entity.
func (_u *EmplacementUpdate) ClearEnfants() *EmplacementUpdate {
	_u.mutation.ClearEnfants()
	return _u
}

// RemoveEnfantIDs removes the "enfants" edge to Emplacement entities by IDs.
func (_u *EmplacementUpdate) RemoveEnfantIDs(ids ...uuid.UUID) *EmplacementUpdate {
	_u.mutation.RemoveEnfantIDs(ids...)
	return _u
}

// RemoveEnfants removes "enfants" edges to Emplacement entities.
func (_u *EmplacementUpdate) RemoveEnfants(v ...*Emplacement) *EmplacementUpdate {
	ids := make([]uuid.UUID, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveEnfantIDs(ids...)
}

// ClearLiensObjets clears all "liens_objets" edges to the LienObjetEmplacement entity.
func (_u *EmplacementUpdate) ClearLiensObjets() *EmplacementUpdate {
	_u.mutation.ClearLiensObjets()
	return _u
}

// RemoveLiensObjetIDs removes the "liens_objets" edge to LienObjetEmplacement entities by IDs.
func (_u *EmplacementUpdate) RemoveLiensObjetIDs(ids ...uuid.UUID) *EmplacementUpdate {
	_u.mutation.RemoveLiensObjetIDs(ids...)
	return _u
}

// RemoveLiensObjets removes "liens_objets" edges to LienObjetEmplacement entities.
func (_u *EmplacementUpdate) RemoveLiensObjets(v ...*LienObjetEmplacement) *EmplacementUpdate {
	ids := make([]uuid.UUID, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveLiensObjetIDs(ids...)
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *EmplacementUpdate) Save(ctx context.Context) (int, error) {
	_u.defaults()
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (_u *EmplacementUpdate) SaveX(ctx context.Context) int {
	affected, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

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

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

// defaults sets the default values of the builder before save.
func (_u *EmplacementUpdate) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := emplacement.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *EmplacementUpdate) check() error {
	if v, ok := _u.mutation.Nom(); ok {
		if err := emplacement.NomValidator(v); err != nil {
			return &ValidationError{Name: "nom", err: fmt.Errorf(`ent: validator failed for field "Emplacement.nom": %w`, err)}
		}
	}
	return nil
}

func (_u *EmplacementUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(emplacement.Table, emplacement.Columns, sqlgraph.NewFieldSpec(emplacement.FieldID, field.TypeUUID))
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.Nom(); ok {
		_spec.SetField(emplacement.FieldNom, field.TypeString, value)
	}
	if value, ok := _u.mutation.Slug(); ok {
		_spec.SetField(emplacement.FieldSlug, field.TypeString, value)
	}
	if _u.mutation.SlugCleared() {
		_spec.ClearField(emplacement.FieldSlug, field.TypeString)
	}
	if value, ok := _u.mutation.Piece(); ok {
		_spec.SetField(emplacement.FieldPiece, field.TypeString, value)
	}
	if _u.mutation.PieceCleared() {
		_spec.ClearField(emplacement.FieldPiece, field.TypeString)
	}
	if value, ok := _u.mutation.Meuble(); ok {
		_spec.SetField(emplacement.FieldMeuble, field.TypeString, value)
	}
	if _u.mutation.MeubleCleared() {
		_spec.ClearField(emplacement.FieldMeuble, field.TypeString)
	}
	if value, ok := _u.mutation.NumeroBoite(); ok {
		_spec.SetField(emplacement.FieldNumeroBoite, field.TypeString, value)
	}
	if _u.mutation.NumeroBoiteCleared() {
		_spec.ClearField(emplacement.FieldNumeroBoite, field.TypeString)
	}
	if value, ok := _u.mutation.Icone(); ok {
		_spec.SetField(emplacement.FieldIcone, field.TypeString, value)
	}
	if _u.mutation.IconeCleared() {
		_spec.ClearField(emplacement.FieldIcone, field.TypeString)
	}
	if value, ok := _u.mutation.CreatedAt(); ok {
		_spec.SetField(emplacement.FieldCreatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(emplacement.FieldUpdatedAt, field.TypeTime, value)
	}
	if _u.mutation.ParentCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   emplacement.ParentTable,
			Columns: []string{emplacement.ParentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(emplacement.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.ParentIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   emplacement.ParentTable,
			Columns: []string{emplacement.ParentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(emplacement.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.EnfantsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   emplacement.EnfantsTable,
			Columns: []string{emplacement.EnfantsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(emplacement.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedEnfantsIDs(); len(nodes) > 0 && !_u.mutation.EnfantsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   emplacement.EnfantsTable,
			Columns: []string{emplacement.EnfantsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(emplacement.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.EnfantsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   emplacement.EnfantsTable,
			Columns: []string{emplacement.EnfantsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(emplacement.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.LiensObjetsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   emplacement.LiensObjetsTable,
			Columns: []string{emplacement.LiensObjetsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(lienobjetemplacement.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedLiensObjetsIDs(); len(nodes) > 0 && !_u.mutation.LiensObjetsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   emplacement.LiensObjetsTable,
			Columns: []string{emplacement.LiensObjetsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(lienobjetemplacement.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.LiensObjetsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   emplacement.LiensObjetsTable,
			Columns: []string{emplacement.LiensObjetsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(lienobjetemplacement.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{emplacement.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// EmplacementUpdateOne is the builder for updating a single Emplacement entity.
type EmplacementUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *EmplacementMutation
}

// SetNom sets the "nom" field.
func (_u *EmplacementUpdateOne) SetNom(v string) *EmplacementUpdateOne {
	_u.mutation.SetNom(v)
	return _u
}

// SetNillableNom sets the "nom" field if the given value is not nil.
func (_u *EmplacementUpdateOne) SetNillableNom(v *string) *EmplacementUpdateOne {
	if v != nil {
		_u.SetNom(*v)
	}
	return _u
}

// SetParentID sets the "parent_id" field.
func (_u *EmplacementUpdateOne) SetParentID(v uuid.UUID) *EmplacementUpdateOne {
	_u.mutation.SetParentID(v)
	return _u
}

// SetNillableParentID sets the "parent_id" field if the given value is not nil.
func (_u *EmplacementUpdateOne) SetNillableParentID(v *uuid.UUID) *EmplacementUpdateOne {
	if v != nil {
		_u.SetParentID(*v)
	}
	return _u
}

// ClearParentID clears the value of the "parent_id" field.
func (_u *EmplacementUpdateOne) ClearParentID() *EmplacementUpdateOne {
	_u.mutation.ClearParentID()
	return _u
}

// SetSlug sets the "slug" field.
func (_u *EmplacementUpdateOne) SetSlug(v string) *EmplacementUpdateOne {
	_u.mutation.SetSlug(v)
	return _u
}

// SetNillableSlug sets the "slug" field if the given value is not nil.
func (_u *EmplacementUpdateOne) SetNillableSlug(v *string) *EmplacementUpdateOne {
	if v != nil {
		_u.SetSlug(*v)
	}
	return _u
}

// ClearSlug clears the value of the "slug" field.
func (_u *EmplacementUpdateOne) ClearSlug() *EmplacementUpdateOne {
	_u.mutation.ClearSlug()
	return _u
}

// SetPiece sets the "piece" field.
func (_u *EmplacementUpdateOne) SetPiece(v string) *EmplacementUpdateOne {
	_u.mutation.SetPiece(v)
	return _u
}

// SetNillablePiece sets the "piece" field if the given value is not nil.
func (_u *EmplacementUpdateOne) SetNillablePiece(v *string) *EmplacementUpdateOne {
	if v != nil {
		_u.SetPiece(*v)
	}
	return _u
}

// ClearPiece clears the value of the "piece" field.
func (_u *EmplacementUpdateOne) ClearPiece() *EmplacementUpdateOne {
	_u.mutation.ClearPiece()
	return _u
}

// SetMeuble sets the "meuble" field.
func (_u *EmplacementUpdateOne) SetMeuble(v string) *EmplacementUpdateOne {
	_u.mutation.SetMeuble(v)
	return _u
}

// SetNillableMeuble sets the "meuble" field if the given value is not nil.
func (_u *EmplacementUpdateOne) SetNillableMeuble(v *string) *EmplacementUpdateOne {
	if v != nil {
		_u.SetMeuble(*v)
	}
	return _u
}

// ClearMeuble clears the value of the "meuble" field.
func (_u *EmplacementUpdateOne) ClearMeuble() *EmplacementUpdateOne {
	_u.mutation.ClearMeuble()
	return _u
}

// SetNumeroBoite sets the "numero_boite" field.
func (_u *EmplacementUpdateOne) SetNumeroBoite(v string) *EmplacementUpdateOne {
	_u.mutation.SetNumeroBoite(v)
	return _u
}

// SetNillableNumeroBoite sets the "numero_boite" field if the given value is not nil.
func (_u *EmplacementUpdateOne) SetNillableNumeroBoite(v *string) *EmplacementUpdateOne {
	if v != nil {
		_u.SetNumeroBoite(*v)
	}
	return _u
}

// ClearNumeroBoite clears the value of the "numero_boite" field.
func (_u *EmplacementUpdateOne) ClearNumeroBoite() *EmplacementUpdateOne {
	_u.mutation.ClearNumeroBoite()
	return _u
}

// SetIcone sets the "icone" field.
func (_u *EmplacementUpdateOne) SetIcone(v string) *EmplacementUpdateOne {
	_u.mutation.SetIcone(v)
	return _u
}

// SetNillableIcone sets the "icone" field if the given value is not nil.
func (_u *EmplacementUpdateOne) SetNillableIcone(v *string) *EmplacementUpdateOne {
	if v != nil {
		_u.SetIcone(*v)
	}
	return _u
}

// ClearIcone clears the value of the "icone" field.
func (_u *EmplacementUpdateOne) ClearIcone() *EmplacementUpdateOne {
	_u.mutation.ClearIcone()
	return _u
}

// SetCreatedAt sets the "created_at" field.
func (_u *EmplacementUpdateOne) SetCreatedAt(v time.Time) *EmplacementUpdateOne {
	_u.mutation.SetCreatedAt(v)
	return _u
}

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

// SetUpdatedAt sets the "updated_at" field.
func (_u *EmplacementUpdateOne) SetUpdatedAt(v time.Time) *EmplacementUpdateOne {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetParent sets the "parent" edge to the Emplacement entity.
func (_u *EmplacementUpdateOne) SetParent(v *Emplacement) *EmplacementUpdateOne {
	return _u.SetParentID(v.ID)
}

// AddEnfantIDs adds the "enfants" edge to the Emplacement entity by IDs.
func (_u *EmplacementUpdateOne) AddEnfantIDs(ids ...uuid.UUID) *EmplacementUpdateOne {
	_u.mutation.AddEnfantIDs(ids...)
	return _u
}

// AddEnfants adds the "enfants" edges to the Emplacement entity.
func (_u *EmplacementUpdateOne) AddEnfants(v ...*Emplacement) *EmplacementUpdateOne {
	ids := make([]uuid.UUID, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddEnfantIDs(ids...)
}

// AddLiensObjetIDs adds the "liens_objets" edge to the LienObjetEmplacement entity by IDs.
func (_u *EmplacementUpdateOne) AddLiensObjetIDs(ids ...uuid.UUID) *EmplacementUpdateOne {
	_u.mutation.AddLiensObjetIDs(ids...)
	return _u
}

// AddLiensObjets adds the "liens_objets" edges to the LienObjetEmplacement entity.
func (_u *EmplacementUpdateOne) AddLiensObjets(v ...*LienObjetEmplacement) *EmplacementUpdateOne {
	ids := make([]uuid.UUID, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddLiensObjetIDs(ids...)
}

// Mutation returns the EmplacementMutation object of the builder.
func (_u *EmplacementUpdateOne) Mutation() *EmplacementMutation {
	return _u.mutation
}

// ClearParent clears the "parent" edge to the Emplacement entity.
func (_u *EmplacementUpdateOne) ClearParent() *EmplacementUpdateOne {
	_u.mutation.ClearParent()
	return _u
}

// ClearEnfants clears all "enfants" edges to the Emplacement entity.
func (_u *EmplacementUpdateOne) ClearEnfants() *EmplacementUpdateOne {
	_u.mutation.ClearEnfants()
	return _u
}

// RemoveEnfantIDs removes the "enfants" edge to Emplacement entities by IDs.
func (_u *EmplacementUpdateOne) RemoveEnfantIDs(ids ...uuid.UUID) *EmplacementUpdateOne {
	_u.mutation.RemoveEnfantIDs(ids...)
	return _u
}

// RemoveEnfants removes "enfants" edges to Emplacement entities.
func (_u *EmplacementUpdateOne) RemoveEnfants(v ...*Emplacement) *EmplacementUpdateOne {
	ids := make([]uuid.UUID, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveEnfantIDs(ids...)
}

// ClearLiensObjets clears all "liens_objets" edges to the LienObjetEmplacement entity.
func (_u *EmplacementUpdateOne) ClearLiensObjets() *EmplacementUpdateOne {
	_u.mutation.ClearLiensObjets()
	return _u
}

// RemoveLiensObjetIDs removes the "liens_objets" edge to LienObjetEmplacement entities by IDs.
func (_u *EmplacementUpdateOne) RemoveLiensObjetIDs(ids ...uuid.UUID) *EmplacementUpdateOne {
	_u.mutation.RemoveLiensObjetIDs(ids...)
	return _u
}

// RemoveLiensObjets removes "liens_objets" edges to LienObjetEmplacement entities.
func (_u *EmplacementUpdateOne) RemoveLiensObjets(v ...*LienObjetEmplacement) *EmplacementUpdateOne {
	ids := make([]uuid.UUID, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveLiensObjetIDs(ids...)
}

// Where appends a list predicates to the EmplacementUpdate builder.
func (_u *EmplacementUpdateOne) Where(ps ...predicate.Emplacement) *EmplacementUpdateOne {
	_u.mutation.Where(ps...)
	return _u
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (_u *EmplacementUpdateOne) Select(field string, fields ...string) *EmplacementUpdateOne {
	_u.fields = append([]string{field}, fields...)
	return _u
}

// Save executes the query and returns the updated Emplacement entity.
func (_u *EmplacementUpdateOne) Save(ctx context.Context) (*Emplacement, error) {
	_u.defaults()
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (_u *EmplacementUpdateOne) SaveX(ctx context.Context) *Emplacement {
	node, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// Exec executes the query on the entity.
func (_u *EmplacementUpdateOne) Exec(ctx context.Context) error {
	_, err := _u.Save(ctx)
	return err
}

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

// defaults sets the default values of the builder before save.
func (_u *EmplacementUpdateOne) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := emplacement.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *EmplacementUpdateOne) check() error {
	if v, ok := _u.mutation.Nom(); ok {
		if err := emplacement.NomValidator(v); err != nil {
			return &ValidationError{Name: "nom", err: fmt.Errorf(`ent: validator failed for field "Emplacement.nom": %w`, err)}
		}
	}
	return nil
}

func (_u *EmplacementUpdateOne) sqlSave(ctx context.Context) (_node *Emplacement, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(emplacement.Table, emplacement.Columns, sqlgraph.NewFieldSpec(emplacement.FieldID, field.TypeUUID))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Emplacement.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := _u.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, emplacement.FieldID)
		for _, f := range fields {
			if !emplacement.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != emplacement.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.Nom(); ok {
		_spec.SetField(emplacement.FieldNom, field.TypeString, value)
	}
	if value, ok := _u.mutation.Slug(); ok {
		_spec.SetField(emplacement.FieldSlug, field.TypeString, value)
	}
	if _u.mutation.SlugCleared() {
		_spec.ClearField(emplacement.FieldSlug, field.TypeString)
	}
	if value, ok := _u.mutation.Piece(); ok {
		_spec.SetField(emplacement.FieldPiece, field.TypeString, value)
	}
	if _u.mutation.PieceCleared() {
		_spec.ClearField(emplacement.FieldPiece, field.TypeString)
	}
	if value, ok := _u.mutation.Meuble(); ok {
		_spec.SetField(emplacement.FieldMeuble, field.TypeString, value)
	}
	if _u.mutation.MeubleCleared() {
		_spec.ClearField(emplacement.FieldMeuble, field.TypeString)
	}
	if value, ok := _u.mutation.NumeroBoite(); ok {
		_spec.SetField(emplacement.FieldNumeroBoite, field.TypeString, value)
	}
	if _u.mutation.NumeroBoiteCleared() {
		_spec.ClearField(emplacement.FieldNumeroBoite, field.TypeString)
	}
	if value, ok := _u.mutation.Icone(); ok {
		_spec.SetField(emplacement.FieldIcone, field.TypeString, value)
	}
	if _u.mutation.IconeCleared() {
		_spec.ClearField(emplacement.FieldIcone, field.TypeString)
	}
	if value, ok := _u.mutation.CreatedAt(); ok {
		_spec.SetField(emplacement.FieldCreatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(emplacement.FieldUpdatedAt, field.TypeTime, value)
	}
	if _u.mutation.ParentCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   emplacement.ParentTable,
			Columns: []string{emplacement.ParentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(emplacement.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.ParentIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   emplacement.ParentTable,
			Columns: []string{emplacement.ParentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(emplacement.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.EnfantsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   emplacement.EnfantsTable,
			Columns: []string{emplacement.EnfantsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(emplacement.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedEnfantsIDs(); len(nodes) > 0 && !_u.mutation.EnfantsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   emplacement.EnfantsTable,
			Columns: []string{emplacement.EnfantsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(emplacement.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.EnfantsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   emplacement.EnfantsTable,
			Columns: []string{emplacement.EnfantsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(emplacement.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.LiensObjetsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   emplacement.LiensObjetsTable,
			Columns: []string{emplacement.LiensObjetsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(lienobjetemplacement.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedLiensObjetsIDs(); len(nodes) > 0 && !_u.mutation.LiensObjetsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   emplacement.LiensObjetsTable,
			Columns: []string{emplacement.LiensObjetsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(lienobjetemplacement.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.LiensObjetsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   emplacement.LiensObjetsTable,
			Columns: []string{emplacement.LiensObjetsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(lienobjetemplacement.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Emplacement{config: _u.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{emplacement.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
