// Code generated by ent, DO NOT EDIT.

package ent

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

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

// WorkExperienceUpdate is the builder for updating WorkExperience entities.
type WorkExperienceUpdate struct {
	config
	hooks    []Hook
	mutation *WorkExperienceMutation
}

// Where appends a list predicates to the WorkExperienceUpdate builder.
func (weu *WorkExperienceUpdate) Where(ps ...predicate.WorkExperience) *WorkExperienceUpdate {
	weu.mutation.Where(ps...)
	return weu
}

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

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

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

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

// SetEmployeeID sets the "employee_id" field.
func (weu *WorkExperienceUpdate) SetEmployeeID(u uint64) *WorkExperienceUpdate {
	weu.mutation.SetEmployeeID(u)
	return weu
}

// SetNillableEmployeeID sets the "employee_id" field if the given value is not nil.
func (weu *WorkExperienceUpdate) SetNillableEmployeeID(u *uint64) *WorkExperienceUpdate {
	if u != nil {
		weu.SetEmployeeID(*u)
	}
	return weu
}

// SetStartDate sets the "start_date" field.
func (weu *WorkExperienceUpdate) SetStartDate(t time.Time) *WorkExperienceUpdate {
	weu.mutation.SetStartDate(t)
	return weu
}

// SetNillableStartDate sets the "start_date" field if the given value is not nil.
func (weu *WorkExperienceUpdate) SetNillableStartDate(t *time.Time) *WorkExperienceUpdate {
	if t != nil {
		weu.SetStartDate(*t)
	}
	return weu
}

// SetEndDate sets the "end_date" field.
func (weu *WorkExperienceUpdate) SetEndDate(t time.Time) *WorkExperienceUpdate {
	weu.mutation.SetEndDate(t)
	return weu
}

// SetNillableEndDate sets the "end_date" field if the given value is not nil.
func (weu *WorkExperienceUpdate) SetNillableEndDate(t *time.Time) *WorkExperienceUpdate {
	if t != nil {
		weu.SetEndDate(*t)
	}
	return weu
}

// SetCompany sets the "company" field.
func (weu *WorkExperienceUpdate) SetCompany(s string) *WorkExperienceUpdate {
	weu.mutation.SetCompany(s)
	return weu
}

// SetNillableCompany sets the "company" field if the given value is not nil.
func (weu *WorkExperienceUpdate) SetNillableCompany(s *string) *WorkExperienceUpdate {
	if s != nil {
		weu.SetCompany(*s)
	}
	return weu
}

// SetExperience sets the "experience" field.
func (weu *WorkExperienceUpdate) SetExperience(s string) *WorkExperienceUpdate {
	weu.mutation.SetExperience(s)
	return weu
}

// SetNillableExperience sets the "experience" field if the given value is not nil.
func (weu *WorkExperienceUpdate) SetNillableExperience(s *string) *WorkExperienceUpdate {
	if s != nil {
		weu.SetExperience(*s)
	}
	return weu
}

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

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

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

// SetEmployee sets the "employee" edge to the Employee entity.
func (weu *WorkExperienceUpdate) SetEmployee(e *Employee) *WorkExperienceUpdate {
	return weu.SetEmployeeID(e.ID)
}

// Mutation returns the WorkExperienceMutation object of the builder.
func (weu *WorkExperienceUpdate) Mutation() *WorkExperienceMutation {
	return weu.mutation
}

