// 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"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/oauthprovider"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/predicate"
)

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

// Where appends a list predicates to the OauthProviderUpdate builder.
func (opu *OauthProviderUpdate) Where(ps ...predicate.OauthProvider) *OauthProviderUpdate {
	opu.mutation.Where(ps...)
	return opu
}

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

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

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

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

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

// SetClientID sets the "client_id" field.
func (opu *OauthProviderUpdate) SetClientID(s string) *OauthProviderUpdate {
	opu.mutation.SetClientID(s)
	return opu
}

// SetClientSecret sets the "client_secret" field.
func (opu *OauthProviderUpdate) SetClientSecret(s string) *OauthProviderUpdate {
	opu.mutation.SetClientSecret(s)
	return opu
}

// SetRedirectURL sets the "redirect_url" field.
func (opu *OauthProviderUpdate) SetRedirectURL(s string) *OauthProviderUpdate {
	opu.mutation.SetRedirectURL(s)
	return opu
}

// SetNillableRedirectURL sets the "redirect_url" field if the given value is not nil.
func (opu *OauthProviderUpdate) SetNillableRedirectURL(s *string) *OauthProviderUpdate {
	if s != nil {
		opu.SetRedirectURL(*s)
	}
	return opu
}

// SetScopes sets the "scopes" field.
func (opu *OauthProviderUpdate) SetScopes(s string) *OauthProviderUpdate {
	opu.mutation.SetScopes(s)
	return opu
}

// SetNillableScopes sets the "scopes" field if the given value is not nil.
func (opu *OauthProviderUpdate) SetNillableScopes(s *string) *OauthProviderUpdate {
	if s != nil {
		opu.SetScopes(*s)
	}
	return opu
}

// SetAuthURL sets the "auth_url" field.
func (opu *OauthProviderUpdate) SetAuthURL(s string) *OauthProviderUpdate {
	opu.mutation.SetAuthURL(s)
	return opu
}

// SetNillableAuthURL sets the "auth_url" field if the given value is not nil.
func (opu *OauthProviderUpdate) SetNillableAuthURL(s *string) *OauthProviderUpdate {
	if s != nil {
		opu.SetAuthURL(*s)
	}
	return opu
}

// SetTokenURL sets the "token_url" field.
func (opu *OauthProviderUpdate) SetTokenURL(s string) *OauthProviderUpdate {
	opu.mutation.SetTokenURL(s)
	return opu
}

// SetNillableTokenURL sets the "token_url" field if the given value is not nil.
func (opu *OauthProviderUpdate) SetNillableTokenURL(s *string) *OauthProviderUpdate {
	if s != nil {
		opu.SetTokenURL(*s)
	}
	return opu
}

// SetInfoURL sets the "info_url" field.
func (opu *OauthProviderUpdate) SetInfoURL(s string) *OauthProviderUpdate {
	opu.mutation.SetInfoURL(s)
	return opu
}

// SetNillableInfoURL sets the "info_url" field if the given value is not nil.
func (opu *OauthProviderUpdate) SetNillableInfoURL(s *string) *OauthProviderUpdate {
	if s != nil {
		opu.SetInfoURL(*s)
	}
	return opu
}

// SetDescription sets the "description" field.
func (opu *OauthProviderUpdate) SetDescription(s string) *OauthProviderUpdate {
	opu.mutation.SetDescription(s)
	return opu
}

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

// SetSystem sets the "system" field.
func (opu *OauthProviderUpdate) SetSystem(b bool) *OauthProviderUpdate {
	opu.mutation.SetSystem(b)
	return opu
}

// SetNillableSystem sets the "system" field if the given value is not nil.
func (opu *OauthProviderUpdate) SetNillableSystem(b *bool) *OauthProviderUpdate {
	if b != nil {
		opu.SetSystem(*b)
	}
	return opu
}

// SetInit sets the "init" field.
func (opu *OauthProviderUpdate) SetInit(b bool) *OauthProviderUpdate {
	opu.mutation.SetInit(b)
	return opu
}

// SetNillableInit sets the "init" field if the given value is not nil.
func (opu *OauthProviderUpdate) SetNillableInit(b *bool) *OauthProviderUpdate {
	if b != nil {
		opu.SetInit(*b)
	}
	return opu
}

