// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/Wei-Shaw/sub2api/ent/predicate"
	"github.com/Wei-Shaw/sub2api/ent/subscriptionplan"
)

// SubscriptionPlanUpdate is the builder for updating SubscriptionPlan entities.
type SubscriptionPlanUpdate struct {
	config
	hooks    []Hook
	mutation *SubscriptionPlanMutation
}

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

// SetGroupID sets the "group_id" field.
func (_u *SubscriptionPlanUpdate) SetGroupID(v int64) *SubscriptionPlanUpdate {
	_u.mutation.ResetGroupID()
	_u.mutation.SetGroupID(v)
	return _u
}

// SetNillableGroupID sets the "group_id" field if the given value is not nil.
func (_u *SubscriptionPlanUpdate) SetNillableGroupID(v *int64) *SubscriptionPlanUpdate {
	if v != nil {
		_u.SetGroupID(*v)
	}
	return _u
}

// AddGroupID adds value to the "group_id" field.
func (_u *SubscriptionPlanUpdate) AddGroupID(v int64) *SubscriptionPlanUpdate {
	_u.mutation.AddGroupID(v)
	return _u
}

// SetName sets the "name" field.
func (_u *SubscriptionPlanUpdate) SetName(v string) *SubscriptionPlanUpdate {
	_u.mutation.SetName(v)
	return _u
}

// SetNillableName sets the "name" field if the given value is not nil.
func (_u *SubscriptionPlanUpdate) SetNillableName(v *string) *SubscriptionPlanUpdate {
	if v != nil {
		_u.SetName(*v)
	}
	return _u
}

// SetDescription sets the "description" field.
func (_u *SubscriptionPlanUpdate) SetDescription(v string) *SubscriptionPlanUpdate {
	_u.mutation.SetDescription(v)
	return _u
}

// SetNillableDescription sets the "description" field if the given value is not nil.
func (_u *SubscriptionPlanUpdate) SetNillableDescription(v *string) *SubscriptionPlanUpdate {
	if v != nil {
		_u.SetDescription(*v)
	}
	return _u
}

// SetPrice sets the "price" field.
func (_u *SubscriptionPlanUpdate) SetPrice(v float64) *SubscriptionPlanUpdate {
	_u.mutation.ResetPrice()
	_u.mutation.SetPrice(v)
	return _u
}

// SetNillablePrice sets the "price" field if the given value is not nil.
func (_u *SubscriptionPlanUpdate) SetNillablePrice(v *float64) *SubscriptionPlanUpdate {
	if v != nil {
		_u.SetPrice(*v)
	}
	return _u
}

// AddPrice adds value to the "price" field.
func (_u *SubscriptionPlanUpdate) AddPrice(v float64) *SubscriptionPlanUpdate {
	_u.mutation.AddPrice(v)
	return _u
}

// SetOriginalPrice sets the "original_price" field.
func (_u *SubscriptionPlanUpdate) SetOriginalPrice(v float64) *SubscriptionPlanUpdate {
	_u.mutation.ResetOriginalPrice()
	_u.mutation.SetOriginalPrice(v)
	return _u
}

// SetNillableOriginalPrice sets the "original_price" field if the given value is not nil.
func (_u *SubscriptionPlanUpdate) SetNillableOriginalPrice(v *float64) *SubscriptionPlanUpdate {
	if v != nil {
		_u.SetOriginalPrice(*v)
	}
	return _u
}

// AddOriginalPrice adds value to the "original_price" field.
func (_u *SubscriptionPlanUpdate) AddOriginalPrice(v float64) *SubscriptionPlanUpdate {
	_u.mutation.AddOriginalPrice(v)
	return _u
}

// ClearOriginalPrice clears the value of the "original_price" field.
func (_u *SubscriptionPlanUpdate) ClearOriginalPrice() *SubscriptionPlanUpdate {
	_u.mutation.ClearOriginalPrice()
	return _u
}

// SetValidityDays sets the "validity_days" field.
func (_u *SubscriptionPlanUpdate) SetValidityDays(v int) *SubscriptionPlanUpdate {
	_u.mutation.ResetValidityDays()
	_u.mutation.SetValidityDays(v)
	return _u
}

// SetNillableValidityDays sets the "validity_days" field if the given value is not nil.
func (_u *SubscriptionPlanUpdate) SetNillableValidityDays(v *int) *SubscriptionPlanUpdate {
	if v != nil {
		_u.SetValidityDays(*v)
	}
	return _u
}

