// 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/usersocial"
)

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

// Where appends a list predicates to the UserSocialUpdate builder.
func (usu *UserSocialUpdate) Where(ps ...predicate.UserSocial) *UserSocialUpdate {
	usu.mutation.Where(ps...)
	return usu
}

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

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

// SetProvider sets the "provider" field.
func (usu *UserSocialUpdate) SetProvider(s string) *UserSocialUpdate {
	usu.mutation.SetProvider(s)
	return usu
}

// SetAccessToken sets the "access_token" field.
func (usu *UserSocialUpdate) SetAccessToken(s string) *UserSocialUpdate {
	usu.mutation.SetAccessToken(s)
	return usu
}

// SetRefreshToken sets the "refresh_token" field.
func (usu *UserSocialUpdate) SetRefreshToken(s string) *UserSocialUpdate {
	usu.mutation.SetRefreshToken(s)
	return usu
}

// SetUID sets the "uid" field.
func (usu *UserSocialUpdate) SetUID(s string) *UserSocialUpdate {
	usu.mutation.SetUID(s)
	return usu
}

// SetOpenID sets the "open_id" field.
func (usu *UserSocialUpdate) SetOpenID(s string) *UserSocialUpdate {
	usu.mutation.SetOpenID(s)
	return usu
}

// SetUnionID sets the "union_id" field.
func (usu *UserSocialUpdate) SetUnionID(s string) *UserSocialUpdate {
	usu.mutation.SetUnionID(s)
	return usu
}

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

// SetExpiresIn sets the "expires_in" field.
func (usu *UserSocialUpdate) SetExpiresIn(u uint64) *UserSocialUpdate {
	usu.mutation.ResetExpiresIn()
	usu.mutation.SetExpiresIn(u)
	return usu
}

// AddExpiresIn adds u to the "expires_in" field.
func (usu *UserSocialUpdate) AddExpiresIn(u int64) *UserSocialUpdate {
	usu.mutation.AddExpiresIn(u)
	return usu
}

// SetUserProfile sets the "user_profile" field.
func (usu *UserSocialUpdate) SetUserProfile(s string) *UserSocialUpdate {
	usu.mutation.SetUserProfile(s)
	return usu
}

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

// Mutation returns the UserSocialMutation object of the builder.
func (usu *UserSocialUpdate) Mutation() *UserSocialMutation {
	return usu.mutation
}

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

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

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

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

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

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

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

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

