// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"
	"wechat-api/ent/employeeconfig"
	"wechat-api/ent/predicate"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// EmployeeConfigUpdate is the builder for updating EmployeeConfig entities.
type EmployeeConfigUpdate struct {
	config
	hooks    []Hook
	mutation *EmployeeConfigMutation
}

// Where appends a list predicates to the EmployeeConfigUpdate builder.
func (ecu *EmployeeConfigUpdate) Where(ps ...predicate.EmployeeConfig) *EmployeeConfigUpdate {
	ecu.mutation.Where(ps...)
	return ecu
}

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

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

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

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

// SetStype sets the "stype" field.
func (ecu *EmployeeConfigUpdate) SetStype(s string) *EmployeeConfigUpdate {
	ecu.mutation.SetStype(s)
	return ecu
}

// SetNillableStype sets the "stype" field if the given value is not nil.
func (ecu *EmployeeConfigUpdate) SetNillableStype(s *string) *EmployeeConfigUpdate {
	if s != nil {
		ecu.SetStype(*s)
	}
	return ecu
}

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

// SetNillableTitle sets the "title" field if the given value is not nil.
func (ecu *EmployeeConfigUpdate) SetNillableTitle(s *string) *EmployeeConfigUpdate {
	if s != nil {
		ecu.SetTitle(*s)
	}
	return ecu
}

// SetPhoto sets the "photo" field.
func (ecu *EmployeeConfigUpdate) SetPhoto(s string) *EmployeeConfigUpdate {
	ecu.mutation.SetPhoto(s)
	return ecu
}

// SetNillablePhoto sets the "photo" field if the given value is not nil.
func (ecu *EmployeeConfigUpdate) SetNillablePhoto(s *string) *EmployeeConfigUpdate {
	if s != nil {
		ecu.SetPhoto(*s)
	}
	return ecu
}

// SetOrganizationID sets the "organization_id" field.
func (ecu *EmployeeConfigUpdate) SetOrganizationID(u uint64) *EmployeeConfigUpdate {
	ecu.mutation.ResetOrganizationID()
	ecu.mutation.SetOrganizationID(u)
	return ecu
}

// SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
func (ecu *EmployeeConfigUpdate) SetNillableOrganizationID(u *uint64) *EmployeeConfigUpdate {
	if u != nil {
		ecu.SetOrganizationID(*u)
	}
	return ecu
}

// AddOrganizationID adds u to the "organization_id" field.
func (ecu *EmployeeConfigUpdate) AddOrganizationID(u int64) *EmployeeConfigUpdate {
	ecu.mutation.AddOrganizationID(u)
	return ecu
}

// ClearOrganizationID clears the value of the "organization_id" field.
func (ecu *EmployeeConfigUpdate) ClearOrganizationID() *EmployeeConfigUpdate {
	ecu.mutation.ClearOrganizationID()
	return ecu
}

// Mutation returns the EmployeeConfigMutation object of the builder.
func (ecu *EmployeeConfigUpdate) Mutation() *EmployeeConfigMutation {
	return ecu.mutation
}

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

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

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

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

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

