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

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

// ShopPlayersUpdate is the builder for updating ShopPlayers entities.
type ShopPlayersUpdate struct {
	config
	hooks    []Hook
	mutation *ShopPlayersMutation
}

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

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

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

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

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

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

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

// SetIsPrimary sets the "is_primary" field.
func (_u *ShopPlayersUpdate) SetIsPrimary(v bool) *ShopPlayersUpdate {
	_u.mutation.SetIsPrimary(v)
	return _u
}

// SetNillableIsPrimary sets the "is_primary" field if the given value is not nil.
func (_u *ShopPlayersUpdate) SetNillableIsPrimary(v *bool) *ShopPlayersUpdate {
	if v != nil {
		_u.SetIsPrimary(*v)
	}
	return _u
}

// ClearIsPrimary clears the value of the "is_primary" field.
func (_u *ShopPlayersUpdate) ClearIsPrimary() *ShopPlayersUpdate {
	_u.mutation.ClearIsPrimary()
	return _u
}

// SetLeftAt sets the "left_at" field.
func (_u *ShopPlayersUpdate) SetLeftAt(v time.Time) *ShopPlayersUpdate {
	_u.mutation.SetLeftAt(v)
	return _u
}

// SetNillableLeftAt sets the "left_at" field if the given value is not nil.
func (_u *ShopPlayersUpdate) SetNillableLeftAt(v *time.Time) *ShopPlayersUpdate {
	if v != nil {
		_u.SetLeftAt(*v)
	}
	return _u
}

// ClearLeftAt clears the value of the "left_at" field.
func (_u *ShopPlayersUpdate) ClearLeftAt() *ShopPlayersUpdate {
	_u.mutation.ClearLeftAt()
	return _u
}

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

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

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

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

func (_u *ShopPlayersUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	_spec := sqlgraph.NewUpdateSpec(shopplayers.Table, shopplayers.Columns, sqlgraph.NewFieldSpec(shopplayers.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(shopplayers.FieldShopID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedShopID(); ok {
		_spec.AddField(shopplayers.FieldShopID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.PlayerID(); ok {
		_spec.SetField(shopplayers.FieldPlayerID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedPlayerID(); ok {
		_spec.AddField(shopplayers.FieldPlayerID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.IsPrimary(); ok {
		_spec.SetField(shopplayers.FieldIsPrimary, field.TypeBool, value)
	}
	if _u.mutation.IsPrimaryCleared() {
		_spec.ClearField(shopplayers.FieldIsPrimary, field.TypeBool)
	}
	if value, ok := _u.mutation.LeftAt(); ok {
		_spec.SetField(shopplayers.FieldLeftAt, field.TypeTime, value)
	}
	if _u.mutation.LeftAtCleared() {
		_spec.ClearField(shopplayers.FieldLeftAt, field.TypeTime)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{shopplayers.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// ShopPlayersUpdateOne is the builder for updating a single ShopPlayers entity.
type ShopPlayersUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *ShopPlayersMutation
}

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

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

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

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

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

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

// SetIsPrimary sets the "is_primary" field.
func (_u *ShopPlayersUpdateOne) SetIsPrimary(v bool) *ShopPlayersUpdateOne {
	_u.mutation.SetIsPrimary(v)
	return _u
}

// SetNillableIsPrimary sets the "is_primary" field if the given value is not nil.
func (_u *ShopPlayersUpdateOne) SetNillableIsPrimary(v *bool) *ShopPlayersUpdateOne {
	if v != nil {
		_u.SetIsPrimary(*v)
	}
	return _u
}

// ClearIsPrimary clears the value of the "is_primary" field.
func (_u *ShopPlayersUpdateOne) ClearIsPrimary() *ShopPlayersUpdateOne {
	_u.mutation.ClearIsPrimary()
	return _u
}

// SetLeftAt sets the "left_at" field.
func (_u *ShopPlayersUpdateOne) SetLeftAt(v time.Time) *ShopPlayersUpdateOne {
	_u.mutation.SetLeftAt(v)
	return _u
}

// SetNillableLeftAt sets the "left_at" field if the given value is not nil.
func (_u *ShopPlayersUpdateOne) SetNillableLeftAt(v *time.Time) *ShopPlayersUpdateOne {
	if v != nil {
		_u.SetLeftAt(*v)
	}
	return _u
}

// ClearLeftAt clears the value of the "left_at" field.
func (_u *ShopPlayersUpdateOne) ClearLeftAt() *ShopPlayersUpdateOne {
	_u.mutation.ClearLeftAt()
	return _u
}

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

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

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

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

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

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

func (_u *ShopPlayersUpdateOne) sqlSave(ctx context.Context) (_node *ShopPlayers, err error) {
	_spec := sqlgraph.NewUpdateSpec(shopplayers.Table, shopplayers.Columns, sqlgraph.NewFieldSpec(shopplayers.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`models: missing "ShopPlayers.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, shopplayers.FieldID)
		for _, f := range fields {
			if !shopplayers.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("models: invalid field %q for query", f)}
			}
			if f != shopplayers.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(shopplayers.FieldShopID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedShopID(); ok {
		_spec.AddField(shopplayers.FieldShopID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.PlayerID(); ok {
		_spec.SetField(shopplayers.FieldPlayerID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedPlayerID(); ok {
		_spec.AddField(shopplayers.FieldPlayerID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.IsPrimary(); ok {
		_spec.SetField(shopplayers.FieldIsPrimary, field.TypeBool, value)
	}
	if _u.mutation.IsPrimaryCleared() {
		_spec.ClearField(shopplayers.FieldIsPrimary, field.TypeBool)
	}
	if value, ok := _u.mutation.LeftAt(); ok {
		_spec.SetField(shopplayers.FieldLeftAt, field.TypeTime, value)
	}
	if _u.mutation.LeftAtCleared() {
		_spec.ClearField(shopplayers.FieldLeftAt, field.TypeTime)
	}
	_node = &ShopPlayers{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{shopplayers.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