func (usu *UserSocialUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := usu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(usersocial.Table, usersocial.Columns, sqlgraph.NewFieldSpec(usersocial.FieldID, field.TypeInt64))
	if ps := usu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := usu.mutation.UpdatedAt(); ok {
		_spec.SetField(usersocial.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := usu.mutation.Provider(); ok {
		_spec.SetField(usersocial.FieldProvider, field.TypeString, value)
	}
	if value, ok := usu.mutation.AccessToken(); ok {
		_spec.SetField(usersocial.FieldAccessToken, field.TypeString, value)
	}
	if value, ok := usu.mutation.RefreshToken(); ok {
		_spec.SetField(usersocial.FieldRefreshToken, field.TypeString, value)
	}
	if value, ok := usu.mutation.UID(); ok {
		_spec.SetField(usersocial.FieldUID, field.TypeString, value)
	}
	if value, ok := usu.mutation.OpenID(); ok {
		_spec.SetField(usersocial.FieldOpenID, field.TypeString, value)
	}
	if value, ok := usu.mutation.UnionID(); ok {
		_spec.SetField(usersocial.FieldUnionID, field.TypeString, value)
	}
	if value, ok := usu.mutation.Key(); ok {
		_spec.SetField(usersocial.FieldKey, field.TypeString, value)
	}
	if value, ok := usu.mutation.ExpiresIn(); ok {
		_spec.SetField(usersocial.FieldExpiresIn, field.TypeUint64, value)
	}
	if value, ok := usu.mutation.AddedExpiresIn(); ok {
		_spec.AddField(usersocial.FieldExpiresIn, field.TypeUint64, value)
	}
	if value, ok := usu.mutation.UserProfile(); ok {
		_spec.SetField(usersocial.FieldUserProfile, field.TypeString, value)
	}
	if usu.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   usersocial.UserTable,
			Columns: []string{usersocial.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := usu.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   usersocial.UserTable,
			Columns: []string{usersocial.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(usu.modifiers...)
	if n, err = sqlgraph.UpdateNodes(ctx, usu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{usersocial.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	usu.mutation.done = true
	return n, nil
}

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

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

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

// SetProvider sets the "provider" field.
func (usuo *UserSocialUpdateOne) SetProvider(s string) *UserSocialUpdateOne {
	usuo.mutation.SetProvider(s)
	return usuo
}

// SetAccessToken sets the "access_token" field.
func (usuo *UserSocialUpdateOne) SetAccessToken(s string) *UserSocialUpdateOne {
	usuo.mutation.SetAccessToken(s)
	return usuo
}

// SetRefreshToken sets the "refresh_token" field.
func (usuo *UserSocialUpdateOne) SetRefreshToken(s string) *UserSocialUpdateOne {
	usuo.mutation.SetRefreshToken(s)
	return usuo
}

// SetUID sets the "uid" field.
func (usuo *UserSocialUpdateOne) SetUID(s string) *UserSocialUpdateOne {
	usuo.mutation.SetUID(s)
	return usuo
}

// SetOpenID sets the "open_id" field.
func (usuo *UserSocialUpdateOne) SetOpenID(s string) *UserSocialUpdateOne {
	usuo.mutation.SetOpenID(s)
	return usuo
}

// SetUnionID sets the "union_id" field.
func (usuo *UserSocialUpdateOne) SetUnionID(s string) *UserSocialUpdateOne {
	usuo.mutation.SetUnionID(s)
	return usuo
}

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

// SetExpiresIn sets the "expires_in" field.
func (usuo *UserSocialUpdateOne) SetExpiresIn(u uint64) *UserSocialUpdateOne {
	usuo.mutation.ResetExpiresIn()
	usuo.mutation.SetExpiresIn(u)
	return usuo
}

// AddExpiresIn adds u to the "expires_in" field.
func (usuo *UserSocialUpdateOne) AddExpiresIn(u int64) *UserSocialUpdateOne {
	usuo.mutation.AddExpiresIn(u)
	return usuo
}

// SetUserProfile sets the "user_profile" field.
func (usuo *UserSocialUpdateOne) SetUserProfile(s string) *UserSocialUpdateOne {
	usuo.mutation.SetUserProfile(s)
	return usuo
}

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

// Mutation returns the UserSocialMutation object of the builder.
func (usuo *UserSocialUpdateOne) Mutation() *UserSocialMutation {
	return usuo.mutation
}

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

// Where appends a list predicates to the UserSocialUpdate builder.
func (usuo *UserSocialUpdateOne) Where(ps ...predicate.UserSocial) *UserSocialUpdateOne {
	usuo.mutation.Where(ps...)
	return usuo
}

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

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

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

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

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

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

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

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

func (usuo *UserSocialUpdateOne) sqlSave(ctx context.Context) (_node *UserSocial, err error) {
	if err := usuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(usersocial.Table, usersocial.Columns, sqlgraph.NewFieldSpec(usersocial.FieldID, field.TypeInt64))
	id, ok := usuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "UserSocial.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := usuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, usersocial.FieldID)
		for _, f := range fields {
			if !usersocial.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != usersocial.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := usuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := usuo.mutation.UpdatedAt(); ok {
		_spec.SetField(usersocial.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := usuo.mutation.Provider(); ok {
		_spec.SetField(usersocial.FieldProvider, field.TypeString, value)
	}
	if value, ok := usuo.mutation.AccessToken(); ok {
		_spec.SetField(usersocial.FieldAccessToken, field.TypeString, value)
	}
	if value, ok := usuo.mutation.RefreshToken(); ok {
		_spec.SetField(usersocial.FieldRefreshToken, field.TypeString, value)
	}
	if value, ok := usuo.mutation.UID(); ok {
		_spec.SetField(usersocial.FieldUID, field.TypeString, value)
	}
	if value, ok := usuo.mutation.OpenID(); ok {
		_spec.SetField(usersocial.FieldOpenID, field.TypeString, value)
	}
	if value, ok := usuo.mutation.UnionID(); ok {
		_spec.SetField(usersocial.FieldUnionID, field.TypeString, value)
	}
	if value, ok := usuo.mutation.Key(); ok {
		_spec.SetField(usersocial.FieldKey, field.TypeString, value)
	}
	if value, ok := usuo.mutation.ExpiresIn(); ok {
		_spec.SetField(usersocial.FieldExpiresIn, field.TypeUint64, value)
	}
	if value, ok := usuo.mutation.AddedExpiresIn(); ok {
		_spec.AddField(usersocial.FieldExpiresIn, field.TypeUint64, value)
	}
	if value, ok := usuo.mutation.UserProfile(); ok {
		_spec.SetField(usersocial.FieldUserProfile, field.TypeString, value)
	}
	if usuo.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   usersocial.UserTable,
			Columns: []string{usersocial.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := usuo.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   usersocial.UserTable,
			Columns: []string{usersocial.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(usuo.modifiers...)
	_node = &UserSocial{config: usuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, usuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{usersocial.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	usuo.mutation.done = true
	return _node, nil
}
