// 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"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/predicate"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/user"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/usermeta"
)

// UserMetaUpdate is the builder for updating UserMeta entities.
type UserMetaUpdate struct {
	config
	hooks     []Hook
	mutation  *UserMetaMutation
	modifiers []func(*sql.UpdateBuilder)
}

// Where appends a list predicates to the UserMetaUpdate builder.
func (umu *UserMetaUpdate) Where(ps ...predicate.UserMeta) *UserMetaUpdate {
	umu.mutation.Where(ps...)
	return umu
}

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

// SetVersion sets the "version" field.
func (umu *UserMetaUpdate) SetVersion(i int64) *UserMetaUpdate {
	umu.mutation.ResetVersion()
	umu.mutation.SetVersion(i)
	return umu
}

// SetNillableVersion sets the "version" field if the given value is not nil.
func (umu *UserMetaUpdate) SetNillableVersion(i *int64) *UserMetaUpdate {
	if i != nil {
		umu.SetVersion(*i)
	}
	return umu
}

// AddVersion adds i to the "version" field.
func (umu *UserMetaUpdate) AddVersion(i int64) *UserMetaUpdate {
	umu.mutation.AddVersion(i)
	return umu
}

// SetUserID sets the "user_id" field.
func (umu *UserMetaUpdate) SetUserID(i int64) *UserMetaUpdate {
	umu.mutation.SetUserID(i)
	return umu
}

// SetKey sets the "key" field.
func (umu *UserMetaUpdate) SetKey(s string) *UserMetaUpdate {
	umu.mutation.SetKey(s)
	return umu
}

// SetNillableKey sets the "key" field if the given value is not nil.
func (umu *UserMetaUpdate) SetNillableKey(s *string) *UserMetaUpdate {
	if s != nil {
		umu.SetKey(*s)
	}
	return umu
}

// ClearKey clears the value of the "key" field.
func (umu *UserMetaUpdate) ClearKey() *UserMetaUpdate {
	umu.mutation.ClearKey()
	return umu
}

// SetValue sets the "value" field.
func (umu *UserMetaUpdate) SetValue(s string) *UserMetaUpdate {
	umu.mutation.SetValue(s)
	return umu
}

// SetNillableValue sets the "value" field if the given value is not nil.
func (umu *UserMetaUpdate) SetNillableValue(s *string) *UserMetaUpdate {
	if s != nil {
		umu.SetValue(*s)
	}
	return umu
}

// ClearValue clears the value of the "value" field.
func (umu *UserMetaUpdate) ClearValue() *UserMetaUpdate {
	umu.mutation.ClearValue()
	return umu
}

// SetUser sets the "user" edge to the User entity.
func (umu *UserMetaUpdate) SetUser(u *User) *UserMetaUpdate {
	return umu.SetUserID(u.ID)
}

// Mutation returns the UserMetaMutation object of the builder.
func (umu *UserMetaUpdate) Mutation() *UserMetaMutation {
	return umu.mutation
}

