// Code generated by ent, DO NOT EDIT.

package models

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

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/shopspring/decimal"
)

// OrdersUpdate is the builder for updating Orders entities.
type OrdersUpdate struct {
	config
	hooks    []Hook
	mutation *OrdersMutation
}

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

// SetConsumerID sets the "consumer_id" field.
func (_u *OrdersUpdate) SetConsumerID(v int64) *OrdersUpdate {
	_u.mutation.ResetConsumerID()
	_u.mutation.SetConsumerID(v)
	return _u
}

// SetNillableConsumerID sets the "consumer_id" field if the given value is not nil.
func (_u *OrdersUpdate) SetNillableConsumerID(v *int64) *OrdersUpdate {
	if v != nil {
		_u.SetConsumerID(*v)
	}
	return _u
}

// AddConsumerID adds value to the "consumer_id" field.
func (_u *OrdersUpdate) AddConsumerID(v int64) *OrdersUpdate {
	_u.mutation.AddConsumerID(v)
	return _u
}

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

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

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

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

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

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

// ClearShopID clears the value of the "shop_id" field.
func (_u *OrdersUpdate) ClearShopID() *OrdersUpdate {
	_u.mutation.ClearShopID()
	return _u
}

// SetServiceSnapshot sets the "service_snapshot" field.
func (_u *OrdersUpdate) SetServiceSnapshot(v map[string]interface{}) *OrdersUpdate {
	_u.mutation.SetServiceSnapshot(v)
	return _u
}

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

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

// SetTotalPrice sets the "total_price" field.
func (_u *OrdersUpdate) SetTotalPrice(v decimal.Decimal) *OrdersUpdate {
	_u.mutation.SetTotalPrice(v)
	return _u
}

// SetNillableTotalPrice sets the "total_price" field if the given value is not nil.
func (_u *OrdersUpdate) SetNillableTotalPrice(v *decimal.Decimal) *OrdersUpdate {
	if v != nil {
		_u.SetTotalPrice(*v)
	}
	return _u
}

// SetNote sets the "note" field.
func (_u *OrdersUpdate) SetNote(v string) *OrdersUpdate {
	_u.mutation.SetNote(v)
	return _u
}

// SetNillableNote sets the "note" field if the given value is not nil.
func (_u *OrdersUpdate) SetNillableNote(v *string) *OrdersUpdate {
	if v != nil {
		_u.SetNote(*v)
	}
	return _u
}

// ClearNote clears the value of the "note" field.
func (_u *OrdersUpdate) ClearNote() *OrdersUpdate {
	_u.mutation.ClearNote()
	return _u
}

// SetVersion sets the "version" field.
func (_u *OrdersUpdate) SetVersion(v int) *OrdersUpdate {
	_u.mutation.ResetVersion()
	_u.mutation.SetVersion(v)
	return _u
}

// SetNillableVersion sets the "version" field if the given value is not nil.
func (_u *OrdersUpdate) SetNillableVersion(v *int) *OrdersUpdate {
	if v != nil {
		_u.SetVersion(*v)
	}
	return _u
}

// AddVersion adds value to the "version" field.
func (_u *OrdersUpdate) AddVersion(v int) *OrdersUpdate {
	_u.mutation.AddVersion(v)
	return _u
}

// SetTimeoutJobID sets the "timeout_job_id" field.
func (_u *OrdersUpdate) SetTimeoutJobID(v string) *OrdersUpdate {
	_u.mutation.SetTimeoutJobID(v)
	return _u
}

// SetNillableTimeoutJobID sets the "timeout_job_id" field if the given value is not nil.
func (_u *OrdersUpdate) SetNillableTimeoutJobID(v *string) *OrdersUpdate {
	if v != nil {
		_u.SetTimeoutJobID(*v)
	}
	return _u
}

// ClearTimeoutJobID clears the value of the "timeout_job_id" field.
func (_u *OrdersUpdate) ClearTimeoutJobID() *OrdersUpdate {
	_u.mutation.ClearTimeoutJobID()
	return _u
}

