// Code generated by ent, DO NOT EDIT.

package models

import (
	"context"
	"errors"
	"fmt"
	"juwan-backend/app/order/rpc/internal/models/orderstatelogs"
	"juwan-backend/app/order/rpc/internal/models/predicate"

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

// OrderStateLogsUpdate is the builder for updating OrderStateLogs entities.
type OrderStateLogsUpdate struct {
	config
	hooks    []Hook
	mutation *OrderStateLogsMutation
}

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

// SetOrderID sets the "order_id" field.
func (_u *OrderStateLogsUpdate) SetOrderID(v int64) *OrderStateLogsUpdate {
	_u.mutation.ResetOrderID()
	_u.mutation.SetOrderID(v)
	return _u
}

// SetNillableOrderID sets the "order_id" field if the given value is not nil.
func (_u *OrderStateLogsUpdate) SetNillableOrderID(v *int64) *OrderStateLogsUpdate {
	if v != nil {
		_u.SetOrderID(*v)
	}
	return _u
}

// AddOrderID adds value to the "order_id" field.
func (_u *OrderStateLogsUpdate) AddOrderID(v int64) *OrderStateLogsUpdate {
	_u.mutation.AddOrderID(v)
	return _u
}

// SetFromStatus sets the "from_status" field.
func (_u *OrderStateLogsUpdate) SetFromStatus(v string) *OrderStateLogsUpdate {
	_u.mutation.SetFromStatus(v)
	return _u
}

// SetNillableFromStatus sets the "from_status" field if the given value is not nil.
func (_u *OrderStateLogsUpdate) SetNillableFromStatus(v *string) *OrderStateLogsUpdate {
	if v != nil {
		_u.SetFromStatus(*v)
	}
	return _u
}

// ClearFromStatus clears the value of the "from_status" field.
func (_u *OrderStateLogsUpdate) ClearFromStatus() *OrderStateLogsUpdate {
	_u.mutation.ClearFromStatus()
	return _u
}

// SetToStatus sets the "to_status" field.
func (_u *OrderStateLogsUpdate) SetToStatus(v string) *OrderStateLogsUpdate {
	_u.mutation.SetToStatus(v)
	return _u
}

// SetNillableToStatus sets the "to_status" field if the given value is not nil.
func (_u *OrderStateLogsUpdate) SetNillableToStatus(v *string) *OrderStateLogsUpdate {
	if v != nil {
		_u.SetToStatus(*v)
	}
	return _u
}

// SetAction sets the "action" field.
func (_u *OrderStateLogsUpdate) SetAction(v string) *OrderStateLogsUpdate {
	_u.mutation.SetAction(v)
	return _u
}

// SetNillableAction sets the "action" field if the given value is not nil.
func (_u *OrderStateLogsUpdate) SetNillableAction(v *string) *OrderStateLogsUpdate {
	if v != nil {
		_u.SetAction(*v)
	}
	return _u
}

// SetActorID sets the "actor_id" field.
func (_u *OrderStateLogsUpdate) SetActorID(v int64) *OrderStateLogsUpdate {
	_u.mutation.ResetActorID()
	_u.mutation.SetActorID(v)
	return _u
}

// SetNillableActorID sets the "actor_id" field if the given value is not nil.
func (_u *OrderStateLogsUpdate) SetNillableActorID(v *int64) *OrderStateLogsUpdate {
	if v != nil {
		_u.SetActorID(*v)
	}
	return _u
}

// AddActorID adds value to the "actor_id" field.
func (_u *OrderStateLogsUpdate) AddActorID(v int64) *OrderStateLogsUpdate {
	_u.mutation.AddActorID(v)
	return _u
}

// SetActorRole sets the "actor_role" field.
func (_u *OrderStateLogsUpdate) SetActorRole(v string) *OrderStateLogsUpdate {
	_u.mutation.SetActorRole(v)
	return _u
}

// SetNillableActorRole sets the "actor_role" field if the given value is not nil.
func (_u *OrderStateLogsUpdate) SetNillableActorRole(v *string) *OrderStateLogsUpdate {
	if v != nil {
		_u.SetActorRole(*v)
	}
	return _u
}

// SetMetadata sets the "metadata" field.
func (_u *OrderStateLogsUpdate) SetMetadata(v map[string]interface{}) *OrderStateLogsUpdate {
	_u.mutation.SetMetadata(v)
	return _u
}

// ClearMetadata clears the value of the "metadata" field.
func (_u *OrderStateLogsUpdate) ClearMetadata() *OrderStateLogsUpdate {
	_u.mutation.ClearMetadata()
	return _u
}

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *OrderStateLogsUpdate) check() error {
	if v, ok := _u.mutation.FromStatus(); ok {
		if err := orderstatelogs.FromStatusValidator(v); err != nil {
			return &ValidationError{Name: "from_status", err: fmt.Errorf(`models: validator failed for field "OrderStateLogs.from_status": %w`, err)}
		}
	}
	if v, ok := _u.mutation.ToStatus(); ok {
		if err := orderstatelogs.ToStatusValidator(v); err != nil {
			return &ValidationError{Name: "to_status", err: fmt.Errorf(`models: validator failed for field "OrderStateLogs.to_status": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Action(); ok {
		if err := orderstatelogs.ActionValidator(v); err != nil {
			return &ValidationError{Name: "action", err: fmt.Errorf(`models: validator failed for field "OrderStateLogs.action": %w`, err)}
		}
	}
	if v, ok := _u.mutation.ActorRole(); ok {
		if err := orderstatelogs.ActorRoleValidator(v); err != nil {
			return &ValidationError{Name: "actor_role", err: fmt.Errorf(`models: validator failed for field "OrderStateLogs.actor_role": %w`, err)}
		}
	}
	return nil
}

func (_u *OrderStateLogsUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(orderstatelogs.Table, orderstatelogs.Columns, sqlgraph.NewFieldSpec(orderstatelogs.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.OrderID(); ok {
		_spec.SetField(orderstatelogs.FieldOrderID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedOrderID(); ok {
		_spec.AddField(orderstatelogs.FieldOrderID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.FromStatus(); ok {
		_spec.SetField(orderstatelogs.FieldFromStatus, field.TypeString, value)
	}
	if _u.mutation.FromStatusCleared() {
		_spec.ClearField(orderstatelogs.FieldFromStatus, field.TypeString)
	}
	if value, ok := _u.mutation.ToStatus(); ok {
		_spec.SetField(orderstatelogs.FieldToStatus, field.TypeString, value)
	}
	if value, ok := _u.mutation.Action(); ok {
		_spec.SetField(orderstatelogs.FieldAction, field.TypeString, value)
	}
	if value, ok := _u.mutation.ActorID(); ok {
		_spec.SetField(orderstatelogs.FieldActorID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedActorID(); ok {
		_spec.AddField(orderstatelogs.FieldActorID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.ActorRole(); ok {
		_spec.SetField(orderstatelogs.FieldActorRole, field.TypeString, value)
	}
	if value, ok := _u.mutation.Metadata(); ok {
		_spec.SetField(orderstatelogs.FieldMetadata, field.TypeJSON, value)
	}
	if _u.mutation.MetadataCleared() {
		_spec.ClearField(orderstatelogs.FieldMetadata, field.TypeJSON)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{orderstatelogs.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// OrderStateLogsUpdateOne is the builder for updating a single OrderStateLogs entity.
type OrderStateLogsUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *OrderStateLogsMutation
}

// SetOrderID sets the "order_id" field.
func (_u *OrderStateLogsUpdateOne) SetOrderID(v int64) *OrderStateLogsUpdateOne {
	_u.mutation.ResetOrderID()
	_u.mutation.SetOrderID(v)
	return _u
}

// SetNillableOrderID sets the "order_id" field if the given value is not nil.
func (_u *OrderStateLogsUpdateOne) SetNillableOrderID(v *int64) *OrderStateLogsUpdateOne {
	if v != nil {
		_u.SetOrderID(*v)
	}
	return _u
}

// AddOrderID adds value to the "order_id" field.
func (_u *OrderStateLogsUpdateOne) AddOrderID(v int64) *OrderStateLogsUpdateOne {
	_u.mutation.AddOrderID(v)
	return _u
}

// SetFromStatus sets the "from_status" field.
func (_u *OrderStateLogsUpdateOne) SetFromStatus(v string) *OrderStateLogsUpdateOne {
	_u.mutation.SetFromStatus(v)
	return _u
}

// SetNillableFromStatus sets the "from_status" field if the given value is not nil.
func (_u *OrderStateLogsUpdateOne) SetNillableFromStatus(v *string) *OrderStateLogsUpdateOne {
	if v != nil {
		_u.SetFromStatus(*v)
	}
	return _u
}

// ClearFromStatus clears the value of the "from_status" field.
func (_u *OrderStateLogsUpdateOne) ClearFromStatus() *OrderStateLogsUpdateOne {
	_u.mutation.ClearFromStatus()
	return _u
}

// SetToStatus sets the "to_status" field.
func (_u *OrderStateLogsUpdateOne) SetToStatus(v string) *OrderStateLogsUpdateOne {
	_u.mutation.SetToStatus(v)
	return _u
}

// SetNillableToStatus sets the "to_status" field if the given value is not nil.
func (_u *OrderStateLogsUpdateOne) SetNillableToStatus(v *string) *OrderStateLogsUpdateOne {
	if v != nil {
		_u.SetToStatus(*v)
	}
	return _u
}

// SetAction sets the "action" field.
func (_u *OrderStateLogsUpdateOne) SetAction(v string) *OrderStateLogsUpdateOne {
	_u.mutation.SetAction(v)
	return _u
}

// SetNillableAction sets the "action" field if the given value is not nil.
func (_u *OrderStateLogsUpdateOne) SetNillableAction(v *string) *OrderStateLogsUpdateOne {
	if v != nil {
		_u.SetAction(*v)
	}
	return _u
}

// SetActorID sets the "actor_id" field.
func (_u *OrderStateLogsUpdateOne) SetActorID(v int64) *OrderStateLogsUpdateOne {
	_u.mutation.ResetActorID()
	_u.mutation.SetActorID(v)
	return _u
}

// SetNillableActorID sets the "actor_id" field if the given value is not nil.
func (_u *OrderStateLogsUpdateOne) SetNillableActorID(v *int64) *OrderStateLogsUpdateOne {
	if v != nil {
		_u.SetActorID(*v)
	}
	return _u
}

// AddActorID adds value to the "actor_id" field.
func (_u *OrderStateLogsUpdateOne) AddActorID(v int64) *OrderStateLogsUpdateOne {
	_u.mutation.AddActorID(v)
	return _u
}

// SetActorRole sets the "actor_role" field.
func (_u *OrderStateLogsUpdateOne) SetActorRole(v string) *OrderStateLogsUpdateOne {
	_u.mutation.SetActorRole(v)
	return _u
}

// SetNillableActorRole sets the "actor_role" field if the given value is not nil.
func (_u *OrderStateLogsUpdateOne) SetNillableActorRole(v *string) *OrderStateLogsUpdateOne {
	if v != nil {
		_u.SetActorRole(*v)
	}
	return _u
}

// SetMetadata sets the "metadata" field.
func (_u *OrderStateLogsUpdateOne) SetMetadata(v map[string]interface{}) *OrderStateLogsUpdateOne {
	_u.mutation.SetMetadata(v)
	return _u
}

// ClearMetadata clears the value of the "metadata" field.
func (_u *OrderStateLogsUpdateOne) ClearMetadata() *OrderStateLogsUpdateOne {
	_u.mutation.ClearMetadata()
	return _u
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *OrderStateLogsUpdateOne) check() error {
	if v, ok := _u.mutation.FromStatus(); ok {
		if err := orderstatelogs.FromStatusValidator(v); err != nil {
			return &ValidationError{Name: "from_status", err: fmt.Errorf(`models: validator failed for field "OrderStateLogs.from_status": %w`, err)}
		}
	}
	if v, ok := _u.mutation.ToStatus(); ok {
		if err := orderstatelogs.ToStatusValidator(v); err != nil {
			return &ValidationError{Name: "to_status", err: fmt.Errorf(`models: validator failed for field "OrderStateLogs.to_status": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Action(); ok {
		if err := orderstatelogs.ActionValidator(v); err != nil {
			return &ValidationError{Name: "action", err: fmt.Errorf(`models: validator failed for field "OrderStateLogs.action": %w`, err)}
		}
	}
	if v, ok := _u.mutation.ActorRole(); ok {
		if err := orderstatelogs.ActorRoleValidator(v); err != nil {
			return &ValidationError{Name: "actor_role", err: fmt.Errorf(`models: validator failed for field "OrderStateLogs.actor_role": %w`, err)}
		}
	}
	return nil
}

func (_u *OrderStateLogsUpdateOne) sqlSave(ctx context.Context) (_node *OrderStateLogs, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(orderstatelogs.Table, orderstatelogs.Columns, sqlgraph.NewFieldSpec(orderstatelogs.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`models: missing "OrderStateLogs.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, orderstatelogs.FieldID)
		for _, f := range fields {
			if !orderstatelogs.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("models: invalid field %q for query", f)}
			}
			if f != orderstatelogs.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.OrderID(); ok {
		_spec.SetField(orderstatelogs.FieldOrderID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedOrderID(); ok {
		_spec.AddField(orderstatelogs.FieldOrderID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.FromStatus(); ok {
		_spec.SetField(orderstatelogs.FieldFromStatus, field.TypeString, value)
	}
	if _u.mutation.FromStatusCleared() {
		_spec.ClearField(orderstatelogs.FieldFromStatus, field.TypeString)
	}
	if value, ok := _u.mutation.ToStatus(); ok {
		_spec.SetField(orderstatelogs.FieldToStatus, field.TypeString, value)
	}
	if value, ok := _u.mutation.Action(); ok {
		_spec.SetField(orderstatelogs.FieldAction, field.TypeString, value)
	}
	if value, ok := _u.mutation.ActorID(); ok {
		_spec.SetField(orderstatelogs.FieldActorID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedActorID(); ok {
		_spec.AddField(orderstatelogs.FieldActorID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.ActorRole(); ok {
		_spec.SetField(orderstatelogs.FieldActorRole, field.TypeString, value)
	}
	if value, ok := _u.mutation.Metadata(); ok {
		_spec.SetField(orderstatelogs.FieldMetadata, field.TypeJSON, value)
	}
	if _u.mutation.MetadataCleared() {
		_spec.ClearField(orderstatelogs.FieldMetadata, field.TypeJSON)
	}
	_node = &OrderStateLogs{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{orderstatelogs.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
