// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"galileo/ent/groupmember"
	"galileo/ent/predicate"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// GroupMemberUpdate is the builder for updating GroupMember entities.
type GroupMemberUpdate struct {
	config
	hooks    []Hook
	mutation *GroupMemberMutation
}

// Where appends a list predicates to the GroupMemberUpdate builder.
func (gmu *GroupMemberUpdate) Where(ps ...predicate.GroupMember) *GroupMemberUpdate {
	gmu.mutation.Where(ps...)
	return gmu
}

// SetGroupID sets the "group_id" field.
func (gmu *GroupMemberUpdate) SetGroupID(i int64) *GroupMemberUpdate {
	gmu.mutation.ResetGroupID()
	gmu.mutation.SetGroupID(i)
	return gmu
}

// AddGroupID adds i to the "group_id" field.
func (gmu *GroupMemberUpdate) AddGroupID(i int64) *GroupMemberUpdate {
	gmu.mutation.AddGroupID(i)
	return gmu
}

// SetUserID sets the "user_id" field.
func (gmu *GroupMemberUpdate) SetUserID(u uint32) *GroupMemberUpdate {
	gmu.mutation.ResetUserID()
	gmu.mutation.SetUserID(u)
	return gmu
}

// AddUserID adds u to the "user_id" field.
func (gmu *GroupMemberUpdate) AddUserID(u int32) *GroupMemberUpdate {
	gmu.mutation.AddUserID(u)
	return gmu
}

// SetRole sets the "role" field.
func (gmu *GroupMemberUpdate) SetRole(u uint8) *GroupMemberUpdate {
	gmu.mutation.ResetRole()
	gmu.mutation.SetRole(u)
	return gmu
}

// SetNillableRole sets the "role" field if the given value is not nil.
func (gmu *GroupMemberUpdate) SetNillableRole(u *uint8) *GroupMemberUpdate {
	if u != nil {
		gmu.SetRole(*u)
	}
	return gmu
}

// AddRole adds u to the "role" field.
func (gmu *GroupMemberUpdate) AddRole(u int8) *GroupMemberUpdate {
	gmu.mutation.AddRole(u)
	return gmu
}

// SetCreatedBy sets the "created_by" field.
func (gmu *GroupMemberUpdate) SetCreatedBy(u uint32) *GroupMemberUpdate {
	gmu.mutation.ResetCreatedBy()
	gmu.mutation.SetCreatedBy(u)
	return gmu
}

// SetNillableCreatedBy sets the "created_by" field if the given value is not nil.
func (gmu *GroupMemberUpdate) SetNillableCreatedBy(u *uint32) *GroupMemberUpdate {
	if u != nil {
		gmu.SetCreatedBy(*u)
	}
	return gmu
}

// AddCreatedBy adds u to the "created_by" field.
func (gmu *GroupMemberUpdate) AddCreatedBy(u int32) *GroupMemberUpdate {
	gmu.mutation.AddCreatedBy(u)
	return gmu
}

// ClearCreatedBy clears the value of the "created_by" field.
func (gmu *GroupMemberUpdate) ClearCreatedBy() *GroupMemberUpdate {
	gmu.mutation.ClearCreatedBy()
	return gmu
}

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

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

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

// SetDeletedBy sets the "deleted_by" field.
func (gmu *GroupMemberUpdate) SetDeletedBy(u uint32) *GroupMemberUpdate {
	gmu.mutation.ResetDeletedBy()
	gmu.mutation.SetDeletedBy(u)
	return gmu
}

// SetNillableDeletedBy sets the "deleted_by" field if the given value is not nil.
func (gmu *GroupMemberUpdate) SetNillableDeletedBy(u *uint32) *GroupMemberUpdate {
	if u != nil {
		gmu.SetDeletedBy(*u)
	}
	return gmu
}

// AddDeletedBy adds u to the "deleted_by" field.
func (gmu *GroupMemberUpdate) AddDeletedBy(u int32) *GroupMemberUpdate {
	gmu.mutation.AddDeletedBy(u)
	return gmu
}

// ClearDeletedBy clears the value of the "deleted_by" field.
func (gmu *GroupMemberUpdate) ClearDeletedBy() *GroupMemberUpdate {
	gmu.mutation.ClearDeletedBy()
	return gmu
}

