// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"

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

// AccountGroupUpdate is the builder for updating AccountGroup entities.
type AccountGroupUpdate struct {
	config
	hooks    []Hook
	mutation *AccountGroupMutation
}

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

// SetAccountID sets the "account_id" field.
func (_u *AccountGroupUpdate) SetAccountID(v int64) *AccountGroupUpdate {
	_u.mutation.SetAccountID(v)
	return _u
}

// SetNillableAccountID sets the "account_id" field if the given value is not nil.
func (_u *AccountGroupUpdate) SetNillableAccountID(v *int64) *AccountGroupUpdate {
	if v != nil {
		_u.SetAccountID(*v)
	}
	return _u
}

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

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

// SetPriority sets the "priority" field.
func (_u *AccountGroupUpdate) SetPriority(v int) *AccountGroupUpdate {
	_u.mutation.ResetPriority()
	_u.mutation.SetPriority(v)
	return _u
}

// SetNillablePriority sets the "priority" field if the given value is not nil.
func (_u *AccountGroupUpdate) SetNillablePriority(v *int) *AccountGroupUpdate {
	if v != nil {
		_u.SetPriority(*v)
	}
	return _u
}

// AddPriority adds value to the "priority" field.
func (_u *AccountGroupUpdate) AddPriority(v int) *AccountGroupUpdate {
	_u.mutation.AddPriority(v)
	return _u
}

// SetAccount sets the "account" edge to the Account entity.
func (_u *AccountGroupUpdate) SetAccount(v *Account) *AccountGroupUpdate {
	return _u.SetAccountID(v.ID)
}

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

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

// ClearAccount clears the "account" edge to the Account entity.
func (_u *AccountGroupUpdate) ClearAccount() *AccountGroupUpdate {
	_u.mutation.ClearAccount()
	return _u
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *AccountGroupUpdate) check() error {
	if _u.mutation.AccountCleared() && len(_u.mutation.AccountIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "AccountGroup.account"`)
	}
	if _u.mutation.GroupCleared() && len(_u.mutation.GroupIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "AccountGroup.group"`)
	}
	return nil
}

func (_u *AccountGroupUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(accountgroup.Table, accountgroup.Columns, sqlgraph.NewFieldSpec(accountgroup.FieldAccountID, field.TypeInt64), sqlgraph.NewFieldSpec(accountgroup.FieldGroupID, 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.Priority(); ok {
		_spec.SetField(accountgroup.FieldPriority, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedPriority(); ok {
		_spec.AddField(accountgroup.FieldPriority, field.TypeInt, value)
	}
	if _u.mutation.AccountCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   accountgroup.AccountTable,
			Columns: []string{accountgroup.AccountColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.AccountIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   accountgroup.AccountTable,
			Columns: []string{accountgroup.AccountColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.GroupCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   accountgroup.GroupTable,
			Columns: []string{accountgroup.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.GroupIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   accountgroup.GroupTable,
			Columns: []string{accountgroup.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{accountgroup.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// AccountGroupUpdateOne is the builder for updating a single AccountGroup entity.
type AccountGroupUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *AccountGroupMutation
}

// SetAccountID sets the "account_id" field.
func (_u *AccountGroupUpdateOne) SetAccountID(v int64) *AccountGroupUpdateOne {
	_u.mutation.SetAccountID(v)
	return _u
}

// SetNillableAccountID sets the "account_id" field if the given value is not nil.
func (_u *AccountGroupUpdateOne) SetNillableAccountID(v *int64) *AccountGroupUpdateOne {
	if v != nil {
		_u.SetAccountID(*v)
	}
	return _u
}

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

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

// SetPriority sets the "priority" field.
func (_u *AccountGroupUpdateOne) SetPriority(v int) *AccountGroupUpdateOne {
	_u.mutation.ResetPriority()
	_u.mutation.SetPriority(v)
	return _u
}

// SetNillablePriority sets the "priority" field if the given value is not nil.
func (_u *AccountGroupUpdateOne) SetNillablePriority(v *int) *AccountGroupUpdateOne {
	if v != nil {
		_u.SetPriority(*v)
	}
	return _u
}

// AddPriority adds value to the "priority" field.
func (_u *AccountGroupUpdateOne) AddPriority(v int) *AccountGroupUpdateOne {
	_u.mutation.AddPriority(v)
	return _u
}

// SetAccount sets the "account" edge to the Account entity.
func (_u *AccountGroupUpdateOne) SetAccount(v *Account) *AccountGroupUpdateOne {
	return _u.SetAccountID(v.ID)
}

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

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

// ClearAccount clears the "account" edge to the Account entity.
func (_u *AccountGroupUpdateOne) ClearAccount() *AccountGroupUpdateOne {
	_u.mutation.ClearAccount()
	return _u
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *AccountGroupUpdateOne) check() error {
	if _u.mutation.AccountCleared() && len(_u.mutation.AccountIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "AccountGroup.account"`)
	}
	if _u.mutation.GroupCleared() && len(_u.mutation.GroupIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "AccountGroup.group"`)
	}
	return nil
}

func (_u *AccountGroupUpdateOne) sqlSave(ctx context.Context) (_node *AccountGroup, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(accountgroup.Table, accountgroup.Columns, sqlgraph.NewFieldSpec(accountgroup.FieldAccountID, field.TypeInt64), sqlgraph.NewFieldSpec(accountgroup.FieldGroupID, field.TypeInt64))
	if id, ok := _u.mutation.AccountID(); !ok {
		return nil, &ValidationError{Name: "account_id", err: errors.New(`ent: missing "AccountGroup.account_id" for update`)}
	} else {
		_spec.Node.CompositeID[0].Value = id
	}
	if id, ok := _u.mutation.GroupID(); !ok {
		return nil, &ValidationError{Name: "group_id", err: errors.New(`ent: missing "AccountGroup.group_id" for update`)}
	} else {
		_spec.Node.CompositeID[1].Value = id
	}
	if fields := _u.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, len(fields))
		for i, f := range fields {
			if !accountgroup.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			_spec.Node.Columns[i] = 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.Priority(); ok {
		_spec.SetField(accountgroup.FieldPriority, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedPriority(); ok {
		_spec.AddField(accountgroup.FieldPriority, field.TypeInt, value)
	}
	if _u.mutation.AccountCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   accountgroup.AccountTable,
			Columns: []string{accountgroup.AccountColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.AccountIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   accountgroup.AccountTable,
			Columns: []string{accountgroup.AccountColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.GroupCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   accountgroup.GroupTable,
			Columns: []string{accountgroup.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.GroupIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   accountgroup.GroupTable,
			Columns: []string{accountgroup.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &AccountGroup{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{accountgroup.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
