// 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/account"
	"github.com/Wei-Shaw/sub2api/ent/apikey"
	"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/usagelog"
	"github.com/Wei-Shaw/sub2api/ent/user"
	"github.com/Wei-Shaw/sub2api/ent/usersubscription"
)

// GroupUpdate is the builder for updating Group entities.
type GroupUpdate struct {
	config
	hooks    []Hook
	mutation *GroupMutation
}

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

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

// SetDeletedAt sets the "deleted_at" field.
func (_u *GroupUpdate) SetDeletedAt(v time.Time) *GroupUpdate {
	_u.mutation.SetDeletedAt(v)
	return _u
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (_u *GroupUpdate) SetNillableDeletedAt(v *time.Time) *GroupUpdate {
	if v != nil {
		_u.SetDeletedAt(*v)
	}
	return _u
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (_u *GroupUpdate) ClearDeletedAt() *GroupUpdate {
	_u.mutation.ClearDeletedAt()
	return _u
}

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

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

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

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

// ClearDescription clears the value of the "description" field.
func (_u *GroupUpdate) ClearDescription() *GroupUpdate {
	_u.mutation.ClearDescription()
	return _u
}

// SetRateMultiplier sets the "rate_multiplier" field.
func (_u *GroupUpdate) SetRateMultiplier(v float64) *GroupUpdate {
	_u.mutation.ResetRateMultiplier()
	_u.mutation.SetRateMultiplier(v)
	return _u
}

// SetNillableRateMultiplier sets the "rate_multiplier" field if the given value is not nil.
func (_u *GroupUpdate) SetNillableRateMultiplier(v *float64) *GroupUpdate {
	if v != nil {
		_u.SetRateMultiplier(*v)
	}
	return _u
}

// AddRateMultiplier adds value to the "rate_multiplier" field.
func (_u *GroupUpdate) AddRateMultiplier(v float64) *GroupUpdate {
	_u.mutation.AddRateMultiplier(v)
	return _u
}

// SetIsExclusive sets the "is_exclusive" field.
func (_u *GroupUpdate) SetIsExclusive(v bool) *GroupUpdate {
	_u.mutation.SetIsExclusive(v)
	return _u
}

// SetNillableIsExclusive sets the "is_exclusive" field if the given value is not nil.
func (_u *GroupUpdate) SetNillableIsExclusive(v *bool) *GroupUpdate {
	if v != nil {
		_u.SetIsExclusive(*v)
	}
	return _u
}

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

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

// SetPlatform sets the "platform" field.
func (_u *GroupUpdate) SetPlatform(v string) *GroupUpdate {
	_u.mutation.SetPlatform(v)
	return _u
}

// SetNillablePlatform sets the "platform" field if the given value is not nil.
func (_u *GroupUpdate) SetNillablePlatform(v *string) *GroupUpdate {
	if v != nil {
		_u.SetPlatform(*v)
	}
	return _u
}

// SetSubscriptionType sets the "subscription_type" field.
func (_u *GroupUpdate) SetSubscriptionType(v string) *GroupUpdate {
	_u.mutation.SetSubscriptionType(v)
	return _u
}

// SetNillableSubscriptionType sets the "subscription_type" field if the given value is not nil.
func (_u *GroupUpdate) SetNillableSubscriptionType(v *string) *GroupUpdate {
	if v != nil {
		_u.SetSubscriptionType(*v)
	}
	return _u
}

// SetDailyLimitUsd sets the "daily_limit_usd" field.
func (_u *GroupUpdate) SetDailyLimitUsd(v float64) *GroupUpdate {
	_u.mutation.ResetDailyLimitUsd()
	_u.mutation.SetDailyLimitUsd(v)
	return _u
}

// SetNillableDailyLimitUsd sets the "daily_limit_usd" field if the given value is not nil.
func (_u *GroupUpdate) SetNillableDailyLimitUsd(v *float64) *GroupUpdate {
	if v != nil {
		_u.SetDailyLimitUsd(*v)
	}
	return _u
}

// AddDailyLimitUsd adds value to the "daily_limit_usd" field.
func (_u *GroupUpdate) AddDailyLimitUsd(v float64) *GroupUpdate {
	_u.mutation.AddDailyLimitUsd(v)
	return _u
}

// ClearDailyLimitUsd clears the value of the "daily_limit_usd" field.
func (_u *GroupUpdate) ClearDailyLimitUsd() *GroupUpdate {
	_u.mutation.ClearDailyLimitUsd()
	return _u
}

// SetWeeklyLimitUsd sets the "weekly_limit_usd" field.
func (_u *GroupUpdate) SetWeeklyLimitUsd(v float64) *GroupUpdate {
	_u.mutation.ResetWeeklyLimitUsd()
	_u.mutation.SetWeeklyLimitUsd(v)
	return _u
}

// SetNillableWeeklyLimitUsd sets the "weekly_limit_usd" field if the given value is not nil.
func (_u *GroupUpdate) SetNillableWeeklyLimitUsd(v *float64) *GroupUpdate {
	if v != nil {
		_u.SetWeeklyLimitUsd(*v)
	}
	return _u
}

// AddWeeklyLimitUsd adds value to the "weekly_limit_usd" field.
func (_u *GroupUpdate) AddWeeklyLimitUsd(v float64) *GroupUpdate {
	_u.mutation.AddWeeklyLimitUsd(v)
	return _u
}

// ClearWeeklyLimitUsd clears the value of the "weekly_limit_usd" field.
func (_u *GroupUpdate) ClearWeeklyLimitUsd() *GroupUpdate {
	_u.mutation.ClearWeeklyLimitUsd()
	return _u
}

// SetMonthlyLimitUsd sets the "monthly_limit_usd" field.
func (_u *GroupUpdate) SetMonthlyLimitUsd(v float64) *GroupUpdate {
	_u.mutation.ResetMonthlyLimitUsd()
	_u.mutation.SetMonthlyLimitUsd(v)
	return _u
}

// SetNillableMonthlyLimitUsd sets the "monthly_limit_usd" field if the given value is not nil.
func (_u *GroupUpdate) SetNillableMonthlyLimitUsd(v *float64) *GroupUpdate {
	if v != nil {
		_u.SetMonthlyLimitUsd(*v)
	}
	return _u
}

// AddMonthlyLimitUsd adds value to the "monthly_limit_usd" field.
func (_u *GroupUpdate) AddMonthlyLimitUsd(v float64) *GroupUpdate {
	_u.mutation.AddMonthlyLimitUsd(v)
	return _u
}

// ClearMonthlyLimitUsd clears the value of the "monthly_limit_usd" field.
func (_u *GroupUpdate) ClearMonthlyLimitUsd() *GroupUpdate {
	_u.mutation.ClearMonthlyLimitUsd()
	return _u
}

// SetDefaultValidityDays sets the "default_validity_days" field.
func (_u *GroupUpdate) SetDefaultValidityDays(v int) *GroupUpdate {
	_u.mutation.ResetDefaultValidityDays()
	_u.mutation.SetDefaultValidityDays(v)
	return _u
}

// SetNillableDefaultValidityDays sets the "default_validity_days" field if the given value is not nil.
func (_u *GroupUpdate) SetNillableDefaultValidityDays(v *int) *GroupUpdate {
	if v != nil {
		_u.SetDefaultValidityDays(*v)
	}
	return _u
}

// AddDefaultValidityDays adds value to the "default_validity_days" field.
func (_u *GroupUpdate) AddDefaultValidityDays(v int) *GroupUpdate {
	_u.mutation.AddDefaultValidityDays(v)
	return _u
}

// AddAPIKeyIDs adds the "api_keys" edge to the ApiKey entity by IDs.
func (_u *GroupUpdate) AddAPIKeyIDs(ids ...int64) *GroupUpdate {
	_u.mutation.AddAPIKeyIDs(ids...)
	return _u
}

// AddAPIKeys adds the "api_keys" edges to the ApiKey entity.
func (_u *GroupUpdate) AddAPIKeys(v ...*ApiKey) *GroupUpdate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddAPIKeyIDs(ids...)
}

// AddRedeemCodeIDs adds the "redeem_codes" edge to the RedeemCode entity by IDs.
func (_u *GroupUpdate) AddRedeemCodeIDs(ids ...int64) *GroupUpdate {
	_u.mutation.AddRedeemCodeIDs(ids...)
	return _u
}

// AddRedeemCodes adds the "redeem_codes" edges to the RedeemCode entity.
func (_u *GroupUpdate) AddRedeemCodes(v ...*RedeemCode) *GroupUpdate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddRedeemCodeIDs(ids...)
}

