// 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/group"
	"github.com/Wei-Shaw/sub2api/ent/predicate"
	"github.com/Wei-Shaw/sub2api/ent/redeemcode"
	"github.com/Wei-Shaw/sub2api/ent/user"
)

// RedeemCodeUpdate is the builder for updating RedeemCode entities.
type RedeemCodeUpdate struct {
	config
	hooks    []Hook
	mutation *RedeemCodeMutation
}

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

// SetCode sets the "code" field.
func (_u *RedeemCodeUpdate) SetCode(v string) *RedeemCodeUpdate {
	_u.mutation.SetCode(v)
	return _u
}

// SetNillableCode sets the "code" field if the given value is not nil.
func (_u *RedeemCodeUpdate) SetNillableCode(v *string) *RedeemCodeUpdate {
	if v != nil {
		_u.SetCode(*v)
	}
	return _u
}

// SetType sets the "type" field.
func (_u *RedeemCodeUpdate) SetType(v string) *RedeemCodeUpdate {
	_u.mutation.SetType(v)
	return _u
}

// SetNillableType sets the "type" field if the given value is not nil.
func (_u *RedeemCodeUpdate) SetNillableType(v *string) *RedeemCodeUpdate {
	if v != nil {
		_u.SetType(*v)
	}
	return _u
}

// SetValue sets the "value" field.
func (_u *RedeemCodeUpdate) SetValue(v float64) *RedeemCodeUpdate {
	_u.mutation.ResetValue()
	_u.mutation.SetValue(v)
	return _u
}

// SetNillableValue sets the "value" field if the given value is not nil.
func (_u *RedeemCodeUpdate) SetNillableValue(v *float64) *RedeemCodeUpdate {
	if v != nil {
		_u.SetValue(*v)
	}
	return _u
}

// AddValue adds value to the "value" field.
func (_u *RedeemCodeUpdate) AddValue(v float64) *RedeemCodeUpdate {
	_u.mutation.AddValue(v)
	return _u
}

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

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

// SetUsedBy sets the "used_by" field.
func (_u *RedeemCodeUpdate) SetUsedBy(v int64) *RedeemCodeUpdate {
	_u.mutation.SetUsedBy(v)
	return _u
}

// SetNillableUsedBy sets the "used_by" field if the given value is not nil.
func (_u *RedeemCodeUpdate) SetNillableUsedBy(v *int64) *RedeemCodeUpdate {
	if v != nil {
		_u.SetUsedBy(*v)
	}
	return _u
}

// ClearUsedBy clears the value of the "used_by" field.
func (_u *RedeemCodeUpdate) ClearUsedBy() *RedeemCodeUpdate {
	_u.mutation.ClearUsedBy()
	return _u
}

// SetUsedAt sets the "used_at" field.
func (_u *RedeemCodeUpdate) SetUsedAt(v time.Time) *RedeemCodeUpdate {
	_u.mutation.SetUsedAt(v)
	return _u
}

// SetNillableUsedAt sets the "used_at" field if the given value is not nil.
func (_u *RedeemCodeUpdate) SetNillableUsedAt(v *time.Time) *RedeemCodeUpdate {
	if v != nil {
		_u.SetUsedAt(*v)
	}
	return _u
}

// ClearUsedAt clears the value of the "used_at" field.
func (_u *RedeemCodeUpdate) ClearUsedAt() *RedeemCodeUpdate {
	_u.mutation.ClearUsedAt()
	return _u
}

// SetNotes sets the "notes" field.
func (_u *RedeemCodeUpdate) SetNotes(v string) *RedeemCodeUpdate {
	_u.mutation.SetNotes(v)
	return _u
}

// SetNillableNotes sets the "notes" field if the given value is not nil.
func (_u *RedeemCodeUpdate) SetNillableNotes(v *string) *RedeemCodeUpdate {
	if v != nil {
		_u.SetNotes(*v)
	}
	return _u
}

// ClearNotes clears the value of the "notes" field.
func (_u *RedeemCodeUpdate) ClearNotes() *RedeemCodeUpdate {
	_u.mutation.ClearNotes()
	return _u
}

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

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

