// Code generated by ent, DO NOT EDIT.

package ent

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

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

// ContactFieldUpdate is the builder for updating ContactField entities.
type ContactFieldUpdate struct {
	config
	hooks    []Hook
	mutation *ContactFieldMutation
}

// Where appends a list predicates to the ContactFieldUpdate builder.
func (cfu *ContactFieldUpdate) Where(ps ...predicate.ContactField) *ContactFieldUpdate {
	cfu.mutation.Where(ps...)
	return cfu
}

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

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

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

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

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

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

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

// ClearDeletedAt clears the value of the "deleted_at" field.
func (cfu *ContactFieldUpdate) ClearDeletedAt() *ContactFieldUpdate {
	cfu.mutation.ClearDeletedAt()
	return cfu
}

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

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

// SetFormID sets the "form_id" field.
func (cfu *ContactFieldUpdate) SetFormID(s string) *ContactFieldUpdate {
	cfu.mutation.SetFormID(s)
	return cfu
}

// SetNillableFormID sets the "form_id" field if the given value is not nil.
func (cfu *ContactFieldUpdate) SetNillableFormID(s *string) *ContactFieldUpdate {
	if s != nil {
		cfu.SetFormID(*s)
	}
	return cfu
}

// SetValue sets the "value" field.
func (cfu *ContactFieldUpdate) SetValue(s []string) *ContactFieldUpdate {
	cfu.mutation.SetValue(s)
	return cfu
}

// AppendValue appends s to the "value" field.
func (cfu *ContactFieldUpdate) AppendValue(s []string) *ContactFieldUpdate {
	cfu.mutation.AppendValue(s)
	return cfu
}

// SetFieldContactID sets the "field_contact" edge to the Contact entity by ID.
func (cfu *ContactFieldUpdate) SetFieldContactID(id uint64) *ContactFieldUpdate {
	cfu.mutation.SetFieldContactID(id)
	return cfu
}

// SetFieldContact sets the "field_contact" edge to the Contact entity.
func (cfu *ContactFieldUpdate) SetFieldContact(c *Contact) *ContactFieldUpdate {
	return cfu.SetFieldContactID(c.ID)
}

// Mutation returns the ContactFieldMutation object of the builder.
func (cfu *ContactFieldUpdate) Mutation() *ContactFieldMutation {
	return cfu.mutation
}

