// Code generated by ent, DO NOT EDIT.

package db

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/chaitin/MonkeyCode/backend/db/predicate"
	"github.com/chaitin/MonkeyCode/backend/db/setting"
)

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

// Where appends a list predicates to the SettingUpdate builder.
func (su *SettingUpdate) Where(ps ...predicate.Setting) *SettingUpdate {
	su.mutation.Where(ps...)
	return su
}

// SetEnableSSO sets the "enable_sso" field.
func (su *SettingUpdate) SetEnableSSO(b bool) *SettingUpdate {
	su.mutation.SetEnableSSO(b)
	return su
}

// SetNillableEnableSSO sets the "enable_sso" field if the given value is not nil.
func (su *SettingUpdate) SetNillableEnableSSO(b *bool) *SettingUpdate {
	if b != nil {
		su.SetEnableSSO(*b)
	}
	return su
}

// SetForceTwoFactorAuth sets the "force_two_factor_auth" field.
func (su *SettingUpdate) SetForceTwoFactorAuth(b bool) *SettingUpdate {
	su.mutation.SetForceTwoFactorAuth(b)
	return su
}

// SetNillableForceTwoFactorAuth sets the "force_two_factor_auth" field if the given value is not nil.
func (su *SettingUpdate) SetNillableForceTwoFactorAuth(b *bool) *SettingUpdate {
	if b != nil {
		su.SetForceTwoFactorAuth(*b)
	}
	return su
}

// SetDisablePasswordLogin sets the "disable_password_login" field.
func (su *SettingUpdate) SetDisablePasswordLogin(b bool) *SettingUpdate {
	su.mutation.SetDisablePasswordLogin(b)
	return su
}

// SetNillableDisablePasswordLogin sets the "disable_password_login" field if the given value is not nil.
func (su *SettingUpdate) SetNillableDisablePasswordLogin(b *bool) *SettingUpdate {
	if b != nil {
		su.SetDisablePasswordLogin(*b)
	}
	return su
}

// SetEnableDingtalkOauth sets the "enable_dingtalk_oauth" field.
func (su *SettingUpdate) SetEnableDingtalkOauth(b bool) *SettingUpdate {
	su.mutation.SetEnableDingtalkOauth(b)
	return su
}

// SetNillableEnableDingtalkOauth sets the "enable_dingtalk_oauth" field if the given value is not nil.
func (su *SettingUpdate) SetNillableEnableDingtalkOauth(b *bool) *SettingUpdate {
	if b != nil {
		su.SetEnableDingtalkOauth(*b)
	}
	return su
}

// SetDingtalkClientID sets the "dingtalk_client_id" field.
func (su *SettingUpdate) SetDingtalkClientID(s string) *SettingUpdate {
	su.mutation.SetDingtalkClientID(s)
	return su
}

// SetNillableDingtalkClientID sets the "dingtalk_client_id" field if the given value is not nil.
func (su *SettingUpdate) SetNillableDingtalkClientID(s *string) *SettingUpdate {
	if s != nil {
		su.SetDingtalkClientID(*s)
	}
	return su
}

// ClearDingtalkClientID clears the value of the "dingtalk_client_id" field.
func (su *SettingUpdate) ClearDingtalkClientID() *SettingUpdate {
	su.mutation.ClearDingtalkClientID()
	return su
}

// SetDingtalkClientSecret sets the "dingtalk_client_secret" field.
func (su *SettingUpdate) SetDingtalkClientSecret(s string) *SettingUpdate {
	su.mutation.SetDingtalkClientSecret(s)
	return su
}

// SetNillableDingtalkClientSecret sets the "dingtalk_client_secret" field if the given value is not nil.
func (su *SettingUpdate) SetNillableDingtalkClientSecret(s *string) *SettingUpdate {
	if s != nil {
		su.SetDingtalkClientSecret(*s)
	}
	return su
}

// ClearDingtalkClientSecret clears the value of the "dingtalk_client_secret" field.
func (su *SettingUpdate) ClearDingtalkClientSecret() *SettingUpdate {
	su.mutation.ClearDingtalkClientSecret()
	return su
}

