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

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

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

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

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

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

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

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

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

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

// SetLeaderID sets the "leader_id" field.
func (du *DepartmentUpdate) SetLeaderID(i int64) *DepartmentUpdate {
	du.mutation.SetLeaderID(i)
	return du
}

// SetNillableLeaderID sets the "leader_id" field if the given value is not nil.
func (du *DepartmentUpdate) SetNillableLeaderID(i *int64) *DepartmentUpdate {
	if i != nil {
		du.SetLeaderID(*i)
	}
	return du
}

// ClearLeaderID clears the value of the "leader_id" field.
func (du *DepartmentUpdate) ClearLeaderID() *DepartmentUpdate {
	du.mutation.ClearLeaderID()
	return du
}

// SetRemark sets the "remark" field.
func (du *DepartmentUpdate) SetRemark(s string) *DepartmentUpdate {
	du.mutation.SetRemark(s)
	return du
}

// SetNillableRemark sets the "remark" field if the given value is not nil.
func (du *DepartmentUpdate) SetNillableRemark(s *string) *DepartmentUpdate {
	if s != nil {
		du.SetRemark(*s)
	}
	return du
}

// ClearRemark clears the value of the "remark" field.
func (du *DepartmentUpdate) ClearRemark() *DepartmentUpdate {
	du.mutation.ClearRemark()
	return du
}

// SetParentID sets the "parent_id" field.
func (du *DepartmentUpdate) SetParentID(i int64) *DepartmentUpdate {
	du.mutation.SetParentID(i)
	return du
}

// SetNillableParentID sets the "parent_id" field if the given value is not nil.
func (du *DepartmentUpdate) SetNillableParentID(i *int64) *DepartmentUpdate {
	if i != nil {
		du.SetParentID(*i)
	}
	return du
}

// ClearParentID clears the value of the "parent_id" field.
func (du *DepartmentUpdate) ClearParentID() *DepartmentUpdate {
	du.mutation.ClearParentID()
	return du
}

// SetParent sets the "parent" edge to the Department entity.
func (du *DepartmentUpdate) SetParent(d *Department) *DepartmentUpdate {
	return du.SetParentID(d.ID)
}

// AddChildIDs adds the "children" edge to the Department entity by IDs.
func (du *DepartmentUpdate) AddChildIDs(ids ...int64) *DepartmentUpdate {
	du.mutation.AddChildIDs(ids...)
	return du
}

// AddChildren adds the "children" edges to the Department entity.
func (du *DepartmentUpdate) AddChildren(d ...*Department) *DepartmentUpdate {
	ids := make([]int64, len(d))
	for i := range d {
		ids[i] = d[i].ID
	}
	return du.AddChildIDs(ids...)
}

// AddUserIDs adds the "users" edge to the User entity by IDs.
func (du *DepartmentUpdate) AddUserIDs(ids ...int64) *DepartmentUpdate {
	du.mutation.AddUserIDs(ids...)
	return du
}

// AddUsers adds the "users" edges to the User entity.
func (du *DepartmentUpdate) AddUsers(u ...*User) *DepartmentUpdate {
	ids := make([]int64, len(u))
	for i := range u {
		ids[i] = u[i].ID
	}
	return du.AddUserIDs(ids...)
}

// SetLeader sets the "leader" edge to the User entity.
func (du *DepartmentUpdate) SetLeader(u *User) *DepartmentUpdate {
	return du.SetLeaderID(u.ID)
}

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

// ClearParent clears the "parent" edge to the Department entity.
func (du *DepartmentUpdate) ClearParent() *DepartmentUpdate {
	du.mutation.ClearParent()
	return du
}

// ClearChildren clears all "children" edges to the Department entity.
func (du *DepartmentUpdate) ClearChildren() *DepartmentUpdate {
	du.mutation.ClearChildren()
	return du
}

// RemoveChildIDs removes the "children" edge to Department entities by IDs.
func (du *DepartmentUpdate) RemoveChildIDs(ids ...int64) *DepartmentUpdate {
	du.mutation.RemoveChildIDs(ids...)
	return du
}