// AddSubscriptionIDs adds the "subscriptions" edge to the UserSubscription entity by IDs.
func (_u *GroupUpdate) AddSubscriptionIDs(ids ...int64) *GroupUpdate {
	_u.mutation.AddSubscriptionIDs(ids...)
	return _u
}

// AddSubscriptions adds the "subscriptions" edges to the UserSubscription entity.
func (_u *GroupUpdate) AddSubscriptions(v ...*UserSubscription) *GroupUpdate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddSubscriptionIDs(ids...)
}

// AddUsageLogIDs adds the "usage_logs" edge to the UsageLog entity by IDs.
func (_u *GroupUpdate) AddUsageLogIDs(ids ...int64) *GroupUpdate {
	_u.mutation.AddUsageLogIDs(ids...)
	return _u
}

// AddUsageLogs adds the "usage_logs" edges to the UsageLog entity.
func (_u *GroupUpdate) AddUsageLogs(v ...*UsageLog) *GroupUpdate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddUsageLogIDs(ids...)
}

// AddAccountIDs adds the "accounts" edge to the Account entity by IDs.
func (_u *GroupUpdate) AddAccountIDs(ids ...int64) *GroupUpdate {
	_u.mutation.AddAccountIDs(ids...)
	return _u
}

// AddAccounts adds the "accounts" edges to the Account entity.
func (_u *GroupUpdate) AddAccounts(v ...*Account) *GroupUpdate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddAccountIDs(ids...)
}

// AddAllowedUserIDs adds the "allowed_users" edge to the User entity by IDs.
func (_u *GroupUpdate) AddAllowedUserIDs(ids ...int64) *GroupUpdate {
	_u.mutation.AddAllowedUserIDs(ids...)
	return _u
}

// AddAllowedUsers adds the "allowed_users" edges to the User entity.
func (_u *GroupUpdate) AddAllowedUsers(v ...*User) *GroupUpdate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddAllowedUserIDs(ids...)
}

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

// ClearAPIKeys clears all "api_keys" edges to the ApiKey entity.
func (_u *GroupUpdate) ClearAPIKeys() *GroupUpdate {
	_u.mutation.ClearAPIKeys()
	return _u
}

// RemoveAPIKeyIDs removes the "api_keys" edge to ApiKey entities by IDs.
func (_u *GroupUpdate) RemoveAPIKeyIDs(ids ...int64) *GroupUpdate {
	_u.mutation.RemoveAPIKeyIDs(ids...)
	return _u
}

// RemoveAPIKeys removes "api_keys" edges to ApiKey entities.
func (_u *GroupUpdate) RemoveAPIKeys(v ...*ApiKey) *GroupUpdate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveAPIKeyIDs(ids...)
}