// AddValidityDays adds value to the "validity_days" field.
func (_u *SubscriptionPlanUpdate) AddValidityDays(v int) *SubscriptionPlanUpdate {
	_u.mutation.AddValidityDays(v)
	return _u
}

// SetValidityUnit sets the "validity_unit" field.
func (_u *SubscriptionPlanUpdate) SetValidityUnit(v string) *SubscriptionPlanUpdate {
	_u.mutation.SetValidityUnit(v)
	return _u
}

// SetNillableValidityUnit sets the "validity_unit" field if the given value is not nil.
func (_u *SubscriptionPlanUpdate) SetNillableValidityUnit(v *string) *SubscriptionPlanUpdate {
	if v != nil {
		_u.SetValidityUnit(*v)
	}
	return _u
}

// SetFeatures sets the "features" field.
func (_u *SubscriptionPlanUpdate) SetFeatures(v string) *SubscriptionPlanUpdate {
	_u.mutation.SetFeatures(v)
	return _u
}

// SetNillableFeatures sets the "features" field if the given value is not nil.
func (_u *SubscriptionPlanUpdate) SetNillableFeatures(v *string) *SubscriptionPlanUpdate {
	if v != nil {
		_u.SetFeatures(*v)
	}
	return _u
}

// SetProductName sets the "product_name" field.
func (_u *SubscriptionPlanUpdate) SetProductName(v string) *SubscriptionPlanUpdate {
	_u.mutation.SetProductName(v)
	return _u
}

// SetNillableProductName sets the "product_name" field if the given value is not nil.
func (_u *SubscriptionPlanUpdate) SetNillableProductName(v *string) *SubscriptionPlanUpdate {
	if v != nil {
		_u.SetProductName(*v)
	}
	return _u
}

// SetForSale sets the "for_sale" field.
func (_u *SubscriptionPlanUpdate) SetForSale(v bool) *SubscriptionPlanUpdate {
	_u.mutation.SetForSale(v)
	return _u
}

// SetNillableForSale sets the "for_sale" field if the given value is not nil.
func (_u *SubscriptionPlanUpdate) SetNillableForSale(v *bool) *SubscriptionPlanUpdate {
	if v != nil {
		_u.SetForSale(*v)
	}
	return _u
}

// SetSortOrder sets the "sort_order" field.
func (_u *SubscriptionPlanUpdate) SetSortOrder(v int) *SubscriptionPlanUpdate {
	_u.mutation.ResetSortOrder()
	_u.mutation.SetSortOrder(v)
	return _u
}

// SetNillableSortOrder sets the "sort_order" field if the given value is not nil.
func (_u *SubscriptionPlanUpdate) SetNillableSortOrder(v *int) *SubscriptionPlanUpdate {
	if v != nil {
		_u.SetSortOrder(*v)
	}
	return _u
}

// AddSortOrder adds value to the "sort_order" field.
func (_u *SubscriptionPlanUpdate) AddSortOrder(v int) *SubscriptionPlanUpdate {
	_u.mutation.AddSortOrder(v)
	return _u
}

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *SubscriptionPlanUpdate) 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 *SubscriptionPlanUpdate) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := subscriptionplan.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *SubscriptionPlanUpdate) check() error {
	if v, ok := _u.mutation.Name(); ok {
		if err := subscriptionplan.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "SubscriptionPlan.name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.ValidityUnit(); ok {
		if err := subscriptionplan.ValidityUnitValidator(v); err != nil {
			return &ValidationError{Name: "validity_unit", err: fmt.Errorf(`ent: validator failed for field "SubscriptionPlan.validity_unit": %w`, err)}
		}
	}
	if v, ok := _u.mutation.ProductName(); ok {
		if err := subscriptionplan.ProductNameValidator(v); err != nil {
			return &ValidationError{Name: "product_name", err: fmt.Errorf(`ent: validator failed for field "SubscriptionPlan.product_name": %w`, err)}
		}
	}
	return nil
}