// Mutation returns the GroupMemberMutation object of the builder.
func (gmu *GroupMemberUpdate) Mutation() *GroupMemberMutation {
	return gmu.mutation
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (gmu *GroupMemberUpdate) Save(ctx context.Context) (int, error) {
	return withHooks[int, GroupMemberMutation](ctx, gmu.sqlSave, gmu.mutation, gmu.hooks)
}

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

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

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

func (gmu *GroupMemberUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(groupmember.Table, groupmember.Columns, sqlgraph.NewFieldSpec(groupmember.FieldID, field.TypeInt64))
	if ps := gmu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := gmu.mutation.GroupID(); ok {
		_spec.SetField(groupmember.FieldGroupID, field.TypeInt64, value)
	}
	if value, ok := gmu.mutation.AddedGroupID(); ok {
		_spec.AddField(groupmember.FieldGroupID, field.TypeInt64, value)
	}
	if value, ok := gmu.mutation.UserID(); ok {
		_spec.SetField(groupmember.FieldUserID, field.TypeUint32, value)
	}
	if value, ok := gmu.mutation.AddedUserID(); ok {
		_spec.AddField(groupmember.FieldUserID, field.TypeUint32, value)
	}
	if value, ok := gmu.mutation.Role(); ok {
		_spec.SetField(groupmember.FieldRole, field.TypeUint8, value)
	}
	if value, ok := gmu.mutation.AddedRole(); ok {
		_spec.AddField(groupmember.FieldRole, field.TypeUint8, value)
	}
	if value, ok := gmu.mutation.CreatedBy(); ok {
		_spec.SetField(groupmember.FieldCreatedBy, field.TypeUint32, value)
	}
	if value, ok := gmu.mutation.AddedCreatedBy(); ok {
		_spec.AddField(groupmember.FieldCreatedBy, field.TypeUint32, value)
	}
	if gmu.mutation.CreatedByCleared() {
		_spec.ClearField(groupmember.FieldCreatedBy, field.TypeUint32)
	}
	if value, ok := gmu.mutation.DeletedAt(); ok {
		_spec.SetField(groupmember.FieldDeletedAt, field.TypeTime, value)
	}
	if gmu.mutation.DeletedAtCleared() {
		_spec.ClearField(groupmember.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := gmu.mutation.DeletedBy(); ok {
		_spec.SetField(groupmember.FieldDeletedBy, field.TypeUint32, value)
	}
	if value, ok := gmu.mutation.AddedDeletedBy(); ok {
		_spec.AddField(groupmember.FieldDeletedBy, field.TypeUint32, value)
	}
	if gmu.mutation.DeletedByCleared() {
		_spec.ClearField(groupmember.FieldDeletedBy, field.TypeUint32)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, gmu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{groupmember.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	gmu.mutation.done = true
	return n, nil
}

// GroupMemberUpdateOne is the builder for updating a single GroupMember entity.
type GroupMemberUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *GroupMemberMutation
}

// SetGroupID sets the "group_id" field.
func (gmuo *GroupMemberUpdateOne) SetGroupID(i int64) *GroupMemberUpdateOne {
	gmuo.mutation.ResetGroupID()
	gmuo.mutation.SetGroupID(i)
	return gmuo
}

// AddGroupID adds i to the "group_id" field.
func (gmuo *GroupMemberUpdateOne) AddGroupID(i int64) *GroupMemberUpdateOne {
	gmuo.mutation.AddGroupID(i)
	return gmuo
}

// SetUserID sets the "user_id" field.
func (gmuo *GroupMemberUpdateOne) SetUserID(u uint32) *GroupMemberUpdateOne {
	gmuo.mutation.ResetUserID()
	gmuo.mutation.SetUserID(u)
	return gmuo
}

// AddUserID adds u to the "user_id" field.
func (gmuo *GroupMemberUpdateOne) AddUserID(u int32) *GroupMemberUpdateOne {
	gmuo.mutation.AddUserID(u)
	return gmuo
}

// SetRole sets the "role" field.
func (gmuo *GroupMemberUpdateOne) SetRole(u uint8) *GroupMemberUpdateOne {
	gmuo.mutation.ResetRole()
	gmuo.mutation.SetRole(u)
	return gmuo
}

// SetNillableRole sets the "role" field if the given value is not nil.
func (gmuo *GroupMemberUpdateOne) SetNillableRole(u *uint8) *GroupMemberUpdateOne {
	if u != nil {
		gmuo.SetRole(*u)
	}
	return gmuo
}

// AddRole adds u to the "role" field.
func (gmuo *GroupMemberUpdateOne) AddRole(u int8) *GroupMemberUpdateOne {
	gmuo.mutation.AddRole(u)
	return gmuo
}

// SetCreatedBy sets the "created_by" field.
func (gmuo *GroupMemberUpdateOne) SetCreatedBy(u uint32) *GroupMemberUpdateOne {
	gmuo.mutation.ResetCreatedBy()
	gmuo.mutation.SetCreatedBy(u)
	return gmuo
}

// SetNillableCreatedBy sets the "created_by" field if the given value is not nil.
func (gmuo *GroupMemberUpdateOne) SetNillableCreatedBy(u *uint32) *GroupMemberUpdateOne {
	if u != nil {
		gmuo.SetCreatedBy(*u)
	}
	return gmuo
}

// AddCreatedBy adds u to the "created_by" field.
func (gmuo *GroupMemberUpdateOne) AddCreatedBy(u int32) *GroupMemberUpdateOne {
	gmuo.mutation.AddCreatedBy(u)
	return gmuo
}

// ClearCreatedBy clears the value of the "created_by" field.
func (gmuo *GroupMemberUpdateOne) ClearCreatedBy() *GroupMemberUpdateOne {
	gmuo.mutation.ClearCreatedBy()
	return gmuo
}

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

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

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

// SetDeletedBy sets the "deleted_by" field.
func (gmuo *GroupMemberUpdateOne) SetDeletedBy(u uint32) *GroupMemberUpdateOne {
	gmuo.mutation.ResetDeletedBy()
	gmuo.mutation.SetDeletedBy(u)
	return gmuo
}

// SetNillableDeletedBy sets the "deleted_by" field if the given value is not nil.
func (gmuo *GroupMemberUpdateOne) SetNillableDeletedBy(u *uint32) *GroupMemberUpdateOne {
	if u != nil {
		gmuo.SetDeletedBy(*u)
	}
	return gmuo
}

// AddDeletedBy adds u to the "deleted_by" field.
func (gmuo *GroupMemberUpdateOne) AddDeletedBy(u int32) *GroupMemberUpdateOne {
	gmuo.mutation.AddDeletedBy(u)
	return gmuo
}

// ClearDeletedBy clears the value of the "deleted_by" field.
func (gmuo *GroupMemberUpdateOne) ClearDeletedBy() *GroupMemberUpdateOne {
	gmuo.mutation.ClearDeletedBy()
	return gmuo
}

// Mutation returns the GroupMemberMutation object of the builder.
func (gmuo *GroupMemberUpdateOne) Mutation() *GroupMemberMutation {
	return gmuo.mutation
}

// Where appends a list predicates to the GroupMemberUpdate builder.
func (gmuo *GroupMemberUpdateOne) Where(ps ...predicate.GroupMember) *GroupMemberUpdateOne {
	gmuo.mutation.Where(ps...)
	return gmuo
}

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

// Save executes the query and returns the updated GroupMember entity.
func (gmuo *GroupMemberUpdateOne) Save(ctx context.Context) (*GroupMember, error) {
	return withHooks[*GroupMember, GroupMemberMutation](ctx, gmuo.sqlSave, gmuo.mutation, gmuo.hooks)
}

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

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

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

func (gmuo *GroupMemberUpdateOne) sqlSave(ctx context.Context) (_node *GroupMember, err error) {
	_spec := sqlgraph.NewUpdateSpec(groupmember.Table, groupmember.Columns, sqlgraph.NewFieldSpec(groupmember.FieldID, field.TypeInt64))
	id, ok := gmuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "GroupMember.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := gmuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, groupmember.FieldID)
		for _, f := range fields {
			if !groupmember.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != groupmember.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := gmuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := gmuo.mutation.GroupID(); ok {
		_spec.SetField(groupmember.FieldGroupID, field.TypeInt64, value)
	}
	if value, ok := gmuo.mutation.AddedGroupID(); ok {
		_spec.AddField(groupmember.FieldGroupID, field.TypeInt64, value)
	}
	if value, ok := gmuo.mutation.UserID(); ok {
		_spec.SetField(groupmember.FieldUserID, field.TypeUint32, value)
	}
	if value, ok := gmuo.mutation.AddedUserID(); ok {
		_spec.AddField(groupmember.FieldUserID, field.TypeUint32, value)
	}
	if value, ok := gmuo.mutation.Role(); ok {
		_spec.SetField(groupmember.FieldRole, field.TypeUint8, value)
	}
	if value, ok := gmuo.mutation.AddedRole(); ok {
		_spec.AddField(groupmember.FieldRole, field.TypeUint8, value)
	}
	if value, ok := gmuo.mutation.CreatedBy(); ok {
		_spec.SetField(groupmember.FieldCreatedBy, field.TypeUint32, value)
	}
	if value, ok := gmuo.mutation.AddedCreatedBy(); ok {
		_spec.AddField(groupmember.FieldCreatedBy, field.TypeUint32, value)
	}
	if gmuo.mutation.CreatedByCleared() {
		_spec.ClearField(groupmember.FieldCreatedBy, field.TypeUint32)
	}
	if value, ok := gmuo.mutation.DeletedAt(); ok {
		_spec.SetField(groupmember.FieldDeletedAt, field.TypeTime, value)
	}
	if gmuo.mutation.DeletedAtCleared() {
		_spec.ClearField(groupmember.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := gmuo.mutation.DeletedBy(); ok {
		_spec.SetField(groupmember.FieldDeletedBy, field.TypeUint32, value)
	}
	if value, ok := gmuo.mutation.AddedDeletedBy(); ok {
		_spec.AddField(groupmember.FieldDeletedBy, field.TypeUint32, value)
	}
	if gmuo.mutation.DeletedByCleared() {
		_spec.ClearField(groupmember.FieldDeletedBy, field.TypeUint32)
	}
	_node = &GroupMember{config: gmuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, gmuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{groupmember.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	gmuo.mutation.done = true
	return _node, nil
}
