// Code generated by ent, DO NOT EDIT.

package ent

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

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

// LabelLogUpdate is the builder for updating LabelLog entities.
type LabelLogUpdate struct {
	config
	hooks    []Hook
	mutation *LabelLogMutation
}

// Where appends a list predicates to the LabelLogUpdate builder.
func (llu *LabelLogUpdate) Where(ps ...predicate.LabelLog) *LabelLogUpdate {
	llu.mutation.Where(ps...)
	return llu
}

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

// SetLabelName sets the "label_name" field.
func (llu *LabelLogUpdate) SetLabelName(s string) *LabelLogUpdate {
	llu.mutation.SetLabelName(s)
	return llu
}

// SetNillableLabelName sets the "label_name" field if the given value is not nil.
func (llu *LabelLogUpdate) SetNillableLabelName(s *string) *LabelLogUpdate {
	if s != nil {
		llu.SetLabelName(*s)
	}
	return llu
}

// SetLabelID sets the "label_id" field.
func (llu *LabelLogUpdate) SetLabelID(i int) *LabelLogUpdate {
	llu.mutation.ResetLabelID()
	llu.mutation.SetLabelID(i)
	return llu
}

// SetNillableLabelID sets the "label_id" field if the given value is not nil.
func (llu *LabelLogUpdate) SetNillableLabelID(i *int) *LabelLogUpdate {
	if i != nil {
		llu.SetLabelID(*i)
	}
	return llu
}

// AddLabelID adds i to the "label_id" field.
func (llu *LabelLogUpdate) AddLabelID(i int) *LabelLogUpdate {
	llu.mutation.AddLabelID(i)
	return llu
}

// SetWxID sets the "wx_id" field.
func (llu *LabelLogUpdate) SetWxID(s string) *LabelLogUpdate {
	llu.mutation.SetWxID(s)
	return llu
}

// SetNillableWxID sets the "wx_id" field if the given value is not nil.
func (llu *LabelLogUpdate) SetNillableWxID(s *string) *LabelLogUpdate {
	if s != nil {
		llu.SetWxID(*s)
	}
	return llu
}

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

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

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

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

// Mutation returns the LabelLogMutation object of the builder.
func (llu *LabelLogUpdate) Mutation() *LabelLogMutation {
	return llu.mutation
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (llu *LabelLogUpdate) Save(ctx context.Context) (int, error) {
	llu.defaults()
	return withHooks(ctx, llu.sqlSave, llu.mutation, llu.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (llu *LabelLogUpdate) defaults() {
	if _, ok := llu.mutation.UpdatedAt(); !ok {
		v := labellog.UpdateDefaultUpdatedAt()
		llu.mutation.SetUpdatedAt(v)
	}
}

func (llu *LabelLogUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(labellog.Table, labellog.Columns, sqlgraph.NewFieldSpec(labellog.FieldID, field.TypeUint64))
	if ps := llu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := llu.mutation.UpdatedAt(); ok {
		_spec.SetField(labellog.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := llu.mutation.LabelName(); ok {
		_spec.SetField(labellog.FieldLabelName, field.TypeString, value)
	}
	if value, ok := llu.mutation.LabelID(); ok {
		_spec.SetField(labellog.FieldLabelID, field.TypeInt, value)
	}
	if value, ok := llu.mutation.AddedLabelID(); ok {
		_spec.AddField(labellog.FieldLabelID, field.TypeInt, value)
	}
	if value, ok := llu.mutation.WxID(); ok {
		_spec.SetField(labellog.FieldWxID, field.TypeString, value)
	}
	if value, ok := llu.mutation.OrganizationID(); ok {
		_spec.SetField(labellog.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := llu.mutation.AddedOrganizationID(); ok {
		_spec.AddField(labellog.FieldOrganizationID, field.TypeUint64, value)
	}
	if llu.mutation.OrganizationIDCleared() {
		_spec.ClearField(labellog.FieldOrganizationID, field.TypeUint64)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, llu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{labellog.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	llu.mutation.done = true
	return n, nil
}

// LabelLogUpdateOne is the builder for updating a single LabelLog entity.
type LabelLogUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *LabelLogMutation
}

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

// SetLabelName sets the "label_name" field.
func (lluo *LabelLogUpdateOne) SetLabelName(s string) *LabelLogUpdateOne {
	lluo.mutation.SetLabelName(s)
	return lluo
}

// SetNillableLabelName sets the "label_name" field if the given value is not nil.
func (lluo *LabelLogUpdateOne) SetNillableLabelName(s *string) *LabelLogUpdateOne {
	if s != nil {
		lluo.SetLabelName(*s)
	}
	return lluo
}

// SetLabelID sets the "label_id" field.
func (lluo *LabelLogUpdateOne) SetLabelID(i int) *LabelLogUpdateOne {
	lluo.mutation.ResetLabelID()
	lluo.mutation.SetLabelID(i)
	return lluo
}

// SetNillableLabelID sets the "label_id" field if the given value is not nil.
func (lluo *LabelLogUpdateOne) SetNillableLabelID(i *int) *LabelLogUpdateOne {
	if i != nil {
		lluo.SetLabelID(*i)
	}
	return lluo
}

// AddLabelID adds i to the "label_id" field.
func (lluo *LabelLogUpdateOne) AddLabelID(i int) *LabelLogUpdateOne {
	lluo.mutation.AddLabelID(i)
	return lluo
}

// SetWxID sets the "wx_id" field.
func (lluo *LabelLogUpdateOne) SetWxID(s string) *LabelLogUpdateOne {
	lluo.mutation.SetWxID(s)
	return lluo
}

// SetNillableWxID sets the "wx_id" field if the given value is not nil.
func (lluo *LabelLogUpdateOne) SetNillableWxID(s *string) *LabelLogUpdateOne {
	if s != nil {
		lluo.SetWxID(*s)
	}
	return lluo
}

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

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

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

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

// Mutation returns the LabelLogMutation object of the builder.
func (lluo *LabelLogUpdateOne) Mutation() *LabelLogMutation {
	return lluo.mutation
}

// Where appends a list predicates to the LabelLogUpdate builder.
func (lluo *LabelLogUpdateOne) Where(ps ...predicate.LabelLog) *LabelLogUpdateOne {
	lluo.mutation.Where(ps...)
	return lluo
}

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

// Save executes the query and returns the updated LabelLog entity.
func (lluo *LabelLogUpdateOne) Save(ctx context.Context) (*LabelLog, error) {
	lluo.defaults()
	return withHooks(ctx, lluo.sqlSave, lluo.mutation, lluo.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (lluo *LabelLogUpdateOne) defaults() {
	if _, ok := lluo.mutation.UpdatedAt(); !ok {
		v := labellog.UpdateDefaultUpdatedAt()
		lluo.mutation.SetUpdatedAt(v)
	}
}

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