// 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/wallettransactions"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/dialect/sql/sqljson"
	"entgo.io/ent/schema/field"
)

// WalletTransactionsUpdate is the builder for updating WalletTransactions entities.
type WalletTransactionsUpdate struct {
	config
	hooks    []Hook
	mutation *WalletTransactionsMutation
}

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

// SetType sets the "type" field.
func (_u *WalletTransactionsUpdate) SetType(v string) *WalletTransactionsUpdate {
	_u.mutation.SetType(v)
	return _u
}

// SetNillableType sets the "type" field if the given value is not nil.
func (_u *WalletTransactionsUpdate) SetNillableType(v *string) *WalletTransactionsUpdate {
	if v != nil {
		_u.SetType(*v)
	}
	return _u
}

// SetDescription sets the "description" field.
func (_u *WalletTransactionsUpdate) SetDescription(v []string) *WalletTransactionsUpdate {
	_u.mutation.SetDescription(v)
	return _u
}

// AppendDescription appends value to the "description" field.
func (_u *WalletTransactionsUpdate) AppendDescription(v []string) *WalletTransactionsUpdate {
	_u.mutation.AppendDescription(v)
	return _u
}

// SetCreatedAt sets the "created_at" field.
func (_u *WalletTransactionsUpdate) SetCreatedAt(v time.Time) *WalletTransactionsUpdate {
	_u.mutation.SetCreatedAt(v)
	return _u
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_u *WalletTransactionsUpdate) SetNillableCreatedAt(v *time.Time) *WalletTransactionsUpdate {
	if v != nil {
		_u.SetCreatedAt(*v)
	}
	return _u
}

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

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *WalletTransactionsUpdate) 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 *WalletTransactionsUpdate) SaveX(ctx context.Context) int {
	affected, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

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

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

func (_u *WalletTransactionsUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	_spec := sqlgraph.NewUpdateSpec(wallettransactions.Table, wallettransactions.Columns, sqlgraph.NewFieldSpec(wallettransactions.FieldID, field.TypeString))
	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.GetType(); ok {
		_spec.SetField(wallettransactions.FieldType, field.TypeString, value)
	}
	if value, ok := _u.mutation.Description(); ok {
		_spec.SetField(wallettransactions.FieldDescription, field.TypeJSON, value)
	}
	if value, ok := _u.mutation.AppendedDescription(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, wallettransactions.FieldDescription, value)
		})
	}
	if value, ok := _u.mutation.CreatedAt(); ok {
		_spec.SetField(wallettransactions.FieldCreatedAt, field.TypeTime, value)
	}
	if _u.mutation.SearchTextCleared() {
		_spec.ClearField(wallettransactions.FieldSearchText, field.TypeString)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{wallettransactions.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// WalletTransactionsUpdateOne is the builder for updating a single WalletTransactions entity.
type WalletTransactionsUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *WalletTransactionsMutation
}

// SetType sets the "type" field.
func (_u *WalletTransactionsUpdateOne) SetType(v string) *WalletTransactionsUpdateOne {
	_u.mutation.SetType(v)
	return _u
}

// SetNillableType sets the "type" field if the given value is not nil.
func (_u *WalletTransactionsUpdateOne) SetNillableType(v *string) *WalletTransactionsUpdateOne {
	if v != nil {
		_u.SetType(*v)
	}
	return _u
}

// SetDescription sets the "description" field.
func (_u *WalletTransactionsUpdateOne) SetDescription(v []string) *WalletTransactionsUpdateOne {
	_u.mutation.SetDescription(v)
	return _u
}

// AppendDescription appends value to the "description" field.
func (_u *WalletTransactionsUpdateOne) AppendDescription(v []string) *WalletTransactionsUpdateOne {
	_u.mutation.AppendDescription(v)
	return _u
}

// SetCreatedAt sets the "created_at" field.
func (_u *WalletTransactionsUpdateOne) SetCreatedAt(v time.Time) *WalletTransactionsUpdateOne {
	_u.mutation.SetCreatedAt(v)
	return _u
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_u *WalletTransactionsUpdateOne) SetNillableCreatedAt(v *time.Time) *WalletTransactionsUpdateOne {
	if v != nil {
		_u.SetCreatedAt(*v)
	}
	return _u
}

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

// Where appends a list predicates to the WalletTransactionsUpdate builder.
func (_u *WalletTransactionsUpdateOne) Where(ps ...predicate.WalletTransactions) *WalletTransactionsUpdateOne {
	_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 *WalletTransactionsUpdateOne) Select(field string, fields ...string) *WalletTransactionsUpdateOne {
	_u.fields = append([]string{field}, fields...)
	return _u
}

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

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

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

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

func (_u *WalletTransactionsUpdateOne) sqlSave(ctx context.Context) (_node *WalletTransactions, err error) {
	_spec := sqlgraph.NewUpdateSpec(wallettransactions.Table, wallettransactions.Columns, sqlgraph.NewFieldSpec(wallettransactions.FieldID, field.TypeString))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`models: missing "WalletTransactions.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, wallettransactions.FieldID)
		for _, f := range fields {
			if !wallettransactions.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("models: invalid field %q for query", f)}
			}
			if f != wallettransactions.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.GetType(); ok {
		_spec.SetField(wallettransactions.FieldType, field.TypeString, value)
	}
	if value, ok := _u.mutation.Description(); ok {
		_spec.SetField(wallettransactions.FieldDescription, field.TypeJSON, value)
	}
	if value, ok := _u.mutation.AppendedDescription(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, wallettransactions.FieldDescription, value)
		})
	}
	if value, ok := _u.mutation.CreatedAt(); ok {
		_spec.SetField(wallettransactions.FieldCreatedAt, field.TypeTime, value)
	}
	if _u.mutation.SearchTextCleared() {
		_spec.ClearField(wallettransactions.FieldSearchText, field.TypeString)
	}
	_node = &WalletTransactions{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{wallettransactions.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
