// 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/cloudreve/Cloudreve/v4/ent/group"
	"github.com/cloudreve/Cloudreve/v4/ent/predicate"
	"github.com/cloudreve/Cloudreve/v4/ent/storagepolicy"
	"github.com/cloudreve/Cloudreve/v4/ent/user"
	"github.com/cloudreve/Cloudreve/v4/inventory/types"
	"github.com/cloudreve/Cloudreve/v4/pkg/boolset"
)

// 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 (gu *GroupUpdate) Where(ps ...predicate.Group) *GroupUpdate {
	gu.mutation.Where(ps...)
	return gu
}

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

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

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

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

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

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

// SetMaxStorage sets the "max_storage" field.
func (gu *GroupUpdate) SetMaxStorage(i int64) *GroupUpdate {
	gu.mutation.ResetMaxStorage()
	gu.mutation.SetMaxStorage(i)
	return gu
}

// SetNillableMaxStorage sets the "max_storage" field if the given value is not nil.
func (gu *GroupUpdate) SetNillableMaxStorage(i *int64) *GroupUpdate {
	if i != nil {
		gu.SetMaxStorage(*i)
	}
	return gu
}

// AddMaxStorage adds i to the "max_storage" field.
func (gu *GroupUpdate) AddMaxStorage(i int64) *GroupUpdate {
	gu.mutation.AddMaxStorage(i)
	return gu
}

// ClearMaxStorage clears the value of the "max_storage" field.
func (gu *GroupUpdate) ClearMaxStorage() *GroupUpdate {
	gu.mutation.ClearMaxStorage()
	return gu
}

// SetSpeedLimit sets the "speed_limit" field.
func (gu *GroupUpdate) SetSpeedLimit(i int) *GroupUpdate {
	gu.mutation.ResetSpeedLimit()
	gu.mutation.SetSpeedLimit(i)
	return gu
}

// SetNillableSpeedLimit sets the "speed_limit" field if the given value is not nil.
func (gu *GroupUpdate) SetNillableSpeedLimit(i *int) *GroupUpdate {
	if i != nil {
		gu.SetSpeedLimit(*i)
	}
	return gu
}

// AddSpeedLimit adds i to the "speed_limit" field.
func (gu *GroupUpdate) AddSpeedLimit(i int) *GroupUpdate {
	gu.mutation.AddSpeedLimit(i)
	return gu
}

// ClearSpeedLimit clears the value of the "speed_limit" field.
func (gu *GroupUpdate) ClearSpeedLimit() *GroupUpdate {
	gu.mutation.ClearSpeedLimit()
	return gu
}

// SetPermissions sets the "permissions" field.
func (gu *GroupUpdate) SetPermissions(bs *boolset.BooleanSet) *GroupUpdate {
	gu.mutation.SetPermissions(bs)
	return gu
}

// SetSettings sets the "settings" field.
func (gu *GroupUpdate) SetSettings(ts *types.GroupSetting) *GroupUpdate {
	gu.mutation.SetSettings(ts)
	return gu
}

// ClearSettings clears the value of the "settings" field.
func (gu *GroupUpdate) ClearSettings() *GroupUpdate {
	gu.mutation.ClearSettings()
	return gu
}

// SetStoragePolicyID sets the "storage_policy_id" field.
func (gu *GroupUpdate) SetStoragePolicyID(i int) *GroupUpdate {
	gu.mutation.SetStoragePolicyID(i)
	return gu
}

// SetNillableStoragePolicyID sets the "storage_policy_id" field if the given value is not nil.
func (gu *GroupUpdate) SetNillableStoragePolicyID(i *int) *GroupUpdate {
	if i != nil {
		gu.SetStoragePolicyID(*i)
	}
	return gu
}

// ClearStoragePolicyID clears the value of the "storage_policy_id" field.
func (gu *GroupUpdate) ClearStoragePolicyID() *GroupUpdate {
	gu.mutation.ClearStoragePolicyID()
	return gu
}

// AddUserIDs adds the "users" edge to the User entity by IDs.
func (gu *GroupUpdate) AddUserIDs(ids ...int) *GroupUpdate {
	gu.mutation.AddUserIDs(ids...)
	return gu
}

// AddUsers adds the "users" edges to the User entity.
func (gu *GroupUpdate) AddUsers(u ...*User) *GroupUpdate {
	ids := make([]int, len(u))
	for i := range u {
		ids[i] = u[i].ID
	}
	return gu.AddUserIDs(ids...)
}

// SetStoragePoliciesID sets the "storage_policies" edge to the StoragePolicy entity by ID.
func (gu *GroupUpdate) SetStoragePoliciesID(id int) *GroupUpdate {
	gu.mutation.SetStoragePoliciesID(id)
	return gu
}

