// Code generated by ent, DO NOT EDIT.

package models

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

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

// ReviewsUpdate is the builder for updating Reviews entities.
type ReviewsUpdate struct {
	config
	hooks    []Hook
	mutation *ReviewsMutation
}

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

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

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

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

// SetFromUserID sets the "from_user_id" field.
func (_u *ReviewsUpdate) SetFromUserID(v int64) *ReviewsUpdate {
	_u.mutation.ResetFromUserID()
	_u.mutation.SetFromUserID(v)
	return _u
}

// SetNillableFromUserID sets the "from_user_id" field if the given value is not nil.
func (_u *ReviewsUpdate) SetNillableFromUserID(v *int64) *ReviewsUpdate {
	if v != nil {
		_u.SetFromUserID(*v)
	}
	return _u
}

// AddFromUserID adds value to the "from_user_id" field.
func (_u *ReviewsUpdate) AddFromUserID(v int64) *ReviewsUpdate {
	_u.mutation.AddFromUserID(v)
	return _u
}

// SetFromUserName sets the "from_user_name" field.
func (_u *ReviewsUpdate) SetFromUserName(v string) *ReviewsUpdate {
	_u.mutation.SetFromUserName(v)
	return _u
}

// SetNillableFromUserName sets the "from_user_name" field if the given value is not nil.
func (_u *ReviewsUpdate) SetNillableFromUserName(v *string) *ReviewsUpdate {
	if v != nil {
		_u.SetFromUserName(*v)
	}
	return _u
}

// SetFromUserAvatar sets the "from_user_avatar" field.
func (_u *ReviewsUpdate) SetFromUserAvatar(v string) *ReviewsUpdate {
	_u.mutation.SetFromUserAvatar(v)
	return _u
}

// SetNillableFromUserAvatar sets the "from_user_avatar" field if the given value is not nil.
func (_u *ReviewsUpdate) SetNillableFromUserAvatar(v *string) *ReviewsUpdate {
	if v != nil {
		_u.SetFromUserAvatar(*v)
	}
	return _u
}

// ClearFromUserAvatar clears the value of the "from_user_avatar" field.
func (_u *ReviewsUpdate) ClearFromUserAvatar() *ReviewsUpdate {
	_u.mutation.ClearFromUserAvatar()
	return _u
}

// SetToUserID sets the "to_user_id" field.
func (_u *ReviewsUpdate) SetToUserID(v int64) *ReviewsUpdate {
	_u.mutation.ResetToUserID()
	_u.mutation.SetToUserID(v)
	return _u
}

// SetNillableToUserID sets the "to_user_id" field if the given value is not nil.
func (_u *ReviewsUpdate) SetNillableToUserID(v *int64) *ReviewsUpdate {
	if v != nil {
		_u.SetToUserID(*v)
	}
	return _u
}

// AddToUserID adds value to the "to_user_id" field.
func (_u *ReviewsUpdate) AddToUserID(v int64) *ReviewsUpdate {
	_u.mutation.AddToUserID(v)
	return _u
}

// SetRating sets the "rating" field.
func (_u *ReviewsUpdate) SetRating(v int16) *ReviewsUpdate {
	_u.mutation.ResetRating()
	_u.mutation.SetRating(v)
	return _u
}

// SetNillableRating sets the "rating" field if the given value is not nil.
func (_u *ReviewsUpdate) SetNillableRating(v *int16) *ReviewsUpdate {
	if v != nil {
		_u.SetRating(*v)
	}
	return _u
}

// AddRating adds value to the "rating" field.
func (_u *ReviewsUpdate) AddRating(v int16) *ReviewsUpdate {
	_u.mutation.AddRating(v)
	return _u
}

// SetContent sets the "content" field.
func (_u *ReviewsUpdate) SetContent(v string) *ReviewsUpdate {
	_u.mutation.SetContent(v)
	return _u
}

// SetNillableContent sets the "content" field if the given value is not nil.
func (_u *ReviewsUpdate) SetNillableContent(v *string) *ReviewsUpdate {
	if v != nil {
		_u.SetContent(*v)
	}
	return _u
}