// ClearRedeemCodes clears all "redeem_codes" edges to the RedeemCode entity.
func (_u *GroupUpdate) ClearRedeemCodes() *GroupUpdate {
	_u.mutation.ClearRedeemCodes()
	return _u
}

// RemoveRedeemCodeIDs removes the "redeem_codes" edge to RedeemCode entities by IDs.
func (_u *GroupUpdate) RemoveRedeemCodeIDs(ids ...int64) *GroupUpdate {
	_u.mutation.RemoveRedeemCodeIDs(ids...)
	return _u
}

// RemoveRedeemCodes removes "redeem_codes" edges to RedeemCode entities.
func (_u *GroupUpdate) RemoveRedeemCodes(v ...*RedeemCode) *GroupUpdate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveRedeemCodeIDs(ids...)
}

// ClearSubscriptions clears all "subscriptions" edges to the UserSubscription entity.
func (_u *GroupUpdate) ClearSubscriptions() *GroupUpdate {
	_u.mutation.ClearSubscriptions()
	return _u
}

// RemoveSubscriptionIDs removes the "subscriptions" edge to UserSubscription entities by IDs.
func (_u *GroupUpdate) RemoveSubscriptionIDs(ids ...int64) *GroupUpdate {
	_u.mutation.RemoveSubscriptionIDs(ids...)
	return _u
}

// RemoveSubscriptions removes "subscriptions" edges to UserSubscription entities.
func (_u *GroupUpdate) RemoveSubscriptions(v ...*UserSubscription) *GroupUpdate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveSubscriptionIDs(ids...)
}

// ClearUsageLogs clears all "usage_logs" edges to the UsageLog entity.
func (_u *GroupUpdate) ClearUsageLogs() *GroupUpdate {
	_u.mutation.ClearUsageLogs()
	return _u
}

// RemoveUsageLogIDs removes the "usage_logs" edge to UsageLog entities by IDs.
func (_u *GroupUpdate) RemoveUsageLogIDs(ids ...int64) *GroupUpdate {
	_u.mutation.RemoveUsageLogIDs(ids...)
	return _u
}

// RemoveUsageLogs removes "usage_logs" edges to UsageLog entities.
func (_u *GroupUpdate) RemoveUsageLogs(v ...*UsageLog) *GroupUpdate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveUsageLogIDs(ids...)
}

// ClearAccounts clears all "accounts" edges to the Account entity.
func (_u *GroupUpdate) ClearAccounts() *GroupUpdate {
	_u.mutation.ClearAccounts()
	return _u
}

// RemoveAccountIDs removes the "accounts" edge to Account entities by IDs.
func (_u *GroupUpdate) RemoveAccountIDs(ids ...int64) *GroupUpdate {
	_u.mutation.RemoveAccountIDs(ids...)
	return _u
}

// RemoveAccounts removes "accounts" edges to Account entities.
func (_u *GroupUpdate) RemoveAccounts(v ...*Account) *GroupUpdate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveAccountIDs(ids...)
}

// ClearAllowedUsers clears all "allowed_users" edges to the User entity.
func (_u *GroupUpdate) ClearAllowedUsers() *GroupUpdate {
	_u.mutation.ClearAllowedUsers()
	return _u
}

// RemoveAllowedUserIDs removes the "allowed_users" edge to User entities by IDs.
func (_u *GroupUpdate) RemoveAllowedUserIDs(ids ...int64) *GroupUpdate {
	_u.mutation.RemoveAllowedUserIDs(ids...)
	return _u
}