// ClearFieldContact clears the "field_contact" edge to the Contact entity.
func (cfu *ContactFieldUpdate) ClearFieldContact() *ContactFieldUpdate {
	cfu.mutation.ClearFieldContact()
	return cfu
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (cfu *ContactFieldUpdate) Save(ctx context.Context) (int, error) {
	if err := cfu.defaults(); err != nil {
		return 0, err
	}
	return withHooks(ctx, cfu.sqlSave, cfu.mutation, cfu.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (cfu *ContactFieldUpdate) defaults() error {
	if _, ok := cfu.mutation.UpdatedAt(); !ok {
		if contactfield.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized contactfield.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := contactfield.UpdateDefaultUpdatedAt()
		cfu.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (cfu *ContactFieldUpdate) check() error {
	if _, ok := cfu.mutation.FieldContactID(); cfu.mutation.FieldContactCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "ContactField.field_contact"`)
	}
	return nil
}

func (cfu *ContactFieldUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := cfu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(contactfield.Table, contactfield.Columns, sqlgraph.NewFieldSpec(contactfield.FieldID, field.TypeUint64))
	if ps := cfu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := cfu.mutation.UpdatedAt(); ok {
		_spec.SetField(contactfield.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := cfu.mutation.Status(); ok {
		_spec.SetField(contactfield.FieldStatus, field.TypeUint8, value)
	}
	if value, ok := cfu.mutation.AddedStatus(); ok {
		_spec.AddField(contactfield.FieldStatus, field.TypeUint8, value)
	}
	if cfu.mutation.StatusCleared() {
		_spec.ClearField(contactfield.FieldStatus, field.TypeUint8)
	}
	if value, ok := cfu.mutation.DeletedAt(); ok {
		_spec.SetField(contactfield.FieldDeletedAt, field.TypeTime, value)
	}
	if cfu.mutation.DeletedAtCleared() {
		_spec.ClearField(contactfield.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := cfu.mutation.FormID(); ok {
		_spec.SetField(contactfield.FieldFormID, field.TypeString, value)
	}
	if value, ok := cfu.mutation.Value(); ok {
		_spec.SetField(contactfield.FieldValue, field.TypeJSON, value)
	}
	if value, ok := cfu.mutation.AppendedValue(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, contactfield.FieldValue, value)
		})
	}
	if cfu.mutation.FieldContactCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   contactfield.FieldContactTable,
			Columns: []string{contactfield.FieldContactColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(contact.FieldID, field.TypeUint64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := cfu.mutation.FieldContactIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   contactfield.FieldContactTable,
			Columns: []string{contactfield.FieldContactColumn},
			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 n, err = sqlgraph.UpdateNodes(ctx, cfu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{contactfield.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	cfu.mutation.done = true
	return n, nil
}

// ContactFieldUpdateOne is the builder for updating a single ContactField entity.
type ContactFieldUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *ContactFieldMutation
}

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

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

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

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

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

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

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

// ClearDeletedAt clears the value of the "deleted_at" field.
func (cfuo *ContactFieldUpdateOne) ClearDeletedAt() *ContactFieldUpdateOne {
	cfuo.mutation.ClearDeletedAt()
	return cfuo
}

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

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

// SetFormID sets the "form_id" field.
func (cfuo *ContactFieldUpdateOne) SetFormID(s string) *ContactFieldUpdateOne {
	cfuo.mutation.SetFormID(s)
	return cfuo
}

// SetNillableFormID sets the "form_id" field if the given value is not nil.
func (cfuo *ContactFieldUpdateOne) SetNillableFormID(s *string) *ContactFieldUpdateOne {
	if s != nil {
		cfuo.SetFormID(*s)
	}
	return cfuo
}

// SetValue sets the "value" field.
func (cfuo *ContactFieldUpdateOne) SetValue(s []string) *ContactFieldUpdateOne {
	cfuo.mutation.SetValue(s)
	return cfuo
}

// AppendValue appends s to the "value" field.
func (cfuo *ContactFieldUpdateOne) AppendValue(s []string) *ContactFieldUpdateOne {
	cfuo.mutation.AppendValue(s)
	return cfuo
}

// SetFieldContactID sets the "field_contact" edge to the Contact entity by ID.
func (cfuo *ContactFieldUpdateOne) SetFieldContactID(id uint64) *ContactFieldUpdateOne {
	cfuo.mutation.SetFieldContactID(id)
	return cfuo
}

// SetFieldContact sets the "field_contact" edge to the Contact entity.
func (cfuo *ContactFieldUpdateOne) SetFieldContact(c *Contact) *ContactFieldUpdateOne {
	return cfuo.SetFieldContactID(c.ID)
}

// Mutation returns the ContactFieldMutation object of the builder.
func (cfuo *ContactFieldUpdateOne) Mutation() *ContactFieldMutation {
	return cfuo.mutation
}

// ClearFieldContact clears the "field_contact" edge to the Contact entity.
func (cfuo *ContactFieldUpdateOne) ClearFieldContact() *ContactFieldUpdateOne {
	cfuo.mutation.ClearFieldContact()
	return cfuo
}

// Where appends a list predicates to the ContactFieldUpdate builder.
func (cfuo *ContactFieldUpdateOne) Where(ps ...predicate.ContactField) *ContactFieldUpdateOne {
	cfuo.mutation.Where(ps...)
	return cfuo
}

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

// Save executes the query and returns the updated ContactField entity.
func (cfuo *ContactFieldUpdateOne) Save(ctx context.Context) (*ContactField, error) {
	if err := cfuo.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, cfuo.sqlSave, cfuo.mutation, cfuo.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (cfuo *ContactFieldUpdateOne) defaults() error {
	if _, ok := cfuo.mutation.UpdatedAt(); !ok {
		if contactfield.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized contactfield.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := contactfield.UpdateDefaultUpdatedAt()
		cfuo.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (cfuo *ContactFieldUpdateOne) check() error {
	if _, ok := cfuo.mutation.FieldContactID(); cfuo.mutation.FieldContactCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "ContactField.field_contact"`)
	}
	return nil
}

func (cfuo *ContactFieldUpdateOne) sqlSave(ctx context.Context) (_node *ContactField, err error) {
	if err := cfuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(contactfield.Table, contactfield.Columns, sqlgraph.NewFieldSpec(contactfield.FieldID, field.TypeUint64))
	id, ok := cfuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "ContactField.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := cfuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, contactfield.FieldID)
		for _, f := range fields {
			if !contactfield.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != contactfield.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := cfuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := cfuo.mutation.UpdatedAt(); ok {
		_spec.SetField(contactfield.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := cfuo.mutation.Status(); ok {
		_spec.SetField(contactfield.FieldStatus, field.TypeUint8, value)
	}
	if value, ok := cfuo.mutation.AddedStatus(); ok {
		_spec.AddField(contactfield.FieldStatus, field.TypeUint8, value)
	}
	if cfuo.mutation.StatusCleared() {
		_spec.ClearField(contactfield.FieldStatus, field.TypeUint8)
	}
	if value, ok := cfuo.mutation.DeletedAt(); ok {
		_spec.SetField(contactfield.FieldDeletedAt, field.TypeTime, value)
	}
	if cfuo.mutation.DeletedAtCleared() {
		_spec.ClearField(contactfield.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := cfuo.mutation.FormID(); ok {
		_spec.SetField(contactfield.FieldFormID, field.TypeString, value)
	}
	if value, ok := cfuo.mutation.Value(); ok {
		_spec.SetField(contactfield.FieldValue, field.TypeJSON, value)
	}
	if value, ok := cfuo.mutation.AppendedValue(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, contactfield.FieldValue, value)
		})
	}
	if cfuo.mutation.FieldContactCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   contactfield.FieldContactTable,
			Columns: []string{contactfield.FieldContactColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(contact.FieldID, field.TypeUint64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := cfuo.mutation.FieldContactIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   contactfield.FieldContactTable,
			Columns: []string{contactfield.FieldContactColumn},
			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)
	}
	_node = &ContactField{config: cfuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, cfuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{contactfield.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	cfuo.mutation.done = true
	return _node, nil
}
