// 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/anzhiyu-c/anheyu-app/ent/predicate"
	"github.com/anzhiyu-c/anheyu-app/ent/user"
	"github.com/anzhiyu-c/anheyu-app/ent/usergroup"
	"github.com/anzhiyu-c/anheyu-app/pkg/domain/model"
)

// UserGroupUpdate is the builder for updating UserGroup entities.
type UserGroupUpdate struct {
	config
	hooks     []Hook
	mutation  *UserGroupMutation
	modifiers []func(*sql.UpdateBuilder)
}

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

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

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

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

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

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

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

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

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

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

// SetPermissions sets the "permissions" field.
func (_u *UserGroupUpdate) SetPermissions(v model.Boolset) *UserGroupUpdate {
	_u.mutation.SetPermissions(v)
	return _u
}

// SetMaxStorage sets the "max_storage" field.
func (_u *UserGroupUpdate) SetMaxStorage(v int64) *UserGroupUpdate {
	_u.mutation.ResetMaxStorage()
	_u.mutation.SetMaxStorage(v)
	return _u
}

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

// AddMaxStorage adds value to the "max_storage" field.
func (_u *UserGroupUpdate) AddMaxStorage(v int64) *UserGroupUpdate {
	_u.mutation.AddMaxStorage(v)
	return _u
}

// SetSpeedLimit sets the "speed_limit" field.
func (_u *UserGroupUpdate) SetSpeedLimit(v int64) *UserGroupUpdate {
	_u.mutation.ResetSpeedLimit()
	_u.mutation.SetSpeedLimit(v)
	return _u
}

// SetNillableSpeedLimit sets the "speed_limit" field if the given value is not nil.
func (_u *UserGroupUpdate) SetNillableSpeedLimit(v *int64) *UserGroupUpdate {
	if v != nil {
		_u.SetSpeedLimit(*v)
	}
	return _u
}

// AddSpeedLimit adds value to the "speed_limit" field.
func (_u *UserGroupUpdate) AddSpeedLimit(v int64) *UserGroupUpdate {
	_u.mutation.AddSpeedLimit(v)
	return _u
}

// SetSettings sets the "settings" field.
func (_u *UserGroupUpdate) SetSettings(v *model.GroupSettings) *UserGroupUpdate {
	_u.mutation.SetSettings(v)
	return _u
}

// AddUserIDs adds the "users" edge to the User entity by IDs.
func (_u *UserGroupUpdate) AddUserIDs(ids ...uint) *UserGroupUpdate {
	_u.mutation.AddUserIDs(ids...)
	return _u
}

