// 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"
	"github.com/cloudreve/Cloudreve/v4/ent/fsevent"
	"github.com/cloudreve/Cloudreve/v4/ent/predicate"
	"github.com/cloudreve/Cloudreve/v4/ent/user"
	"github.com/gofrs/uuid"
)

// FsEventUpdate is the builder for updating FsEvent entities.
type FsEventUpdate struct {
	config
	hooks    []Hook
	mutation *FsEventMutation
}

// Where appends a list predicates to the FsEventUpdate builder.
func (feu *FsEventUpdate) Where(ps ...predicate.FsEvent) *FsEventUpdate {
	feu.mutation.Where(ps...)
	return feu
}

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

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

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

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

// SetEvent sets the "event" field.
func (feu *FsEventUpdate) SetEvent(s string) *FsEventUpdate {
	feu.mutation.SetEvent(s)
	return feu
}

// SetNillableEvent sets the "event" field if the given value is not nil.
func (feu *FsEventUpdate) SetNillableEvent(s *string) *FsEventUpdate {
	if s != nil {
		feu.SetEvent(*s)
	}
	return feu
}

// SetSubscriber sets the "subscriber" field.
func (feu *FsEventUpdate) SetSubscriber(u uuid.UUID) *FsEventUpdate {
	feu.mutation.SetSubscriber(u)
	return feu
}

// SetNillableSubscriber sets the "subscriber" field if the given value is not nil.
func (feu *FsEventUpdate) SetNillableSubscriber(u *uuid.UUID) *FsEventUpdate {
	if u != nil {
		feu.SetSubscriber(*u)
	}
	return feu
}

// SetUserFsevent sets the "user_fsevent" field.
func (feu *FsEventUpdate) SetUserFsevent(i int) *FsEventUpdate {
	feu.mutation.SetUserFsevent(i)
	return feu
}

// SetNillableUserFsevent sets the "user_fsevent" field if the given value is not nil.
func (feu *FsEventUpdate) SetNillableUserFsevent(i *int) *FsEventUpdate {
	if i != nil {
		feu.SetUserFsevent(*i)
	}
	return feu
}

// ClearUserFsevent clears the value of the "user_fsevent" field.
func (feu *FsEventUpdate) ClearUserFsevent() *FsEventUpdate {
	feu.mutation.ClearUserFsevent()
	return feu
}

// SetUserID sets the "user" edge to the User entity by ID.
func (feu *FsEventUpdate) SetUserID(id int) *FsEventUpdate {
	feu.mutation.SetUserID(id)
	return feu
}

// SetNillableUserID sets the "user" edge to the User entity by ID if the given value is not nil.
func (feu *FsEventUpdate) SetNillableUserID(id *int) *FsEventUpdate {
	if id != nil {
		feu = feu.SetUserID(*id)
	}
	return feu
}

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

// Mutation returns the FsEventMutation object of the builder.
func (feu *FsEventUpdate) Mutation() *FsEventMutation {
	return feu.mutation
}

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

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

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

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

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

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

