// Code generated by ent, DO NOT EDIT.

package models

import (
	"context"
	"errors"
	"fmt"
	"juwan-backend/app/wallet/rpc/internal/models/predicate"
	"juwan-backend/app/wallet/rpc/internal/models/wallet"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/shopspring/decimal"
)

// WalletUpdate is the builder for updating Wallet entities.
type WalletUpdate struct {
	config
	hooks    []Hook
	mutation *WalletMutation
}

// Where appends a list predicates to the WalletUpdate builder.
func (_u *WalletUpdate) Where(ps ...predicate.Wallet) *WalletUpdate {
	_u.mutation.Where(ps...)
	return _u
}

// SetBalance sets the "balance" field.
func (_u *WalletUpdate) SetBalance(v decimal.Decimal) *WalletUpdate {
	_u.mutation.SetBalance(v)
	return _u
}

// SetNillableBalance sets the "balance" field if the given value is not nil.
func (_u *WalletUpdate) SetNillableBalance(v *decimal.Decimal) *WalletUpdate {
	if v != nil {
		_u.SetBalance(*v)
	}
	return _u
}

// SetFrozenBalance sets the "frozen_balance" field.
func (_u *WalletUpdate) SetFrozenBalance(v decimal.Decimal) *WalletUpdate {
	_u.mutation.SetFrozenBalance(v)
	return _u
}

// SetNillableFrozenBalance sets the "frozen_balance" field if the given value is not nil.
func (_u *WalletUpdate) SetNillableFrozenBalance(v *decimal.Decimal) *WalletUpdate {
	if v != nil {
		_u.SetFrozenBalance(*v)
	}
	return _u
}

// SetVersion sets the "version" field.
func (_u *WalletUpdate) SetVersion(v int) *WalletUpdate {
	_u.mutation.ResetVersion()
	_u.mutation.SetVersion(v)
	return _u
}

// SetNillableVersion sets the "version" field if the given value is not nil.
func (_u *WalletUpdate) SetNillableVersion(v *int) *WalletUpdate {
	if v != nil {
		_u.SetVersion(*v)
	}
	return _u
}

// AddVersion adds value to the "version" field.
func (_u *WalletUpdate) AddVersion(v int) *WalletUpdate {
	_u.mutation.AddVersion(v)
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *WalletUpdate) SetUpdatedAt(v time.Time) *WalletUpdate {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (_u *WalletUpdate) SetNillableUpdatedAt(v *time.Time) *WalletUpdate {
	if v != nil {
		_u.SetUpdatedAt(*v)
	}
	return _u
}

// Mutation returns the WalletMutation object of the builder.
func (_u *WalletUpdate) Mutation() *WalletMutation {
	return _u.mutation
}

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

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

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

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

func (_u *WalletUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	_spec := sqlgraph.NewUpdateSpec(wallet.Table, wallet.Columns, sqlgraph.NewFieldSpec(wallet.FieldID, field.TypeInt64))
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.Balance(); ok {
		_spec.SetField(wallet.FieldBalance, field.TypeOther, value)
	}
	if value, ok := _u.mutation.FrozenBalance(); ok {
		_spec.SetField(wallet.FieldFrozenBalance, field.TypeOther, value)
	}
	if value, ok := _u.mutation.Version(); ok {
		_spec.SetField(wallet.FieldVersion, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedVersion(); ok {
		_spec.AddField(wallet.FieldVersion, field.TypeInt, value)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(wallet.FieldUpdatedAt, field.TypeTime, value)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{wallet.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// WalletUpdateOne is the builder for updating a single Wallet entity.
type WalletUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *WalletMutation
}

// SetBalance sets the "balance" field.
func (_u *WalletUpdateOne) SetBalance(v decimal.Decimal) *WalletUpdateOne {
	_u.mutation.SetBalance(v)
	return _u
}

// SetNillableBalance sets the "balance" field if the given value is not nil.
func (_u *WalletUpdateOne) SetNillableBalance(v *decimal.Decimal) *WalletUpdateOne {
	if v != nil {
		_u.SetBalance(*v)
	}
	return _u
}

// SetFrozenBalance sets the "frozen_balance" field.
func (_u *WalletUpdateOne) SetFrozenBalance(v decimal.Decimal) *WalletUpdateOne {
	_u.mutation.SetFrozenBalance(v)
	return _u
}

// SetNillableFrozenBalance sets the "frozen_balance" field if the given value is not nil.
func (_u *WalletUpdateOne) SetNillableFrozenBalance(v *decimal.Decimal) *WalletUpdateOne {
	if v != nil {
		_u.SetFrozenBalance(*v)
	}
	return _u
}

// SetVersion sets the "version" field.
func (_u *WalletUpdateOne) SetVersion(v int) *WalletUpdateOne {
	_u.mutation.ResetVersion()
	_u.mutation.SetVersion(v)
	return _u
}

// SetNillableVersion sets the "version" field if the given value is not nil.
func (_u *WalletUpdateOne) SetNillableVersion(v *int) *WalletUpdateOne {
	if v != nil {
		_u.SetVersion(*v)
	}
	return _u
}

// AddVersion adds value to the "version" field.
func (_u *WalletUpdateOne) AddVersion(v int) *WalletUpdateOne {
	_u.mutation.AddVersion(v)
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *WalletUpdateOne) SetUpdatedAt(v time.Time) *WalletUpdateOne {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (_u *WalletUpdateOne) SetNillableUpdatedAt(v *time.Time) *WalletUpdateOne {
	if v != nil {
		_u.SetUpdatedAt(*v)
	}
	return _u
}

// Mutation returns the WalletMutation object of the builder.
func (_u *WalletUpdateOne) Mutation() *WalletMutation {
	return _u.mutation
}

// Where appends a list predicates to the WalletUpdate builder.
func (_u *WalletUpdateOne) Where(ps ...predicate.Wallet) *WalletUpdateOne {
	_u.mutation.Where(ps...)
	return _u
}

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

// Save executes the query and returns the updated Wallet entity.
func (_u *WalletUpdateOne) Save(ctx context.Context) (*Wallet, error) {
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

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

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

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

func (_u *WalletUpdateOne) sqlSave(ctx context.Context) (_node *Wallet, err error) {
	_spec := sqlgraph.NewUpdateSpec(wallet.Table, wallet.Columns, sqlgraph.NewFieldSpec(wallet.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`models: missing "Wallet.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := _u.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, wallet.FieldID)
		for _, f := range fields {
			if !wallet.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("models: invalid field %q for query", f)}
			}
			if f != wallet.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.Balance(); ok {
		_spec.SetField(wallet.FieldBalance, field.TypeOther, value)
	}
	if value, ok := _u.mutation.FrozenBalance(); ok {
		_spec.SetField(wallet.FieldFrozenBalance, field.TypeOther, value)
	}
	if value, ok := _u.mutation.Version(); ok {
		_spec.SetField(wallet.FieldVersion, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedVersion(); ok {
		_spec.AddField(wallet.FieldVersion, field.TypeInt, value)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(wallet.FieldUpdatedAt, field.TypeTime, value)
	}
	_node = &Wallet{config: _u.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{wallet.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