func (_u *SubscriptionPlanUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(subscriptionplan.Table, subscriptionplan.Columns, sqlgraph.NewFieldSpec(subscriptionplan.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.GroupID(); ok {
		_spec.SetField(subscriptionplan.FieldGroupID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedGroupID(); ok {
		_spec.AddField(subscriptionplan.FieldGroupID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(subscriptionplan.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Description(); ok {
		_spec.SetField(subscriptionplan.FieldDescription, field.TypeString, value)
	}
	if value, ok := _u.mutation.Price(); ok {
		_spec.SetField(subscriptionplan.FieldPrice, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedPrice(); ok {
		_spec.AddField(subscriptionplan.FieldPrice, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.OriginalPrice(); ok {
		_spec.SetField(subscriptionplan.FieldOriginalPrice, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedOriginalPrice(); ok {
		_spec.AddField(subscriptionplan.FieldOriginalPrice, field.TypeFloat64, value)
	}
	if _u.mutation.OriginalPriceCleared() {
		_spec.ClearField(subscriptionplan.FieldOriginalPrice, field.TypeFloat64)
	}
	if value, ok := _u.mutation.ValidityDays(); ok {
		_spec.SetField(subscriptionplan.FieldValidityDays, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedValidityDays(); ok {
		_spec.AddField(subscriptionplan.FieldValidityDays, field.TypeInt, value)
	}
	if value, ok := _u.mutation.ValidityUnit(); ok {
		_spec.SetField(subscriptionplan.FieldValidityUnit, field.TypeString, value)
	}
	if value, ok := _u.mutation.Features(); ok {
		_spec.SetField(subscriptionplan.FieldFeatures, field.TypeString, value)
	}
	if value, ok := _u.mutation.ProductName(); ok {
		_spec.SetField(subscriptionplan.FieldProductName, field.TypeString, value)
	}
	if value, ok := _u.mutation.ForSale(); ok {
		_spec.SetField(subscriptionplan.FieldForSale, field.TypeBool, value)
	}
	if value, ok := _u.mutation.SortOrder(); ok {
		_spec.SetField(subscriptionplan.FieldSortOrder, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedSortOrder(); ok {
		_spec.AddField(subscriptionplan.FieldSortOrder, field.TypeInt, value)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(subscriptionplan.FieldUpdatedAt, field.TypeTime, value)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{subscriptionplan.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// SubscriptionPlanUpdateOne is the builder for updating a single SubscriptionPlan entity.
type SubscriptionPlanUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *SubscriptionPlanMutation
}

// SetGroupID sets the "group_id" field.
func (_u *SubscriptionPlanUpdateOne) SetGroupID(v int64) *SubscriptionPlanUpdateOne {
	_u.mutation.ResetGroupID()
	_u.mutation.SetGroupID(v)
	return _u
}

// SetNillableGroupID sets the "group_id" field if the given value is not nil.
func (_u *SubscriptionPlanUpdateOne) SetNillableGroupID(v *int64) *SubscriptionPlanUpdateOne {
	if v != nil {
		_u.SetGroupID(*v)
	}
	return _u
}

// AddGroupID adds value to the "group_id" field.
func (_u *SubscriptionPlanUpdateOne) AddGroupID(v int64) *SubscriptionPlanUpdateOne {
	_u.mutation.AddGroupID(v)
	return _u
}

// SetName sets the "name" field.
func (_u *SubscriptionPlanUpdateOne) SetName(v string) *SubscriptionPlanUpdateOne {
	_u.mutation.SetName(v)
	return _u
}

// SetNillableName sets the "name" field if the given value is not nil.
func (_u *SubscriptionPlanUpdateOne) SetNillableName(v *string) *SubscriptionPlanUpdateOne {
	if v != nil {
		_u.SetName(*v)
	}
	return _u
}

// SetDescription sets the "description" field.
func (_u *SubscriptionPlanUpdateOne) SetDescription(v string) *SubscriptionPlanUpdateOne {
	_u.mutation.SetDescription(v)
	return _u
}

// SetNillableDescription sets the "description" field if the given value is not nil.
func (_u *SubscriptionPlanUpdateOne) SetNillableDescription(v *string) *SubscriptionPlanUpdateOne {
	if v != nil {
		_u.SetDescription(*v)
	}
	return _u
}

// SetPrice sets the "price" field.
func (_u *SubscriptionPlanUpdateOne) SetPrice(v float64) *SubscriptionPlanUpdateOne {
	_u.mutation.ResetPrice()
	_u.mutation.SetPrice(v)
	return _u
}

// SetNillablePrice sets the "price" field if the given value is not nil.
func (_u *SubscriptionPlanUpdateOne) SetNillablePrice(v *float64) *SubscriptionPlanUpdateOne {
	if v != nil {
		_u.SetPrice(*v)
	}
	return _u
}

// AddPrice adds value to the "price" field.
func (_u *SubscriptionPlanUpdateOne) AddPrice(v float64) *SubscriptionPlanUpdateOne {
	_u.mutation.AddPrice(v)
	return _u
}

// SetOriginalPrice sets the "original_price" field.
func (_u *SubscriptionPlanUpdateOne) SetOriginalPrice(v float64) *SubscriptionPlanUpdateOne {
	_u.mutation.ResetOriginalPrice()
	_u.mutation.SetOriginalPrice(v)
	return _u
}

// SetNillableOriginalPrice sets the "original_price" field if the given value is not nil.
func (_u *SubscriptionPlanUpdateOne) SetNillableOriginalPrice(v *float64) *SubscriptionPlanUpdateOne {
	if v != nil {
		_u.SetOriginalPrice(*v)
	}
	return _u
}

// AddOriginalPrice adds value to the "original_price" field.
func (_u *SubscriptionPlanUpdateOne) AddOriginalPrice(v float64) *SubscriptionPlanUpdateOne {
	_u.mutation.AddOriginalPrice(v)
	return _u
}

// ClearOriginalPrice clears the value of the "original_price" field.
func (_u *SubscriptionPlanUpdateOne) ClearOriginalPrice() *SubscriptionPlanUpdateOne {
	_u.mutation.ClearOriginalPrice()
	return _u
}

// SetValidityDays sets the "validity_days" field.
func (_u *SubscriptionPlanUpdateOne) SetValidityDays(v int) *SubscriptionPlanUpdateOne {
	_u.mutation.ResetValidityDays()
	_u.mutation.SetValidityDays(v)
	return _u
}

// SetNillableValidityDays sets the "validity_days" field if the given value is not nil.
func (_u *SubscriptionPlanUpdateOne) SetNillableValidityDays(v *int) *SubscriptionPlanUpdateOne {
	if v != nil {
		_u.SetValidityDays(*v)
	}
	return _u
}

// AddValidityDays adds value to the "validity_days" field.
func (_u *SubscriptionPlanUpdateOne) AddValidityDays(v int) *SubscriptionPlanUpdateOne {
	_u.mutation.AddValidityDays(v)
	return _u
}

// SetValidityUnit sets the "validity_unit" field.
func (_u *SubscriptionPlanUpdateOne) SetValidityUnit(v string) *SubscriptionPlanUpdateOne {
	_u.mutation.SetValidityUnit(v)
	return _u
}

// SetNillableValidityUnit sets the "validity_unit" field if the given value is not nil.
func (_u *SubscriptionPlanUpdateOne) SetNillableValidityUnit(v *string) *SubscriptionPlanUpdateOne {
	if v != nil {
		_u.SetValidityUnit(*v)
	}
	return _u
}

// SetFeatures sets the "features" field.
func (_u *SubscriptionPlanUpdateOne) SetFeatures(v string) *SubscriptionPlanUpdateOne {
	_u.mutation.SetFeatures(v)
	return _u
}

// SetNillableFeatures sets the "features" field if the given value is not nil.
func (_u *SubscriptionPlanUpdateOne) SetNillableFeatures(v *string) *SubscriptionPlanUpdateOne {
	if v != nil {
		_u.SetFeatures(*v)
	}
	return _u
}

// SetProductName sets the "product_name" field.
func (_u *SubscriptionPlanUpdateOne) SetProductName(v string) *SubscriptionPlanUpdateOne {
	_u.mutation.SetProductName(v)
	return _u
}

// SetNillableProductName sets the "product_name" field if the given value is not nil.
func (_u *SubscriptionPlanUpdateOne) SetNillableProductName(v *string) *SubscriptionPlanUpdateOne {
	if v != nil {
		_u.SetProductName(*v)
	}
	return _u
}

// SetForSale sets the "for_sale" field.
func (_u *SubscriptionPlanUpdateOne) SetForSale(v bool) *SubscriptionPlanUpdateOne {
	_u.mutation.SetForSale(v)
	return _u
}

// SetNillableForSale sets the "for_sale" field if the given value is not nil.
func (_u *SubscriptionPlanUpdateOne) SetNillableForSale(v *bool) *SubscriptionPlanUpdateOne {
	if v != nil {
		_u.SetForSale(*v)
	}
	return _u
}

// SetSortOrder sets the "sort_order" field.
func (_u *SubscriptionPlanUpdateOne) SetSortOrder(v int) *SubscriptionPlanUpdateOne {
	_u.mutation.ResetSortOrder()
	_u.mutation.SetSortOrder(v)
	return _u
}

// SetNillableSortOrder sets the "sort_order" field if the given value is not nil.
func (_u *SubscriptionPlanUpdateOne) SetNillableSortOrder(v *int) *SubscriptionPlanUpdateOne {
	if v != nil {
		_u.SetSortOrder(*v)
	}
	return _u
}

// AddSortOrder adds value to the "sort_order" field.
func (_u *SubscriptionPlanUpdateOne) AddSortOrder(v int) *SubscriptionPlanUpdateOne {
	_u.mutation.AddSortOrder(v)
	return _u
}

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

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

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *SubscriptionPlanUpdateOne) 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 *SubscriptionPlanUpdateOne) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := subscriptionplan.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *SubscriptionPlanUpdateOne) check() error {
	if v, ok := _u.mutation.Name(); ok {
		if err := subscriptionplan.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "SubscriptionPlan.name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.ValidityUnit(); ok {
		if err := subscriptionplan.ValidityUnitValidator(v); err != nil {
			return &ValidationError{Name: "validity_unit", err: fmt.Errorf(`ent: validator failed for field "SubscriptionPlan.validity_unit": %w`, err)}
		}
	}
	if v, ok := _u.mutation.ProductName(); ok {
		if err := subscriptionplan.ProductNameValidator(v); err != nil {
			return &ValidationError{Name: "product_name", err: fmt.Errorf(`ent: validator failed for field "SubscriptionPlan.product_name": %w`, err)}
		}
	}
	return nil
}

func (_u *SubscriptionPlanUpdateOne) sqlSave(ctx context.Context) (_node *SubscriptionPlan, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(subscriptionplan.Table, subscriptionplan.Columns, sqlgraph.NewFieldSpec(subscriptionplan.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "SubscriptionPlan.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, subscriptionplan.FieldID)
		for _, f := range fields {
			if !subscriptionplan.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != subscriptionplan.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.GroupID(); ok {
		_spec.SetField(subscriptionplan.FieldGroupID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedGroupID(); ok {
		_spec.AddField(subscriptionplan.FieldGroupID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(subscriptionplan.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Description(); ok {
		_spec.SetField(subscriptionplan.FieldDescription, field.TypeString, value)
	}
	if value, ok := _u.mutation.Price(); ok {
		_spec.SetField(subscriptionplan.FieldPrice, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedPrice(); ok {
		_spec.AddField(subscriptionplan.FieldPrice, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.OriginalPrice(); ok {
		_spec.SetField(subscriptionplan.FieldOriginalPrice, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedOriginalPrice(); ok {
		_spec.AddField(subscriptionplan.FieldOriginalPrice, field.TypeFloat64, value)
	}
	if _u.mutation.OriginalPriceCleared() {
		_spec.ClearField(subscriptionplan.FieldOriginalPrice, field.TypeFloat64)
	}
	if value, ok := _u.mutation.ValidityDays(); ok {
		_spec.SetField(subscriptionplan.FieldValidityDays, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedValidityDays(); ok {
		_spec.AddField(subscriptionplan.FieldValidityDays, field.TypeInt, value)
	}
	if value, ok := _u.mutation.ValidityUnit(); ok {
		_spec.SetField(subscriptionplan.FieldValidityUnit, field.TypeString, value)
	}
	if value, ok := _u.mutation.Features(); ok {
		_spec.SetField(subscriptionplan.FieldFeatures, field.TypeString, value)
	}
	if value, ok := _u.mutation.ProductName(); ok {
		_spec.SetField(subscriptionplan.FieldProductName, field.TypeString, value)
	}
	if value, ok := _u.mutation.ForSale(); ok {
		_spec.SetField(subscriptionplan.FieldForSale, field.TypeBool, value)
	}
	if value, ok := _u.mutation.SortOrder(); ok {
		_spec.SetField(subscriptionplan.FieldSortOrder, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedSortOrder(); ok {
		_spec.AddField(subscriptionplan.FieldSortOrder, field.TypeInt, value)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(subscriptionplan.FieldUpdatedAt, field.TypeTime, value)
	}
	_node = &SubscriptionPlan{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{subscriptionplan.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
