// 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/dictionary"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/dictionarydetail"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/predicate"
	"git.noahlan.cn/noahlan/ntool-biz/core/orm/nent/types"
)

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

// Where appends a list predicates to the DictionaryDetailUpdate builder.
func (ddu *DictionaryDetailUpdate) Where(ps ...predicate.DictionaryDetail) *DictionaryDetailUpdate {
	ddu.mutation.Where(ps...)
	return ddu
}

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

// SetStatus sets the "status" field.
func (ddu *DictionaryDetailUpdate) SetStatus(t types.Status) *DictionaryDetailUpdate {
	ddu.mutation.SetStatus(t)
	return ddu
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (ddu *DictionaryDetailUpdate) SetNillableStatus(t *types.Status) *DictionaryDetailUpdate {
	if t != nil {
		ddu.SetStatus(*t)
	}
	return ddu
}

// SetSort sets the "sort" field.
func (ddu *DictionaryDetailUpdate) SetSort(u uint32) *DictionaryDetailUpdate {
	ddu.mutation.ResetSort()
	ddu.mutation.SetSort(u)
	return ddu
}

// SetNillableSort sets the "sort" field if the given value is not nil.
func (ddu *DictionaryDetailUpdate) SetNillableSort(u *uint32) *DictionaryDetailUpdate {
	if u != nil {
		ddu.SetSort(*u)
	}
	return ddu
}

// AddSort adds u to the "sort" field.
func (ddu *DictionaryDetailUpdate) AddSort(u int32) *DictionaryDetailUpdate {
	ddu.mutation.AddSort(u)
	return ddu
}

// SetVersion sets the "version" field.
func (ddu *DictionaryDetailUpdate) SetVersion(i int64) *DictionaryDetailUpdate {
	ddu.mutation.ResetVersion()
	ddu.mutation.SetVersion(i)
	return ddu
}

// SetNillableVersion sets the "version" field if the given value is not nil.
func (ddu *DictionaryDetailUpdate) SetNillableVersion(i *int64) *DictionaryDetailUpdate {
	if i != nil {
		ddu.SetVersion(*i)
	}
	return ddu
}

// AddVersion adds i to the "version" field.
func (ddu *DictionaryDetailUpdate) AddVersion(i int64) *DictionaryDetailUpdate {
	ddu.mutation.AddVersion(i)
	return ddu
}

// SetTitle sets the "title" field.
func (ddu *DictionaryDetailUpdate) SetTitle(s string) *DictionaryDetailUpdate {
	ddu.mutation.SetTitle(s)
	return ddu
}

// SetKey sets the "key" field.
func (ddu *DictionaryDetailUpdate) SetKey(s string) *DictionaryDetailUpdate {
	ddu.mutation.SetKey(s)
	return ddu
}

// SetValue sets the "value" field.
func (ddu *DictionaryDetailUpdate) SetValue(s string) *DictionaryDetailUpdate {
	ddu.mutation.SetValue(s)
	return ddu
}

// SetDictionaryID sets the "dictionary_id" field.
func (ddu *DictionaryDetailUpdate) SetDictionaryID(i int64) *DictionaryDetailUpdate {
	ddu.mutation.SetDictionaryID(i)
	return ddu
}

// SetDictionary sets the "dictionary" edge to the Dictionary entity.
func (ddu *DictionaryDetailUpdate) SetDictionary(d *Dictionary) *DictionaryDetailUpdate {
	return ddu.SetDictionaryID(d.ID)
}

// Mutation returns the DictionaryDetailMutation object of the builder.
func (ddu *DictionaryDetailUpdate) Mutation() *DictionaryDetailMutation {
	return ddu.mutation
}

// ClearDictionary clears the "dictionary" edge to the Dictionary entity.
func (ddu *DictionaryDetailUpdate) ClearDictionary() *DictionaryDetailUpdate {
	ddu.mutation.ClearDictionary()
	return ddu
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (ddu *DictionaryDetailUpdate) Save(ctx context.Context) (int, error) {
	if err := ddu.defaults(); err != nil {
		return 0, err
	}
	return withHooks(ctx, ddu.sqlSave, ddu.mutation, ddu.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (ddu *DictionaryDetailUpdate) defaults() error {
	if _, ok := ddu.mutation.UpdatedAt(); !ok {
		if dictionarydetail.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized dictionarydetail.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := dictionarydetail.UpdateDefaultUpdatedAt()
		ddu.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (ddu *DictionaryDetailUpdate) check() error {
	if v, ok := ddu.mutation.Status(); ok {
		if err := dictionarydetail.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "DictionaryDetail.status": %w`, err)}
		}
	}
	if _, ok := ddu.mutation.DictionaryID(); ddu.mutation.DictionaryCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "DictionaryDetail.dictionary"`)
	}
	return nil
}

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

func (ddu *DictionaryDetailUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := ddu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(dictionarydetail.Table, dictionarydetail.Columns, sqlgraph.NewFieldSpec(dictionarydetail.FieldID, field.TypeInt64))
	if ps := ddu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := ddu.mutation.UpdatedAt(); ok {
		_spec.SetField(dictionarydetail.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := ddu.mutation.Status(); ok {
		_spec.SetField(dictionarydetail.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := ddu.mutation.Sort(); ok {
		_spec.SetField(dictionarydetail.FieldSort, field.TypeUint32, value)
	}
	if value, ok := ddu.mutation.AddedSort(); ok {
		_spec.AddField(dictionarydetail.FieldSort, field.TypeUint32, value)
	}
	if value, ok := ddu.mutation.Version(); ok {
		_spec.SetField(dictionarydetail.FieldVersion, field.TypeInt64, value)
	}
	if value, ok := ddu.mutation.AddedVersion(); ok {
		_spec.AddField(dictionarydetail.FieldVersion, field.TypeInt64, value)
	}
	if value, ok := ddu.mutation.Title(); ok {
		_spec.SetField(dictionarydetail.FieldTitle, field.TypeString, value)
	}
	if value, ok := ddu.mutation.Key(); ok {
		_spec.SetField(dictionarydetail.FieldKey, field.TypeString, value)
	}
	if value, ok := ddu.mutation.Value(); ok {
		_spec.SetField(dictionarydetail.FieldValue, field.TypeString, value)
	}
	if ddu.mutation.DictionaryCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   dictionarydetail.DictionaryTable,
			Columns: []string{dictionarydetail.DictionaryColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(dictionary.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := ddu.mutation.DictionaryIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   dictionarydetail.DictionaryTable,
			Columns: []string{dictionarydetail.DictionaryColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(dictionary.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_spec.AddModifiers(ddu.modifiers...)
	if n, err = sqlgraph.UpdateNodes(ctx, ddu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{dictionarydetail.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	ddu.mutation.done = true
	return n, nil
}

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

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

// SetStatus sets the "status" field.
func (dduo *DictionaryDetailUpdateOne) SetStatus(t types.Status) *DictionaryDetailUpdateOne {
	dduo.mutation.SetStatus(t)
	return dduo
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (dduo *DictionaryDetailUpdateOne) SetNillableStatus(t *types.Status) *DictionaryDetailUpdateOne {
	if t != nil {
		dduo.SetStatus(*t)
	}
	return dduo
}

// SetSort sets the "sort" field.
func (dduo *DictionaryDetailUpdateOne) SetSort(u uint32) *DictionaryDetailUpdateOne {
	dduo.mutation.ResetSort()
	dduo.mutation.SetSort(u)
	return dduo
}

// SetNillableSort sets the "sort" field if the given value is not nil.
func (dduo *DictionaryDetailUpdateOne) SetNillableSort(u *uint32) *DictionaryDetailUpdateOne {
	if u != nil {
		dduo.SetSort(*u)
	}
	return dduo
}

// AddSort adds u to the "sort" field.
func (dduo *DictionaryDetailUpdateOne) AddSort(u int32) *DictionaryDetailUpdateOne {
	dduo.mutation.AddSort(u)
	return dduo
}

// SetVersion sets the "version" field.
func (dduo *DictionaryDetailUpdateOne) SetVersion(i int64) *DictionaryDetailUpdateOne {
	dduo.mutation.ResetVersion()
	dduo.mutation.SetVersion(i)
	return dduo
}

// SetNillableVersion sets the "version" field if the given value is not nil.
func (dduo *DictionaryDetailUpdateOne) SetNillableVersion(i *int64) *DictionaryDetailUpdateOne {
	if i != nil {
		dduo.SetVersion(*i)
	}
	return dduo
}

// AddVersion adds i to the "version" field.
func (dduo *DictionaryDetailUpdateOne) AddVersion(i int64) *DictionaryDetailUpdateOne {
	dduo.mutation.AddVersion(i)
	return dduo
}

// SetTitle sets the "title" field.
func (dduo *DictionaryDetailUpdateOne) SetTitle(s string) *DictionaryDetailUpdateOne {
	dduo.mutation.SetTitle(s)
	return dduo
}

// SetKey sets the "key" field.
func (dduo *DictionaryDetailUpdateOne) SetKey(s string) *DictionaryDetailUpdateOne {
	dduo.mutation.SetKey(s)
	return dduo
}

// SetValue sets the "value" field.
func (dduo *DictionaryDetailUpdateOne) SetValue(s string) *DictionaryDetailUpdateOne {
	dduo.mutation.SetValue(s)
	return dduo
}

// SetDictionaryID sets the "dictionary_id" field.
func (dduo *DictionaryDetailUpdateOne) SetDictionaryID(i int64) *DictionaryDetailUpdateOne {
	dduo.mutation.SetDictionaryID(i)
	return dduo
}

// SetDictionary sets the "dictionary" edge to the Dictionary entity.
func (dduo *DictionaryDetailUpdateOne) SetDictionary(d *Dictionary) *DictionaryDetailUpdateOne {
	return dduo.SetDictionaryID(d.ID)
}

// Mutation returns the DictionaryDetailMutation object of the builder.
func (dduo *DictionaryDetailUpdateOne) Mutation() *DictionaryDetailMutation {
	return dduo.mutation
}

// ClearDictionary clears the "dictionary" edge to the Dictionary entity.
func (dduo *DictionaryDetailUpdateOne) ClearDictionary() *DictionaryDetailUpdateOne {
	dduo.mutation.ClearDictionary()
	return dduo
}

// Where appends a list predicates to the DictionaryDetailUpdate builder.
func (dduo *DictionaryDetailUpdateOne) Where(ps ...predicate.DictionaryDetail) *DictionaryDetailUpdateOne {
	dduo.mutation.Where(ps...)
	return dduo
}

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

// Save executes the query and returns the updated DictionaryDetail entity.
func (dduo *DictionaryDetailUpdateOne) Save(ctx context.Context) (*DictionaryDetail, error) {
	if err := dduo.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, dduo.sqlSave, dduo.mutation, dduo.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (dduo *DictionaryDetailUpdateOne) defaults() error {
	if _, ok := dduo.mutation.UpdatedAt(); !ok {
		if dictionarydetail.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized dictionarydetail.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := dictionarydetail.UpdateDefaultUpdatedAt()
		dduo.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (dduo *DictionaryDetailUpdateOne) check() error {
	if v, ok := dduo.mutation.Status(); ok {
		if err := dictionarydetail.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "DictionaryDetail.status": %w`, err)}
		}
	}
	if _, ok := dduo.mutation.DictionaryID(); dduo.mutation.DictionaryCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "DictionaryDetail.dictionary"`)
	}
	return nil
}

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

func (dduo *DictionaryDetailUpdateOne) sqlSave(ctx context.Context) (_node *DictionaryDetail, err error) {
	if err := dduo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(dictionarydetail.Table, dictionarydetail.Columns, sqlgraph.NewFieldSpec(dictionarydetail.FieldID, field.TypeInt64))
	id, ok := dduo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "DictionaryDetail.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := dduo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, dictionarydetail.FieldID)
		for _, f := range fields {
			if !dictionarydetail.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != dictionarydetail.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := dduo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := dduo.mutation.UpdatedAt(); ok {
		_spec.SetField(dictionarydetail.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := dduo.mutation.Status(); ok {
		_spec.SetField(dictionarydetail.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := dduo.mutation.Sort(); ok {
		_spec.SetField(dictionarydetail.FieldSort, field.TypeUint32, value)
	}
	if value, ok := dduo.mutation.AddedSort(); ok {
		_spec.AddField(dictionarydetail.FieldSort, field.TypeUint32, value)
	}
	if value, ok := dduo.mutation.Version(); ok {
		_spec.SetField(dictionarydetail.FieldVersion, field.TypeInt64, value)
	}
	if value, ok := dduo.mutation.AddedVersion(); ok {
		_spec.AddField(dictionarydetail.FieldVersion, field.TypeInt64, value)
	}
	if value, ok := dduo.mutation.Title(); ok {
		_spec.SetField(dictionarydetail.FieldTitle, field.TypeString, value)
	}
	if value, ok := dduo.mutation.Key(); ok {
		_spec.SetField(dictionarydetail.FieldKey, field.TypeString, value)
	}
	if value, ok := dduo.mutation.Value(); ok {
		_spec.SetField(dictionarydetail.FieldValue, field.TypeString, value)
	}
	if dduo.mutation.DictionaryCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   dictionarydetail.DictionaryTable,
			Columns: []string{dictionarydetail.DictionaryColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(dictionary.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := dduo.mutation.DictionaryIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   dictionarydetail.DictionaryTable,
			Columns: []string{dictionarydetail.DictionaryColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(dictionary.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_spec.AddModifiers(dduo.modifiers...)
	_node = &DictionaryDetail{config: dduo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, dduo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{dictionarydetail.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	dduo.mutation.done = true
	return _node, nil
}