func (feu *FsEventUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(fsevent.Table, fsevent.Columns, sqlgraph.NewFieldSpec(fsevent.FieldID, field.TypeInt))
	if ps := feu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := feu.mutation.UpdatedAt(); ok {
		_spec.SetField(fsevent.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := feu.mutation.DeletedAt(); ok {
		_spec.SetField(fsevent.FieldDeletedAt, field.TypeTime, value)
	}
	if feu.mutation.DeletedAtCleared() {
		_spec.ClearField(fsevent.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := feu.mutation.Event(); ok {
		_spec.SetField(fsevent.FieldEvent, field.TypeString, value)
	}
	if value, ok := feu.mutation.Subscriber(); ok {
		_spec.SetField(fsevent.FieldSubscriber, field.TypeUUID, value)
	}
	if feu.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   fsevent.UserTable,
			Columns: []string{fsevent.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := feu.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   fsevent.UserTable,
			Columns: []string{fsevent.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
			},
		}
		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, feu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{fsevent.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	feu.mutation.done = true
	return n, nil
}

// FsEventUpdateOne is the builder for updating a single FsEvent entity.
type FsEventUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *FsEventMutation
}

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

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

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

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

// SetEvent sets the "event" field.
func (feuo *FsEventUpdateOne) SetEvent(s string) *FsEventUpdateOne {
	feuo.mutation.SetEvent(s)
	return feuo
}

// SetNillableEvent sets the "event" field if the given value is not nil.
func (feuo *FsEventUpdateOne) SetNillableEvent(s *string) *FsEventUpdateOne {
	if s != nil {
		feuo.SetEvent(*s)
	}
	return feuo
}

// SetSubscriber sets the "subscriber" field.
func (feuo *FsEventUpdateOne) SetSubscriber(u uuid.UUID) *FsEventUpdateOne {
	feuo.mutation.SetSubscriber(u)
	return feuo
}

// SetNillableSubscriber sets the "subscriber" field if the given value is not nil.
func (feuo *FsEventUpdateOne) SetNillableSubscriber(u *uuid.UUID) *FsEventUpdateOne {
	if u != nil {
		feuo.SetSubscriber(*u)
	}
	return feuo
}

// SetUserFsevent sets the "user_fsevent" field.
func (feuo *FsEventUpdateOne) SetUserFsevent(i int) *FsEventUpdateOne {
	feuo.mutation.SetUserFsevent(i)
	return feuo
}

// SetNillableUserFsevent sets the "user_fsevent" field if the given value is not nil.
func (feuo *FsEventUpdateOne) SetNillableUserFsevent(i *int) *FsEventUpdateOne {
	if i != nil {
		feuo.SetUserFsevent(*i)
	}
	return feuo
}

// ClearUserFsevent clears the value of the "user_fsevent" field.
func (feuo *FsEventUpdateOne) ClearUserFsevent() *FsEventUpdateOne {
	feuo.mutation.ClearUserFsevent()
	return feuo
}

// SetUserID sets the "user" edge to the User entity by ID.
func (feuo *FsEventUpdateOne) SetUserID(id int) *FsEventUpdateOne {
	feuo.mutation.SetUserID(id)
	return feuo
}

// SetNillableUserID sets the "user" edge to the User entity by ID if the given value is not nil.
func (feuo *FsEventUpdateOne) SetNillableUserID(id *int) *FsEventUpdateOne {
	if id != nil {
		feuo = feuo.SetUserID(*id)
	}
	return feuo
}

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

// Mutation returns the FsEventMutation object of the builder.
func (feuo *FsEventUpdateOne) Mutation() *FsEventMutation {
	return feuo.mutation
}

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

// Where appends a list predicates to the FsEventUpdate builder.
func (feuo *FsEventUpdateOne) Where(ps ...predicate.FsEvent) *FsEventUpdateOne {
	feuo.mutation.Where(ps...)
	return feuo
}

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

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

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

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

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

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

func (feuo *FsEventUpdateOne) sqlSave(ctx context.Context) (_node *FsEvent, err error) {
	_spec := sqlgraph.NewUpdateSpec(fsevent.Table, fsevent.Columns, sqlgraph.NewFieldSpec(fsevent.FieldID, field.TypeInt))
	id, ok := feuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "FsEvent.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := feuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, fsevent.FieldID)
		for _, f := range fields {
			if !fsevent.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != fsevent.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := feuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := feuo.mutation.UpdatedAt(); ok {
		_spec.SetField(fsevent.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := feuo.mutation.DeletedAt(); ok {
		_spec.SetField(fsevent.FieldDeletedAt, field.TypeTime, value)
	}
	if feuo.mutation.DeletedAtCleared() {
		_spec.ClearField(fsevent.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := feuo.mutation.Event(); ok {
		_spec.SetField(fsevent.FieldEvent, field.TypeString, value)
	}
	if value, ok := feuo.mutation.Subscriber(); ok {
		_spec.SetField(fsevent.FieldSubscriber, field.TypeUUID, value)
	}
	if feuo.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   fsevent.UserTable,
			Columns: []string{fsevent.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := feuo.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   fsevent.UserTable,
			Columns: []string{fsevent.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &FsEvent{config: feuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, feuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{fsevent.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	feuo.mutation.done = true
	return _node, nil
}