// Mutation returns the OauthProviderMutation object of the builder.
func (opu *OauthProviderUpdate) Mutation() *OauthProviderMutation {
	return opu.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (opu *OauthProviderUpdate) defaults() {
	if _, ok := opu.mutation.UpdatedAt(); !ok {
		v := oauthprovider.UpdateDefaultUpdatedAt()
		opu.mutation.SetUpdatedAt(v)
	}
}

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

func (opu *OauthProviderUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(oauthprovider.Table, oauthprovider.Columns, sqlgraph.NewFieldSpec(oauthprovider.FieldID, field.TypeInt64))
	if ps := opu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := opu.mutation.UpdatedAt(); ok {
		_spec.SetField(oauthprovider.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := opu.mutation.DeletedAt(); ok {
		_spec.SetField(oauthprovider.FieldDeletedAt, field.TypeTime, value)
	}
	if opu.mutation.DeletedAtCleared() {
		_spec.ClearField(oauthprovider.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := opu.mutation.Name(); ok {
		_spec.SetField(oauthprovider.FieldName, field.TypeString, value)
	}
	if value, ok := opu.mutation.ClientID(); ok {
		_spec.SetField(oauthprovider.FieldClientID, field.TypeString, value)
	}
	if value, ok := opu.mutation.ClientSecret(); ok {
		_spec.SetField(oauthprovider.FieldClientSecret, field.TypeString, value)
	}
	if value, ok := opu.mutation.RedirectURL(); ok {
		_spec.SetField(oauthprovider.FieldRedirectURL, field.TypeString, value)
	}
	if value, ok := opu.mutation.Scopes(); ok {
		_spec.SetField(oauthprovider.FieldScopes, field.TypeString, value)
	}
	if value, ok := opu.mutation.AuthURL(); ok {
		_spec.SetField(oauthprovider.FieldAuthURL, field.TypeString, value)
	}
	if value, ok := opu.mutation.TokenURL(); ok {
		_spec.SetField(oauthprovider.FieldTokenURL, field.TypeString, value)
	}
	if value, ok := opu.mutation.InfoURL(); ok {
		_spec.SetField(oauthprovider.FieldInfoURL, field.TypeString, value)
	}
	if value, ok := opu.mutation.Description(); ok {
		_spec.SetField(oauthprovider.FieldDescription, field.TypeString, value)
	}
	if value, ok := opu.mutation.System(); ok {
		_spec.SetField(oauthprovider.FieldSystem, field.TypeBool, value)
	}
	if value, ok := opu.mutation.Init(); ok {
		_spec.SetField(oauthprovider.FieldInit, field.TypeBool, value)
	}
	_spec.AddModifiers(opu.modifiers...)
	if n, err = sqlgraph.UpdateNodes(ctx, opu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{oauthprovider.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	opu.mutation.done = true
	return n, nil
}

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

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

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

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

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

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

// SetClientID sets the "client_id" field.
func (opuo *OauthProviderUpdateOne) SetClientID(s string) *OauthProviderUpdateOne {
	opuo.mutation.SetClientID(s)
	return opuo
}

// SetClientSecret sets the "client_secret" field.
func (opuo *OauthProviderUpdateOne) SetClientSecret(s string) *OauthProviderUpdateOne {
	opuo.mutation.SetClientSecret(s)
	return opuo
}

// SetRedirectURL sets the "redirect_url" field.
func (opuo *OauthProviderUpdateOne) SetRedirectURL(s string) *OauthProviderUpdateOne {
	opuo.mutation.SetRedirectURL(s)
	return opuo
}

// SetNillableRedirectURL sets the "redirect_url" field if the given value is not nil.
func (opuo *OauthProviderUpdateOne) SetNillableRedirectURL(s *string) *OauthProviderUpdateOne {
	if s != nil {
		opuo.SetRedirectURL(*s)
	}
	return opuo
}

// SetScopes sets the "scopes" field.
func (opuo *OauthProviderUpdateOne) SetScopes(s string) *OauthProviderUpdateOne {
	opuo.mutation.SetScopes(s)
	return opuo
}

// SetNillableScopes sets the "scopes" field if the given value is not nil.
func (opuo *OauthProviderUpdateOne) SetNillableScopes(s *string) *OauthProviderUpdateOne {
	if s != nil {
		opuo.SetScopes(*s)
	}
	return opuo
}

// SetAuthURL sets the "auth_url" field.
func (opuo *OauthProviderUpdateOne) SetAuthURL(s string) *OauthProviderUpdateOne {
	opuo.mutation.SetAuthURL(s)
	return opuo
}

// SetNillableAuthURL sets the "auth_url" field if the given value is not nil.
func (opuo *OauthProviderUpdateOne) SetNillableAuthURL(s *string) *OauthProviderUpdateOne {
	if s != nil {
		opuo.SetAuthURL(*s)
	}
	return opuo
}

// SetTokenURL sets the "token_url" field.
func (opuo *OauthProviderUpdateOne) SetTokenURL(s string) *OauthProviderUpdateOne {
	opuo.mutation.SetTokenURL(s)
	return opuo
}

// SetNillableTokenURL sets the "token_url" field if the given value is not nil.
func (opuo *OauthProviderUpdateOne) SetNillableTokenURL(s *string) *OauthProviderUpdateOne {
	if s != nil {
		opuo.SetTokenURL(*s)
	}
	return opuo
}

// SetInfoURL sets the "info_url" field.
func (opuo *OauthProviderUpdateOne) SetInfoURL(s string) *OauthProviderUpdateOne {
	opuo.mutation.SetInfoURL(s)
	return opuo
}

// SetNillableInfoURL sets the "info_url" field if the given value is not nil.
func (opuo *OauthProviderUpdateOne) SetNillableInfoURL(s *string) *OauthProviderUpdateOne {
	if s != nil {
		opuo.SetInfoURL(*s)
	}
	return opuo
}

// SetDescription sets the "description" field.
func (opuo *OauthProviderUpdateOne) SetDescription(s string) *OauthProviderUpdateOne {
	opuo.mutation.SetDescription(s)
	return opuo
}

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

// SetSystem sets the "system" field.
func (opuo *OauthProviderUpdateOne) SetSystem(b bool) *OauthProviderUpdateOne {
	opuo.mutation.SetSystem(b)
	return opuo
}

// SetNillableSystem sets the "system" field if the given value is not nil.
func (opuo *OauthProviderUpdateOne) SetNillableSystem(b *bool) *OauthProviderUpdateOne {
	if b != nil {
		opuo.SetSystem(*b)
	}
	return opuo
}

// SetInit sets the "init" field.
func (opuo *OauthProviderUpdateOne) SetInit(b bool) *OauthProviderUpdateOne {
	opuo.mutation.SetInit(b)
	return opuo
}

// SetNillableInit sets the "init" field if the given value is not nil.
func (opuo *OauthProviderUpdateOne) SetNillableInit(b *bool) *OauthProviderUpdateOne {
	if b != nil {
		opuo.SetInit(*b)
	}
	return opuo
}

// Mutation returns the OauthProviderMutation object of the builder.
func (opuo *OauthProviderUpdateOne) Mutation() *OauthProviderMutation {
	return opuo.mutation
}

// Where appends a list predicates to the OauthProviderUpdate builder.
func (opuo *OauthProviderUpdateOne) Where(ps ...predicate.OauthProvider) *OauthProviderUpdateOne {
	opuo.mutation.Where(ps...)
	return opuo
}

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

// Save executes the query and returns the updated OauthProvider entity.
func (opuo *OauthProviderUpdateOne) Save(ctx context.Context) (*OauthProvider, error) {
	opuo.defaults()
	return withHooks(ctx, opuo.sqlSave, opuo.mutation, opuo.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (opuo *OauthProviderUpdateOne) defaults() {
	if _, ok := opuo.mutation.UpdatedAt(); !ok {
		v := oauthprovider.UpdateDefaultUpdatedAt()
		opuo.mutation.SetUpdatedAt(v)
	}
}

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

func (opuo *OauthProviderUpdateOne) sqlSave(ctx context.Context) (_node *OauthProvider, err error) {
	_spec := sqlgraph.NewUpdateSpec(oauthprovider.Table, oauthprovider.Columns, sqlgraph.NewFieldSpec(oauthprovider.FieldID, field.TypeInt64))
	id, ok := opuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "OauthProvider.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := opuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, oauthprovider.FieldID)
		for _, f := range fields {
			if !oauthprovider.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != oauthprovider.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := opuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := opuo.mutation.UpdatedAt(); ok {
		_spec.SetField(oauthprovider.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := opuo.mutation.DeletedAt(); ok {
		_spec.SetField(oauthprovider.FieldDeletedAt, field.TypeTime, value)
	}
	if opuo.mutation.DeletedAtCleared() {
		_spec.ClearField(oauthprovider.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := opuo.mutation.Name(); ok {
		_spec.SetField(oauthprovider.FieldName, field.TypeString, value)
	}
	if value, ok := opuo.mutation.ClientID(); ok {
		_spec.SetField(oauthprovider.FieldClientID, field.TypeString, value)
	}
	if value, ok := opuo.mutation.ClientSecret(); ok {
		_spec.SetField(oauthprovider.FieldClientSecret, field.TypeString, value)
	}
	if value, ok := opuo.mutation.RedirectURL(); ok {
		_spec.SetField(oauthprovider.FieldRedirectURL, field.TypeString, value)
	}
	if value, ok := opuo.mutation.Scopes(); ok {
		_spec.SetField(oauthprovider.FieldScopes, field.TypeString, value)
	}
	if value, ok := opuo.mutation.AuthURL(); ok {
		_spec.SetField(oauthprovider.FieldAuthURL, field.TypeString, value)
	}
	if value, ok := opuo.mutation.TokenURL(); ok {
		_spec.SetField(oauthprovider.FieldTokenURL, field.TypeString, value)
	}
	if value, ok := opuo.mutation.InfoURL(); ok {
		_spec.SetField(oauthprovider.FieldInfoURL, field.TypeString, value)
	}
	if value, ok := opuo.mutation.Description(); ok {
		_spec.SetField(oauthprovider.FieldDescription, field.TypeString, value)
	}
	if value, ok := opuo.mutation.System(); ok {
		_spec.SetField(oauthprovider.FieldSystem, field.TypeBool, value)
	}
	if value, ok := opuo.mutation.Init(); ok {
		_spec.SetField(oauthprovider.FieldInit, field.TypeBool, value)
	}
	_spec.AddModifiers(opuo.modifiers...)
	_node = &OauthProvider{config: opuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, opuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{oauthprovider.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	opuo.mutation.done = true
	return _node, nil
}