// ClearContent clears the value of the "content" field.
func (_u *ReviewsUpdate) ClearContent() *ReviewsUpdate {
	_u.mutation.ClearContent()
	return _u
}

// SetSealed sets the "sealed" field.
func (_u *ReviewsUpdate) SetSealed(v bool) *ReviewsUpdate {
	_u.mutation.SetSealed(v)
	return _u
}

// SetNillableSealed sets the "sealed" field if the given value is not nil.
func (_u *ReviewsUpdate) SetNillableSealed(v *bool) *ReviewsUpdate {
	if v != nil {
		_u.SetSealed(*v)
	}
	return _u
}

// SetUnsealedAt sets the "unsealed_at" field.
func (_u *ReviewsUpdate) SetUnsealedAt(v time.Time) *ReviewsUpdate {
	_u.mutation.SetUnsealedAt(v)
	return _u
}

// SetNillableUnsealedAt sets the "unsealed_at" field if the given value is not nil.
func (_u *ReviewsUpdate) SetNillableUnsealedAt(v *time.Time) *ReviewsUpdate {
	if v != nil {
		_u.SetUnsealedAt(*v)
	}
	return _u
}

// ClearUnsealedAt clears the value of the "unsealed_at" field.
func (_u *ReviewsUpdate) ClearUnsealedAt() *ReviewsUpdate {
	_u.mutation.ClearUnsealedAt()
	return _u
}

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *ReviewsUpdate) 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 *ReviewsUpdate) check() error {
	if v, ok := _u.mutation.FromUserName(); ok {
		if err := reviews.FromUserNameValidator(v); err != nil {
			return &ValidationError{Name: "from_user_name", err: fmt.Errorf(`models: validator failed for field "Reviews.from_user_name": %w`, err)}
		}
	}
	return nil
}