// ClearEmployee clears the "employee" edge to the Employee entity.
func (weu *WorkExperienceUpdate) ClearEmployee() *WorkExperienceUpdate {
	weu.mutation.ClearEmployee()
	return weu
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (weu *WorkExperienceUpdate) check() error {
	if _, ok := weu.mutation.EmployeeID(); weu.mutation.EmployeeCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "WorkExperience.employee"`)
	}
	return nil
}

func (weu *WorkExperienceUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := weu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(workexperience.Table, workexperience.Columns, sqlgraph.NewFieldSpec(workexperience.FieldID, field.TypeUint64))
	if ps := weu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := weu.mutation.UpdatedAt(); ok {
		_spec.SetField(workexperience.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := weu.mutation.DeletedAt(); ok {
		_spec.SetField(workexperience.FieldDeletedAt, field.TypeTime, value)
	}
	if weu.mutation.DeletedAtCleared() {
		_spec.ClearField(workexperience.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := weu.mutation.StartDate(); ok {
		_spec.SetField(workexperience.FieldStartDate, field.TypeTime, value)
	}
	if value, ok := weu.mutation.EndDate(); ok {
		_spec.SetField(workexperience.FieldEndDate, field.TypeTime, value)
	}
	if value, ok := weu.mutation.Company(); ok {
		_spec.SetField(workexperience.FieldCompany, field.TypeString, value)
	}
	if value, ok := weu.mutation.Experience(); ok {
		_spec.SetField(workexperience.FieldExperience, field.TypeString, value)
	}
	if value, ok := weu.mutation.OrganizationID(); ok {
		_spec.SetField(workexperience.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := weu.mutation.AddedOrganizationID(); ok {
		_spec.AddField(workexperience.FieldOrganizationID, field.TypeUint64, value)
	}
	if weu.mutation.EmployeeCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   workexperience.EmployeeTable,
			Columns: []string{workexperience.EmployeeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(employee.FieldID, field.TypeUint64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := weu.mutation.EmployeeIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   workexperience.EmployeeTable,
			Columns: []string{workexperience.EmployeeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(employee.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, weu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{workexperience.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	weu.mutation.done = true
	return n, nil
}

// WorkExperienceUpdateOne is the builder for updating a single WorkExperience entity.
type WorkExperienceUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *WorkExperienceMutation
}

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

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

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

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

// SetEmployeeID sets the "employee_id" field.
func (weuo *WorkExperienceUpdateOne) SetEmployeeID(u uint64) *WorkExperienceUpdateOne {
	weuo.mutation.SetEmployeeID(u)
	return weuo
}

// SetNillableEmployeeID sets the "employee_id" field if the given value is not nil.
func (weuo *WorkExperienceUpdateOne) SetNillableEmployeeID(u *uint64) *WorkExperienceUpdateOne {
	if u != nil {
		weuo.SetEmployeeID(*u)
	}
	return weuo
}

// SetStartDate sets the "start_date" field.
func (weuo *WorkExperienceUpdateOne) SetStartDate(t time.Time) *WorkExperienceUpdateOne {
	weuo.mutation.SetStartDate(t)
	return weuo
}

// SetNillableStartDate sets the "start_date" field if the given value is not nil.
func (weuo *WorkExperienceUpdateOne) SetNillableStartDate(t *time.Time) *WorkExperienceUpdateOne {
	if t != nil {
		weuo.SetStartDate(*t)
	}
	return weuo
}

// SetEndDate sets the "end_date" field.
func (weuo *WorkExperienceUpdateOne) SetEndDate(t time.Time) *WorkExperienceUpdateOne {
	weuo.mutation.SetEndDate(t)
	return weuo
}

// SetNillableEndDate sets the "end_date" field if the given value is not nil.
func (weuo *WorkExperienceUpdateOne) SetNillableEndDate(t *time.Time) *WorkExperienceUpdateOne {
	if t != nil {
		weuo.SetEndDate(*t)
	}
	return weuo
}

// SetCompany sets the "company" field.
func (weuo *WorkExperienceUpdateOne) SetCompany(s string) *WorkExperienceUpdateOne {
	weuo.mutation.SetCompany(s)
	return weuo
}

// SetNillableCompany sets the "company" field if the given value is not nil.
func (weuo *WorkExperienceUpdateOne) SetNillableCompany(s *string) *WorkExperienceUpdateOne {
	if s != nil {
		weuo.SetCompany(*s)
	}
	return weuo
}

// SetExperience sets the "experience" field.
func (weuo *WorkExperienceUpdateOne) SetExperience(s string) *WorkExperienceUpdateOne {
	weuo.mutation.SetExperience(s)
	return weuo
}

// SetNillableExperience sets the "experience" field if the given value is not nil.
func (weuo *WorkExperienceUpdateOne) SetNillableExperience(s *string) *WorkExperienceUpdateOne {
	if s != nil {
		weuo.SetExperience(*s)
	}
	return weuo
}

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

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

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

// SetEmployee sets the "employee" edge to the Employee entity.
func (weuo *WorkExperienceUpdateOne) SetEmployee(e *Employee) *WorkExperienceUpdateOne {
	return weuo.SetEmployeeID(e.ID)
}

// Mutation returns the WorkExperienceMutation object of the builder.
func (weuo *WorkExperienceUpdateOne) Mutation() *WorkExperienceMutation {
	return weuo.mutation
}

// ClearEmployee clears the "employee" edge to the Employee entity.
func (weuo *WorkExperienceUpdateOne) ClearEmployee() *WorkExperienceUpdateOne {
	weuo.mutation.ClearEmployee()
	return weuo
}

// Where appends a list predicates to the WorkExperienceUpdate builder.
func (weuo *WorkExperienceUpdateOne) Where(ps ...predicate.WorkExperience) *WorkExperienceUpdateOne {
	weuo.mutation.Where(ps...)
	return weuo
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (weuo *WorkExperienceUpdateOne) check() error {
	if _, ok := weuo.mutation.EmployeeID(); weuo.mutation.EmployeeCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "WorkExperience.employee"`)
	}
	return nil
}

func (weuo *WorkExperienceUpdateOne) sqlSave(ctx context.Context) (_node *WorkExperience, err error) {
	if err := weuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(workexperience.Table, workexperience.Columns, sqlgraph.NewFieldSpec(workexperience.FieldID, field.TypeUint64))
	id, ok := weuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "WorkExperience.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := weuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, workexperience.FieldID)
		for _, f := range fields {
			if !workexperience.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != workexperience.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := weuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := weuo.mutation.UpdatedAt(); ok {
		_spec.SetField(workexperience.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := weuo.mutation.DeletedAt(); ok {
		_spec.SetField(workexperience.FieldDeletedAt, field.TypeTime, value)
	}
	if weuo.mutation.DeletedAtCleared() {
		_spec.ClearField(workexperience.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := weuo.mutation.StartDate(); ok {
		_spec.SetField(workexperience.FieldStartDate, field.TypeTime, value)
	}
	if value, ok := weuo.mutation.EndDate(); ok {
		_spec.SetField(workexperience.FieldEndDate, field.TypeTime, value)
	}
	if value, ok := weuo.mutation.Company(); ok {
		_spec.SetField(workexperience.FieldCompany, field.TypeString, value)
	}
	if value, ok := weuo.mutation.Experience(); ok {
		_spec.SetField(workexperience.FieldExperience, field.TypeString, value)
	}
	if value, ok := weuo.mutation.OrganizationID(); ok {
		_spec.SetField(workexperience.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := weuo.mutation.AddedOrganizationID(); ok {
		_spec.AddField(workexperience.FieldOrganizationID, field.TypeUint64, value)
	}
	if weuo.mutation.EmployeeCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   workexperience.EmployeeTable,
			Columns: []string{workexperience.EmployeeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(employee.FieldID, field.TypeUint64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := weuo.mutation.EmployeeIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   workexperience.EmployeeTable,
			Columns: []string{workexperience.EmployeeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(employee.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &WorkExperience{config: weuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, weuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{workexperience.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	weuo.mutation.done = true
	return _node, nil
}