// SetNillableStoragePoliciesID sets the "storage_policies" edge to the StoragePolicy entity by ID if the given value is not nil.
func (gu *GroupUpdate) SetNillableStoragePoliciesID(id *int) *GroupUpdate {
	if id != nil {
		gu = gu.SetStoragePoliciesID(*id)
	}
	return gu
}

// SetStoragePolicies sets the "storage_policies" edge to the StoragePolicy entity.
func (gu *GroupUpdate) SetStoragePolicies(s *StoragePolicy) *GroupUpdate {
	return gu.SetStoragePoliciesID(s.ID)
}

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

// ClearUsers clears all "users" edges to the User entity.
func (gu *GroupUpdate) ClearUsers() *GroupUpdate {
	gu.mutation.ClearUsers()
	return gu
}

// RemoveUserIDs removes the "users" edge to User entities by IDs.
func (gu *GroupUpdate) RemoveUserIDs(ids ...int) *GroupUpdate {
	gu.mutation.RemoveUserIDs(ids...)
	return gu
}

// RemoveUsers removes "users" edges to User entities.
func (gu *GroupUpdate) RemoveUsers(u ...*User) *GroupUpdate {
	ids := make([]int, len(u))
	for i := range u {
		ids[i] = u[i].ID
	}
	return gu.RemoveUserIDs(ids...)
}