// RemoveChildren removes "children" edges to Department entities.
func (du *DepartmentUpdate) RemoveChildren(d ...*Department) *DepartmentUpdate {
	ids := make([]int64, len(d))
	for i := range d {
		ids[i] = d[i].ID
	}
	return du.RemoveChildIDs(ids...)
}

// ClearUsers clears all "users" edges to the User entity.
func (du *DepartmentUpdate) ClearUsers() *DepartmentUpdate {
	du.mutation.ClearUsers()
	return du
}

// RemoveUserIDs removes the "users" edge to User entities by IDs.
func (du *DepartmentUpdate) RemoveUserIDs(ids ...int64) *DepartmentUpdate {
	du.mutation.RemoveUserIDs(ids...)
	return du
}

// RemoveUsers removes "users" edges to User entities.
func (du *DepartmentUpdate) RemoveUsers(u ...*User) *DepartmentUpdate {
	ids := make([]int64, len(u))
	for i := range u {
		ids[i] = u[i].ID
	}
	return du.RemoveUserIDs(ids...)
}

// ClearLeader clears the "leader" edge to the User entity.
func (du *DepartmentUpdate) ClearLeader() *DepartmentUpdate {
	du.mutation.ClearLeader()
	return du
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (du *DepartmentUpdate) 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 *DepartmentUpdate) SaveX(ctx context.Context) int {
	affected, err := du.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

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

// ExecX is like Exec, but panics if an error occurs.
func (du *DepartmentUpdate) 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 *DepartmentUpdate) defaults() error {
	if _, ok := du.mutation.UpdatedAt(); !ok {
		if department.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized department.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := department.UpdateDefaultUpdatedAt()
		du.mutation.SetUpdatedAt(v)
	}
	return nil
}

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

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