// RemoveAllowedUsers removes "allowed_users" edges to User entities.
func (_u *GroupUpdate) RemoveAllowedUsers(v ...*User) *GroupUpdate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveAllowedUserIDs(ids...)
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *GroupUpdate) Save(ctx context.Context) (int, error) {
	if err := _u.defaults(); err != nil {
		return 0, err
	}
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *GroupUpdate) 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 *GroupUpdate) defaults() error {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		if group.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized group.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := group.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (_u *GroupUpdate) check() error {
	if v, ok := _u.mutation.Name(); ok {
		if err := group.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Group.name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Status(); ok {
		if err := group.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Group.status": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Platform(); ok {
		if err := group.PlatformValidator(v); err != nil {
			return &ValidationError{Name: "platform", err: fmt.Errorf(`ent: validator failed for field "Group.platform": %w`, err)}
		}
	}
	if v, ok := _u.mutation.SubscriptionType(); ok {
		if err := group.SubscriptionTypeValidator(v); err != nil {
			return &ValidationError{Name: "subscription_type", err: fmt.Errorf(`ent: validator failed for field "Group.subscription_type": %w`, err)}
		}
	}
	return nil
}

func (_u *GroupUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(group.Table, group.Columns, sqlgraph.NewFieldSpec(group.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.UpdatedAt(); ok {
		_spec.SetField(group.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.DeletedAt(); ok {
		_spec.SetField(group.FieldDeletedAt, field.TypeTime, value)
	}
	if _u.mutation.DeletedAtCleared() {
		_spec.ClearField(group.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(group.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Description(); ok {
		_spec.SetField(group.FieldDescription, field.TypeString, value)
	}
	if _u.mutation.DescriptionCleared() {
		_spec.ClearField(group.FieldDescription, field.TypeString)
	}
	if value, ok := _u.mutation.RateMultiplier(); ok {
		_spec.SetField(group.FieldRateMultiplier, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedRateMultiplier(); ok {
		_spec.AddField(group.FieldRateMultiplier, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.IsExclusive(); ok {
		_spec.SetField(group.FieldIsExclusive, field.TypeBool, value)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(group.FieldStatus, field.TypeString, value)
	}
	if value, ok := _u.mutation.Platform(); ok {
		_spec.SetField(group.FieldPlatform, field.TypeString, value)
	}
	if value, ok := _u.mutation.SubscriptionType(); ok {
		_spec.SetField(group.FieldSubscriptionType, field.TypeString, value)
	}
	if value, ok := _u.mutation.DailyLimitUsd(); ok {
		_spec.SetField(group.FieldDailyLimitUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedDailyLimitUsd(); ok {
		_spec.AddField(group.FieldDailyLimitUsd, field.TypeFloat64, value)
	}
	if _u.mutation.DailyLimitUsdCleared() {
		_spec.ClearField(group.FieldDailyLimitUsd, field.TypeFloat64)
	}
	if value, ok := _u.mutation.WeeklyLimitUsd(); ok {
		_spec.SetField(group.FieldWeeklyLimitUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedWeeklyLimitUsd(); ok {
		_spec.AddField(group.FieldWeeklyLimitUsd, field.TypeFloat64, value)
	}
	if _u.mutation.WeeklyLimitUsdCleared() {
		_spec.ClearField(group.FieldWeeklyLimitUsd, field.TypeFloat64)
	}
	if value, ok := _u.mutation.MonthlyLimitUsd(); ok {
		_spec.SetField(group.FieldMonthlyLimitUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedMonthlyLimitUsd(); ok {
		_spec.AddField(group.FieldMonthlyLimitUsd, field.TypeFloat64, value)
	}
	if _u.mutation.MonthlyLimitUsdCleared() {
		_spec.ClearField(group.FieldMonthlyLimitUsd, field.TypeFloat64)
	}
	if value, ok := _u.mutation.DefaultValidityDays(); ok {
		_spec.SetField(group.FieldDefaultValidityDays, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedDefaultValidityDays(); ok {
		_spec.AddField(group.FieldDefaultValidityDays, field.TypeInt, value)
	}
	if _u.mutation.APIKeysCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.APIKeysTable,
			Columns: []string{group.APIKeysColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(apikey.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedAPIKeysIDs(); len(nodes) > 0 && !_u.mutation.APIKeysCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.APIKeysTable,
			Columns: []string{group.APIKeysColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(apikey.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.APIKeysIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.APIKeysTable,
			Columns: []string{group.APIKeysColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(apikey.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.RedeemCodesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.RedeemCodesTable,
			Columns: []string{group.RedeemCodesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(redeemcode.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedRedeemCodesIDs(); len(nodes) > 0 && !_u.mutation.RedeemCodesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.RedeemCodesTable,
			Columns: []string{group.RedeemCodesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(redeemcode.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RedeemCodesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.RedeemCodesTable,
			Columns: []string{group.RedeemCodesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(redeemcode.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.SubscriptionsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.SubscriptionsTable,
			Columns: []string{group.SubscriptionsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usersubscription.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedSubscriptionsIDs(); len(nodes) > 0 && !_u.mutation.SubscriptionsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.SubscriptionsTable,
			Columns: []string{group.SubscriptionsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usersubscription.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.SubscriptionsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.SubscriptionsTable,
			Columns: []string{group.SubscriptionsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usersubscription.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.UsageLogsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.UsageLogsTable,
			Columns: []string{group.UsageLogsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedUsageLogsIDs(); len(nodes) > 0 && !_u.mutation.UsageLogsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.UsageLogsTable,
			Columns: []string{group.UsageLogsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.UsageLogsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.UsageLogsTable,
			Columns: []string{group.UsageLogsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usagelog.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.AccountsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   group.AccountsTable,
			Columns: group.AccountsPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
			},
		}
		createE := &AccountGroupCreate{config: _u.config, mutation: newAccountGroupMutation(_u.config, OpCreate)}
		createE.defaults()
		_, specE := createE.createSpec()
		edge.Target.Fields = specE.Fields
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedAccountsIDs(); len(nodes) > 0 && !_u.mutation.AccountsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   group.AccountsTable,
			Columns: group.AccountsPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		createE := &AccountGroupCreate{config: _u.config, mutation: newAccountGroupMutation(_u.config, OpCreate)}
		createE.defaults()
		_, specE := createE.createSpec()
		edge.Target.Fields = specE.Fields
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.AccountsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   group.AccountsTable,
			Columns: group.AccountsPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		createE := &AccountGroupCreate{config: _u.config, mutation: newAccountGroupMutation(_u.config, OpCreate)}
		createE.defaults()
		_, specE := createE.createSpec()
		edge.Target.Fields = specE.Fields
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.AllowedUsersCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   group.AllowedUsersTable,
			Columns: group.AllowedUsersPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		createE := &UserAllowedGroupCreate{config: _u.config, mutation: newUserAllowedGroupMutation(_u.config, OpCreate)}
		createE.defaults()
		_, specE := createE.createSpec()
		edge.Target.Fields = specE.Fields
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedAllowedUsersIDs(); len(nodes) > 0 && !_u.mutation.AllowedUsersCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   group.AllowedUsersTable,
			Columns: group.AllowedUsersPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		createE := &UserAllowedGroupCreate{config: _u.config, mutation: newUserAllowedGroupMutation(_u.config, OpCreate)}
		createE.defaults()
		_, specE := createE.createSpec()
		edge.Target.Fields = specE.Fields
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.AllowedUsersIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   group.AllowedUsersTable,
			Columns: group.AllowedUsersPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		createE := &UserAllowedGroupCreate{config: _u.config, mutation: newUserAllowedGroupMutation(_u.config, OpCreate)}
		createE.defaults()
		_, specE := createE.createSpec()
		edge.Target.Fields = specE.Fields
		_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{group.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// GroupUpdateOne is the builder for updating a single Group entity.
type GroupUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *GroupMutation
}

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

// SetDeletedAt sets the "deleted_at" field.
func (_u *GroupUpdateOne) SetDeletedAt(v time.Time) *GroupUpdateOne {
	_u.mutation.SetDeletedAt(v)
	return _u
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (_u *GroupUpdateOne) SetNillableDeletedAt(v *time.Time) *GroupUpdateOne {
	if v != nil {
		_u.SetDeletedAt(*v)
	}
	return _u
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (_u *GroupUpdateOne) ClearDeletedAt() *GroupUpdateOne {
	_u.mutation.ClearDeletedAt()
	return _u
}

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

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

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

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

// ClearDescription clears the value of the "description" field.
func (_u *GroupUpdateOne) ClearDescription() *GroupUpdateOne {
	_u.mutation.ClearDescription()
	return _u
}

// SetRateMultiplier sets the "rate_multiplier" field.
func (_u *GroupUpdateOne) SetRateMultiplier(v float64) *GroupUpdateOne {
	_u.mutation.ResetRateMultiplier()
	_u.mutation.SetRateMultiplier(v)
	return _u
}

// SetNillableRateMultiplier sets the "rate_multiplier" field if the given value is not nil.
func (_u *GroupUpdateOne) SetNillableRateMultiplier(v *float64) *GroupUpdateOne {
	if v != nil {
		_u.SetRateMultiplier(*v)
	}
	return _u
}

// AddRateMultiplier adds value to the "rate_multiplier" field.
func (_u *GroupUpdateOne) AddRateMultiplier(v float64) *GroupUpdateOne {
	_u.mutation.AddRateMultiplier(v)
	return _u
}

// SetIsExclusive sets the "is_exclusive" field.
func (_u *GroupUpdateOne) SetIsExclusive(v bool) *GroupUpdateOne {
	_u.mutation.SetIsExclusive(v)
	return _u
}

// SetNillableIsExclusive sets the "is_exclusive" field if the given value is not nil.
func (_u *GroupUpdateOne) SetNillableIsExclusive(v *bool) *GroupUpdateOne {
	if v != nil {
		_u.SetIsExclusive(*v)
	}
	return _u
}

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

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

// SetPlatform sets the "platform" field.
func (_u *GroupUpdateOne) SetPlatform(v string) *GroupUpdateOne {
	_u.mutation.SetPlatform(v)
	return _u
}

// SetNillablePlatform sets the "platform" field if the given value is not nil.
func (_u *GroupUpdateOne) SetNillablePlatform(v *string) *GroupUpdateOne {
	if v != nil {
		_u.SetPlatform(*v)
	}
	return _u
}

// SetSubscriptionType sets the "subscription_type" field.
func (_u *GroupUpdateOne) SetSubscriptionType(v string) *GroupUpdateOne {
	_u.mutation.SetSubscriptionType(v)
	return _u
}

// SetNillableSubscriptionType sets the "subscription_type" field if the given value is not nil.
func (_u *GroupUpdateOne) SetNillableSubscriptionType(v *string) *GroupUpdateOne {
	if v != nil {
		_u.SetSubscriptionType(*v)
	}
	return _u
}

// SetDailyLimitUsd sets the "daily_limit_usd" field.
func (_u *GroupUpdateOne) SetDailyLimitUsd(v float64) *GroupUpdateOne {
	_u.mutation.ResetDailyLimitUsd()
	_u.mutation.SetDailyLimitUsd(v)
	return _u
}

// SetNillableDailyLimitUsd sets the "daily_limit_usd" field if the given value is not nil.
func (_u *GroupUpdateOne) SetNillableDailyLimitUsd(v *float64) *GroupUpdateOne {
	if v != nil {
		_u.SetDailyLimitUsd(*v)
	}
	return _u
}

// AddDailyLimitUsd adds value to the "daily_limit_usd" field.
func (_u *GroupUpdateOne) AddDailyLimitUsd(v float64) *GroupUpdateOne {
	_u.mutation.AddDailyLimitUsd(v)
	return _u
}

// ClearDailyLimitUsd clears the value of the "daily_limit_usd" field.
func (_u *GroupUpdateOne) ClearDailyLimitUsd() *GroupUpdateOne {
	_u.mutation.ClearDailyLimitUsd()
	return _u
}

// SetWeeklyLimitUsd sets the "weekly_limit_usd" field.
func (_u *GroupUpdateOne) SetWeeklyLimitUsd(v float64) *GroupUpdateOne {
	_u.mutation.ResetWeeklyLimitUsd()
	_u.mutation.SetWeeklyLimitUsd(v)
	return _u
}

// SetNillableWeeklyLimitUsd sets the "weekly_limit_usd" field if the given value is not nil.
func (_u *GroupUpdateOne) SetNillableWeeklyLimitUsd(v *float64) *GroupUpdateOne {
	if v != nil {
		_u.SetWeeklyLimitUsd(*v)
	}
	return _u
}

// AddWeeklyLimitUsd adds value to the "weekly_limit_usd" field.
func (_u *GroupUpdateOne) AddWeeklyLimitUsd(v float64) *GroupUpdateOne {
	_u.mutation.AddWeeklyLimitUsd(v)
	return _u
}

// ClearWeeklyLimitUsd clears the value of the "weekly_limit_usd" field.
func (_u *GroupUpdateOne) ClearWeeklyLimitUsd() *GroupUpdateOne {
	_u.mutation.ClearWeeklyLimitUsd()
	return _u
}

// SetMonthlyLimitUsd sets the "monthly_limit_usd" field.
func (_u *GroupUpdateOne) SetMonthlyLimitUsd(v float64) *GroupUpdateOne {
	_u.mutation.ResetMonthlyLimitUsd()
	_u.mutation.SetMonthlyLimitUsd(v)
	return _u
}

// SetNillableMonthlyLimitUsd sets the "monthly_limit_usd" field if the given value is not nil.
func (_u *GroupUpdateOne) SetNillableMonthlyLimitUsd(v *float64) *GroupUpdateOne {
	if v != nil {
		_u.SetMonthlyLimitUsd(*v)
	}
	return _u
}

// AddMonthlyLimitUsd adds value to the "monthly_limit_usd" field.
func (_u *GroupUpdateOne) AddMonthlyLimitUsd(v float64) *GroupUpdateOne {
	_u.mutation.AddMonthlyLimitUsd(v)
	return _u
}

// ClearMonthlyLimitUsd clears the value of the "monthly_limit_usd" field.
func (_u *GroupUpdateOne) ClearMonthlyLimitUsd() *GroupUpdateOne {
	_u.mutation.ClearMonthlyLimitUsd()
	return _u
}

// SetDefaultValidityDays sets the "default_validity_days" field.
func (_u *GroupUpdateOne) SetDefaultValidityDays(v int) *GroupUpdateOne {
	_u.mutation.ResetDefaultValidityDays()
	_u.mutation.SetDefaultValidityDays(v)
	return _u
}

// SetNillableDefaultValidityDays sets the "default_validity_days" field if the given value is not nil.
func (_u *GroupUpdateOne) SetNillableDefaultValidityDays(v *int) *GroupUpdateOne {
	if v != nil {
		_u.SetDefaultValidityDays(*v)
	}
	return _u
}

// AddDefaultValidityDays adds value to the "default_validity_days" field.
func (_u *GroupUpdateOne) AddDefaultValidityDays(v int) *GroupUpdateOne {
	_u.mutation.AddDefaultValidityDays(v)
	return _u
}

// AddAPIKeyIDs adds the "api_keys" edge to the ApiKey entity by IDs.
func (_u *GroupUpdateOne) AddAPIKeyIDs(ids ...int64) *GroupUpdateOne {
	_u.mutation.AddAPIKeyIDs(ids...)
	return _u
}

// AddAPIKeys adds the "api_keys" edges to the ApiKey entity.
func (_u *GroupUpdateOne) AddAPIKeys(v ...*ApiKey) *GroupUpdateOne {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddAPIKeyIDs(ids...)
}

// AddRedeemCodeIDs adds the "redeem_codes" edge to the RedeemCode entity by IDs.
func (_u *GroupUpdateOne) AddRedeemCodeIDs(ids ...int64) *GroupUpdateOne {
	_u.mutation.AddRedeemCodeIDs(ids...)
	return _u
}

// AddRedeemCodes adds the "redeem_codes" edges to the RedeemCode entity.
func (_u *GroupUpdateOne) AddRedeemCodes(v ...*RedeemCode) *GroupUpdateOne {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddRedeemCodeIDs(ids...)
}

// AddSubscriptionIDs adds the "subscriptions" edge to the UserSubscription entity by IDs.
func (_u *GroupUpdateOne) AddSubscriptionIDs(ids ...int64) *GroupUpdateOne {
	_u.mutation.AddSubscriptionIDs(ids...)
	return _u
}

// AddSubscriptions adds the "subscriptions" edges to the UserSubscription entity.
func (_u *GroupUpdateOne) AddSubscriptions(v ...*UserSubscription) *GroupUpdateOne {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddSubscriptionIDs(ids...)
}

// AddUsageLogIDs adds the "usage_logs" edge to the UsageLog entity by IDs.
func (_u *GroupUpdateOne) AddUsageLogIDs(ids ...int64) *GroupUpdateOne {
	_u.mutation.AddUsageLogIDs(ids...)
	return _u
}

// AddUsageLogs adds the "usage_logs" edges to the UsageLog entity.
func (_u *GroupUpdateOne) AddUsageLogs(v ...*UsageLog) *GroupUpdateOne {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddUsageLogIDs(ids...)
}

// AddAccountIDs adds the "accounts" edge to the Account entity by IDs.
func (_u *GroupUpdateOne) AddAccountIDs(ids ...int64) *GroupUpdateOne {
	_u.mutation.AddAccountIDs(ids...)
	return _u
}

// AddAccounts adds the "accounts" edges to the Account entity.
func (_u *GroupUpdateOne) AddAccounts(v ...*Account) *GroupUpdateOne {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddAccountIDs(ids...)
}

// AddAllowedUserIDs adds the "allowed_users" edge to the User entity by IDs.
func (_u *GroupUpdateOne) AddAllowedUserIDs(ids ...int64) *GroupUpdateOne {
	_u.mutation.AddAllowedUserIDs(ids...)
	return _u
}

// AddAllowedUsers adds the "allowed_users" edges to the User entity.
func (_u *GroupUpdateOne) AddAllowedUsers(v ...*User) *GroupUpdateOne {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddAllowedUserIDs(ids...)
}

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

// ClearAPIKeys clears all "api_keys" edges to the ApiKey entity.
func (_u *GroupUpdateOne) ClearAPIKeys() *GroupUpdateOne {
	_u.mutation.ClearAPIKeys()
	return _u
}

// RemoveAPIKeyIDs removes the "api_keys" edge to ApiKey entities by IDs.
func (_u *GroupUpdateOne) RemoveAPIKeyIDs(ids ...int64) *GroupUpdateOne {
	_u.mutation.RemoveAPIKeyIDs(ids...)
	return _u
}

// RemoveAPIKeys removes "api_keys" edges to ApiKey entities.
func (_u *GroupUpdateOne) RemoveAPIKeys(v ...*ApiKey) *GroupUpdateOne {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveAPIKeyIDs(ids...)
}

// ClearRedeemCodes clears all "redeem_codes" edges to the RedeemCode entity.
func (_u *GroupUpdateOne) ClearRedeemCodes() *GroupUpdateOne {
	_u.mutation.ClearRedeemCodes()
	return _u
}

// RemoveRedeemCodeIDs removes the "redeem_codes" edge to RedeemCode entities by IDs.
func (_u *GroupUpdateOne) RemoveRedeemCodeIDs(ids ...int64) *GroupUpdateOne {
	_u.mutation.RemoveRedeemCodeIDs(ids...)
	return _u
}

// RemoveRedeemCodes removes "redeem_codes" edges to RedeemCode entities.
func (_u *GroupUpdateOne) RemoveRedeemCodes(v ...*RedeemCode) *GroupUpdateOne {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveRedeemCodeIDs(ids...)
}

// ClearSubscriptions clears all "subscriptions" edges to the UserSubscription entity.
func (_u *GroupUpdateOne) ClearSubscriptions() *GroupUpdateOne {
	_u.mutation.ClearSubscriptions()
	return _u
}

// RemoveSubscriptionIDs removes the "subscriptions" edge to UserSubscription entities by IDs.
func (_u *GroupUpdateOne) RemoveSubscriptionIDs(ids ...int64) *GroupUpdateOne {
	_u.mutation.RemoveSubscriptionIDs(ids...)
	return _u
}

// RemoveSubscriptions removes "subscriptions" edges to UserSubscription entities.
func (_u *GroupUpdateOne) RemoveSubscriptions(v ...*UserSubscription) *GroupUpdateOne {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveSubscriptionIDs(ids...)
}

// ClearUsageLogs clears all "usage_logs" edges to the UsageLog entity.
func (_u *GroupUpdateOne) ClearUsageLogs() *GroupUpdateOne {
	_u.mutation.ClearUsageLogs()
	return _u
}

// RemoveUsageLogIDs removes the "usage_logs" edge to UsageLog entities by IDs.
func (_u *GroupUpdateOne) RemoveUsageLogIDs(ids ...int64) *GroupUpdateOne {
	_u.mutation.RemoveUsageLogIDs(ids...)
	return _u
}

// RemoveUsageLogs removes "usage_logs" edges to UsageLog entities.
func (_u *GroupUpdateOne) RemoveUsageLogs(v ...*UsageLog) *GroupUpdateOne {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveUsageLogIDs(ids...)
}

// ClearAccounts clears all "accounts" edges to the Account entity.
func (_u *GroupUpdateOne) ClearAccounts() *GroupUpdateOne {
	_u.mutation.ClearAccounts()
	return _u
}

// RemoveAccountIDs removes the "accounts" edge to Account entities by IDs.
func (_u *GroupUpdateOne) RemoveAccountIDs(ids ...int64) *GroupUpdateOne {
	_u.mutation.RemoveAccountIDs(ids...)
	return _u
}

// RemoveAccounts removes "accounts" edges to Account entities.
func (_u *GroupUpdateOne) RemoveAccounts(v ...*Account) *GroupUpdateOne {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveAccountIDs(ids...)
}

// ClearAllowedUsers clears all "allowed_users" edges to the User entity.
func (_u *GroupUpdateOne) ClearAllowedUsers() *GroupUpdateOne {
	_u.mutation.ClearAllowedUsers()
	return _u
}

// RemoveAllowedUserIDs removes the "allowed_users" edge to User entities by IDs.
func (_u *GroupUpdateOne) RemoveAllowedUserIDs(ids ...int64) *GroupUpdateOne {
	_u.mutation.RemoveAllowedUserIDs(ids...)
	return _u
}

// RemoveAllowedUsers removes "allowed_users" edges to User entities.
func (_u *GroupUpdateOne) RemoveAllowedUsers(v ...*User) *GroupUpdateOne {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveAllowedUserIDs(ids...)
}

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

// Save executes the query and returns the updated Group entity.
func (_u *GroupUpdateOne) Save(ctx context.Context) (*Group, error) {
	if err := _u.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *GroupUpdateOne) 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 *GroupUpdateOne) defaults() error {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		if group.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized group.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := group.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (_u *GroupUpdateOne) check() error {
	if v, ok := _u.mutation.Name(); ok {
		if err := group.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Group.name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Status(); ok {
		if err := group.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Group.status": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Platform(); ok {
		if err := group.PlatformValidator(v); err != nil {
			return &ValidationError{Name: "platform", err: fmt.Errorf(`ent: validator failed for field "Group.platform": %w`, err)}
		}
	}
	if v, ok := _u.mutation.SubscriptionType(); ok {
		if err := group.SubscriptionTypeValidator(v); err != nil {
			return &ValidationError{Name: "subscription_type", err: fmt.Errorf(`ent: validator failed for field "Group.subscription_type": %w`, err)}
		}
	}
	return nil
}

func (_u *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(group.Table, group.Columns, sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Group.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, group.FieldID)
		for _, f := range fields {
			if !group.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != group.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.UpdatedAt(); ok {
		_spec.SetField(group.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.DeletedAt(); ok {
		_spec.SetField(group.FieldDeletedAt, field.TypeTime, value)
	}
	if _u.mutation.DeletedAtCleared() {
		_spec.ClearField(group.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(group.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Description(); ok {
		_spec.SetField(group.FieldDescription, field.TypeString, value)
	}
	if _u.mutation.DescriptionCleared() {
		_spec.ClearField(group.FieldDescription, field.TypeString)
	}
	if value, ok := _u.mutation.RateMultiplier(); ok {
		_spec.SetField(group.FieldRateMultiplier, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedRateMultiplier(); ok {
		_spec.AddField(group.FieldRateMultiplier, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.IsExclusive(); ok {
		_spec.SetField(group.FieldIsExclusive, field.TypeBool, value)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(group.FieldStatus, field.TypeString, value)
	}
	if value, ok := _u.mutation.Platform(); ok {
		_spec.SetField(group.FieldPlatform, field.TypeString, value)
	}
	if value, ok := _u.mutation.SubscriptionType(); ok {
		_spec.SetField(group.FieldSubscriptionType, field.TypeString, value)
	}
	if value, ok := _u.mutation.DailyLimitUsd(); ok {
		_spec.SetField(group.FieldDailyLimitUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedDailyLimitUsd(); ok {
		_spec.AddField(group.FieldDailyLimitUsd, field.TypeFloat64, value)
	}
	if _u.mutation.DailyLimitUsdCleared() {
		_spec.ClearField(group.FieldDailyLimitUsd, field.TypeFloat64)
	}
	if value, ok := _u.mutation.WeeklyLimitUsd(); ok {
		_spec.SetField(group.FieldWeeklyLimitUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedWeeklyLimitUsd(); ok {
		_spec.AddField(group.FieldWeeklyLimitUsd, field.TypeFloat64, value)
	}
	if _u.mutation.WeeklyLimitUsdCleared() {
		_spec.ClearField(group.FieldWeeklyLimitUsd, field.TypeFloat64)
	}
	if value, ok := _u.mutation.MonthlyLimitUsd(); ok {
		_spec.SetField(group.FieldMonthlyLimitUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedMonthlyLimitUsd(); ok {
		_spec.AddField(group.FieldMonthlyLimitUsd, field.TypeFloat64, value)
	}
	if _u.mutation.MonthlyLimitUsdCleared() {
		_spec.ClearField(group.FieldMonthlyLimitUsd, field.TypeFloat64)
	}
	if value, ok := _u.mutation.DefaultValidityDays(); ok {
		_spec.SetField(group.FieldDefaultValidityDays, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedDefaultValidityDays(); ok {
		_spec.AddField(group.FieldDefaultValidityDays, field.TypeInt, value)
	}
	if _u.mutation.APIKeysCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.APIKeysTable,
			Columns: []string{group.APIKeysColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(apikey.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedAPIKeysIDs(); len(nodes) > 0 && !_u.mutation.APIKeysCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.APIKeysTable,
			Columns: []string{group.APIKeysColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(apikey.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.APIKeysIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.APIKeysTable,
			Columns: []string{group.APIKeysColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(apikey.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.RedeemCodesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.RedeemCodesTable,
			Columns: []string{group.RedeemCodesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(redeemcode.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedRedeemCodesIDs(); len(nodes) > 0 && !_u.mutation.RedeemCodesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.RedeemCodesTable,
			Columns: []string{group.RedeemCodesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(redeemcode.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RedeemCodesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.RedeemCodesTable,
			Columns: []string{group.RedeemCodesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(redeemcode.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.SubscriptionsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.SubscriptionsTable,
			Columns: []string{group.SubscriptionsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usersubscription.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedSubscriptionsIDs(); len(nodes) > 0 && !_u.mutation.SubscriptionsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.SubscriptionsTable,
			Columns: []string{group.SubscriptionsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usersubscription.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.SubscriptionsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.SubscriptionsTable,
			Columns: []string{group.SubscriptionsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usersubscription.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.UsageLogsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.UsageLogsTable,
			Columns: []string{group.UsageLogsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedUsageLogsIDs(); len(nodes) > 0 && !_u.mutation.UsageLogsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.UsageLogsTable,
			Columns: []string{group.UsageLogsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.UsageLogsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.UsageLogsTable,
			Columns: []string{group.UsageLogsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usagelog.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.AccountsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   group.AccountsTable,
			Columns: group.AccountsPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
			},
		}
		createE := &AccountGroupCreate{config: _u.config, mutation: newAccountGroupMutation(_u.config, OpCreate)}
		createE.defaults()
		_, specE := createE.createSpec()
		edge.Target.Fields = specE.Fields
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedAccountsIDs(); len(nodes) > 0 && !_u.mutation.AccountsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   group.AccountsTable,
			Columns: group.AccountsPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		createE := &AccountGroupCreate{config: _u.config, mutation: newAccountGroupMutation(_u.config, OpCreate)}
		createE.defaults()
		_, specE := createE.createSpec()
		edge.Target.Fields = specE.Fields
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.AccountsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   group.AccountsTable,
			Columns: group.AccountsPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		createE := &AccountGroupCreate{config: _u.config, mutation: newAccountGroupMutation(_u.config, OpCreate)}
		createE.defaults()
		_, specE := createE.createSpec()
		edge.Target.Fields = specE.Fields
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.AllowedUsersCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   group.AllowedUsersTable,
			Columns: group.AllowedUsersPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		createE := &UserAllowedGroupCreate{config: _u.config, mutation: newUserAllowedGroupMutation(_u.config, OpCreate)}
		createE.defaults()
		_, specE := createE.createSpec()
		edge.Target.Fields = specE.Fields
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedAllowedUsersIDs(); len(nodes) > 0 && !_u.mutation.AllowedUsersCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   group.AllowedUsersTable,
			Columns: group.AllowedUsersPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		createE := &UserAllowedGroupCreate{config: _u.config, mutation: newUserAllowedGroupMutation(_u.config, OpCreate)}
		createE.defaults()
		_, specE := createE.createSpec()
		edge.Target.Fields = specE.Fields
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.AllowedUsersIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   group.AllowedUsersTable,
			Columns: group.AllowedUsersPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		createE := &UserAllowedGroupCreate{config: _u.config, mutation: newUserAllowedGroupMutation(_u.config, OpCreate)}
		createE.defaults()
		_, specE := createE.createSpec()
		edge.Target.Fields = specE.Fields
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Group{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{group.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