// ClearGroupID clears the value of the "group_id" field.
func (_u *RedeemCodeUpdate) ClearGroupID() *RedeemCodeUpdate {
	_u.mutation.ClearGroupID()
	return _u
}

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

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

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

// SetUserID sets the "user" edge to the User entity by ID.
func (_u *RedeemCodeUpdate) SetUserID(id int64) *RedeemCodeUpdate {
	_u.mutation.SetUserID(id)
	return _u
}

// SetNillableUserID sets the "user" edge to the User entity by ID if the given value is not nil.
func (_u *RedeemCodeUpdate) SetNillableUserID(id *int64) *RedeemCodeUpdate {
	if id != nil {
		_u = _u.SetUserID(*id)
	}
	return _u
}

// SetUser sets the "user" edge to the User entity.
func (_u *RedeemCodeUpdate) SetUser(v *User) *RedeemCodeUpdate {
	return _u.SetUserID(v.ID)
}

// SetGroup sets the "group" edge to the Group entity.
func (_u *RedeemCodeUpdate) SetGroup(v *Group) *RedeemCodeUpdate {
	return _u.SetGroupID(v.ID)
}

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

// ClearUser clears the "user" edge to the User entity.
func (_u *RedeemCodeUpdate) ClearUser() *RedeemCodeUpdate {
	_u.mutation.ClearUser()
	return _u
}

// ClearGroup clears the "group" edge to the Group entity.
func (_u *RedeemCodeUpdate) ClearGroup() *RedeemCodeUpdate {
	_u.mutation.ClearGroup()
	return _u
}

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *RedeemCodeUpdate) 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 *RedeemCodeUpdate) check() error {
	if v, ok := _u.mutation.Code(); ok {
		if err := redeemcode.CodeValidator(v); err != nil {
			return &ValidationError{Name: "code", err: fmt.Errorf(`ent: validator failed for field "RedeemCode.code": %w`, err)}
		}
	}
	if v, ok := _u.mutation.GetType(); ok {
		if err := redeemcode.TypeValidator(v); err != nil {
			return &ValidationError{Name: "type", err: fmt.Errorf(`ent: validator failed for field "RedeemCode.type": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Status(); ok {
		if err := redeemcode.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "RedeemCode.status": %w`, err)}
		}
	}
	return nil
}

