// Code generated by ent, DO NOT EDIT.

package models

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

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

// ShopInvitationsUpdate is the builder for updating ShopInvitations entities.
type ShopInvitationsUpdate struct {
	config
	hooks    []Hook
	mutation *ShopInvitationsMutation
}

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

// SetShopID sets the "shop_id" field.
func (_u *ShopInvitationsUpdate) SetShopID(v int64) *ShopInvitationsUpdate {
	_u.mutation.ResetShopID()
	_u.mutation.SetShopID(v)
	return _u
}

// SetNillableShopID sets the "shop_id" field if the given value is not nil.
func (_u *ShopInvitationsUpdate) SetNillableShopID(v *int64) *ShopInvitationsUpdate {
	if v != nil {
		_u.SetShopID(*v)
	}
	return _u
}

// AddShopID adds value to the "shop_id" field.
func (_u *ShopInvitationsUpdate) AddShopID(v int64) *ShopInvitationsUpdate {
	_u.mutation.AddShopID(v)
	return _u
}

// SetPlayerID sets the "player_id" field.
func (_u *ShopInvitationsUpdate) SetPlayerID(v int64) *ShopInvitationsUpdate {
	_u.mutation.ResetPlayerID()
	_u.mutation.SetPlayerID(v)
	return _u
}

// SetNillablePlayerID sets the "player_id" field if the given value is not nil.
func (_u *ShopInvitationsUpdate) SetNillablePlayerID(v *int64) *ShopInvitationsUpdate {
	if v != nil {
		_u.SetPlayerID(*v)
	}
	return _u
}

// AddPlayerID adds value to the "player_id" field.
func (_u *ShopInvitationsUpdate) AddPlayerID(v int64) *ShopInvitationsUpdate {
	_u.mutation.AddPlayerID(v)
	return _u
}

