// 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"
)

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

// Where appends a list predicates to the DictionaryUpdate builder.
func (du *DictionaryUpdate) Where(ps ...predicate.Dictionary) *DictionaryUpdate {
	du.mutation.Where(ps...)
	return du
}

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

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

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

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

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

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

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

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

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

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

// ClearDescription clears the value of the "description" field.
func (du *DictionaryUpdate) ClearDescription() *DictionaryUpdate {
	du.mutation.ClearDescription()
	return du
}

// AddDetailIDs adds the "details" edge to the DictionaryDetail entity by IDs.
func (du *DictionaryUpdate) AddDetailIDs(ids ...int64) *DictionaryUpdate {
	du.mutation.AddDetailIDs(ids...)
	return du
}

// AddDetails adds the "details" edges to the DictionaryDetail entity.
func (du *DictionaryUpdate) AddDetails(d ...*DictionaryDetail) *DictionaryUpdate {
	ids := make([]int64, len(d))
	for i := range d {
		ids[i] = d[i].ID
	}
	return du.AddDetailIDs(ids...)
}

// Mutation returns the DictionaryMutation object of the builder.
func (du *DictionaryUpdate) Mutation() *DictionaryMutation {
	return du.mutation
}

// ClearDetails clears all "details" edges to the DictionaryDetail entity.
func (du *DictionaryUpdate) ClearDetails() *DictionaryUpdate {
	du.mutation.ClearDetails()
	return du
}

// RemoveDetailIDs removes the "details" edge to DictionaryDetail entities by IDs.
func (du *DictionaryUpdate) RemoveDetailIDs(ids ...int64) *DictionaryUpdate {
	du.mutation.RemoveDetailIDs(ids...)
	return du
}

// RemoveDetails removes "details" edges to DictionaryDetail entities.
func (du *DictionaryUpdate) RemoveDetails(d ...*DictionaryDetail) *DictionaryUpdate {
	ids := make([]int64, len(d))
	for i := range d {
		ids[i] = d[i].ID
	}
	return du.RemoveDetailIDs(ids...)
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (du *DictionaryUpdate) check() error {
	if v, ok := du.mutation.Status(); ok {
		if err := dictionary.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Dictionary.status": %w`, err)}
		}
	}
	return nil
}

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

