// 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/directlink"
	"github.com/cloudreve/Cloudreve/v4/ent/file"
	"github.com/cloudreve/Cloudreve/v4/ent/predicate"
)

// DirectLinkUpdate is the builder for updating DirectLink entities.
type DirectLinkUpdate struct {
	config
	hooks    []Hook
	mutation *DirectLinkMutation
}

// Where appends a list predicates to the DirectLinkUpdate builder.
func (dlu *DirectLinkUpdate) Where(ps ...predicate.DirectLink) *DirectLinkUpdate {
	dlu.mutation.Where(ps...)
	return dlu
}

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

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

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

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

// SetName sets the "name" field.
func (dlu *DirectLinkUpdate) SetName(s string) *DirectLinkUpdate {
	dlu.mutation.SetName(s)
	return dlu
}

// SetNillableName sets the "name" field if the given value is not nil.
func (dlu *DirectLinkUpdate) SetNillableName(s *string) *DirectLinkUpdate {
	if s != nil {
		dlu.SetName(*s)
	}
	return dlu
}

// SetDownloads sets the "downloads" field.
func (dlu *DirectLinkUpdate) SetDownloads(i int) *DirectLinkUpdate {
	dlu.mutation.ResetDownloads()
	dlu.mutation.SetDownloads(i)
	return dlu
}

// SetNillableDownloads sets the "downloads" field if the given value is not nil.
func (dlu *DirectLinkUpdate) SetNillableDownloads(i *int) *DirectLinkUpdate {
	if i != nil {
		dlu.SetDownloads(*i)
	}
	return dlu
}

// AddDownloads adds i to the "downloads" field.
func (dlu *DirectLinkUpdate) AddDownloads(i int) *DirectLinkUpdate {
	dlu.mutation.AddDownloads(i)
	return dlu
}

// SetFileID sets the "file_id" field.
func (dlu *DirectLinkUpdate) SetFileID(i int) *DirectLinkUpdate {
	dlu.mutation.SetFileID(i)
	return dlu
}

// SetNillableFileID sets the "file_id" field if the given value is not nil.
func (dlu *DirectLinkUpdate) SetNillableFileID(i *int) *DirectLinkUpdate {
	if i != nil {
		dlu.SetFileID(*i)
	}
	return dlu
}

// SetSpeed sets the "speed" field.
func (dlu *DirectLinkUpdate) SetSpeed(i int) *DirectLinkUpdate {
	dlu.mutation.ResetSpeed()
	dlu.mutation.SetSpeed(i)
	return dlu
}

// SetNillableSpeed sets the "speed" field if the given value is not nil.
func (dlu *DirectLinkUpdate) SetNillableSpeed(i *int) *DirectLinkUpdate {
	if i != nil {
		dlu.SetSpeed(*i)
	}
	return dlu
}

// AddSpeed adds i to the "speed" field.
func (dlu *DirectLinkUpdate) AddSpeed(i int) *DirectLinkUpdate {
	dlu.mutation.AddSpeed(i)
	return dlu
}

// SetFile sets the "file" edge to the File entity.
func (dlu *DirectLinkUpdate) SetFile(f *File) *DirectLinkUpdate {
	return dlu.SetFileID(f.ID)
}

// Mutation returns the DirectLinkMutation object of the builder.
func (dlu *DirectLinkUpdate) Mutation() *DirectLinkMutation {
	return dlu.mutation
}