func (_u *RedeemCodeUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(redeemcode.Table, redeemcode.Columns, sqlgraph.NewFieldSpec(redeemcode.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.Code(); ok {
		_spec.SetField(redeemcode.FieldCode, field.TypeString, value)
	}
	if value, ok := _u.mutation.GetType(); ok {
		_spec.SetField(redeemcode.FieldType, field.TypeString, value)
	}
	if value, ok := _u.mutation.Value(); ok {
		_spec.SetField(redeemcode.FieldValue, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedValue(); ok {
		_spec.AddField(redeemcode.FieldValue, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(redeemcode.FieldStatus, field.TypeString, value)
	}
	if value, ok := _u.mutation.UsedAt(); ok {
		_spec.SetField(redeemcode.FieldUsedAt, field.TypeTime, value)
	}
	if _u.mutation.UsedAtCleared() {
		_spec.ClearField(redeemcode.FieldUsedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.Notes(); ok {
		_spec.SetField(redeemcode.FieldNotes, field.TypeString, value)
	}
	if _u.mutation.NotesCleared() {
		_spec.ClearField(redeemcode.FieldNotes, field.TypeString)
	}
	if value, ok := _u.mutation.ValidityDays(); ok {
		_spec.SetField(redeemcode.FieldValidityDays, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedValidityDays(); ok {
		_spec.AddField(redeemcode.FieldValidityDays, field.TypeInt, value)
	}
	if _u.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   redeemcode.UserTable,
			Columns: []string{redeemcode.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   redeemcode.UserTable,
			Columns: []string{redeemcode.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.GroupCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   redeemcode.GroupTable,
			Columns: []string{redeemcode.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.GroupIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   redeemcode.GroupTable,
			Columns: []string{redeemcode.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{redeemcode.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// RedeemCodeUpdateOne is the builder for updating a single RedeemCode entity.
type RedeemCodeUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *RedeemCodeMutation
}

// SetCode sets the "code" field.
func (_u *RedeemCodeUpdateOne) SetCode(v string) *RedeemCodeUpdateOne {
	_u.mutation.SetCode(v)
	return _u
}

// SetNillableCode sets the "code" field if the given value is not nil.
func (_u *RedeemCodeUpdateOne) SetNillableCode(v *string) *RedeemCodeUpdateOne {
	if v != nil {
		_u.SetCode(*v)
	}
	return _u
}

// SetType sets the "type" field.
func (_u *RedeemCodeUpdateOne) SetType(v string) *RedeemCodeUpdateOne {
	_u.mutation.SetType(v)
	return _u
}

// SetNillableType sets the "type" field if the given value is not nil.
func (_u *RedeemCodeUpdateOne) SetNillableType(v *string) *RedeemCodeUpdateOne {
	if v != nil {
		_u.SetType(*v)
	}
	return _u
}

// SetValue sets the "value" field.
func (_u *RedeemCodeUpdateOne) SetValue(v float64) *RedeemCodeUpdateOne {
	_u.mutation.ResetValue()
	_u.mutation.SetValue(v)
	return _u
}

// SetNillableValue sets the "value" field if the given value is not nil.
func (_u *RedeemCodeUpdateOne) SetNillableValue(v *float64) *RedeemCodeUpdateOne {
	if v != nil {
		_u.SetValue(*v)
	}
	return _u
}

// AddValue adds value to the "value" field.
func (_u *RedeemCodeUpdateOne) AddValue(v float64) *RedeemCodeUpdateOne {
	_u.mutation.AddValue(v)
	return _u
}

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

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

// SetUsedBy sets the "used_by" field.
func (_u *RedeemCodeUpdateOne) SetUsedBy(v int64) *RedeemCodeUpdateOne {
	_u.mutation.SetUsedBy(v)
	return _u
}

// SetNillableUsedBy sets the "used_by" field if the given value is not nil.
func (_u *RedeemCodeUpdateOne) SetNillableUsedBy(v *int64) *RedeemCodeUpdateOne {
	if v != nil {
		_u.SetUsedBy(*v)
	}
	return _u
}

// ClearUsedBy clears the value of the "used_by" field.
func (_u *RedeemCodeUpdateOne) ClearUsedBy() *RedeemCodeUpdateOne {
	_u.mutation.ClearUsedBy()
	return _u
}

// SetUsedAt sets the "used_at" field.
func (_u *RedeemCodeUpdateOne) SetUsedAt(v time.Time) *RedeemCodeUpdateOne {
	_u.mutation.SetUsedAt(v)
	return _u
}

// SetNillableUsedAt sets the "used_at" field if the given value is not nil.
func (_u *RedeemCodeUpdateOne) SetNillableUsedAt(v *time.Time) *RedeemCodeUpdateOne {
	if v != nil {
		_u.SetUsedAt(*v)
	}
	return _u
}

// ClearUsedAt clears the value of the "used_at" field.
func (_u *RedeemCodeUpdateOne) ClearUsedAt() *RedeemCodeUpdateOne {
	_u.mutation.ClearUsedAt()
	return _u
}

// SetNotes sets the "notes" field.
func (_u *RedeemCodeUpdateOne) SetNotes(v string) *RedeemCodeUpdateOne {
	_u.mutation.SetNotes(v)
	return _u
}

// SetNillableNotes sets the "notes" field if the given value is not nil.
func (_u *RedeemCodeUpdateOne) SetNillableNotes(v *string) *RedeemCodeUpdateOne {
	if v != nil {
		_u.SetNotes(*v)
	}
	return _u
}

// ClearNotes clears the value of the "notes" field.
func (_u *RedeemCodeUpdateOne) ClearNotes() *RedeemCodeUpdateOne {
	_u.mutation.ClearNotes()
	return _u
}

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

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

// ClearGroupID clears the value of the "group_id" field.
func (_u *RedeemCodeUpdateOne) ClearGroupID() *RedeemCodeUpdateOne {
	_u.mutation.ClearGroupID()
	return _u
}

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

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

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

// SetUserID sets the "user" edge to the User entity by ID.
func (_u *RedeemCodeUpdateOne) SetUserID(id int64) *RedeemCodeUpdateOne {
	_u.mutation.SetUserID(id)
	return _u
}

// SetNillableUserID sets the "user" edge to the User entity by ID if the given value is not nil.
func (_u *RedeemCodeUpdateOne) SetNillableUserID(id *int64) *RedeemCodeUpdateOne {
	if id != nil {
		_u = _u.SetUserID(*id)
	}
	return _u
}

// SetUser sets the "user" edge to the User entity.
func (_u *RedeemCodeUpdateOne) SetUser(v *User) *RedeemCodeUpdateOne {
	return _u.SetUserID(v.ID)
}

// SetGroup sets the "group" edge to the Group entity.
func (_u *RedeemCodeUpdateOne) SetGroup(v *Group) *RedeemCodeUpdateOne {
	return _u.SetGroupID(v.ID)
}

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

// ClearUser clears the "user" edge to the User entity.
func (_u *RedeemCodeUpdateOne) ClearUser() *RedeemCodeUpdateOne {
	_u.mutation.ClearUser()
	return _u
}

// ClearGroup clears the "group" edge to the Group entity.
func (_u *RedeemCodeUpdateOne) ClearGroup() *RedeemCodeUpdateOne {
	_u.mutation.ClearGroup()
	return _u
}

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *RedeemCodeUpdateOne) 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 *RedeemCodeUpdateOne) check() error {
	if v, ok := _u.mutation.Code(); ok {
		if err := redeemcode.CodeValidator(v); err != nil {
			return &ValidationError{Name: "code", err: fmt.Errorf(`ent: validator failed for field "RedeemCode.code": %w`, err)}
		}
	}
	if v, ok := _u.mutation.GetType(); ok {
		if err := redeemcode.TypeValidator(v); err != nil {
			return &ValidationError{Name: "type", err: fmt.Errorf(`ent: validator failed for field "RedeemCode.type": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Status(); ok {
		if err := redeemcode.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "RedeemCode.status": %w`, err)}
		}
	}
	return nil
}

func (_u *RedeemCodeUpdateOne) sqlSave(ctx context.Context) (_node *RedeemCode, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(redeemcode.Table, redeemcode.Columns, sqlgraph.NewFieldSpec(redeemcode.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "RedeemCode.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, redeemcode.FieldID)
		for _, f := range fields {
			if !redeemcode.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != redeemcode.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.Code(); ok {
		_spec.SetField(redeemcode.FieldCode, field.TypeString, value)
	}
	if value, ok := _u.mutation.GetType(); ok {
		_spec.SetField(redeemcode.FieldType, field.TypeString, value)
	}
	if value, ok := _u.mutation.Value(); ok {
		_spec.SetField(redeemcode.FieldValue, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedValue(); ok {
		_spec.AddField(redeemcode.FieldValue, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(redeemcode.FieldStatus, field.TypeString, value)
	}
	if value, ok := _u.mutation.UsedAt(); ok {
		_spec.SetField(redeemcode.FieldUsedAt, field.TypeTime, value)
	}
	if _u.mutation.UsedAtCleared() {
		_spec.ClearField(redeemcode.FieldUsedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.Notes(); ok {
		_spec.SetField(redeemcode.FieldNotes, field.TypeString, value)
	}
	if _u.mutation.NotesCleared() {
		_spec.ClearField(redeemcode.FieldNotes, field.TypeString)
	}
	if value, ok := _u.mutation.ValidityDays(); ok {
		_spec.SetField(redeemcode.FieldValidityDays, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedValidityDays(); ok {
		_spec.AddField(redeemcode.FieldValidityDays, field.TypeInt, value)
	}
	if _u.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   redeemcode.UserTable,
			Columns: []string{redeemcode.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   redeemcode.UserTable,
			Columns: []string{redeemcode.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.GroupCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   redeemcode.GroupTable,
			Columns: []string{redeemcode.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.GroupIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   redeemcode.GroupTable,
			Columns: []string{redeemcode.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &RedeemCode{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{redeemcode.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