func (ecu *EmployeeConfigUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(employeeconfig.Table, employeeconfig.Columns, sqlgraph.NewFieldSpec(employeeconfig.FieldID, field.TypeUint64))
	if ps := ecu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := ecu.mutation.UpdatedAt(); ok {
		_spec.SetField(employeeconfig.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := ecu.mutation.DeletedAt(); ok {
		_spec.SetField(employeeconfig.FieldDeletedAt, field.TypeTime, value)
	}
	if ecu.mutation.DeletedAtCleared() {
		_spec.ClearField(employeeconfig.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := ecu.mutation.Stype(); ok {
		_spec.SetField(employeeconfig.FieldStype, field.TypeString, value)
	}
	if value, ok := ecu.mutation.Title(); ok {
		_spec.SetField(employeeconfig.FieldTitle, field.TypeString, value)
	}
	if value, ok := ecu.mutation.Photo(); ok {
		_spec.SetField(employeeconfig.FieldPhoto, field.TypeString, value)
	}
	if value, ok := ecu.mutation.OrganizationID(); ok {
		_spec.SetField(employeeconfig.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := ecu.mutation.AddedOrganizationID(); ok {
		_spec.AddField(employeeconfig.FieldOrganizationID, field.TypeUint64, value)
	}
	if ecu.mutation.OrganizationIDCleared() {
		_spec.ClearField(employeeconfig.FieldOrganizationID, field.TypeUint64)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, ecu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{employeeconfig.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	ecu.mutation.done = true
	return n, nil
}

// EmployeeConfigUpdateOne is the builder for updating a single EmployeeConfig entity.
type EmployeeConfigUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *EmployeeConfigMutation
}

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

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

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

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

// SetStype sets the "stype" field.
func (ecuo *EmployeeConfigUpdateOne) SetStype(s string) *EmployeeConfigUpdateOne {
	ecuo.mutation.SetStype(s)
	return ecuo
}

// SetNillableStype sets the "stype" field if the given value is not nil.
func (ecuo *EmployeeConfigUpdateOne) SetNillableStype(s *string) *EmployeeConfigUpdateOne {
	if s != nil {
		ecuo.SetStype(*s)
	}
	return ecuo
}

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

// SetNillableTitle sets the "title" field if the given value is not nil.
func (ecuo *EmployeeConfigUpdateOne) SetNillableTitle(s *string) *EmployeeConfigUpdateOne {
	if s != nil {
		ecuo.SetTitle(*s)
	}
	return ecuo
}

// SetPhoto sets the "photo" field.
func (ecuo *EmployeeConfigUpdateOne) SetPhoto(s string) *EmployeeConfigUpdateOne {
	ecuo.mutation.SetPhoto(s)
	return ecuo
}

// SetNillablePhoto sets the "photo" field if the given value is not nil.
func (ecuo *EmployeeConfigUpdateOne) SetNillablePhoto(s *string) *EmployeeConfigUpdateOne {
	if s != nil {
		ecuo.SetPhoto(*s)
	}
	return ecuo
}

// SetOrganizationID sets the "organization_id" field.
func (ecuo *EmployeeConfigUpdateOne) SetOrganizationID(u uint64) *EmployeeConfigUpdateOne {
	ecuo.mutation.ResetOrganizationID()
	ecuo.mutation.SetOrganizationID(u)
	return ecuo
}

// SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
func (ecuo *EmployeeConfigUpdateOne) SetNillableOrganizationID(u *uint64) *EmployeeConfigUpdateOne {
	if u != nil {
		ecuo.SetOrganizationID(*u)
	}
	return ecuo
}

// AddOrganizationID adds u to the "organization_id" field.
func (ecuo *EmployeeConfigUpdateOne) AddOrganizationID(u int64) *EmployeeConfigUpdateOne {
	ecuo.mutation.AddOrganizationID(u)
	return ecuo
}

// ClearOrganizationID clears the value of the "organization_id" field.
func (ecuo *EmployeeConfigUpdateOne) ClearOrganizationID() *EmployeeConfigUpdateOne {
	ecuo.mutation.ClearOrganizationID()
	return ecuo
}

// Mutation returns the EmployeeConfigMutation object of the builder.
func (ecuo *EmployeeConfigUpdateOne) Mutation() *EmployeeConfigMutation {
	return ecuo.mutation
}

// Where appends a list predicates to the EmployeeConfigUpdate builder.
func (ecuo *EmployeeConfigUpdateOne) Where(ps ...predicate.EmployeeConfig) *EmployeeConfigUpdateOne {
	ecuo.mutation.Where(ps...)
	return ecuo
}

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

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

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

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

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

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

func (ecuo *EmployeeConfigUpdateOne) sqlSave(ctx context.Context) (_node *EmployeeConfig, err error) {
	_spec := sqlgraph.NewUpdateSpec(employeeconfig.Table, employeeconfig.Columns, sqlgraph.NewFieldSpec(employeeconfig.FieldID, field.TypeUint64))
	id, ok := ecuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "EmployeeConfig.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := ecuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, employeeconfig.FieldID)
		for _, f := range fields {
			if !employeeconfig.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != employeeconfig.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := ecuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := ecuo.mutation.UpdatedAt(); ok {
		_spec.SetField(employeeconfig.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := ecuo.mutation.DeletedAt(); ok {
		_spec.SetField(employeeconfig.FieldDeletedAt, field.TypeTime, value)
	}
	if ecuo.mutation.DeletedAtCleared() {
		_spec.ClearField(employeeconfig.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := ecuo.mutation.Stype(); ok {
		_spec.SetField(employeeconfig.FieldStype, field.TypeString, value)
	}
	if value, ok := ecuo.mutation.Title(); ok {
		_spec.SetField(employeeconfig.FieldTitle, field.TypeString, value)
	}
	if value, ok := ecuo.mutation.Photo(); ok {
		_spec.SetField(employeeconfig.FieldPhoto, field.TypeString, value)
	}
	if value, ok := ecuo.mutation.OrganizationID(); ok {
		_spec.SetField(employeeconfig.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := ecuo.mutation.AddedOrganizationID(); ok {
		_spec.AddField(employeeconfig.FieldOrganizationID, field.TypeUint64, value)
	}
	if ecuo.mutation.OrganizationIDCleared() {
		_spec.ClearField(employeeconfig.FieldOrganizationID, field.TypeUint64)
	}
	_node = &EmployeeConfig{config: ecuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, ecuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{employeeconfig.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	ecuo.mutation.done = true
	return _node, nil
}