// ClearUser clears the "user" edge to the User entity.
func (umu *UserMetaUpdate) ClearUser() *UserMetaUpdate {
	umu.mutation.ClearUser()
	return umu
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (umu *UserMetaUpdate) check() error {
	if _, ok := umu.mutation.UserID(); umu.mutation.UserCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "UserMeta.user"`)
	}
	return nil
}

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (umu *UserMetaUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *UserMetaUpdate {
	umu.modifiers = append(umu.modifiers, modifiers...)
	return umu
}

func (umu *UserMetaUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := umu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(usermeta.Table, usermeta.Columns, sqlgraph.NewFieldSpec(usermeta.FieldID, field.TypeInt64))
	if ps := umu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := umu.mutation.UpdatedAt(); ok {
		_spec.SetField(usermeta.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := umu.mutation.Version(); ok {
		_spec.SetField(usermeta.FieldVersion, field.TypeInt64, value)
	}
	if value, ok := umu.mutation.AddedVersion(); ok {
		_spec.AddField(usermeta.FieldVersion, field.TypeInt64, value)
	}
	if value, ok := umu.mutation.Key(); ok {
		_spec.SetField(usermeta.FieldKey, field.TypeString, value)
	}
	if umu.mutation.KeyCleared() {
		_spec.ClearField(usermeta.FieldKey, field.TypeString)
	}
	if value, ok := umu.mutation.Value(); ok {
		_spec.SetField(usermeta.FieldValue, field.TypeString, value)
	}
	if umu.mutation.ValueCleared() {
		_spec.ClearField(usermeta.FieldValue, field.TypeString)
	}
	if umu.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   usermeta.UserTable,
			Columns: []string{usermeta.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := umu.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   usermeta.UserTable,
			Columns: []string{usermeta.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_spec.AddModifiers(umu.modifiers...)
	if n, err = sqlgraph.UpdateNodes(ctx, umu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{usermeta.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	umu.mutation.done = true
	return n, nil
}

// UserMetaUpdateOne is the builder for updating a single UserMeta entity.
type UserMetaUpdateOne struct {
	config
	fields    []string
	hooks     []Hook
	mutation  *UserMetaMutation
	modifiers []func(*sql.UpdateBuilder)
}

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

// SetVersion sets the "version" field.
func (umuo *UserMetaUpdateOne) SetVersion(i int64) *UserMetaUpdateOne {
	umuo.mutation.ResetVersion()
	umuo.mutation.SetVersion(i)
	return umuo
}

// SetNillableVersion sets the "version" field if the given value is not nil.
func (umuo *UserMetaUpdateOne) SetNillableVersion(i *int64) *UserMetaUpdateOne {
	if i != nil {
		umuo.SetVersion(*i)
	}
	return umuo
}

// AddVersion adds i to the "version" field.
func (umuo *UserMetaUpdateOne) AddVersion(i int64) *UserMetaUpdateOne {
	umuo.mutation.AddVersion(i)
	return umuo
}

// SetUserID sets the "user_id" field.
func (umuo *UserMetaUpdateOne) SetUserID(i int64) *UserMetaUpdateOne {
	umuo.mutation.SetUserID(i)
	return umuo
}

// SetKey sets the "key" field.
func (umuo *UserMetaUpdateOne) SetKey(s string) *UserMetaUpdateOne {
	umuo.mutation.SetKey(s)
	return umuo
}

// SetNillableKey sets the "key" field if the given value is not nil.
func (umuo *UserMetaUpdateOne) SetNillableKey(s *string) *UserMetaUpdateOne {
	if s != nil {
		umuo.SetKey(*s)
	}
	return umuo
}

// ClearKey clears the value of the "key" field.
func (umuo *UserMetaUpdateOne) ClearKey() *UserMetaUpdateOne {
	umuo.mutation.ClearKey()
	return umuo
}

// SetValue sets the "value" field.
func (umuo *UserMetaUpdateOne) SetValue(s string) *UserMetaUpdateOne {
	umuo.mutation.SetValue(s)
	return umuo
}

// SetNillableValue sets the "value" field if the given value is not nil.
func (umuo *UserMetaUpdateOne) SetNillableValue(s *string) *UserMetaUpdateOne {
	if s != nil {
		umuo.SetValue(*s)
	}
	return umuo
}

// ClearValue clears the value of the "value" field.
func (umuo *UserMetaUpdateOne) ClearValue() *UserMetaUpdateOne {
	umuo.mutation.ClearValue()
	return umuo
}

// SetUser sets the "user" edge to the User entity.
func (umuo *UserMetaUpdateOne) SetUser(u *User) *UserMetaUpdateOne {
	return umuo.SetUserID(u.ID)
}

// Mutation returns the UserMetaMutation object of the builder.
func (umuo *UserMetaUpdateOne) Mutation() *UserMetaMutation {
	return umuo.mutation
}

// ClearUser clears the "user" edge to the User entity.
func (umuo *UserMetaUpdateOne) ClearUser() *UserMetaUpdateOne {
	umuo.mutation.ClearUser()
	return umuo
}

// Where appends a list predicates to the UserMetaUpdate builder.
func (umuo *UserMetaUpdateOne) Where(ps ...predicate.UserMeta) *UserMetaUpdateOne {
	umuo.mutation.Where(ps...)
	return umuo
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (umuo *UserMetaUpdateOne) check() error {
	if _, ok := umuo.mutation.UserID(); umuo.mutation.UserCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "UserMeta.user"`)
	}
	return nil
}

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (umuo *UserMetaUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *UserMetaUpdateOne {
	umuo.modifiers = append(umuo.modifiers, modifiers...)
	return umuo
}

func (umuo *UserMetaUpdateOne) sqlSave(ctx context.Context) (_node *UserMeta, err error) {
	if err := umuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(usermeta.Table, usermeta.Columns, sqlgraph.NewFieldSpec(usermeta.FieldID, field.TypeInt64))
	id, ok := umuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "UserMeta.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := umuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, usermeta.FieldID)
		for _, f := range fields {
			if !usermeta.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != usermeta.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := umuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := umuo.mutation.UpdatedAt(); ok {
		_spec.SetField(usermeta.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := umuo.mutation.Version(); ok {
		_spec.SetField(usermeta.FieldVersion, field.TypeInt64, value)
	}
	if value, ok := umuo.mutation.AddedVersion(); ok {
		_spec.AddField(usermeta.FieldVersion, field.TypeInt64, value)
	}
	if value, ok := umuo.mutation.Key(); ok {
		_spec.SetField(usermeta.FieldKey, field.TypeString, value)
	}
	if umuo.mutation.KeyCleared() {
		_spec.ClearField(usermeta.FieldKey, field.TypeString)
	}
	if value, ok := umuo.mutation.Value(); ok {
		_spec.SetField(usermeta.FieldValue, field.TypeString, value)
	}
	if umuo.mutation.ValueCleared() {
		_spec.ClearField(usermeta.FieldValue, field.TypeString)
	}
	if umuo.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   usermeta.UserTable,
			Columns: []string{usermeta.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := umuo.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   usermeta.UserTable,
			Columns: []string{usermeta.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_spec.AddModifiers(umuo.modifiers...)
	_node = &UserMeta{config: umuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, umuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{usermeta.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	umuo.mutation.done = true
	return _node, nil
}