// SetSearchText sets the "search_text" field.
func (_u *OrdersUpdate) SetSearchText(v string) *OrdersUpdate {
	_u.mutation.SetSearchText(v)
	return _u
}

// SetNillableSearchText sets the "search_text" field if the given value is not nil.
func (_u *OrdersUpdate) SetNillableSearchText(v *string) *OrdersUpdate {
	if v != nil {
		_u.SetSearchText(*v)
	}
	return _u
}

// ClearSearchText clears the value of the "search_text" field.
func (_u *OrdersUpdate) ClearSearchText() *OrdersUpdate {
	_u.mutation.ClearSearchText()
	return _u
}

// SetAcceptedAt sets the "accepted_at" field.
func (_u *OrdersUpdate) SetAcceptedAt(v time.Time) *OrdersUpdate {
	_u.mutation.SetAcceptedAt(v)
	return _u
}

// SetNillableAcceptedAt sets the "accepted_at" field if the given value is not nil.
func (_u *OrdersUpdate) SetNillableAcceptedAt(v *time.Time) *OrdersUpdate {
	if v != nil {
		_u.SetAcceptedAt(*v)
	}
	return _u
}

// ClearAcceptedAt clears the value of the "accepted_at" field.
func (_u *OrdersUpdate) ClearAcceptedAt() *OrdersUpdate {
	_u.mutation.ClearAcceptedAt()
	return _u
}

// SetClosedAt sets the "closed_at" field.
func (_u *OrdersUpdate) SetClosedAt(v time.Time) *OrdersUpdate {
	_u.mutation.SetClosedAt(v)
	return _u
}

// SetNillableClosedAt sets the "closed_at" field if the given value is not nil.
func (_u *OrdersUpdate) SetNillableClosedAt(v *time.Time) *OrdersUpdate {
	if v != nil {
		_u.SetClosedAt(*v)
	}
	return _u
}

// ClearClosedAt clears the value of the "closed_at" field.
func (_u *OrdersUpdate) ClearClosedAt() *OrdersUpdate {
	_u.mutation.ClearClosedAt()
	return _u
}

// SetCompletedAt sets the "completed_at" field.
func (_u *OrdersUpdate) SetCompletedAt(v time.Time) *OrdersUpdate {
	_u.mutation.SetCompletedAt(v)
	return _u
}

// SetNillableCompletedAt sets the "completed_at" field if the given value is not nil.
func (_u *OrdersUpdate) SetNillableCompletedAt(v *time.Time) *OrdersUpdate {
	if v != nil {
		_u.SetCompletedAt(*v)
	}
	return _u
}

// ClearCompletedAt clears the value of the "completed_at" field.
func (_u *OrdersUpdate) ClearCompletedAt() *OrdersUpdate {
	_u.mutation.ClearCompletedAt()
	return _u
}

// SetCancelledAt sets the "cancelled_at" field.
func (_u *OrdersUpdate) SetCancelledAt(v time.Time) *OrdersUpdate {
	_u.mutation.SetCancelledAt(v)
	return _u
}

// SetNillableCancelledAt sets the "cancelled_at" field if the given value is not nil.
func (_u *OrdersUpdate) SetNillableCancelledAt(v *time.Time) *OrdersUpdate {
	if v != nil {
		_u.SetCancelledAt(*v)
	}
	return _u
}

// ClearCancelledAt clears the value of the "cancelled_at" field.
func (_u *OrdersUpdate) ClearCancelledAt() *OrdersUpdate {
	_u.mutation.ClearCancelledAt()
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *OrdersUpdate) SetUpdatedAt(v time.Time) *OrdersUpdate {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

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

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *OrdersUpdate) Save(ctx context.Context) (int, error) {
	_u.defaults()
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (_u *OrdersUpdate) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := orders.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *OrdersUpdate) check() error {
	if v, ok := _u.mutation.Status(); ok {
		if err := orders.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`models: validator failed for field "Orders.status": %w`, err)}
		}
	}
	if v, ok := _u.mutation.TimeoutJobID(); ok {
		if err := orders.TimeoutJobIDValidator(v); err != nil {
			return &ValidationError{Name: "timeout_job_id", err: fmt.Errorf(`models: validator failed for field "Orders.timeout_job_id": %w`, err)}
		}
	}
	return nil
}