// ClearStoragePolicies clears the "storage_policies" edge to the StoragePolicy entity.
func (gu *GroupUpdate) ClearStoragePolicies() *GroupUpdate {
	gu.mutation.ClearStoragePolicies()
	return gu
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (gu *GroupUpdate) defaults() error {
	if _, ok := gu.mutation.UpdatedAt(); !ok {
		if group.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized group.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := group.UpdateDefaultUpdatedAt()
		gu.mutation.SetUpdatedAt(v)
	}
	return nil
}

func (gu *GroupUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(group.Table, group.Columns, sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt))
	if ps := gu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := gu.mutation.UpdatedAt(); ok {
		_spec.SetField(group.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := gu.mutation.DeletedAt(); ok {
		_spec.SetField(group.FieldDeletedAt, field.TypeTime, value)
	}
	if gu.mutation.DeletedAtCleared() {
		_spec.ClearField(group.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := gu.mutation.Name(); ok {
		_spec.SetField(group.FieldName, field.TypeString, value)
	}
	if value, ok := gu.mutation.MaxStorage(); ok {
		_spec.SetField(group.FieldMaxStorage, field.TypeInt64, value)
	}
	if value, ok := gu.mutation.AddedMaxStorage(); ok {
		_spec.AddField(group.FieldMaxStorage, field.TypeInt64, value)
	}
	if gu.mutation.MaxStorageCleared() {
		_spec.ClearField(group.FieldMaxStorage, field.TypeInt64)
	}
	if value, ok := gu.mutation.SpeedLimit(); ok {
		_spec.SetField(group.FieldSpeedLimit, field.TypeInt, value)
	}
	if value, ok := gu.mutation.AddedSpeedLimit(); ok {
		_spec.AddField(group.FieldSpeedLimit, field.TypeInt, value)
	}
	if gu.mutation.SpeedLimitCleared() {
		_spec.ClearField(group.FieldSpeedLimit, field.TypeInt)
	}
	if value, ok := gu.mutation.Permissions(); ok {
		_spec.SetField(group.FieldPermissions, field.TypeBytes, value)
	}
	if value, ok := gu.mutation.Settings(); ok {
		_spec.SetField(group.FieldSettings, field.TypeJSON, value)
	}
	if gu.mutation.SettingsCleared() {
		_spec.ClearField(group.FieldSettings, field.TypeJSON)
	}
	if gu.mutation.UsersCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.UsersTable,
			Columns: []string{group.UsersColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := gu.mutation.RemovedUsersIDs(); len(nodes) > 0 && !gu.mutation.UsersCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.UsersTable,
			Columns: []string{group.UsersColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := gu.mutation.UsersIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.UsersTable,
			Columns: []string{group.UsersColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if gu.mutation.StoragePoliciesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   group.StoragePoliciesTable,
			Columns: []string{group.StoragePoliciesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(storagepolicy.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := gu.mutation.StoragePoliciesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   group.StoragePoliciesTable,
			Columns: []string{group.StoragePoliciesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(storagepolicy.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, gu.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
	}
	gu.mutation.done = true
	return n, 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 (guo *GroupUpdateOne) SetUpdatedAt(t time.Time) *GroupUpdateOne {
	guo.mutation.SetUpdatedAt(t)
	return guo
}

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

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

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

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

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

// SetMaxStorage sets the "max_storage" field.
func (guo *GroupUpdateOne) SetMaxStorage(i int64) *GroupUpdateOne {
	guo.mutation.ResetMaxStorage()
	guo.mutation.SetMaxStorage(i)
	return guo
}

// SetNillableMaxStorage sets the "max_storage" field if the given value is not nil.
func (guo *GroupUpdateOne) SetNillableMaxStorage(i *int64) *GroupUpdateOne {
	if i != nil {
		guo.SetMaxStorage(*i)
	}
	return guo
}

// AddMaxStorage adds i to the "max_storage" field.
func (guo *GroupUpdateOne) AddMaxStorage(i int64) *GroupUpdateOne {
	guo.mutation.AddMaxStorage(i)
	return guo
}

// ClearMaxStorage clears the value of the "max_storage" field.
func (guo *GroupUpdateOne) ClearMaxStorage() *GroupUpdateOne {
	guo.mutation.ClearMaxStorage()
	return guo
}

// SetSpeedLimit sets the "speed_limit" field.
func (guo *GroupUpdateOne) SetSpeedLimit(i int) *GroupUpdateOne {
	guo.mutation.ResetSpeedLimit()
	guo.mutation.SetSpeedLimit(i)
	return guo
}

// SetNillableSpeedLimit sets the "speed_limit" field if the given value is not nil.
func (guo *GroupUpdateOne) SetNillableSpeedLimit(i *int) *GroupUpdateOne {
	if i != nil {
		guo.SetSpeedLimit(*i)
	}
	return guo
}

// AddSpeedLimit adds i to the "speed_limit" field.
func (guo *GroupUpdateOne) AddSpeedLimit(i int) *GroupUpdateOne {
	guo.mutation.AddSpeedLimit(i)
	return guo
}

// ClearSpeedLimit clears the value of the "speed_limit" field.
func (guo *GroupUpdateOne) ClearSpeedLimit() *GroupUpdateOne {
	guo.mutation.ClearSpeedLimit()
	return guo
}

// SetPermissions sets the "permissions" field.
func (guo *GroupUpdateOne) SetPermissions(bs *boolset.BooleanSet) *GroupUpdateOne {
	guo.mutation.SetPermissions(bs)
	return guo
}

// SetSettings sets the "settings" field.
func (guo *GroupUpdateOne) SetSettings(ts *types.GroupSetting) *GroupUpdateOne {
	guo.mutation.SetSettings(ts)
	return guo
}

// ClearSettings clears the value of the "settings" field.
func (guo *GroupUpdateOne) ClearSettings() *GroupUpdateOne {
	guo.mutation.ClearSettings()
	return guo
}

// SetStoragePolicyID sets the "storage_policy_id" field.
func (guo *GroupUpdateOne) SetStoragePolicyID(i int) *GroupUpdateOne {
	guo.mutation.SetStoragePolicyID(i)
	return guo
}

// SetNillableStoragePolicyID sets the "storage_policy_id" field if the given value is not nil.
func (guo *GroupUpdateOne) SetNillableStoragePolicyID(i *int) *GroupUpdateOne {
	if i != nil {
		guo.SetStoragePolicyID(*i)
	}
	return guo
}

// ClearStoragePolicyID clears the value of the "storage_policy_id" field.
func (guo *GroupUpdateOne) ClearStoragePolicyID() *GroupUpdateOne {
	guo.mutation.ClearStoragePolicyID()
	return guo
}

// AddUserIDs adds the "users" edge to the User entity by IDs.
func (guo *GroupUpdateOne) AddUserIDs(ids ...int) *GroupUpdateOne {
	guo.mutation.AddUserIDs(ids...)
	return guo
}

// AddUsers adds the "users" edges to the User entity.
func (guo *GroupUpdateOne) AddUsers(u ...*User) *GroupUpdateOne {
	ids := make([]int, len(u))
	for i := range u {
		ids[i] = u[i].ID
	}
	return guo.AddUserIDs(ids...)
}

// SetStoragePoliciesID sets the "storage_policies" edge to the StoragePolicy entity by ID.
func (guo *GroupUpdateOne) SetStoragePoliciesID(id int) *GroupUpdateOne {
	guo.mutation.SetStoragePoliciesID(id)
	return guo
}

// SetNillableStoragePoliciesID sets the "storage_policies" edge to the StoragePolicy entity by ID if the given value is not nil.
func (guo *GroupUpdateOne) SetNillableStoragePoliciesID(id *int) *GroupUpdateOne {
	if id != nil {
		guo = guo.SetStoragePoliciesID(*id)
	}
	return guo
}

// SetStoragePolicies sets the "storage_policies" edge to the StoragePolicy entity.
func (guo *GroupUpdateOne) SetStoragePolicies(s *StoragePolicy) *GroupUpdateOne {
	return guo.SetStoragePoliciesID(s.ID)
}

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

// ClearUsers clears all "users" edges to the User entity.
func (guo *GroupUpdateOne) ClearUsers() *GroupUpdateOne {
	guo.mutation.ClearUsers()
	return guo
}

// RemoveUserIDs removes the "users" edge to User entities by IDs.
func (guo *GroupUpdateOne) RemoveUserIDs(ids ...int) *GroupUpdateOne {
	guo.mutation.RemoveUserIDs(ids...)
	return guo
}

// RemoveUsers removes "users" edges to User entities.
func (guo *GroupUpdateOne) RemoveUsers(u ...*User) *GroupUpdateOne {
	ids := make([]int, len(u))
	for i := range u {
		ids[i] = u[i].ID
	}
	return guo.RemoveUserIDs(ids...)
}

// ClearStoragePolicies clears the "storage_policies" edge to the StoragePolicy entity.
func (guo *GroupUpdateOne) ClearStoragePolicies() *GroupUpdateOne {
	guo.mutation.ClearStoragePolicies()
	return guo
}

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

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (guo *GroupUpdateOne) Select(field string, fields ...string) *GroupUpdateOne {
	guo.fields = append([]string{field}, fields...)
	return guo
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (guo *GroupUpdateOne) defaults() error {
	if _, ok := guo.mutation.UpdatedAt(); !ok {
		if group.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized group.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := group.UpdateDefaultUpdatedAt()
		guo.mutation.SetUpdatedAt(v)
	}
	return nil
}

func (guo *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error) {
	_spec := sqlgraph.NewUpdateSpec(group.Table, group.Columns, sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt))
	id, ok := guo.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 := guo.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 := guo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := guo.mutation.UpdatedAt(); ok {
		_spec.SetField(group.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := guo.mutation.DeletedAt(); ok {
		_spec.SetField(group.FieldDeletedAt, field.TypeTime, value)
	}
	if guo.mutation.DeletedAtCleared() {
		_spec.ClearField(group.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := guo.mutation.Name(); ok {
		_spec.SetField(group.FieldName, field.TypeString, value)
	}
	if value, ok := guo.mutation.MaxStorage(); ok {
		_spec.SetField(group.FieldMaxStorage, field.TypeInt64, value)
	}
	if value, ok := guo.mutation.AddedMaxStorage(); ok {
		_spec.AddField(group.FieldMaxStorage, field.TypeInt64, value)
	}
	if guo.mutation.MaxStorageCleared() {
		_spec.ClearField(group.FieldMaxStorage, field.TypeInt64)
	}
	if value, ok := guo.mutation.SpeedLimit(); ok {
		_spec.SetField(group.FieldSpeedLimit, field.TypeInt, value)
	}
	if value, ok := guo.mutation.AddedSpeedLimit(); ok {
		_spec.AddField(group.FieldSpeedLimit, field.TypeInt, value)
	}
	if guo.mutation.SpeedLimitCleared() {
		_spec.ClearField(group.FieldSpeedLimit, field.TypeInt)
	}
	if value, ok := guo.mutation.Permissions(); ok {
		_spec.SetField(group.FieldPermissions, field.TypeBytes, value)
	}
	if value, ok := guo.mutation.Settings(); ok {
		_spec.SetField(group.FieldSettings, field.TypeJSON, value)
	}
	if guo.mutation.SettingsCleared() {
		_spec.ClearField(group.FieldSettings, field.TypeJSON)
	}
	if guo.mutation.UsersCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.UsersTable,
			Columns: []string{group.UsersColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := guo.mutation.RemovedUsersIDs(); len(nodes) > 0 && !guo.mutation.UsersCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.UsersTable,
			Columns: []string{group.UsersColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := guo.mutation.UsersIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   group.UsersTable,
			Columns: []string{group.UsersColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if guo.mutation.StoragePoliciesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   group.StoragePoliciesTable,
			Columns: []string{group.StoragePoliciesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(storagepolicy.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := guo.mutation.StoragePoliciesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   group.StoragePoliciesTable,
			Columns: []string{group.StoragePoliciesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(storagepolicy.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Group{config: guo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, guo.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
	}
	guo.mutation.done = true
	return _node, nil
}