// SetStatus sets the "status" field.
func (_u *ShopInvitationsUpdate) SetStatus(v string) *ShopInvitationsUpdate {
	_u.mutation.SetStatus(v)
	return _u
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (_u *ShopInvitationsUpdate) SetNillableStatus(v *string) *ShopInvitationsUpdate {
	if v != nil {
		_u.SetStatus(*v)
	}
	return _u
}

// SetInvitedBy sets the "invited_by" field.
func (_u *ShopInvitationsUpdate) SetInvitedBy(v int64) *ShopInvitationsUpdate {
	_u.mutation.ResetInvitedBy()
	_u.mutation.SetInvitedBy(v)
	return _u
}

// SetNillableInvitedBy sets the "invited_by" field if the given value is not nil.
func (_u *ShopInvitationsUpdate) SetNillableInvitedBy(v *int64) *ShopInvitationsUpdate {
	if v != nil {
		_u.SetInvitedBy(*v)
	}
	return _u
}

// AddInvitedBy adds value to the "invited_by" field.
func (_u *ShopInvitationsUpdate) AddInvitedBy(v int64) *ShopInvitationsUpdate {
	_u.mutation.AddInvitedBy(v)
	return _u
}

// SetRespondedAt sets the "responded_at" field.
func (_u *ShopInvitationsUpdate) SetRespondedAt(v time.Time) *ShopInvitationsUpdate {
	_u.mutation.SetRespondedAt(v)
	return _u
}

// SetNillableRespondedAt sets the "responded_at" field if the given value is not nil.
func (_u *ShopInvitationsUpdate) SetNillableRespondedAt(v *time.Time) *ShopInvitationsUpdate {
	if v != nil {
		_u.SetRespondedAt(*v)
	}
	return _u
}

// ClearRespondedAt clears the value of the "responded_at" field.
func (_u *ShopInvitationsUpdate) ClearRespondedAt() *ShopInvitationsUpdate {
	_u.mutation.ClearRespondedAt()
	return _u
}

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

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

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

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

func (_u *ShopInvitationsUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(shopinvitations.Table, shopinvitations.Columns, sqlgraph.NewFieldSpec(shopinvitations.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.ShopID(); ok {
		_spec.SetField(shopinvitations.FieldShopID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedShopID(); ok {
		_spec.AddField(shopinvitations.FieldShopID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.PlayerID(); ok {
		_spec.SetField(shopinvitations.FieldPlayerID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedPlayerID(); ok {
		_spec.AddField(shopinvitations.FieldPlayerID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(shopinvitations.FieldStatus, field.TypeString, value)
	}
	if value, ok := _u.mutation.InvitedBy(); ok {
		_spec.SetField(shopinvitations.FieldInvitedBy, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedInvitedBy(); ok {
		_spec.AddField(shopinvitations.FieldInvitedBy, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.RespondedAt(); ok {
		_spec.SetField(shopinvitations.FieldRespondedAt, field.TypeTime, value)
	}
	if _u.mutation.RespondedAtCleared() {
		_spec.ClearField(shopinvitations.FieldRespondedAt, field.TypeTime)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{shopinvitations.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// ShopInvitationsUpdateOne is the builder for updating a single ShopInvitations entity.
type ShopInvitationsUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *ShopInvitationsMutation
}

// SetShopID sets the "shop_id" field.
func (_u *ShopInvitationsUpdateOne) SetShopID(v int64) *ShopInvitationsUpdateOne {
	_u.mutation.ResetShopID()
	_u.mutation.SetShopID(v)
	return _u
}

// SetNillableShopID sets the "shop_id" field if the given value is not nil.
func (_u *ShopInvitationsUpdateOne) SetNillableShopID(v *int64) *ShopInvitationsUpdateOne {
	if v != nil {
		_u.SetShopID(*v)
	}
	return _u
}

// AddShopID adds value to the "shop_id" field.
func (_u *ShopInvitationsUpdateOne) AddShopID(v int64) *ShopInvitationsUpdateOne {
	_u.mutation.AddShopID(v)
	return _u
}

// SetPlayerID sets the "player_id" field.
func (_u *ShopInvitationsUpdateOne) SetPlayerID(v int64) *ShopInvitationsUpdateOne {
	_u.mutation.ResetPlayerID()
	_u.mutation.SetPlayerID(v)
	return _u
}

// SetNillablePlayerID sets the "player_id" field if the given value is not nil.
func (_u *ShopInvitationsUpdateOne) SetNillablePlayerID(v *int64) *ShopInvitationsUpdateOne {
	if v != nil {
		_u.SetPlayerID(*v)
	}
	return _u
}

// AddPlayerID adds value to the "player_id" field.
func (_u *ShopInvitationsUpdateOne) AddPlayerID(v int64) *ShopInvitationsUpdateOne {
	_u.mutation.AddPlayerID(v)
	return _u
}

// SetStatus sets the "status" field.
func (_u *ShopInvitationsUpdateOne) SetStatus(v string) *ShopInvitationsUpdateOne {
	_u.mutation.SetStatus(v)
	return _u
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (_u *ShopInvitationsUpdateOne) SetNillableStatus(v *string) *ShopInvitationsUpdateOne {
	if v != nil {
		_u.SetStatus(*v)
	}
	return _u
}

// SetInvitedBy sets the "invited_by" field.
func (_u *ShopInvitationsUpdateOne) SetInvitedBy(v int64) *ShopInvitationsUpdateOne {
	_u.mutation.ResetInvitedBy()
	_u.mutation.SetInvitedBy(v)
	return _u
}

// SetNillableInvitedBy sets the "invited_by" field if the given value is not nil.
func (_u *ShopInvitationsUpdateOne) SetNillableInvitedBy(v *int64) *ShopInvitationsUpdateOne {
	if v != nil {
		_u.SetInvitedBy(*v)
	}
	return _u
}

// AddInvitedBy adds value to the "invited_by" field.
func (_u *ShopInvitationsUpdateOne) AddInvitedBy(v int64) *ShopInvitationsUpdateOne {
	_u.mutation.AddInvitedBy(v)
	return _u
}

// SetRespondedAt sets the "responded_at" field.
func (_u *ShopInvitationsUpdateOne) SetRespondedAt(v time.Time) *ShopInvitationsUpdateOne {
	_u.mutation.SetRespondedAt(v)
	return _u
}

// SetNillableRespondedAt sets the "responded_at" field if the given value is not nil.
func (_u *ShopInvitationsUpdateOne) SetNillableRespondedAt(v *time.Time) *ShopInvitationsUpdateOne {
	if v != nil {
		_u.SetRespondedAt(*v)
	}
	return _u
}

// ClearRespondedAt clears the value of the "responded_at" field.
func (_u *ShopInvitationsUpdateOne) ClearRespondedAt() *ShopInvitationsUpdateOne {
	_u.mutation.ClearRespondedAt()
	return _u
}

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

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

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

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

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

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

func (_u *ShopInvitationsUpdateOne) sqlSave(ctx context.Context) (_node *ShopInvitations, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(shopinvitations.Table, shopinvitations.Columns, sqlgraph.NewFieldSpec(shopinvitations.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`models: missing "ShopInvitations.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, shopinvitations.FieldID)
		for _, f := range fields {
			if !shopinvitations.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("models: invalid field %q for query", f)}
			}
			if f != shopinvitations.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.ShopID(); ok {
		_spec.SetField(shopinvitations.FieldShopID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedShopID(); ok {
		_spec.AddField(shopinvitations.FieldShopID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.PlayerID(); ok {
		_spec.SetField(shopinvitations.FieldPlayerID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedPlayerID(); ok {
		_spec.AddField(shopinvitations.FieldPlayerID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(shopinvitations.FieldStatus, field.TypeString, value)
	}
	if value, ok := _u.mutation.InvitedBy(); ok {
		_spec.SetField(shopinvitations.FieldInvitedBy, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedInvitedBy(); ok {
		_spec.AddField(shopinvitations.FieldInvitedBy, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.RespondedAt(); ok {
		_spec.SetField(shopinvitations.FieldRespondedAt, field.TypeTime, value)
	}
	if _u.mutation.RespondedAtCleared() {
		_spec.ClearField(shopinvitations.FieldRespondedAt, field.TypeTime)
	}
	_node = &ShopInvitations{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{shopinvitations.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
