// Code generated by ent, DO NOT EDIT.

package ent

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

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

// LabelRelationshipUpdate is the builder for updating LabelRelationship entities.
type LabelRelationshipUpdate struct {
	config
	hooks    []Hook
	mutation *LabelRelationshipMutation
}

// Where appends a list predicates to the LabelRelationshipUpdate builder.
func (lru *LabelRelationshipUpdate) Where(ps ...predicate.LabelRelationship) *LabelRelationshipUpdate {
	lru.mutation.Where(ps...)
	return lru
}

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

// SetStatus sets the "status" field.
func (lru *LabelRelationshipUpdate) SetStatus(u uint8) *LabelRelationshipUpdate {
	lru.mutation.ResetStatus()
	lru.mutation.SetStatus(u)
	return lru
}

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

// AddStatus adds u to the "status" field.
func (lru *LabelRelationshipUpdate) AddStatus(u int8) *LabelRelationshipUpdate {
	lru.mutation.AddStatus(u)
	return lru
}

// ClearStatus clears the value of the "status" field.
func (lru *LabelRelationshipUpdate) ClearStatus() *LabelRelationshipUpdate {
	lru.mutation.ClearStatus()
	return lru
}

// SetLabelID sets the "label_id" field.
func (lru *LabelRelationshipUpdate) SetLabelID(u uint64) *LabelRelationshipUpdate {
	lru.mutation.SetLabelID(u)
	return lru
}

// SetNillableLabelID sets the "label_id" field if the given value is not nil.
func (lru *LabelRelationshipUpdate) SetNillableLabelID(u *uint64) *LabelRelationshipUpdate {
	if u != nil {
		lru.SetLabelID(*u)
	}
	return lru
}

// SetContactID sets the "contact_id" field.
func (lru *LabelRelationshipUpdate) SetContactID(u uint64) *LabelRelationshipUpdate {
	lru.mutation.SetContactID(u)
	return lru
}

// SetNillableContactID sets the "contact_id" field if the given value is not nil.
func (lru *LabelRelationshipUpdate) SetNillableContactID(u *uint64) *LabelRelationshipUpdate {
	if u != nil {
		lru.SetContactID(*u)
	}
	return lru
}

// SetOrganizationID sets the "organization_id" field.
func (lru *LabelRelationshipUpdate) SetOrganizationID(u uint64) *LabelRelationshipUpdate {
	lru.mutation.ResetOrganizationID()
	lru.mutation.SetOrganizationID(u)
	return lru
}

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

// AddOrganizationID adds u to the "organization_id" field.
func (lru *LabelRelationshipUpdate) AddOrganizationID(u int64) *LabelRelationshipUpdate {
	lru.mutation.AddOrganizationID(u)
	return lru
}

// ClearOrganizationID clears the value of the "organization_id" field.
func (lru *LabelRelationshipUpdate) ClearOrganizationID() *LabelRelationshipUpdate {
	lru.mutation.ClearOrganizationID()
	return lru
}

// SetContactsID sets the "contacts" edge to the Contact entity by ID.
func (lru *LabelRelationshipUpdate) SetContactsID(id uint64) *LabelRelationshipUpdate {
	lru.mutation.SetContactsID(id)
	return lru
}

// SetContacts sets the "contacts" edge to the Contact entity.
func (lru *LabelRelationshipUpdate) SetContacts(c *Contact) *LabelRelationshipUpdate {
	return lru.SetContactsID(c.ID)
}

// SetLabelsID sets the "labels" edge to the Label entity by ID.
func (lru *LabelRelationshipUpdate) SetLabelsID(id uint64) *LabelRelationshipUpdate {
	lru.mutation.SetLabelsID(id)
	return lru
}

// SetLabels sets the "labels" edge to the Label entity.
func (lru *LabelRelationshipUpdate) SetLabels(l *Label) *LabelRelationshipUpdate {
	return lru.SetLabelsID(l.ID)
}

// Mutation returns the LabelRelationshipMutation object of the builder.
func (lru *LabelRelationshipUpdate) Mutation() *LabelRelationshipMutation {
	return lru.mutation
}

// ClearContacts clears the "contacts" edge to the Contact entity.
func (lru *LabelRelationshipUpdate) ClearContacts() *LabelRelationshipUpdate {
	lru.mutation.ClearContacts()
	return lru
}

// ClearLabels clears the "labels" edge to the Label entity.
func (lru *LabelRelationshipUpdate) ClearLabels() *LabelRelationshipUpdate {
	lru.mutation.ClearLabels()
	return lru
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (lru *LabelRelationshipUpdate) check() error {
	if _, ok := lru.mutation.ContactsID(); lru.mutation.ContactsCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "LabelRelationship.contacts"`)
	}
	if _, ok := lru.mutation.LabelsID(); lru.mutation.LabelsCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "LabelRelationship.labels"`)
	}
	return nil
}