// SetCreatedAt sets the "created_at" field.
func (su *SettingUpdate) SetCreatedAt(t time.Time) *SettingUpdate {
	su.mutation.SetCreatedAt(t)
	return su
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (su *SettingUpdate) SetNillableCreatedAt(t *time.Time) *SettingUpdate {
	if t != nil {
		su.SetCreatedAt(*t)
	}
	return su
}

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

// Mutation returns the SettingMutation object of the builder.
func (su *SettingUpdate) Mutation() *SettingMutation {
	return su.mutation
}

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

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

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

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

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

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

func (su *SettingUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(setting.Table, setting.Columns, sqlgraph.NewFieldSpec(setting.FieldID, field.TypeUUID))
	if ps := su.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := su.mutation.EnableSSO(); ok {
		_spec.SetField(setting.FieldEnableSSO, field.TypeBool, value)
	}
	if value, ok := su.mutation.ForceTwoFactorAuth(); ok {
		_spec.SetField(setting.FieldForceTwoFactorAuth, field.TypeBool, value)
	}
	if value, ok := su.mutation.DisablePasswordLogin(); ok {
		_spec.SetField(setting.FieldDisablePasswordLogin, field.TypeBool, value)
	}
	if value, ok := su.mutation.EnableDingtalkOauth(); ok {
		_spec.SetField(setting.FieldEnableDingtalkOauth, field.TypeBool, value)
	}
	if value, ok := su.mutation.DingtalkClientID(); ok {
		_spec.SetField(setting.FieldDingtalkClientID, field.TypeString, value)
	}
	if su.mutation.DingtalkClientIDCleared() {
		_spec.ClearField(setting.FieldDingtalkClientID, field.TypeString)
	}
	if value, ok := su.mutation.DingtalkClientSecret(); ok {
		_spec.SetField(setting.FieldDingtalkClientSecret, field.TypeString, value)
	}
	if su.mutation.DingtalkClientSecretCleared() {
		_spec.ClearField(setting.FieldDingtalkClientSecret, field.TypeString)
	}
	if value, ok := su.mutation.CreatedAt(); ok {
		_spec.SetField(setting.FieldCreatedAt, field.TypeTime, value)
	}
	if value, ok := su.mutation.UpdatedAt(); ok {
		_spec.SetField(setting.FieldUpdatedAt, field.TypeTime, value)
	}
	_spec.AddModifiers(su.modifiers...)
	if n, err = sqlgraph.UpdateNodes(ctx, su.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{setting.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	su.mutation.done = true
	return n, nil
}

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

// SetEnableSSO sets the "enable_sso" field.
func (suo *SettingUpdateOne) SetEnableSSO(b bool) *SettingUpdateOne {
	suo.mutation.SetEnableSSO(b)
	return suo
}

// SetNillableEnableSSO sets the "enable_sso" field if the given value is not nil.
func (suo *SettingUpdateOne) SetNillableEnableSSO(b *bool) *SettingUpdateOne {
	if b != nil {
		suo.SetEnableSSO(*b)
	}
	return suo
}

// SetForceTwoFactorAuth sets the "force_two_factor_auth" field.
func (suo *SettingUpdateOne) SetForceTwoFactorAuth(b bool) *SettingUpdateOne {
	suo.mutation.SetForceTwoFactorAuth(b)
	return suo
}

// SetNillableForceTwoFactorAuth sets the "force_two_factor_auth" field if the given value is not nil.
func (suo *SettingUpdateOne) SetNillableForceTwoFactorAuth(b *bool) *SettingUpdateOne {
	if b != nil {
		suo.SetForceTwoFactorAuth(*b)
	}
	return suo
}

// SetDisablePasswordLogin sets the "disable_password_login" field.
func (suo *SettingUpdateOne) SetDisablePasswordLogin(b bool) *SettingUpdateOne {
	suo.mutation.SetDisablePasswordLogin(b)
	return suo
}

// SetNillableDisablePasswordLogin sets the "disable_password_login" field if the given value is not nil.
func (suo *SettingUpdateOne) SetNillableDisablePasswordLogin(b *bool) *SettingUpdateOne {
	if b != nil {
		suo.SetDisablePasswordLogin(*b)
	}
	return suo
}

// SetEnableDingtalkOauth sets the "enable_dingtalk_oauth" field.
func (suo *SettingUpdateOne) SetEnableDingtalkOauth(b bool) *SettingUpdateOne {
	suo.mutation.SetEnableDingtalkOauth(b)
	return suo
}

// SetNillableEnableDingtalkOauth sets the "enable_dingtalk_oauth" field if the given value is not nil.
func (suo *SettingUpdateOne) SetNillableEnableDingtalkOauth(b *bool) *SettingUpdateOne {
	if b != nil {
		suo.SetEnableDingtalkOauth(*b)
	}
	return suo
}

// SetDingtalkClientID sets the "dingtalk_client_id" field.
func (suo *SettingUpdateOne) SetDingtalkClientID(s string) *SettingUpdateOne {
	suo.mutation.SetDingtalkClientID(s)
	return suo
}

// SetNillableDingtalkClientID sets the "dingtalk_client_id" field if the given value is not nil.
func (suo *SettingUpdateOne) SetNillableDingtalkClientID(s *string) *SettingUpdateOne {
	if s != nil {
		suo.SetDingtalkClientID(*s)
	}
	return suo
}

// ClearDingtalkClientID clears the value of the "dingtalk_client_id" field.
func (suo *SettingUpdateOne) ClearDingtalkClientID() *SettingUpdateOne {
	suo.mutation.ClearDingtalkClientID()
	return suo
}

// SetDingtalkClientSecret sets the "dingtalk_client_secret" field.
func (suo *SettingUpdateOne) SetDingtalkClientSecret(s string) *SettingUpdateOne {
	suo.mutation.SetDingtalkClientSecret(s)
	return suo
}

// SetNillableDingtalkClientSecret sets the "dingtalk_client_secret" field if the given value is not nil.
func (suo *SettingUpdateOne) SetNillableDingtalkClientSecret(s *string) *SettingUpdateOne {
	if s != nil {
		suo.SetDingtalkClientSecret(*s)
	}
	return suo
}

// ClearDingtalkClientSecret clears the value of the "dingtalk_client_secret" field.
func (suo *SettingUpdateOne) ClearDingtalkClientSecret() *SettingUpdateOne {
	suo.mutation.ClearDingtalkClientSecret()
	return suo
}

// SetCreatedAt sets the "created_at" field.
func (suo *SettingUpdateOne) SetCreatedAt(t time.Time) *SettingUpdateOne {
	suo.mutation.SetCreatedAt(t)
	return suo
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (suo *SettingUpdateOne) SetNillableCreatedAt(t *time.Time) *SettingUpdateOne {
	if t != nil {
		suo.SetCreatedAt(*t)
	}
	return suo
}

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

// Mutation returns the SettingMutation object of the builder.
func (suo *SettingUpdateOne) Mutation() *SettingMutation {
	return suo.mutation
}

// Where appends a list predicates to the SettingUpdate builder.
func (suo *SettingUpdateOne) Where(ps ...predicate.Setting) *SettingUpdateOne {
	suo.mutation.Where(ps...)
	return suo
}

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

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

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

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

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

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

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

func (suo *SettingUpdateOne) sqlSave(ctx context.Context) (_node *Setting, err error) {
	_spec := sqlgraph.NewUpdateSpec(setting.Table, setting.Columns, sqlgraph.NewFieldSpec(setting.FieldID, field.TypeUUID))
	id, ok := suo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`db: missing "Setting.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := suo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, setting.FieldID)
		for _, f := range fields {
			if !setting.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("db: invalid field %q for query", f)}
			}
			if f != setting.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := suo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := suo.mutation.EnableSSO(); ok {
		_spec.SetField(setting.FieldEnableSSO, field.TypeBool, value)
	}
	if value, ok := suo.mutation.ForceTwoFactorAuth(); ok {
		_spec.SetField(setting.FieldForceTwoFactorAuth, field.TypeBool, value)
	}
	if value, ok := suo.mutation.DisablePasswordLogin(); ok {
		_spec.SetField(setting.FieldDisablePasswordLogin, field.TypeBool, value)
	}
	if value, ok := suo.mutation.EnableDingtalkOauth(); ok {
		_spec.SetField(setting.FieldEnableDingtalkOauth, field.TypeBool, value)
	}
	if value, ok := suo.mutation.DingtalkClientID(); ok {
		_spec.SetField(setting.FieldDingtalkClientID, field.TypeString, value)
	}
	if suo.mutation.DingtalkClientIDCleared() {
		_spec.ClearField(setting.FieldDingtalkClientID, field.TypeString)
	}
	if value, ok := suo.mutation.DingtalkClientSecret(); ok {
		_spec.SetField(setting.FieldDingtalkClientSecret, field.TypeString, value)
	}
	if suo.mutation.DingtalkClientSecretCleared() {
		_spec.ClearField(setting.FieldDingtalkClientSecret, field.TypeString)
	}
	if value, ok := suo.mutation.CreatedAt(); ok {
		_spec.SetField(setting.FieldCreatedAt, field.TypeTime, value)
	}
	if value, ok := suo.mutation.UpdatedAt(); ok {
		_spec.SetField(setting.FieldUpdatedAt, field.TypeTime, value)
	}
	_spec.AddModifiers(suo.modifiers...)
	_node = &Setting{config: suo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, suo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{setting.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	suo.mutation.done = true
	return _node, nil
}