func (du *DepartmentUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := du.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(department.Table, department.Columns, sqlgraph.NewFieldSpec(department.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(department.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := du.mutation.Status(); ok {
		_spec.SetField(department.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := du.mutation.Version(); ok {
		_spec.SetField(department.FieldVersion, field.TypeInt64, value)
	}
	if value, ok := du.mutation.AddedVersion(); ok {
		_spec.AddField(department.FieldVersion, field.TypeInt64, value)
	}
	if value, ok := du.mutation.Name(); ok {
		_spec.SetField(department.FieldName, field.TypeString, value)
	}
	if value, ok := du.mutation.Remark(); ok {
		_spec.SetField(department.FieldRemark, field.TypeString, value)
	}
	if du.mutation.RemarkCleared() {
		_spec.ClearField(department.FieldRemark, field.TypeString)
	}
	if du.mutation.ParentCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   department.ParentTable,
			Columns: []string{department.ParentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(department.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := du.mutation.ParentIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   department.ParentTable,
			Columns: []string{department.ParentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(department.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if du.mutation.ChildrenCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.ChildrenTable,
			Columns: []string{department.ChildrenColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(department.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := du.mutation.RemovedChildrenIDs(); len(nodes) > 0 && !du.mutation.ChildrenCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.ChildrenTable,
			Columns: []string{department.ChildrenColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(department.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.ChildrenIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.ChildrenTable,
			Columns: []string{department.ChildrenColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(department.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if du.mutation.UsersCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   department.UsersTable,
			Columns: department.UsersPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := du.mutation.RemovedUsersIDs(); len(nodes) > 0 && !du.mutation.UsersCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   department.UsersTable,
			Columns: department.UsersPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.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.UsersIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   department.UsersTable,
			Columns: department.UsersPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if du.mutation.LeaderCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   department.LeaderTable,
			Columns: []string{department.LeaderColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := du.mutation.LeaderIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   department.LeaderTable,
			Columns: []string{department.LeaderColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.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{department.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	du.mutation.done = true
	return n, nil
}

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

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

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

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

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

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

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

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

// SetLeaderID sets the "leader_id" field.
func (duo *DepartmentUpdateOne) SetLeaderID(i int64) *DepartmentUpdateOne {
	duo.mutation.SetLeaderID(i)
	return duo
}

// SetNillableLeaderID sets the "leader_id" field if the given value is not nil.
func (duo *DepartmentUpdateOne) SetNillableLeaderID(i *int64) *DepartmentUpdateOne {
	if i != nil {
		duo.SetLeaderID(*i)
	}
	return duo
}

// ClearLeaderID clears the value of the "leader_id" field.
func (duo *DepartmentUpdateOne) ClearLeaderID() *DepartmentUpdateOne {
	duo.mutation.ClearLeaderID()
	return duo
}

// SetRemark sets the "remark" field.
func (duo *DepartmentUpdateOne) SetRemark(s string) *DepartmentUpdateOne {
	duo.mutation.SetRemark(s)
	return duo
}

// SetNillableRemark sets the "remark" field if the given value is not nil.
func (duo *DepartmentUpdateOne) SetNillableRemark(s *string) *DepartmentUpdateOne {
	if s != nil {
		duo.SetRemark(*s)
	}
	return duo
}

// ClearRemark clears the value of the "remark" field.
func (duo *DepartmentUpdateOne) ClearRemark() *DepartmentUpdateOne {
	duo.mutation.ClearRemark()
	return duo
}

// SetParentID sets the "parent_id" field.
func (duo *DepartmentUpdateOne) SetParentID(i int64) *DepartmentUpdateOne {
	duo.mutation.SetParentID(i)
	return duo
}

// SetNillableParentID sets the "parent_id" field if the given value is not nil.
func (duo *DepartmentUpdateOne) SetNillableParentID(i *int64) *DepartmentUpdateOne {
	if i != nil {
		duo.SetParentID(*i)
	}
	return duo
}

// ClearParentID clears the value of the "parent_id" field.
func (duo *DepartmentUpdateOne) ClearParentID() *DepartmentUpdateOne {
	duo.mutation.ClearParentID()
	return duo
}

// SetParent sets the "parent" edge to the Department entity.
func (duo *DepartmentUpdateOne) SetParent(d *Department) *DepartmentUpdateOne {
	return duo.SetParentID(d.ID)
}

// AddChildIDs adds the "children" edge to the Department entity by IDs.
func (duo *DepartmentUpdateOne) AddChildIDs(ids ...int64) *DepartmentUpdateOne {
	duo.mutation.AddChildIDs(ids...)
	return duo
}

// AddChildren adds the "children" edges to the Department entity.
func (duo *DepartmentUpdateOne) AddChildren(d ...*Department) *DepartmentUpdateOne {
	ids := make([]int64, len(d))
	for i := range d {
		ids[i] = d[i].ID
	}
	return duo.AddChildIDs(ids...)
}

// AddUserIDs adds the "users" edge to the User entity by IDs.
func (duo *DepartmentUpdateOne) AddUserIDs(ids ...int64) *DepartmentUpdateOne {
	duo.mutation.AddUserIDs(ids...)
	return duo
}

// AddUsers adds the "users" edges to the User entity.
func (duo *DepartmentUpdateOne) AddUsers(u ...*User) *DepartmentUpdateOne {
	ids := make([]int64, len(u))
	for i := range u {
		ids[i] = u[i].ID
	}
	return duo.AddUserIDs(ids...)
}

// SetLeader sets the "leader" edge to the User entity.
func (duo *DepartmentUpdateOne) SetLeader(u *User) *DepartmentUpdateOne {
	return duo.SetLeaderID(u.ID)
}

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

// ClearParent clears the "parent" edge to the Department entity.
func (duo *DepartmentUpdateOne) ClearParent() *DepartmentUpdateOne {
	duo.mutation.ClearParent()
	return duo
}

// ClearChildren clears all "children" edges to the Department entity.
func (duo *DepartmentUpdateOne) ClearChildren() *DepartmentUpdateOne {
	duo.mutation.ClearChildren()
	return duo
}

// RemoveChildIDs removes the "children" edge to Department entities by IDs.
func (duo *DepartmentUpdateOne) RemoveChildIDs(ids ...int64) *DepartmentUpdateOne {
	duo.mutation.RemoveChildIDs(ids...)
	return duo
}

// RemoveChildren removes "children" edges to Department entities.
func (duo *DepartmentUpdateOne) RemoveChildren(d ...*Department) *DepartmentUpdateOne {
	ids := make([]int64, len(d))
	for i := range d {
		ids[i] = d[i].ID
	}
	return duo.RemoveChildIDs(ids...)
}

// ClearUsers clears all "users" edges to the User entity.
func (duo *DepartmentUpdateOne) ClearUsers() *DepartmentUpdateOne {
	duo.mutation.ClearUsers()
	return duo
}

// RemoveUserIDs removes the "users" edge to User entities by IDs.
func (duo *DepartmentUpdateOne) RemoveUserIDs(ids ...int64) *DepartmentUpdateOne {
	duo.mutation.RemoveUserIDs(ids...)
	return duo
}

// RemoveUsers removes "users" edges to User entities.
func (duo *DepartmentUpdateOne) RemoveUsers(u ...*User) *DepartmentUpdateOne {
	ids := make([]int64, len(u))
	for i := range u {
		ids[i] = u[i].ID
	}
	return duo.RemoveUserIDs(ids...)
}

// ClearLeader clears the "leader" edge to the User entity.
func (duo *DepartmentUpdateOne) ClearLeader() *DepartmentUpdateOne {
	duo.mutation.ClearLeader()
	return duo
}

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

// Save executes the query and returns the updated Department entity.
func (duo *DepartmentUpdateOne) Save(ctx context.Context) (*Department, 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 *DepartmentUpdateOne) SaveX(ctx context.Context) *Department {
	node, err := duo.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

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

// ExecX is like Exec, but panics if an error occurs.
func (duo *DepartmentUpdateOne) 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 *DepartmentUpdateOne) defaults() error {
	if _, ok := duo.mutation.UpdatedAt(); !ok {
		if department.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized department.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := department.UpdateDefaultUpdatedAt()
		duo.mutation.SetUpdatedAt(v)
	}
	return nil
}

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

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

func (duo *DepartmentUpdateOne) sqlSave(ctx context.Context) (_node *Department, err error) {
	if err := duo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(department.Table, department.Columns, sqlgraph.NewFieldSpec(department.FieldID, field.TypeInt64))
	id, ok := duo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Department.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, department.FieldID)
		for _, f := range fields {
			if !department.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != department.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(department.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := duo.mutation.Status(); ok {
		_spec.SetField(department.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := duo.mutation.Version(); ok {
		_spec.SetField(department.FieldVersion, field.TypeInt64, value)
	}
	if value, ok := duo.mutation.AddedVersion(); ok {
		_spec.AddField(department.FieldVersion, field.TypeInt64, value)
	}
	if value, ok := duo.mutation.Name(); ok {
		_spec.SetField(department.FieldName, field.TypeString, value)
	}
	if value, ok := duo.mutation.Remark(); ok {
		_spec.SetField(department.FieldRemark, field.TypeString, value)
	}
	if duo.mutation.RemarkCleared() {
		_spec.ClearField(department.FieldRemark, field.TypeString)
	}
	if duo.mutation.ParentCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   department.ParentTable,
			Columns: []string{department.ParentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(department.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := duo.mutation.ParentIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   department.ParentTable,
			Columns: []string{department.ParentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(department.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if duo.mutation.ChildrenCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.ChildrenTable,
			Columns: []string{department.ChildrenColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(department.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := duo.mutation.RemovedChildrenIDs(); len(nodes) > 0 && !duo.mutation.ChildrenCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.ChildrenTable,
			Columns: []string{department.ChildrenColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(department.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.ChildrenIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.ChildrenTable,
			Columns: []string{department.ChildrenColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(department.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if duo.mutation.UsersCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   department.UsersTable,
			Columns: department.UsersPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := duo.mutation.RemovedUsersIDs(); len(nodes) > 0 && !duo.mutation.UsersCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   department.UsersTable,
			Columns: department.UsersPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.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.UsersIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   department.UsersTable,
			Columns: department.UsersPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if duo.mutation.LeaderCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   department.LeaderTable,
			Columns: []string{department.LeaderColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := duo.mutation.LeaderIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   department.LeaderTable,
			Columns: []string{department.LeaderColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.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 = &Department{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{department.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	duo.mutation.done = true
	return _node, nil
}