func (lru *LabelRelationshipUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := lru.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(labelrelationship.Table, labelrelationship.Columns, sqlgraph.NewFieldSpec(labelrelationship.FieldID, field.TypeUint64))
	if ps := lru.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := lru.mutation.UpdatedAt(); ok {
		_spec.SetField(labelrelationship.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := lru.mutation.Status(); ok {
		_spec.SetField(labelrelationship.FieldStatus, field.TypeUint8, value)
	}
	if value, ok := lru.mutation.AddedStatus(); ok {
		_spec.AddField(labelrelationship.FieldStatus, field.TypeUint8, value)
	}
	if lru.mutation.StatusCleared() {
		_spec.ClearField(labelrelationship.FieldStatus, field.TypeUint8)
	}
	if value, ok := lru.mutation.OrganizationID(); ok {
		_spec.SetField(labelrelationship.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := lru.mutation.AddedOrganizationID(); ok {
		_spec.AddField(labelrelationship.FieldOrganizationID, field.TypeUint64, value)
	}
	if lru.mutation.OrganizationIDCleared() {
		_spec.ClearField(labelrelationship.FieldOrganizationID, field.TypeUint64)
	}
	if lru.mutation.ContactsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   labelrelationship.ContactsTable,
			Columns: []string{labelrelationship.ContactsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(contact.FieldID, field.TypeUint64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := lru.mutation.ContactsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   labelrelationship.ContactsTable,
			Columns: []string{labelrelationship.ContactsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(contact.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if lru.mutation.LabelsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   labelrelationship.LabelsTable,
			Columns: []string{labelrelationship.LabelsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(label.FieldID, field.TypeUint64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := lru.mutation.LabelsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   labelrelationship.LabelsTable,
			Columns: []string{labelrelationship.LabelsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(label.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, lru.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{labelrelationship.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	lru.mutation.done = true
	return n, nil
}

// LabelRelationshipUpdateOne is the builder for updating a single LabelRelationship entity.
type LabelRelationshipUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *LabelRelationshipMutation
}

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

// SetStatus sets the "status" field.
func (lruo *LabelRelationshipUpdateOne) SetStatus(u uint8) *LabelRelationshipUpdateOne {
	lruo.mutation.ResetStatus()
	lruo.mutation.SetStatus(u)
	return lruo
}

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

// AddStatus adds u to the "status" field.
func (lruo *LabelRelationshipUpdateOne) AddStatus(u int8) *LabelRelationshipUpdateOne {
	lruo.mutation.AddStatus(u)
	return lruo
}

// ClearStatus clears the value of the "status" field.
func (lruo *LabelRelationshipUpdateOne) ClearStatus() *LabelRelationshipUpdateOne {
	lruo.mutation.ClearStatus()
	return lruo
}

// SetLabelID sets the "label_id" field.
func (lruo *LabelRelationshipUpdateOne) SetLabelID(u uint64) *LabelRelationshipUpdateOne {
	lruo.mutation.SetLabelID(u)
	return lruo
}

// SetNillableLabelID sets the "label_id" field if the given value is not nil.
func (lruo *LabelRelationshipUpdateOne) SetNillableLabelID(u *uint64) *LabelRelationshipUpdateOne {
	if u != nil {
		lruo.SetLabelID(*u)
	}
	return lruo
}

// SetContactID sets the "contact_id" field.
func (lruo *LabelRelationshipUpdateOne) SetContactID(u uint64) *LabelRelationshipUpdateOne {
	lruo.mutation.SetContactID(u)
	return lruo
}

// SetNillableContactID sets the "contact_id" field if the given value is not nil.
func (lruo *LabelRelationshipUpdateOne) SetNillableContactID(u *uint64) *LabelRelationshipUpdateOne {
	if u != nil {
		lruo.SetContactID(*u)
	}
	return lruo
}

// SetOrganizationID sets the "organization_id" field.
func (lruo *LabelRelationshipUpdateOne) SetOrganizationID(u uint64) *LabelRelationshipUpdateOne {
	lruo.mutation.ResetOrganizationID()
	lruo.mutation.SetOrganizationID(u)
	return lruo
}

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

// AddOrganizationID adds u to the "organization_id" field.
func (lruo *LabelRelationshipUpdateOne) AddOrganizationID(u int64) *LabelRelationshipUpdateOne {
	lruo.mutation.AddOrganizationID(u)
	return lruo
}

// ClearOrganizationID clears the value of the "organization_id" field.
func (lruo *LabelRelationshipUpdateOne) ClearOrganizationID() *LabelRelationshipUpdateOne {
	lruo.mutation.ClearOrganizationID()
	return lruo
}

// SetContactsID sets the "contacts" edge to the Contact entity by ID.
func (lruo *LabelRelationshipUpdateOne) SetContactsID(id uint64) *LabelRelationshipUpdateOne {
	lruo.mutation.SetContactsID(id)
	return lruo
}

// SetContacts sets the "contacts" edge to the Contact entity.
func (lruo *LabelRelationshipUpdateOne) SetContacts(c *Contact) *LabelRelationshipUpdateOne {
	return lruo.SetContactsID(c.ID)
}

// SetLabelsID sets the "labels" edge to the Label entity by ID.
func (lruo *LabelRelationshipUpdateOne) SetLabelsID(id uint64) *LabelRelationshipUpdateOne {
	lruo.mutation.SetLabelsID(id)
	return lruo
}

// SetLabels sets the "labels" edge to the Label entity.
func (lruo *LabelRelationshipUpdateOne) SetLabels(l *Label) *LabelRelationshipUpdateOne {
	return lruo.SetLabelsID(l.ID)
}

// Mutation returns the LabelRelationshipMutation object of the builder.
func (lruo *LabelRelationshipUpdateOne) Mutation() *LabelRelationshipMutation {
	return lruo.mutation
}

// ClearContacts clears the "contacts" edge to the Contact entity.
func (lruo *LabelRelationshipUpdateOne) ClearContacts() *LabelRelationshipUpdateOne {
	lruo.mutation.ClearContacts()
	return lruo
}

// ClearLabels clears the "labels" edge to the Label entity.
func (lruo *LabelRelationshipUpdateOne) ClearLabels() *LabelRelationshipUpdateOne {
	lruo.mutation.ClearLabels()
	return lruo
}

// Where appends a list predicates to the LabelRelationshipUpdate builder.
func (lruo *LabelRelationshipUpdateOne) Where(ps ...predicate.LabelRelationship) *LabelRelationshipUpdateOne {
	lruo.mutation.Where(ps...)
	return lruo
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (lruo *LabelRelationshipUpdateOne) check() error {
	if _, ok := lruo.mutation.ContactsID(); lruo.mutation.ContactsCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "LabelRelationship.contacts"`)
	}
	if _, ok := lruo.mutation.LabelsID(); lruo.mutation.LabelsCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "LabelRelationship.labels"`)
	}
	return nil
}

func (lruo *LabelRelationshipUpdateOne) sqlSave(ctx context.Context) (_node *LabelRelationship, err error) {
	if err := lruo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(labelrelationship.Table, labelrelationship.Columns, sqlgraph.NewFieldSpec(labelrelationship.FieldID, field.TypeUint64))
	id, ok := lruo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "LabelRelationship.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := lruo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, labelrelationship.FieldID)
		for _, f := range fields {
			if !labelrelationship.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != labelrelationship.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := lruo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := lruo.mutation.UpdatedAt(); ok {
		_spec.SetField(labelrelationship.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := lruo.mutation.Status(); ok {
		_spec.SetField(labelrelationship.FieldStatus, field.TypeUint8, value)
	}
	if value, ok := lruo.mutation.AddedStatus(); ok {
		_spec.AddField(labelrelationship.FieldStatus, field.TypeUint8, value)
	}
	if lruo.mutation.StatusCleared() {
		_spec.ClearField(labelrelationship.FieldStatus, field.TypeUint8)
	}
	if value, ok := lruo.mutation.OrganizationID(); ok {
		_spec.SetField(labelrelationship.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := lruo.mutation.AddedOrganizationID(); ok {
		_spec.AddField(labelrelationship.FieldOrganizationID, field.TypeUint64, value)
	}
	if lruo.mutation.OrganizationIDCleared() {
		_spec.ClearField(labelrelationship.FieldOrganizationID, field.TypeUint64)
	}
	if lruo.mutation.ContactsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   labelrelationship.ContactsTable,
			Columns: []string{labelrelationship.ContactsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(contact.FieldID, field.TypeUint64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := lruo.mutation.ContactsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   labelrelationship.ContactsTable,
			Columns: []string{labelrelationship.ContactsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(contact.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if lruo.mutation.LabelsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   labelrelationship.LabelsTable,
			Columns: []string{labelrelationship.LabelsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(label.FieldID, field.TypeUint64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := lruo.mutation.LabelsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   labelrelationship.LabelsTable,
			Columns: []string{labelrelationship.LabelsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(label.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &LabelRelationship{config: lruo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, lruo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{labelrelationship.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	lruo.mutation.done = true
	return _node, nil
}