func (_u *ReviewsUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(reviews.Table, reviews.Columns, sqlgraph.NewFieldSpec(reviews.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(reviews.FieldOrderID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedOrderID(); ok {
		_spec.AddField(reviews.FieldOrderID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.FromUserID(); ok {
		_spec.SetField(reviews.FieldFromUserID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedFromUserID(); ok {
		_spec.AddField(reviews.FieldFromUserID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.FromUserName(); ok {
		_spec.SetField(reviews.FieldFromUserName, field.TypeString, value)
	}
	if value, ok := _u.mutation.FromUserAvatar(); ok {
		_spec.SetField(reviews.FieldFromUserAvatar, field.TypeString, value)
	}
	if _u.mutation.FromUserAvatarCleared() {
		_spec.ClearField(reviews.FieldFromUserAvatar, field.TypeString)
	}
	if value, ok := _u.mutation.ToUserID(); ok {
		_spec.SetField(reviews.FieldToUserID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedToUserID(); ok {
		_spec.AddField(reviews.FieldToUserID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.Rating(); ok {
		_spec.SetField(reviews.FieldRating, field.TypeInt16, value)
	}
	if value, ok := _u.mutation.AddedRating(); ok {
		_spec.AddField(reviews.FieldRating, field.TypeInt16, value)
	}
	if value, ok := _u.mutation.Content(); ok {
		_spec.SetField(reviews.FieldContent, field.TypeString, value)
	}
	if _u.mutation.ContentCleared() {
		_spec.ClearField(reviews.FieldContent, field.TypeString)
	}
	if value, ok := _u.mutation.Sealed(); ok {
		_spec.SetField(reviews.FieldSealed, field.TypeBool, value)
	}
	if value, ok := _u.mutation.UnsealedAt(); ok {
		_spec.SetField(reviews.FieldUnsealedAt, field.TypeTime, value)
	}
	if _u.mutation.UnsealedAtCleared() {
		_spec.ClearField(reviews.FieldUnsealedAt, field.TypeTime)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{reviews.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// ReviewsUpdateOne is the builder for updating a single Reviews entity.
type ReviewsUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *ReviewsMutation
}

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

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

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

// SetFromUserID sets the "from_user_id" field.
func (_u *ReviewsUpdateOne) SetFromUserID(v int64) *ReviewsUpdateOne {
	_u.mutation.ResetFromUserID()
	_u.mutation.SetFromUserID(v)
	return _u
}

// SetNillableFromUserID sets the "from_user_id" field if the given value is not nil.
func (_u *ReviewsUpdateOne) SetNillableFromUserID(v *int64) *ReviewsUpdateOne {
	if v != nil {
		_u.SetFromUserID(*v)
	}
	return _u
}

// AddFromUserID adds value to the "from_user_id" field.
func (_u *ReviewsUpdateOne) AddFromUserID(v int64) *ReviewsUpdateOne {
	_u.mutation.AddFromUserID(v)
	return _u
}

// SetFromUserName sets the "from_user_name" field.
func (_u *ReviewsUpdateOne) SetFromUserName(v string) *ReviewsUpdateOne {
	_u.mutation.SetFromUserName(v)
	return _u
}

// SetNillableFromUserName sets the "from_user_name" field if the given value is not nil.
func (_u *ReviewsUpdateOne) SetNillableFromUserName(v *string) *ReviewsUpdateOne {
	if v != nil {
		_u.SetFromUserName(*v)
	}
	return _u
}

// SetFromUserAvatar sets the "from_user_avatar" field.
func (_u *ReviewsUpdateOne) SetFromUserAvatar(v string) *ReviewsUpdateOne {
	_u.mutation.SetFromUserAvatar(v)
	return _u
}

// SetNillableFromUserAvatar sets the "from_user_avatar" field if the given value is not nil.
func (_u *ReviewsUpdateOne) SetNillableFromUserAvatar(v *string) *ReviewsUpdateOne {
	if v != nil {
		_u.SetFromUserAvatar(*v)
	}
	return _u
}

// ClearFromUserAvatar clears the value of the "from_user_avatar" field.
func (_u *ReviewsUpdateOne) ClearFromUserAvatar() *ReviewsUpdateOne {
	_u.mutation.ClearFromUserAvatar()
	return _u
}

// SetToUserID sets the "to_user_id" field.
func (_u *ReviewsUpdateOne) SetToUserID(v int64) *ReviewsUpdateOne {
	_u.mutation.ResetToUserID()
	_u.mutation.SetToUserID(v)
	return _u
}

// SetNillableToUserID sets the "to_user_id" field if the given value is not nil.
func (_u *ReviewsUpdateOne) SetNillableToUserID(v *int64) *ReviewsUpdateOne {
	if v != nil {
		_u.SetToUserID(*v)
	}
	return _u
}

// AddToUserID adds value to the "to_user_id" field.
func (_u *ReviewsUpdateOne) AddToUserID(v int64) *ReviewsUpdateOne {
	_u.mutation.AddToUserID(v)
	return _u
}

// SetRating sets the "rating" field.
func (_u *ReviewsUpdateOne) SetRating(v int16) *ReviewsUpdateOne {
	_u.mutation.ResetRating()
	_u.mutation.SetRating(v)
	return _u
}

// SetNillableRating sets the "rating" field if the given value is not nil.
func (_u *ReviewsUpdateOne) SetNillableRating(v *int16) *ReviewsUpdateOne {
	if v != nil {
		_u.SetRating(*v)
	}
	return _u
}

// AddRating adds value to the "rating" field.
func (_u *ReviewsUpdateOne) AddRating(v int16) *ReviewsUpdateOne {
	_u.mutation.AddRating(v)
	return _u
}

// SetContent sets the "content" field.
func (_u *ReviewsUpdateOne) SetContent(v string) *ReviewsUpdateOne {
	_u.mutation.SetContent(v)
	return _u
}

// SetNillableContent sets the "content" field if the given value is not nil.
func (_u *ReviewsUpdateOne) SetNillableContent(v *string) *ReviewsUpdateOne {
	if v != nil {
		_u.SetContent(*v)
	}
	return _u
}

// ClearContent clears the value of the "content" field.
func (_u *ReviewsUpdateOne) ClearContent() *ReviewsUpdateOne {
	_u.mutation.ClearContent()
	return _u
}

// SetSealed sets the "sealed" field.
func (_u *ReviewsUpdateOne) SetSealed(v bool) *ReviewsUpdateOne {
	_u.mutation.SetSealed(v)
	return _u
}

// SetNillableSealed sets the "sealed" field if the given value is not nil.
func (_u *ReviewsUpdateOne) SetNillableSealed(v *bool) *ReviewsUpdateOne {
	if v != nil {
		_u.SetSealed(*v)
	}
	return _u
}

// SetUnsealedAt sets the "unsealed_at" field.
func (_u *ReviewsUpdateOne) SetUnsealedAt(v time.Time) *ReviewsUpdateOne {
	_u.mutation.SetUnsealedAt(v)
	return _u
}

// SetNillableUnsealedAt sets the "unsealed_at" field if the given value is not nil.
func (_u *ReviewsUpdateOne) SetNillableUnsealedAt(v *time.Time) *ReviewsUpdateOne {
	if v != nil {
		_u.SetUnsealedAt(*v)
	}
	return _u
}

// ClearUnsealedAt clears the value of the "unsealed_at" field.
func (_u *ReviewsUpdateOne) ClearUnsealedAt() *ReviewsUpdateOne {
	_u.mutation.ClearUnsealedAt()
	return _u
}

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

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *ReviewsUpdateOne) 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 *ReviewsUpdateOne) check() error {
	if v, ok := _u.mutation.FromUserName(); ok {
		if err := reviews.FromUserNameValidator(v); err != nil {
			return &ValidationError{Name: "from_user_name", err: fmt.Errorf(`models: validator failed for field "Reviews.from_user_name": %w`, err)}
		}
	}
	return nil
}

func (_u *ReviewsUpdateOne) sqlSave(ctx context.Context) (_node *Reviews, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(reviews.Table, reviews.Columns, sqlgraph.NewFieldSpec(reviews.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`models: missing "Reviews.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, reviews.FieldID)
		for _, f := range fields {
			if !reviews.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("models: invalid field %q for query", f)}
			}
			if f != reviews.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(reviews.FieldOrderID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedOrderID(); ok {
		_spec.AddField(reviews.FieldOrderID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.FromUserID(); ok {
		_spec.SetField(reviews.FieldFromUserID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedFromUserID(); ok {
		_spec.AddField(reviews.FieldFromUserID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.FromUserName(); ok {
		_spec.SetField(reviews.FieldFromUserName, field.TypeString, value)
	}
	if value, ok := _u.mutation.FromUserAvatar(); ok {
		_spec.SetField(reviews.FieldFromUserAvatar, field.TypeString, value)
	}
	if _u.mutation.FromUserAvatarCleared() {
		_spec.ClearField(reviews.FieldFromUserAvatar, field.TypeString)
	}
	if value, ok := _u.mutation.ToUserID(); ok {
		_spec.SetField(reviews.FieldToUserID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedToUserID(); ok {
		_spec.AddField(reviews.FieldToUserID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.Rating(); ok {
		_spec.SetField(reviews.FieldRating, field.TypeInt16, value)
	}
	if value, ok := _u.mutation.AddedRating(); ok {
		_spec.AddField(reviews.FieldRating, field.TypeInt16, value)
	}
	if value, ok := _u.mutation.Content(); ok {
		_spec.SetField(reviews.FieldContent, field.TypeString, value)
	}
	if _u.mutation.ContentCleared() {
		_spec.ClearField(reviews.FieldContent, field.TypeString)
	}
	if value, ok := _u.mutation.Sealed(); ok {
		_spec.SetField(reviews.FieldSealed, field.TypeBool, value)
	}
	if value, ok := _u.mutation.UnsealedAt(); ok {
		_spec.SetField(reviews.FieldUnsealedAt, field.TypeTime, value)
	}
	if _u.mutation.UnsealedAtCleared() {
		_spec.ClearField(reviews.FieldUnsealedAt, field.TypeTime)
	}
	_node = &Reviews{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{reviews.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