func (du *DictionaryUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := du.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(dictionary.Table, dictionary.Columns, sqlgraph.NewFieldSpec(dictionary.FieldID, field.TypeInt64))
	if ps := du.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := du.mutation.UpdatedAt(); ok {
		_spec.SetField(dictionary.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := du.mutation.Status(); ok {
		_spec.SetField(dictionary.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := du.mutation.Version(); ok {
		_spec.SetField(dictionary.FieldVersion, field.TypeInt64, value)
	}
	if value, ok := du.mutation.AddedVersion(); ok {
		_spec.AddField(dictionary.FieldVersion, field.TypeInt64, value)
	}
	if value, ok := du.mutation.Title(); ok {
		_spec.SetField(dictionary.FieldTitle, field.TypeString, value)
	}
	if value, ok := du.mutation.Name(); ok {
		_spec.SetField(dictionary.FieldName, field.TypeString, value)
	}
	if value, ok := du.mutation.Description(); ok {
		_spec.SetField(dictionary.FieldDescription, field.TypeString, value)
	}
	if du.mutation.DescriptionCleared() {
		_spec.ClearField(dictionary.FieldDescription, field.TypeString)
	}
	if du.mutation.DetailsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   dictionary.DetailsTable,
			Columns: []string{dictionary.DetailsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(dictionarydetail.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := du.mutation.RemovedDetailsIDs(); len(nodes) > 0 && !du.mutation.DetailsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   dictionary.DetailsTable,
			Columns: []string{dictionary.DetailsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(dictionarydetail.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := du.mutation.DetailsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   dictionary.DetailsTable,
			Columns: []string{dictionary.DetailsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(dictionarydetail.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(du.modifiers...)
	if n, err = sqlgraph.UpdateNodes(ctx, du.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{dictionary.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	du.mutation.done = true
	return n, nil
}

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

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

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

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

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

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

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

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

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

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

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

// ClearDescription clears the value of the "description" field.
func (duo *DictionaryUpdateOne) ClearDescription() *DictionaryUpdateOne {
	duo.mutation.ClearDescription()
	return duo
}

// AddDetailIDs adds the "details" edge to the DictionaryDetail entity by IDs.
func (duo *DictionaryUpdateOne) AddDetailIDs(ids ...int64) *DictionaryUpdateOne {
	duo.mutation.AddDetailIDs(ids...)
	return duo
}

// AddDetails adds the "details" edges to the DictionaryDetail entity.
func (duo *DictionaryUpdateOne) AddDetails(d ...*DictionaryDetail) *DictionaryUpdateOne {
	ids := make([]int64, len(d))
	for i := range d {
		ids[i] = d[i].ID
	}
	return duo.AddDetailIDs(ids...)
}

// Mutation returns the DictionaryMutation object of the builder.
func (duo *DictionaryUpdateOne) Mutation() *DictionaryMutation {
	return duo.mutation
}

// ClearDetails clears all "details" edges to the DictionaryDetail entity.
func (duo *DictionaryUpdateOne) ClearDetails() *DictionaryUpdateOne {
	duo.mutation.ClearDetails()
	return duo
}

// RemoveDetailIDs removes the "details" edge to DictionaryDetail entities by IDs.
func (duo *DictionaryUpdateOne) RemoveDetailIDs(ids ...int64) *DictionaryUpdateOne {
	duo.mutation.RemoveDetailIDs(ids...)
	return duo
}

// RemoveDetails removes "details" edges to DictionaryDetail entities.
func (duo *DictionaryUpdateOne) RemoveDetails(d ...*DictionaryDetail) *DictionaryUpdateOne {
	ids := make([]int64, len(d))
	for i := range d {
		ids[i] = d[i].ID
	}
	return duo.RemoveDetailIDs(ids...)
}

// Where appends a list predicates to the DictionaryUpdate builder.
func (duo *DictionaryUpdateOne) Where(ps ...predicate.Dictionary) *DictionaryUpdateOne {
	duo.mutation.Where(ps...)
	return duo
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (duo *DictionaryUpdateOne) check() error {
	if v, ok := duo.mutation.Status(); ok {
		if err := dictionary.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Dictionary.status": %w`, err)}
		}
	}
	return nil
}

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

func (duo *DictionaryUpdateOne) sqlSave(ctx context.Context) (_node *Dictionary, err error) {
	if err := duo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(dictionary.Table, dictionary.Columns, sqlgraph.NewFieldSpec(dictionary.FieldID, field.TypeInt64))
	id, ok := duo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Dictionary.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := duo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, dictionary.FieldID)
		for _, f := range fields {
			if !dictionary.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != dictionary.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := duo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := duo.mutation.UpdatedAt(); ok {
		_spec.SetField(dictionary.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := duo.mutation.Status(); ok {
		_spec.SetField(dictionary.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := duo.mutation.Version(); ok {
		_spec.SetField(dictionary.FieldVersion, field.TypeInt64, value)
	}
	if value, ok := duo.mutation.AddedVersion(); ok {
		_spec.AddField(dictionary.FieldVersion, field.TypeInt64, value)
	}
	if value, ok := duo.mutation.Title(); ok {
		_spec.SetField(dictionary.FieldTitle, field.TypeString, value)
	}
	if value, ok := duo.mutation.Name(); ok {
		_spec.SetField(dictionary.FieldName, field.TypeString, value)
	}
	if value, ok := duo.mutation.Description(); ok {
		_spec.SetField(dictionary.FieldDescription, field.TypeString, value)
	}
	if duo.mutation.DescriptionCleared() {
		_spec.ClearField(dictionary.FieldDescription, field.TypeString)
	}
	if duo.mutation.DetailsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   dictionary.DetailsTable,
			Columns: []string{dictionary.DetailsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(dictionarydetail.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := duo.mutation.RemovedDetailsIDs(); len(nodes) > 0 && !duo.mutation.DetailsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   dictionary.DetailsTable,
			Columns: []string{dictionary.DetailsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(dictionarydetail.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := duo.mutation.DetailsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   dictionary.DetailsTable,
			Columns: []string{dictionary.DetailsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(dictionarydetail.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(duo.modifiers...)
	_node = &Dictionary{config: duo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, duo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{dictionary.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	duo.mutation.done = true
	return _node, nil
}
