// 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/davaccount"
	"github.com/cloudreve/Cloudreve/v4/ent/predicate"
	"github.com/cloudreve/Cloudreve/v4/ent/user"
	"github.com/cloudreve/Cloudreve/v4/inventory/types"
	"github.com/cloudreve/Cloudreve/v4/pkg/boolset"
)

// DavAccountUpdate is the builder for updating DavAccount entities.
type DavAccountUpdate struct {
	config
	hooks    []Hook
	mutation *DavAccountMutation
}

// Where appends a list predicates to the DavAccountUpdate builder.
func (dau *DavAccountUpdate) Where(ps ...predicate.DavAccount) *DavAccountUpdate {
	dau.mutation.Where(ps...)
	return dau
}

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

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

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

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

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

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

// SetURI sets the "uri" field.
func (dau *DavAccountUpdate) SetURI(s string) *DavAccountUpdate {
	dau.mutation.SetURI(s)
	return dau
}

// SetNillableURI sets the "uri" field if the given value is not nil.
func (dau *DavAccountUpdate) SetNillableURI(s *string) *DavAccountUpdate {
	if s != nil {
		dau.SetURI(*s)
	}
	return dau
}

// SetPassword sets the "password" field.
func (dau *DavAccountUpdate) SetPassword(s string) *DavAccountUpdate {
	dau.mutation.SetPassword(s)
	return dau
}

// SetNillablePassword sets the "password" field if the given value is not nil.
func (dau *DavAccountUpdate) SetNillablePassword(s *string) *DavAccountUpdate {
	if s != nil {
		dau.SetPassword(*s)
	}
	return dau
}

// SetOptions sets the "options" field.
func (dau *DavAccountUpdate) SetOptions(bs *boolset.BooleanSet) *DavAccountUpdate {
	dau.mutation.SetOptions(bs)
	return dau
}

// SetProps sets the "props" field.
func (dau *DavAccountUpdate) SetProps(tap *types.DavAccountProps) *DavAccountUpdate {
	dau.mutation.SetProps(tap)
	return dau
}

// ClearProps clears the value of the "props" field.
func (dau *DavAccountUpdate) ClearProps() *DavAccountUpdate {
	dau.mutation.ClearProps()
	return dau
}

// SetOwnerID sets the "owner_id" field.
func (dau *DavAccountUpdate) SetOwnerID(i int) *DavAccountUpdate {
	dau.mutation.SetOwnerID(i)
	return dau
}

// SetNillableOwnerID sets the "owner_id" field if the given value is not nil.
func (dau *DavAccountUpdate) SetNillableOwnerID(i *int) *DavAccountUpdate {
	if i != nil {
		dau.SetOwnerID(*i)
	}
	return dau
}

// SetOwner sets the "owner" edge to the User entity.
func (dau *DavAccountUpdate) SetOwner(u *User) *DavAccountUpdate {
	return dau.SetOwnerID(u.ID)
}

// Mutation returns the DavAccountMutation object of the builder.
func (dau *DavAccountUpdate) Mutation() *DavAccountMutation {
	return dau.mutation
}