// AddUsers adds the "users" edges to the User entity.
func (_u *UserGroupUpdate) AddUsers(v ...*User) *UserGroupUpdate {
	ids := make([]uint, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddUserIDs(ids...)
}

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

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

// RemoveUserIDs removes the "users" edge to User entities by IDs.
func (_u *UserGroupUpdate) RemoveUserIDs(ids ...uint) *UserGroupUpdate {
	_u.mutation.RemoveUserIDs(ids...)
	return _u
}

// RemoveUsers removes "users" edges to User entities.
func (_u *UserGroupUpdate) RemoveUsers(v ...*User) *UserGroupUpdate {
	ids := make([]uint, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveUserIDs(ids...)
}

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *UserGroupUpdate) check() error {
	if v, ok := _u.mutation.Name(); ok {
		if err := usergroup.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "UserGroup.name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Description(); ok {
		if err := usergroup.DescriptionValidator(v); err != nil {
			return &ValidationError{Name: "description", err: fmt.Errorf(`ent: validator failed for field "UserGroup.description": %w`, err)}
		}
	}
	return nil
}

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *UserGroupUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *UserGroupUpdate {
	_u.modifiers = append(_u.modifiers, modifiers...)
	return _u
}

func (_u *UserGroupUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(usergroup.Table, usergroup.Columns, sqlgraph.NewFieldSpec(usergroup.FieldID, field.TypeUint))
	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.DeletedAt(); ok {
		_spec.SetField(usergroup.FieldDeletedAt, field.TypeTime, value)
	}
	if _u.mutation.DeletedAtCleared() {
		_spec.ClearField(usergroup.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(usergroup.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(usergroup.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Description(); ok {
		_spec.SetField(usergroup.FieldDescription, field.TypeString, value)
	}
	if _u.mutation.DescriptionCleared() {
		_spec.ClearField(usergroup.FieldDescription, field.TypeString)
	}
	if value, ok := _u.mutation.Permissions(); ok {
		_spec.SetField(usergroup.FieldPermissions, field.TypeOther, value)
	}
	if value, ok := _u.mutation.MaxStorage(); ok {
		_spec.SetField(usergroup.FieldMaxStorage, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedMaxStorage(); ok {
		_spec.AddField(usergroup.FieldMaxStorage, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.SpeedLimit(); ok {
		_spec.SetField(usergroup.FieldSpeedLimit, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedSpeedLimit(); ok {
		_spec.AddField(usergroup.FieldSpeedLimit, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.Settings(); ok {
		_spec.SetField(usergroup.FieldSettings, field.TypeOther, value)
	}
	if _u.mutation.UsersCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   usergroup.UsersTable,
			Columns: []string{usergroup.UsersColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUint),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedUsersIDs(); len(nodes) > 0 && !_u.mutation.UsersCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   usergroup.UsersTable,
			Columns: []string{usergroup.UsersColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUint),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.UsersIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   usergroup.UsersTable,
			Columns: []string{usergroup.UsersColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUint),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_spec.AddModifiers(_u.modifiers...)
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{usergroup.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// UserGroupUpdateOne is the builder for updating a single UserGroup entity.
type UserGroupUpdateOne struct {
	config
	fields    []string
	hooks     []Hook
	mutation  *UserGroupMutation
	modifiers []func(*sql.UpdateBuilder)
}

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

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

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

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

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

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

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

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

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

// SetPermissions sets the "permissions" field.
func (_u *UserGroupUpdateOne) SetPermissions(v model.Boolset) *UserGroupUpdateOne {
	_u.mutation.SetPermissions(v)
	return _u
}

// SetMaxStorage sets the "max_storage" field.
func (_u *UserGroupUpdateOne) SetMaxStorage(v int64) *UserGroupUpdateOne {
	_u.mutation.ResetMaxStorage()
	_u.mutation.SetMaxStorage(v)
	return _u
}

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

// AddMaxStorage adds value to the "max_storage" field.
func (_u *UserGroupUpdateOne) AddMaxStorage(v int64) *UserGroupUpdateOne {
	_u.mutation.AddMaxStorage(v)
	return _u
}

// SetSpeedLimit sets the "speed_limit" field.
func (_u *UserGroupUpdateOne) SetSpeedLimit(v int64) *UserGroupUpdateOne {
	_u.mutation.ResetSpeedLimit()
	_u.mutation.SetSpeedLimit(v)
	return _u
}

// SetNillableSpeedLimit sets the "speed_limit" field if the given value is not nil.
func (_u *UserGroupUpdateOne) SetNillableSpeedLimit(v *int64) *UserGroupUpdateOne {
	if v != nil {
		_u.SetSpeedLimit(*v)
	}
	return _u
}

// AddSpeedLimit adds value to the "speed_limit" field.
func (_u *UserGroupUpdateOne) AddSpeedLimit(v int64) *UserGroupUpdateOne {
	_u.mutation.AddSpeedLimit(v)
	return _u
}

// SetSettings sets the "settings" field.
func (_u *UserGroupUpdateOne) SetSettings(v *model.GroupSettings) *UserGroupUpdateOne {
	_u.mutation.SetSettings(v)
	return _u
}

// AddUserIDs adds the "users" edge to the User entity by IDs.
func (_u *UserGroupUpdateOne) AddUserIDs(ids ...uint) *UserGroupUpdateOne {
	_u.mutation.AddUserIDs(ids...)
	return _u
}

// AddUsers adds the "users" edges to the User entity.
func (_u *UserGroupUpdateOne) AddUsers(v ...*User) *UserGroupUpdateOne {
	ids := make([]uint, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddUserIDs(ids...)
}

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

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

// RemoveUserIDs removes the "users" edge to User entities by IDs.
func (_u *UserGroupUpdateOne) RemoveUserIDs(ids ...uint) *UserGroupUpdateOne {
	_u.mutation.RemoveUserIDs(ids...)
	return _u
}

// RemoveUsers removes "users" edges to User entities.
func (_u *UserGroupUpdateOne) RemoveUsers(v ...*User) *UserGroupUpdateOne {
	ids := make([]uint, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveUserIDs(ids...)
}

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

// Save executes the query and returns the updated UserGroup entity.
func (_u *UserGroupUpdateOne) Save(ctx context.Context) (*UserGroup, 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 *UserGroupUpdateOne) SaveX(ctx context.Context) *UserGroup {
	node, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *UserGroupUpdateOne) check() error {
	if v, ok := _u.mutation.Name(); ok {
		if err := usergroup.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "UserGroup.name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Description(); ok {
		if err := usergroup.DescriptionValidator(v); err != nil {
			return &ValidationError{Name: "description", err: fmt.Errorf(`ent: validator failed for field "UserGroup.description": %w`, err)}
		}
	}
	return nil
}

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *UserGroupUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *UserGroupUpdateOne {
	_u.modifiers = append(_u.modifiers, modifiers...)
	return _u
}

func (_u *UserGroupUpdateOne) sqlSave(ctx context.Context) (_node *UserGroup, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(usergroup.Table, usergroup.Columns, sqlgraph.NewFieldSpec(usergroup.FieldID, field.TypeUint))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "UserGroup.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, usergroup.FieldID)
		for _, f := range fields {
			if !usergroup.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != usergroup.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.DeletedAt(); ok {
		_spec.SetField(usergroup.FieldDeletedAt, field.TypeTime, value)
	}
	if _u.mutation.DeletedAtCleared() {
		_spec.ClearField(usergroup.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(usergroup.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(usergroup.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Description(); ok {
		_spec.SetField(usergroup.FieldDescription, field.TypeString, value)
	}
	if _u.mutation.DescriptionCleared() {
		_spec.ClearField(usergroup.FieldDescription, field.TypeString)
	}
	if value, ok := _u.mutation.Permissions(); ok {
		_spec.SetField(usergroup.FieldPermissions, field.TypeOther, value)
	}
	if value, ok := _u.mutation.MaxStorage(); ok {
		_spec.SetField(usergroup.FieldMaxStorage, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedMaxStorage(); ok {
		_spec.AddField(usergroup.FieldMaxStorage, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.SpeedLimit(); ok {
		_spec.SetField(usergroup.FieldSpeedLimit, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedSpeedLimit(); ok {
		_spec.AddField(usergroup.FieldSpeedLimit, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.Settings(); ok {
		_spec.SetField(usergroup.FieldSettings, field.TypeOther, value)
	}
	if _u.mutation.UsersCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   usergroup.UsersTable,
			Columns: []string{usergroup.UsersColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUint),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedUsersIDs(); len(nodes) > 0 && !_u.mutation.UsersCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   usergroup.UsersTable,
			Columns: []string{usergroup.UsersColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUint),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.UsersIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   usergroup.UsersTable,
			Columns: []string{usergroup.UsersColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUint),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_spec.AddModifiers(_u.modifiers...)
	_node = &UserGroup{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{usergroup.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