// ClearFile clears the "file" edge to the File entity.
func (dlu *DirectLinkUpdate) ClearFile() *DirectLinkUpdate {
	dlu.mutation.ClearFile()
	return dlu
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (dlu *DirectLinkUpdate) check() error {
	if _, ok := dlu.mutation.FileID(); dlu.mutation.FileCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "DirectLink.file"`)
	}
	return nil
}

func (dlu *DirectLinkUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := dlu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(directlink.Table, directlink.Columns, sqlgraph.NewFieldSpec(directlink.FieldID, field.TypeInt))
	if ps := dlu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := dlu.mutation.UpdatedAt(); ok {
		_spec.SetField(directlink.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := dlu.mutation.DeletedAt(); ok {
		_spec.SetField(directlink.FieldDeletedAt, field.TypeTime, value)
	}
	if dlu.mutation.DeletedAtCleared() {
		_spec.ClearField(directlink.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := dlu.mutation.Name(); ok {
		_spec.SetField(directlink.FieldName, field.TypeString, value)
	}
	if value, ok := dlu.mutation.Downloads(); ok {
		_spec.SetField(directlink.FieldDownloads, field.TypeInt, value)
	}
	if value, ok := dlu.mutation.AddedDownloads(); ok {
		_spec.AddField(directlink.FieldDownloads, field.TypeInt, value)
	}
	if value, ok := dlu.mutation.Speed(); ok {
		_spec.SetField(directlink.FieldSpeed, field.TypeInt, value)
	}
	if value, ok := dlu.mutation.AddedSpeed(); ok {
		_spec.AddField(directlink.FieldSpeed, field.TypeInt, value)
	}
	if dlu.mutation.FileCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   directlink.FileTable,
			Columns: []string{directlink.FileColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(file.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := dlu.mutation.FileIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   directlink.FileTable,
			Columns: []string{directlink.FileColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(file.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, dlu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{directlink.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	dlu.mutation.done = true
	return n, nil
}

// DirectLinkUpdateOne is the builder for updating a single DirectLink entity.
type DirectLinkUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *DirectLinkMutation
}

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

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

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

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

// SetName sets the "name" field.
func (dluo *DirectLinkUpdateOne) SetName(s string) *DirectLinkUpdateOne {
	dluo.mutation.SetName(s)
	return dluo
}

// SetNillableName sets the "name" field if the given value is not nil.
func (dluo *DirectLinkUpdateOne) SetNillableName(s *string) *DirectLinkUpdateOne {
	if s != nil {
		dluo.SetName(*s)
	}
	return dluo
}

// SetDownloads sets the "downloads" field.
func (dluo *DirectLinkUpdateOne) SetDownloads(i int) *DirectLinkUpdateOne {
	dluo.mutation.ResetDownloads()
	dluo.mutation.SetDownloads(i)
	return dluo
}

// SetNillableDownloads sets the "downloads" field if the given value is not nil.
func (dluo *DirectLinkUpdateOne) SetNillableDownloads(i *int) *DirectLinkUpdateOne {
	if i != nil {
		dluo.SetDownloads(*i)
	}
	return dluo
}

// AddDownloads adds i to the "downloads" field.
func (dluo *DirectLinkUpdateOne) AddDownloads(i int) *DirectLinkUpdateOne {
	dluo.mutation.AddDownloads(i)
	return dluo
}

// SetFileID sets the "file_id" field.
func (dluo *DirectLinkUpdateOne) SetFileID(i int) *DirectLinkUpdateOne {
	dluo.mutation.SetFileID(i)
	return dluo
}

// SetNillableFileID sets the "file_id" field if the given value is not nil.
func (dluo *DirectLinkUpdateOne) SetNillableFileID(i *int) *DirectLinkUpdateOne {
	if i != nil {
		dluo.SetFileID(*i)
	}
	return dluo
}

// SetSpeed sets the "speed" field.
func (dluo *DirectLinkUpdateOne) SetSpeed(i int) *DirectLinkUpdateOne {
	dluo.mutation.ResetSpeed()
	dluo.mutation.SetSpeed(i)
	return dluo
}

// SetNillableSpeed sets the "speed" field if the given value is not nil.
func (dluo *DirectLinkUpdateOne) SetNillableSpeed(i *int) *DirectLinkUpdateOne {
	if i != nil {
		dluo.SetSpeed(*i)
	}
	return dluo
}

// AddSpeed adds i to the "speed" field.
func (dluo *DirectLinkUpdateOne) AddSpeed(i int) *DirectLinkUpdateOne {
	dluo.mutation.AddSpeed(i)
	return dluo
}

// SetFile sets the "file" edge to the File entity.
func (dluo *DirectLinkUpdateOne) SetFile(f *File) *DirectLinkUpdateOne {
	return dluo.SetFileID(f.ID)
}

// Mutation returns the DirectLinkMutation object of the builder.
func (dluo *DirectLinkUpdateOne) Mutation() *DirectLinkMutation {
	return dluo.mutation
}

// ClearFile clears the "file" edge to the File entity.
func (dluo *DirectLinkUpdateOne) ClearFile() *DirectLinkUpdateOne {
	dluo.mutation.ClearFile()
	return dluo
}

// Where appends a list predicates to the DirectLinkUpdate builder.
func (dluo *DirectLinkUpdateOne) Where(ps ...predicate.DirectLink) *DirectLinkUpdateOne {
	dluo.mutation.Where(ps...)
	return dluo
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (dluo *DirectLinkUpdateOne) check() error {
	if _, ok := dluo.mutation.FileID(); dluo.mutation.FileCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "DirectLink.file"`)
	}
	return nil
}

func (dluo *DirectLinkUpdateOne) sqlSave(ctx context.Context) (_node *DirectLink, err error) {
	if err := dluo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(directlink.Table, directlink.Columns, sqlgraph.NewFieldSpec(directlink.FieldID, field.TypeInt))
	id, ok := dluo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "DirectLink.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := dluo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, directlink.FieldID)
		for _, f := range fields {
			if !directlink.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != directlink.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := dluo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := dluo.mutation.UpdatedAt(); ok {
		_spec.SetField(directlink.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := dluo.mutation.DeletedAt(); ok {
		_spec.SetField(directlink.FieldDeletedAt, field.TypeTime, value)
	}
	if dluo.mutation.DeletedAtCleared() {
		_spec.ClearField(directlink.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := dluo.mutation.Name(); ok {
		_spec.SetField(directlink.FieldName, field.TypeString, value)
	}
	if value, ok := dluo.mutation.Downloads(); ok {
		_spec.SetField(directlink.FieldDownloads, field.TypeInt, value)
	}
	if value, ok := dluo.mutation.AddedDownloads(); ok {
		_spec.AddField(directlink.FieldDownloads, field.TypeInt, value)
	}
	if value, ok := dluo.mutation.Speed(); ok {
		_spec.SetField(directlink.FieldSpeed, field.TypeInt, value)
	}
	if value, ok := dluo.mutation.AddedSpeed(); ok {
		_spec.AddField(directlink.FieldSpeed, field.TypeInt, value)
	}
	if dluo.mutation.FileCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   directlink.FileTable,
			Columns: []string{directlink.FileColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(file.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := dluo.mutation.FileIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   directlink.FileTable,
			Columns: []string{directlink.FileColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(file.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &DirectLink{config: dluo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, dluo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{directlink.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	dluo.mutation.done = true
	return _node, nil
}