// ClearOwner clears the "owner" edge to the User entity.
func (dau *DavAccountUpdate) ClearOwner() *DavAccountUpdate {
	dau.mutation.ClearOwner()
	return dau
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (dau *DavAccountUpdate) check() error {
	if _, ok := dau.mutation.OwnerID(); dau.mutation.OwnerCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "DavAccount.owner"`)
	}
	return nil
}

func (dau *DavAccountUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := dau.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(davaccount.Table, davaccount.Columns, sqlgraph.NewFieldSpec(davaccount.FieldID, field.TypeInt))
	if ps := dau.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := dau.mutation.UpdatedAt(); ok {
		_spec.SetField(davaccount.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := dau.mutation.DeletedAt(); ok {
		_spec.SetField(davaccount.FieldDeletedAt, field.TypeTime, value)
	}
	if dau.mutation.DeletedAtCleared() {
		_spec.ClearField(davaccount.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := dau.mutation.Name(); ok {
		_spec.SetField(davaccount.FieldName, field.TypeString, value)
	}
	if value, ok := dau.mutation.URI(); ok {
		_spec.SetField(davaccount.FieldURI, field.TypeString, value)
	}
	if value, ok := dau.mutation.Password(); ok {
		_spec.SetField(davaccount.FieldPassword, field.TypeString, value)
	}
	if value, ok := dau.mutation.Options(); ok {
		_spec.SetField(davaccount.FieldOptions, field.TypeBytes, value)
	}
	if value, ok := dau.mutation.Props(); ok {
		_spec.SetField(davaccount.FieldProps, field.TypeJSON, value)
	}
	if dau.mutation.PropsCleared() {
		_spec.ClearField(davaccount.FieldProps, field.TypeJSON)
	}
	if dau.mutation.OwnerCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   davaccount.OwnerTable,
			Columns: []string{davaccount.OwnerColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := dau.mutation.OwnerIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   davaccount.OwnerTable,
			Columns: []string{davaccount.OwnerColumn},
			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, dau.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{davaccount.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	dau.mutation.done = true
	return n, nil
}

// DavAccountUpdateOne is the builder for updating a single DavAccount entity.
type DavAccountUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *DavAccountMutation
}

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

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

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

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

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

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

// SetURI sets the "uri" field.
func (dauo *DavAccountUpdateOne) SetURI(s string) *DavAccountUpdateOne {
	dauo.mutation.SetURI(s)
	return dauo
}

// SetNillableURI sets the "uri" field if the given value is not nil.
func (dauo *DavAccountUpdateOne) SetNillableURI(s *string) *DavAccountUpdateOne {
	if s != nil {
		dauo.SetURI(*s)
	}
	return dauo
}

// SetPassword sets the "password" field.
func (dauo *DavAccountUpdateOne) SetPassword(s string) *DavAccountUpdateOne {
	dauo.mutation.SetPassword(s)
	return dauo
}

// SetNillablePassword sets the "password" field if the given value is not nil.
func (dauo *DavAccountUpdateOne) SetNillablePassword(s *string) *DavAccountUpdateOne {
	if s != nil {
		dauo.SetPassword(*s)
	}
	return dauo
}

// SetOptions sets the "options" field.
func (dauo *DavAccountUpdateOne) SetOptions(bs *boolset.BooleanSet) *DavAccountUpdateOne {
	dauo.mutation.SetOptions(bs)
	return dauo
}

// SetProps sets the "props" field.
func (dauo *DavAccountUpdateOne) SetProps(tap *types.DavAccountProps) *DavAccountUpdateOne {
	dauo.mutation.SetProps(tap)
	return dauo
}

// ClearProps clears the value of the "props" field.
func (dauo *DavAccountUpdateOne) ClearProps() *DavAccountUpdateOne {
	dauo.mutation.ClearProps()
	return dauo
}

// SetOwnerID sets the "owner_id" field.
func (dauo *DavAccountUpdateOne) SetOwnerID(i int) *DavAccountUpdateOne {
	dauo.mutation.SetOwnerID(i)
	return dauo
}

// SetNillableOwnerID sets the "owner_id" field if the given value is not nil.
func (dauo *DavAccountUpdateOne) SetNillableOwnerID(i *int) *DavAccountUpdateOne {
	if i != nil {
		dauo.SetOwnerID(*i)
	}
	return dauo
}

// SetOwner sets the "owner" edge to the User entity.
func (dauo *DavAccountUpdateOne) SetOwner(u *User) *DavAccountUpdateOne {
	return dauo.SetOwnerID(u.ID)
}

// Mutation returns the DavAccountMutation object of the builder.
func (dauo *DavAccountUpdateOne) Mutation() *DavAccountMutation {
	return dauo.mutation
}

// ClearOwner clears the "owner" edge to the User entity.
func (dauo *DavAccountUpdateOne) ClearOwner() *DavAccountUpdateOne {
	dauo.mutation.ClearOwner()
	return dauo
}

// Where appends a list predicates to the DavAccountUpdate builder.
func (dauo *DavAccountUpdateOne) Where(ps ...predicate.DavAccount) *DavAccountUpdateOne {
	dauo.mutation.Where(ps...)
	return dauo
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (dauo *DavAccountUpdateOne) check() error {
	if _, ok := dauo.mutation.OwnerID(); dauo.mutation.OwnerCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "DavAccount.owner"`)
	}
	return nil
}

func (dauo *DavAccountUpdateOne) sqlSave(ctx context.Context) (_node *DavAccount, err error) {
	if err := dauo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(davaccount.Table, davaccount.Columns, sqlgraph.NewFieldSpec(davaccount.FieldID, field.TypeInt))
	id, ok := dauo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "DavAccount.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := dauo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, davaccount.FieldID)
		for _, f := range fields {
			if !davaccount.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != davaccount.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := dauo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := dauo.mutation.UpdatedAt(); ok {
		_spec.SetField(davaccount.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := dauo.mutation.DeletedAt(); ok {
		_spec.SetField(davaccount.FieldDeletedAt, field.TypeTime, value)
	}
	if dauo.mutation.DeletedAtCleared() {
		_spec.ClearField(davaccount.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := dauo.mutation.Name(); ok {
		_spec.SetField(davaccount.FieldName, field.TypeString, value)
	}
	if value, ok := dauo.mutation.URI(); ok {
		_spec.SetField(davaccount.FieldURI, field.TypeString, value)
	}
	if value, ok := dauo.mutation.Password(); ok {
		_spec.SetField(davaccount.FieldPassword, field.TypeString, value)
	}
	if value, ok := dauo.mutation.Options(); ok {
		_spec.SetField(davaccount.FieldOptions, field.TypeBytes, value)
	}
	if value, ok := dauo.mutation.Props(); ok {
		_spec.SetField(davaccount.FieldProps, field.TypeJSON, value)
	}
	if dauo.mutation.PropsCleared() {
		_spec.ClearField(davaccount.FieldProps, field.TypeJSON)
	}
	if dauo.mutation.OwnerCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   davaccount.OwnerTable,
			Columns: []string{davaccount.OwnerColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := dauo.mutation.OwnerIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   davaccount.OwnerTable,
			Columns: []string{davaccount.OwnerColumn},
			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 = &DavAccount{config: dauo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, dauo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{davaccount.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	dauo.mutation.done = true
	return _node, nil
}