func (_u *OrdersUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(orders.Table, orders.Columns, sqlgraph.NewFieldSpec(orders.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.ConsumerID(); ok {
		_spec.SetField(orders.FieldConsumerID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedConsumerID(); ok {
		_spec.AddField(orders.FieldConsumerID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.PlayerID(); ok {
		_spec.SetField(orders.FieldPlayerID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedPlayerID(); ok {
		_spec.AddField(orders.FieldPlayerID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.ShopID(); ok {
		_spec.SetField(orders.FieldShopID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedShopID(); ok {
		_spec.AddField(orders.FieldShopID, field.TypeInt64, value)
	}
	if _u.mutation.ShopIDCleared() {
		_spec.ClearField(orders.FieldShopID, field.TypeInt64)
	}
	if value, ok := _u.mutation.ServiceSnapshot(); ok {
		_spec.SetField(orders.FieldServiceSnapshot, field.TypeJSON, value)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(orders.FieldStatus, field.TypeString, value)
	}
	if value, ok := _u.mutation.TotalPrice(); ok {
		_spec.SetField(orders.FieldTotalPrice, field.TypeOther, value)
	}
	if value, ok := _u.mutation.Note(); ok {
		_spec.SetField(orders.FieldNote, field.TypeString, value)
	}
	if _u.mutation.NoteCleared() {
		_spec.ClearField(orders.FieldNote, field.TypeString)
	}
	if value, ok := _u.mutation.Version(); ok {
		_spec.SetField(orders.FieldVersion, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedVersion(); ok {
		_spec.AddField(orders.FieldVersion, field.TypeInt, value)
	}
	if value, ok := _u.mutation.TimeoutJobID(); ok {
		_spec.SetField(orders.FieldTimeoutJobID, field.TypeString, value)
	}
	if _u.mutation.TimeoutJobIDCleared() {
		_spec.ClearField(orders.FieldTimeoutJobID, field.TypeString)
	}
	if value, ok := _u.mutation.SearchText(); ok {
		_spec.SetField(orders.FieldSearchText, field.TypeString, value)
	}
	if _u.mutation.SearchTextCleared() {
		_spec.ClearField(orders.FieldSearchText, field.TypeString)
	}
	if value, ok := _u.mutation.AcceptedAt(); ok {
		_spec.SetField(orders.FieldAcceptedAt, field.TypeTime, value)
	}
	if _u.mutation.AcceptedAtCleared() {
		_spec.ClearField(orders.FieldAcceptedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.ClosedAt(); ok {
		_spec.SetField(orders.FieldClosedAt, field.TypeTime, value)
	}
	if _u.mutation.ClosedAtCleared() {
		_spec.ClearField(orders.FieldClosedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.CompletedAt(); ok {
		_spec.SetField(orders.FieldCompletedAt, field.TypeTime, value)
	}
	if _u.mutation.CompletedAtCleared() {
		_spec.ClearField(orders.FieldCompletedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.CancelledAt(); ok {
		_spec.SetField(orders.FieldCancelledAt, field.TypeTime, value)
	}
	if _u.mutation.CancelledAtCleared() {
		_spec.ClearField(orders.FieldCancelledAt, field.TypeTime)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(orders.FieldUpdatedAt, field.TypeTime, value)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{orders.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// OrdersUpdateOne is the builder for updating a single Orders entity.
type OrdersUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *OrdersMutation
}

// SetConsumerID sets the "consumer_id" field.
func (_u *OrdersUpdateOne) SetConsumerID(v int64) *OrdersUpdateOne {
	_u.mutation.ResetConsumerID()
	_u.mutation.SetConsumerID(v)
	return _u
}

// SetNillableConsumerID sets the "consumer_id" field if the given value is not nil.
func (_u *OrdersUpdateOne) SetNillableConsumerID(v *int64) *OrdersUpdateOne {
	if v != nil {
		_u.SetConsumerID(*v)
	}
	return _u
}

// AddConsumerID adds value to the "consumer_id" field.
func (_u *OrdersUpdateOne) AddConsumerID(v int64) *OrdersUpdateOne {
	_u.mutation.AddConsumerID(v)
	return _u
}

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

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

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

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

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

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

// ClearShopID clears the value of the "shop_id" field.
func (_u *OrdersUpdateOne) ClearShopID() *OrdersUpdateOne {
	_u.mutation.ClearShopID()
	return _u
}

// SetServiceSnapshot sets the "service_snapshot" field.
func (_u *OrdersUpdateOne) SetServiceSnapshot(v map[string]interface{}) *OrdersUpdateOne {
	_u.mutation.SetServiceSnapshot(v)
	return _u
}

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

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

// SetTotalPrice sets the "total_price" field.
func (_u *OrdersUpdateOne) SetTotalPrice(v decimal.Decimal) *OrdersUpdateOne {
	_u.mutation.SetTotalPrice(v)
	return _u
}

// SetNillableTotalPrice sets the "total_price" field if the given value is not nil.
func (_u *OrdersUpdateOne) SetNillableTotalPrice(v *decimal.Decimal) *OrdersUpdateOne {
	if v != nil {
		_u.SetTotalPrice(*v)
	}
	return _u
}

// SetNote sets the "note" field.
func (_u *OrdersUpdateOne) SetNote(v string) *OrdersUpdateOne {
	_u.mutation.SetNote(v)
	return _u
}

// SetNillableNote sets the "note" field if the given value is not nil.
func (_u *OrdersUpdateOne) SetNillableNote(v *string) *OrdersUpdateOne {
	if v != nil {
		_u.SetNote(*v)
	}
	return _u
}

// ClearNote clears the value of the "note" field.
func (_u *OrdersUpdateOne) ClearNote() *OrdersUpdateOne {
	_u.mutation.ClearNote()
	return _u
}

// SetVersion sets the "version" field.
func (_u *OrdersUpdateOne) SetVersion(v int) *OrdersUpdateOne {
	_u.mutation.ResetVersion()
	_u.mutation.SetVersion(v)
	return _u
}

// SetNillableVersion sets the "version" field if the given value is not nil.
func (_u *OrdersUpdateOne) SetNillableVersion(v *int) *OrdersUpdateOne {
	if v != nil {
		_u.SetVersion(*v)
	}
	return _u
}

// AddVersion adds value to the "version" field.
func (_u *OrdersUpdateOne) AddVersion(v int) *OrdersUpdateOne {
	_u.mutation.AddVersion(v)
	return _u
}

// SetTimeoutJobID sets the "timeout_job_id" field.
func (_u *OrdersUpdateOne) SetTimeoutJobID(v string) *OrdersUpdateOne {
	_u.mutation.SetTimeoutJobID(v)
	return _u
}

// SetNillableTimeoutJobID sets the "timeout_job_id" field if the given value is not nil.
func (_u *OrdersUpdateOne) SetNillableTimeoutJobID(v *string) *OrdersUpdateOne {
	if v != nil {
		_u.SetTimeoutJobID(*v)
	}
	return _u
}

// ClearTimeoutJobID clears the value of the "timeout_job_id" field.
func (_u *OrdersUpdateOne) ClearTimeoutJobID() *OrdersUpdateOne {
	_u.mutation.ClearTimeoutJobID()
	return _u
}

// SetSearchText sets the "search_text" field.
func (_u *OrdersUpdateOne) SetSearchText(v string) *OrdersUpdateOne {
	_u.mutation.SetSearchText(v)
	return _u
}

// SetNillableSearchText sets the "search_text" field if the given value is not nil.
func (_u *OrdersUpdateOne) SetNillableSearchText(v *string) *OrdersUpdateOne {
	if v != nil {
		_u.SetSearchText(*v)
	}
	return _u
}

// ClearSearchText clears the value of the "search_text" field.
func (_u *OrdersUpdateOne) ClearSearchText() *OrdersUpdateOne {
	_u.mutation.ClearSearchText()
	return _u
}

// SetAcceptedAt sets the "accepted_at" field.
func (_u *OrdersUpdateOne) SetAcceptedAt(v time.Time) *OrdersUpdateOne {
	_u.mutation.SetAcceptedAt(v)
	return _u
}

// SetNillableAcceptedAt sets the "accepted_at" field if the given value is not nil.
func (_u *OrdersUpdateOne) SetNillableAcceptedAt(v *time.Time) *OrdersUpdateOne {
	if v != nil {
		_u.SetAcceptedAt(*v)
	}
	return _u
}

// ClearAcceptedAt clears the value of the "accepted_at" field.
func (_u *OrdersUpdateOne) ClearAcceptedAt() *OrdersUpdateOne {
	_u.mutation.ClearAcceptedAt()
	return _u
}

// SetClosedAt sets the "closed_at" field.
func (_u *OrdersUpdateOne) SetClosedAt(v time.Time) *OrdersUpdateOne {
	_u.mutation.SetClosedAt(v)
	return _u
}

// SetNillableClosedAt sets the "closed_at" field if the given value is not nil.
func (_u *OrdersUpdateOne) SetNillableClosedAt(v *time.Time) *OrdersUpdateOne {
	if v != nil {
		_u.SetClosedAt(*v)
	}
	return _u
}

// ClearClosedAt clears the value of the "closed_at" field.
func (_u *OrdersUpdateOne) ClearClosedAt() *OrdersUpdateOne {
	_u.mutation.ClearClosedAt()
	return _u
}

// SetCompletedAt sets the "completed_at" field.
func (_u *OrdersUpdateOne) SetCompletedAt(v time.Time) *OrdersUpdateOne {
	_u.mutation.SetCompletedAt(v)
	return _u
}

// SetNillableCompletedAt sets the "completed_at" field if the given value is not nil.
func (_u *OrdersUpdateOne) SetNillableCompletedAt(v *time.Time) *OrdersUpdateOne {
	if v != nil {
		_u.SetCompletedAt(*v)
	}
	return _u
}

// ClearCompletedAt clears the value of the "completed_at" field.
func (_u *OrdersUpdateOne) ClearCompletedAt() *OrdersUpdateOne {
	_u.mutation.ClearCompletedAt()
	return _u
}

// SetCancelledAt sets the "cancelled_at" field.
func (_u *OrdersUpdateOne) SetCancelledAt(v time.Time) *OrdersUpdateOne {
	_u.mutation.SetCancelledAt(v)
	return _u
}

// SetNillableCancelledAt sets the "cancelled_at" field if the given value is not nil.
func (_u *OrdersUpdateOne) SetNillableCancelledAt(v *time.Time) *OrdersUpdateOne {
	if v != nil {
		_u.SetCancelledAt(*v)
	}
	return _u
}

// ClearCancelledAt clears the value of the "cancelled_at" field.
func (_u *OrdersUpdateOne) ClearCancelledAt() *OrdersUpdateOne {
	_u.mutation.ClearCancelledAt()
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *OrdersUpdateOne) SetUpdatedAt(v time.Time) *OrdersUpdateOne {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

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

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

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

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

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

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

// defaults sets the default values of the builder before save.
func (_u *OrdersUpdateOne) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := orders.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *OrdersUpdateOne) check() error {
	if v, ok := _u.mutation.Status(); ok {
		if err := orders.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`models: validator failed for field "Orders.status": %w`, err)}
		}
	}
	if v, ok := _u.mutation.TimeoutJobID(); ok {
		if err := orders.TimeoutJobIDValidator(v); err != nil {
			return &ValidationError{Name: "timeout_job_id", err: fmt.Errorf(`models: validator failed for field "Orders.timeout_job_id": %w`, err)}
		}
	}
	return nil
}

func (_u *OrdersUpdateOne) sqlSave(ctx context.Context) (_node *Orders, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(orders.Table, orders.Columns, sqlgraph.NewFieldSpec(orders.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`models: missing "Orders.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, orders.FieldID)
		for _, f := range fields {
			if !orders.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("models: invalid field %q for query", f)}
			}
			if f != orders.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.ConsumerID(); ok {
		_spec.SetField(orders.FieldConsumerID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedConsumerID(); ok {
		_spec.AddField(orders.FieldConsumerID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.PlayerID(); ok {
		_spec.SetField(orders.FieldPlayerID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedPlayerID(); ok {
		_spec.AddField(orders.FieldPlayerID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.ShopID(); ok {
		_spec.SetField(orders.FieldShopID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedShopID(); ok {
		_spec.AddField(orders.FieldShopID, field.TypeInt64, value)
	}
	if _u.mutation.ShopIDCleared() {
		_spec.ClearField(orders.FieldShopID, field.TypeInt64)
	}
	if value, ok := _u.mutation.ServiceSnapshot(); ok {
		_spec.SetField(orders.FieldServiceSnapshot, field.TypeJSON, value)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(orders.FieldStatus, field.TypeString, value)
	}
	if value, ok := _u.mutation.TotalPrice(); ok {
		_spec.SetField(orders.FieldTotalPrice, field.TypeOther, value)
	}
	if value, ok := _u.mutation.Note(); ok {
		_spec.SetField(orders.FieldNote, field.TypeString, value)
	}
	if _u.mutation.NoteCleared() {
		_spec.ClearField(orders.FieldNote, field.TypeString)
	}
	if value, ok := _u.mutation.Version(); ok {
		_spec.SetField(orders.FieldVersion, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedVersion(); ok {
		_spec.AddField(orders.FieldVersion, field.TypeInt, value)
	}
	if value, ok := _u.mutation.TimeoutJobID(); ok {
		_spec.SetField(orders.FieldTimeoutJobID, field.TypeString, value)
	}
	if _u.mutation.TimeoutJobIDCleared() {
		_spec.ClearField(orders.FieldTimeoutJobID, field.TypeString)
	}
	if value, ok := _u.mutation.SearchText(); ok {
		_spec.SetField(orders.FieldSearchText, field.TypeString, value)
	}
	if _u.mutation.SearchTextCleared() {
		_spec.ClearField(orders.FieldSearchText, field.TypeString)
	}
	if value, ok := _u.mutation.AcceptedAt(); ok {
		_spec.SetField(orders.FieldAcceptedAt, field.TypeTime, value)
	}
	if _u.mutation.AcceptedAtCleared() {
		_spec.ClearField(orders.FieldAcceptedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.ClosedAt(); ok {
		_spec.SetField(orders.FieldClosedAt, field.TypeTime, value)
	}
	if _u.mutation.ClosedAtCleared() {
		_spec.ClearField(orders.FieldClosedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.CompletedAt(); ok {
		_spec.SetField(orders.FieldCompletedAt, field.TypeTime, value)
	}
	if _u.mutation.CompletedAtCleared() {
		_spec.ClearField(orders.FieldCompletedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.CancelledAt(); ok {
		_spec.SetField(orders.FieldCancelledAt, field.TypeTime, value)
	}
	if _u.mutation.CancelledAtCleared() {
		_spec.ClearField(orders.FieldCancelledAt, field.TypeTime)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(orders.FieldUpdatedAt, field.TypeTime, value)
	}
	_node = &Orders{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{orders.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
